写作背景:最近在写多商户小程序商城

项目构建于看云,https://www.kancloud.cn/tpcms/merchant

效果图:

第一步:新建路由

Route::group(['prefix' => '/merchant', 'namespace' => "Merchant",'middleware' => ['merchant']], function () {

第二步:新建控制器

<?php
namespace App\Http\Controllers\Merchant;

use App\Http\Controllers\Core\BaseController;
use Input, App;
use Illuminate\Http\Request;
use App\Models\User;
use App\Models\Merchant;

class InfoController extends BaseController
{
    private $validateRule = [
        'password'                      => 'confirmed|min:6',
        'mobile'                        => 'digits:11',
        'merchant_name'                      => 'required',
        'merchant_logo'                         => 'required',
        'in_selling'                         => 'required',
        'account'                         => 'required'
    ];
    private $errorMsg = [
        'password.required'             => '新密码必填',
        'password.confirmed'            => '两次密码不一致',
        'password.min'                  => '新密码最小6位数',
        'mobile.digits'                 => '手机号码为11位数',
        'merchant_name.required'             => '商家名称必填',
        'merchant_logo.required'                => '商家Logo必填',
        'in_selling.required'                  => '上架状态必填',
        'account.required'             => '账号必填'
    ];

    public function index()
    {
        $item = Merchant::user();
        return view('merchant.info.item', ['item' => $item]);
    }

    public function store(Request $request)
    {
        $this->validate($request, $this->validateRule, $this->errorMsg);
        $id = Merchant::userId();
        $user = Merchant::find($id);

        # 登录的商户 与 表单账号账号
        if($user->account != Input::get('account')) {
            return $this->validateError(['account' =>'账号不能变更!']);
        }

        //判断密码是否一致
        $oldPassword = Input::get('old_password', '');
        if (!empty($oldPassword) && !App::make('hash')->check($oldPassword , $user->password)) {
            return $this->validateError(['old_password' =>'输入的原密码不正确!']);
        }
        $password = Input::get('password', '');
        if (!empty($password)) {
            $user->password = bcrypt($password);
        }
        $user->mobile = Input::get('mobile', '');
        $user->in_selling = Input::get('in_selling', 0);
        $user->contact_name = Input::get('contact_name', 0);
        $user->merchant_logo = Input::get('merchant_logo', 0);
        
        if (!$user->save()) {
            return $this->retJson(503, '操作出错!');
        }
        return $this->retJson(200, '操作成功!');
    }

}

第三步:新建静态页面

@section('title', "商家信息")
@section('content_title', '商家信息')
@section('content_title_small', "$item->merchant_name")
@section('content')
    <!-- Main content -->
    <section class="content">
    <!-- Horizontal Form -->
        <div class="box box-info">
            <div class="box-header with-border">
                <h3 class="box-title">商家信息</h3>
            </div>
            <!-- /.box-header -->
            <!-- form start -->
            <form id="form" class="form-horizontal" action="{{ url('/merchant/info') }}" method="post">
                <div class="box-body">
                    <div class="form-group">
                        <label for="merchant_name" class="col-sm-2 control-label">商家名称</label>
                        <div class="col-sm-10">
                            <input type="text" class="form-control" name="merchant_name" value="{{ $item->merchant_name }}">
                        </div>
                    </div>

                    <div class="form-group">
                        <label for="name" class="col-sm-2 control-label">商家Logo</label>
                        <div class="col-sm-10">
                            <div class="input-group">
                                <input type="text" class="form-control" name="merchant_logo" value="{{ isset($item['merchant_logo']) ? $item['merchant_logo'] : '' }}" id="image_url">
                                <span class="input-group-btn">
                                    <button type="button" class="btn btn-info btn-flat" id="upImageBtn">上传图片 750px x 340px</button>
                                </span>
                            </div>
                        </div>
                    </div>
                    
                    <div class="form-group">
                        <label for="mobile" class="col-sm-2 control-label">联系人</label>
                        <div class="col-sm-10">
                            <input type="text" class="form-control" name="contact_name" value="{{ isset($item) ? $item['contact_name'] : '' }}">
                        </div>
                    </div>

                    <div class="form-group">
                        <label for="mobile" class="col-sm-2 control-label">手机号</label>
                        <div class="col-sm-10">
                            <input type="text" class="form-control" name="mobile" value="{{ isset($item) ? $item['mobile'] : '' }}">
                        </div>
                    </div>
                    
                    <div class="form-group">
                        <label for="sex" class="col-sm-2 control-label">是否上架</label>
                        <div class="col-sm-10">
                            <label class="radio-inline">
                                <input type="radio" name="in_selling" value="1" checked>是
                            </label>
                            <label class="radio-inline">
                                <input type="radio" name="in_selling" value="0" {{ isset($item) && @$item['in_selling'] == '0' ? 'checked' : '' }}>否
                            </label>
                        </div>
                    </div>

                    <div class="form-group">
                        <label for="account" class="col-sm-2 control-label">账号</label>
                        <div class="col-sm-10">
                            <input type="text" class="form-control" name="account" value="{{ $item->account }}" readonly>
                        </div>
                    </div>

                    <div class="form-group">
                        <label for="old_password" class="col-sm-2 control-label">原密码</label>
                        <div class="col-sm-10">
                            <input type="password" class="form-control" name="old_password" value="">
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="password" class="col-sm-2 control-label">新密码</label>
                        <div class="col-sm-10">
                            <input type="password" class="form-control" name="password" value="">
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="password_confirmation" class="col-sm-2 control-label">确认新密码</label>
                        <div class="col-sm-10">
                            <input type="password" class="form-control" name="password_confirmation" value="">
                        </div>
                    </div>
                </div>
                <!-- /.box-body -->
                <div class="box-footer">
                    <button type="submit" class="btn btn-info pull-right _submit_" data-form-id="form"
                            data-refresh-url="{{ url("/merchant") }}">提交</button>
                </div>
                <!-- /.box-footer -->
            </form>
        </div>
        <!-- /.box -->
    </section>
@endsection
@section('css')
    <link rel="stylesheet" href="{{ $assets_url }}/plugins/datepicker/datepicker3.css">
    <link rel="stylesheet" href="{{ $assets_url }}/plugins/kindeditor/themes/default/default.css" />
    <style>
        .disabled.day {
            color: #ddd !important;
        }
    </style>
@endsection
@section('js')
    <script src="{{ $assets_url }}/plugins/datepicker/bootstrap-datepicker.js"></script>
    <script charset="utf-8" src="{{ $assets_url }}/plugins/kindeditor/kindeditor-all-min.js"></script>
    <script src="{{ $assets_url }}/plugins/art-dialog/jquery.artDialog.js?skin=default"></script>
    <script charset="utf-8" src="{{ $assets_url }}/plugins/art-dialog/plugins/iframeTools.js"></script>
    <script>
        $.fn.datepicker.dates['zh-CN'] = {
            days: ["星期天", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期日"],
            daysShort: ["日", "一", "二", "三", "四", "五", "六", "日"],
            daysMin: ["日", "一", "二", "三", "四", "五", "六", "日"],
            months: ["一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月"],
            monthsShort: ["一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月"],
            today: "今天",
            clear: "清除",
        }
        $(".datepicker").datepicker(
            {
                format: "yyyy-mm-dd",
                language: 'zh-CN',
                todayHighlight: true,
                autoclose: true,
            }
        );
        $("#startTime").change(function () {
            $(".datepicker").datepicker('setStartDate' , $("#startTime").val());
        });
        $("#endTime").change(function () {
            $(".datepicker").datepicker('setStartDate' , $("#endTime").val());
        });
        $("#targetUrlBtn").click(function() {
            miniapp_page_help();
        });

        KindEditor.ready(function(K) {
            var editor1 = K.editor({
                uploadJson : "{{ url('/api/v1.0/upload') }}",
            });

            K('#upImageBtn').click(function(){
                editor1.loadPlugin('image', function() {
                    editor1.plugin.imageDialog({
                        imageUrl : K('#image_url').val(),
                        clickFn : function(url, title, width, height, border, align) {
                            K('#image_url').val(url);
                            editor1.hideDialog();
                        }
                    });
                });
            });
        });
    </script>
@endsection
@extends('layouts.merchant')

ThinkPHP
4 声望3 粉丝