Sfoglia il codice sorgente

增加微信小程序接口

Zhouzhendong 6 anni fa
parent
commit
c2a8c75704

+ 2 - 1
app/Http/Controllers/Auth/LoginController.php

@@ -3,12 +3,14 @@
 namespace App\Http\Controllers\Auth;
 
 use App\Http\Controllers\Controller;
+use App\User;
 use Illuminate\Foundation\Auth\AuthenticatesUsers;
 use Illuminate\Http\JsonResponse;
 use Illuminate\Http\RedirectResponse;
 use Illuminate\Http\Request;
 use Illuminate\Http\Response;
 use Illuminate\Support\Facades\Auth;
+use Illuminate\Support\Facades\Hash;
 use Illuminate\Validation\ValidationException;
 
 class LoginController extends Controller
@@ -57,7 +59,6 @@ class LoginController extends Controller
      */
     public function login(Request $request)
     {
-        $this->validateLogin($request);
 
         if (method_exists($this, 'hasTooManyLoginAttempts') &&
             $this->hasTooManyLoginAttempts($request)) {

+ 5 - 1
app/Http/Controllers/Auth/RegisterController.php

@@ -76,6 +76,7 @@ class RegisterController extends Controller
     }
     public function register(Request $request)
     {
+
         $headTo=$request->input('headTo')??'';
         $this->validator($request->all())->validate();
 
@@ -87,7 +88,10 @@ class RegisterController extends Controller
             $user->roles()->sync($roleIdArr);
         }
 //        $this->guard()->login($user);
-
+        $carrier_id=$request->input('carrier_id')??'';
+        if ($carrier_id){
+            $user->carriers()->sync($carrier_id);
+        }
         $this->log(__METHOD__,__FUNCTION__,json_encode($request->toArray()),$user['id']);
         if($headTo){return redirect(url($headTo))->with('successTip',"录入用户 {$user->name} 成功");}
         return $this->registered($request, $user)

+ 13 - 3
app/Http/Controllers/UserController.php

@@ -2,6 +2,7 @@
 
 namespace App\Http\Controllers;
 
+use App\Carrier;
 use App\Exports\UserExport;
 use App\Role;
 use App\User;
@@ -23,7 +24,7 @@ class UserController extends Controller
     public function index()
     {
         if(!Gate::allows('用户-查询')){ return redirect(url('/'));  }
-        $users=User::with('roles')->orderBy('id','desc')->paginate(35);
+        $users=User::with(['roles','carriers'])->orderBy('id','desc')->paginate(35);
         $users->each(function (User $user){
             $user['isSuperAdmin']=$user->isSuperAdmin();
         });
@@ -39,7 +40,8 @@ class UserController extends Controller
     {
         if(!Gate::allows('用户-录入')){ return redirect(url('/'));  }
         $roles=Role::all();
-        return view('maintenance.user.create',['rolesAll'=>$roles]);
+        $carriers=Carrier::select('id','name')->get();
+        return view('maintenance.user.create',['rolesAll'=>$roles,'carriers'=>$carriers]);
     }
 
 //    /**
@@ -90,7 +92,9 @@ class UserController extends Controller
         if(!Gate::allows('用户-编辑')){ return redirect(url('/'));  }
         $rolesAll=Role::all();
         $roles=$user->roles()->get();
-        return view('maintenance.user.edit',compact('user','rolesAll','roles'));
+        $carriers=Carrier::select('id','name')->get();
+        $carrierUser=$user->carriers()->get();
+        return view('maintenance.user.edit',compact('user','rolesAll','roles','carriers','carrierUser'));
     }
 
     /**
@@ -113,6 +117,12 @@ class UserController extends Controller
         }else{
             $user->roles()->sync([]);
         }
+        $carrier_id=$request->input('carrier_id')??'';
+        if($carrier_id){
+            $user->carriers()->sync($carrier_id);
+        }else{
+            $user->carriers()->sync([]);
+        }
         $this->log(__METHOD__,__FUNCTION__,json_encode($request->toArray()),Auth::user()['id']);
         return redirect('maintenance/user/')->with('successTip',"成功修改用户“{$user['name']}”!");
     }

+ 107 - 0
app/Http/Controllers/api/WxController.php

@@ -0,0 +1,107 @@
+<?php
+
+namespace App\Http\Controllers\Api;
+
+use App\Unit;
+use App\User;
+use App\Waybill;
+use Illuminate\Http\Request;
+use App\Http\Controllers\Controller;
+use Illuminate\Support\Facades\Auth;
+use Illuminate\Support\Facades\Hash;
+use Illuminate\Support\Facades\Validator;
+
+class WxController extends Controller
+{
+    public function index(Request $request){
+        $carrierArr=[];
+        $carriersName=[];
+        $api_token=$request->input('api_token');
+        $user=User::with('carriers')->where('api_token',$api_token)->first();
+        if ($user){
+            foreach ($user->carriers as $carrier){
+                array_push($carrierArr, $carrier->id);
+                array_push($carriersName, $carrier->name);
+            }
+            $count=Waybill::select('id')->whereIn('carrier_id',$carrierArr)->whereDate('created_at',date("Y-m-d "))->count();
+            $waybill=Waybill::orderBy('created_at','DESC')->select('created_at','waybill_number','wms_bill_number','carrier_bill')
+                ->whereIn('carrier_id',$carrierArr)->where('status','!=','已完结')->where('status','!=','无模型')->get();
+            return ['waybill'=>$waybill,'carriersName'=>$carriersName,'count'=>$count];
+        }
+        return ['exception'=>'出错了!'];
+    }
+
+    public function show(Request $request){
+        $units_name=[];
+        $waybill_number=$request->input('waybill_number');
+        $waybill=Waybill::select('id','waybill_number','wms_bill_number','owner_id','origination','destination','recipient','recipient_mobile',
+                'carrier_weight','carrier_weight_unit_id','carrier_weight_other','carrier_weight_unit_id_other','pick_up_fee','carrier_bill')
+            ->where('waybill_number',$waybill_number)->first();
+        $units=Unit::select('name')->get();
+        foreach ($units as $unit){
+            array_push($units_name, $unit->name);
+        }
+        return ['waybill'=>$waybill,'units_name'=>$units_name];
+    }
+
+    public function update(Request $request){
+        $id=$request->input('id');
+        $validator=Validator::make($request->input(),[
+            'carrier_bill'=>"required|max:50|unique:waybills,carrier_bill,$id",
+            'pick_up_fee'=>'nullable|min:0|numeric|max:999999',
+            'carrier_weight'=>'required|min:0|numeric|max:999999',
+            'carrier_weight_unit_name'=>'required_with:carrier_weight',
+            'carrier_weight_other'=>'nullable|min:0|numeric|max:999999',
+            'carrier_weight_unit_other_name'=>'required_with:carrier_weight_other',
+        ],[
+            'required'=>':attribute 为必填项',
+            'alpha_num'=>':attribute 应为字母或数字',
+            'max'=>':attribute 字符过多或输入值过大',
+            'min'=>':attribute 不得为负',
+            'numeric'=>':attribute 应为数字',
+            'unique'=>':attribute 已存在',
+            'required_with'=>':attribute 未填',
+        ],[
+            'carrier_bill'=>'承运商单号',
+            'pick_up_fee'=>'提货费',
+            'carrier_weight'=>'计数一',
+            'carrier_weight_unit_id'=>'计数单位一',
+            'carrier_weight_other'=>'计数二',
+            'carrier_weight_unit_other_name'=>'计数单位二',
+        ]);
+        if ($validator->fails()) {
+            //将返回错误循环组装成字符串
+            $arr = [];
+            foreach ($validator->getMessageBag()->toArray() as $k=>$error){
+                array_push($arr, $error[0]);
+            }
+            return [
+                'success' => false,
+                'errors' => $arr
+            ];
+        }
+        $unit=Unit::where('name',$request->input('carrier_weight_unit_name'))->first();
+        $unit_other=Unit::where('name',$request->input('carrier_weight_unit_other_name'))->first();
+        $waybill=Waybill::find($id);
+        $carrier_bill=$request->input('carrier_bill');
+        $pick_up_fee=$request->input('pick_up_fee');
+        $carrier_weight=$request->input('carrier_weight');
+        $carrier_weight_unit_id=$unit->id;
+        $carrier_weight_other=$request->input('carrier_weight_other');
+        if ($unit_other){
+            $carrier_weight_unit_id_other=$unit_other->id;
+            $waybill->carrier_weight_unit_id_other=$carrier_weight_unit_id_other;
+        }
+        $waybill->carrier_bill=$carrier_bill;
+        if($pick_up_fee)$waybill->pick_up_fee=$pick_up_fee;
+        $waybill->carrier_weight=$carrier_weight;
+        $waybill->carrier_weight_unit_id=$carrier_weight_unit_id;
+        $waybill->carrier_weight_other=$carrier_weight_other;
+
+        if ($waybill->save()){
+            $this->log(__METHOD__,__FUNCTION__,json_encode($request->toArray()),Auth::user()['id']);
+            return ['success'=>true];
+        }
+        else return ['success'=>false];
+    }
+}

+ 27 - 0
app/Http/Controllers/api/WxLoginController.php

@@ -0,0 +1,27 @@
+<?php
+
+namespace App\Http\Controllers\Api;
+
+use App\User;
+use Illuminate\Http\Request;
+use App\Http\Controllers\Controller;
+use Illuminate\Support\Facades\Auth;
+use Illuminate\Support\Facades\Hash;
+
+class WxLoginController extends Controller
+{
+    public function login(Request $request){
+        $name = $request->input('name');
+        $pwd = $request->input('password');
+        $user = User::where('name', $name)->first();
+        if ($user){
+            $isRight=Hash::check($pwd,$user['password']);
+            if ($isRight){
+                $this->log(__METHOD__,__FUNCTION__,json_encode($request->toArray()),$user->id);
+                return ['api_token'=>$user->api_token];
+            }
+            else return ['api_token'=>''];
+        }
+        return ['api_token'=>''];
+    }
+}

+ 7 - 0
app/User.php

@@ -8,9 +8,11 @@ use Illuminate\Foundation\Auth\User as Authenticatable;
 use Illuminate\Support\Collection;
 use Illuminate\Support\Facades\Auth;
 use Illuminate\Support\Facades\Gate;
+use Illuminate\Database\Eloquent\SoftDeletes;
 
 class User extends Authenticatable
 {
+    use SoftDeletes;
     use Notifiable;
 
     /**
@@ -54,6 +56,11 @@ class User extends Authenticatable
     function roles(){
         return $this->belongsToMany('App\Role','user_role','id_user','id_role');
     }
+
+    function carriers(){
+        return $this->belongsToMany('App\Carrier','carrier_user','user_id','carrier_id');
+    }
+
     function authorities(){
         $authorities = new Collection([]);
         $this->roles()->each(function ($role)use(&$authorities){

+ 1 - 1
database/migrations/2019_11_22_094024_create_waybills_table.php

@@ -29,7 +29,7 @@ class CreateWaybillsTable extends Migration
             $table->decimal('collect_fee')->nullable()->comment('到付金额');
             $table->text('ordering_remark')->nullable()->comment('下单备注');
             $table->bigInteger('carrier_id')->nullable()->index()->comment('承运商');
-            $table->string('carrier_bill')->nullable()->comment('承运商单号');
+            $table->string('carrier_bill')->nullable()->unique()->comment('承运商单号');
             $table->bigInteger('origination_city_id')->nullable()->index()->comment('始发市');
             $table->bigInteger('destination_city_id')->nullable()->index()->comment('目的市');
             $table->decimal('warehouse_weight')->nullable()->comment('仓库计重(抛)');

+ 33 - 0
database/migrations/2020_01_08_103927_create_carrier_user_table.php

@@ -0,0 +1,33 @@
+<?php
+
+use Illuminate\Support\Facades\Schema;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Database\Migrations\Migration;
+
+class CreateCarrierUserTable extends Migration
+{
+    /**
+     * Run the migrations.
+     *
+     * @return void
+     */
+    public function up()
+    {
+        Schema::create('carrier_user', function (Blueprint $table) {
+            $table->bigIncrements('id');
+            $table->bigInteger('carrier_id')->comment('承运商表');
+            $table->bigInteger('user_id')->comment('用户表');
+            $table->timestamps();
+        });
+    }
+
+    /**
+     * Reverse the migrations.
+     *
+     * @return void
+     */
+    public function down()
+    {
+        Schema::dropIfExists('carrier_user');
+    }
+}

+ 32 - 0
database/migrations/2020_01_08_152729_change_user_filed.php

@@ -0,0 +1,32 @@
+<?php
+
+use Illuminate\Support\Facades\Schema;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Database\Migrations\Migration;
+
+class ChangeUserFiled extends Migration
+{
+    /**
+     * Run the migrations.
+     *
+     * @return void
+     */
+    public function up()
+    {
+        Schema::table('users', function (Blueprint $table) {
+            $table->softDeletes();
+        });
+    }
+
+    /**
+     * Reverse the migrations.
+     *
+     * @return void
+     */
+    public function down()
+    {
+        Schema::table('users', function (Blueprint $table) {
+            $table->dropColumn('deleted_at');
+        });
+    }
+}

+ 56 - 2
resources/views/maintenance/user/create.blade.php

@@ -94,6 +94,31 @@
                                     @enderror
                                 </div>
                             </div>
+                            <div class="form-group row">
+                                <label for="role" class="col-md-3 col-form-label text-md-right">承运商</label>
+
+                                <div class="col-md-7">
+                                    <div class="input-group">
+                                        <select id="carrierAddingItem" class="form-control">
+                                            <option :value="carrier.id" v-for="carrier in carrierAll">@{{ carrier.name }}</option>
+                                        </select>
+                                        <span class="input-group-append">
+                                            <span class="input-group-text btn-outline-info" style="cursor: pointer;" @click="addCarrier">
+                                                添加左边的承运商
+                                            </span>
+                                        </span>
+                                    </div>
+                                    <select size="8" class="form-control "
+                                            title="两次点击移除目标">
+                                        <option :value="carrier.id" v-for="carrier in carriers" @dblclick="clickingCarrierOption">
+                                            @{{ carrier.name }}
+                                        </option>
+                                    </select>
+                                    <div hidden v-for="carrier in carriers">
+                                        <input hidden name="carrier_id[]" :value="carrier.id">
+                                    </div>
+                                </div>
+                            </div>
 
                             <input type="hidden" name="headTo" value="{{url('maintenance/user/create')}}">
                             <div class="form-group row mb-0">
@@ -122,7 +147,13 @@
                     @endforeach
                 ],
                 roles:[],
-                rolesStr:'{{ old('role') }}'
+                rolesStr:'{{ old('role') }}',
+                carrierAll:[
+                    @foreach($carriers as $carrier)
+                    {id:'{{$carrier->id}}',name:'{{$carrier->name}}'},
+                    @endforeach
+                ],
+                carriers:[],
             },
             mounted:function(){
                 $(".tooltipTarget").tooltip({'trigger':'hover'});
@@ -174,7 +205,30 @@
                     setTimeout(function(){
                         $(".tooltipTarget").tooltip({'trigger':'hover'});
                     },20);
-                }
+                },
+                addCarrier:function () {
+                    let $carrierAddingItem = $('#carrierAddingItem');
+                    let val=$carrierAddingItem.val();
+                    let carrierName = $carrierAddingItem.find("option:selected").text();
+                    let isLackOf=this.carriers.every(function(carrier){
+                        return carrier.id !== val;
+                    });
+                    if(isLackOf){
+                        this.carriers.push({id:val,name: carrierName})
+                    }else{
+                        tempTip.setDuration(1500);
+                        tempTip.show('已添加'+carrierName)
+                    }
+                },
+                clickingCarrierOption:function(e){
+                    let target = $(e.target);
+                    for (let i=0;i<this.carriers.length;i++){
+                        if(this.carriers[i].id===target.val()){
+                            this.carriers.splice(i,1);
+                            break;
+                        }
+                    }
+                },
             },
         });
 

+ 60 - 2
resources/views/maintenance/user/edit.blade.php

@@ -69,6 +69,31 @@
                             @enderror
                         </div>
                     </div>
+                    <div class="form-group row">
+                        <label for="role" class="col-2 col-form-label text-md-right">承运商</label>
+
+                        <div class="col-md-8">
+                            <div class="input-group">
+                                <select id="carrierAddingItem" class="form-control">
+                                    <option :value="carrier.id" v-for="carrier in carrierAll">@{{ carrier.name }}</option>
+                                </select>
+                                <span class="input-group-append">
+                                            <span class="input-group-text btn-outline-info" style="cursor: pointer;" @click="addCarrier">
+                                                添加左边的承运商
+                                            </span>
+                                        </span>
+                            </div>
+                            <select size="8" class="form-control "
+                                    title="两次点击移除目标">
+                                <option :value="carrier.id" v-for="carrier in carriers" @dblclick="clickingCarrierOption">
+                                    @{{ carrier.name }}
+                                </option>
+                            </select>
+                            <div hidden v-for="carrier in carriers">
+                                <input hidden name="carrier_id[]" :value="carrier.id">
+                            </div>
+                        </div>
+                    </div>
                     <div class="form-group row">
                         <div class="col-8 offset-2">
                             <input type="submit" class="btn btn-outline-dark form-control">
@@ -96,7 +121,17 @@
                     {id:'{{$role->id}}',name:'{{$role->name}}',},
                     @endforeach
                 ],
-                rolesStr:'{{ old('role') }}'
+                rolesStr:'{{ old('role') }}',
+                carrierAll:[
+                        @foreach($carriers as $carrier)
+                    {id:'{{$carrier->id}}',name:'{{$carrier->name}}'},
+                    @endforeach
+                ],
+                carriers:[
+                    @foreach($carrierUser as $carrier)
+                    {id:'{{$carrier->id}}',name:'{{$carrier->name}}'},
+                    @endforeach
+                ],
             },
             mounted:function(){
                 $(".tooltipTarget").tooltip({'trigger':'hover'});
@@ -149,7 +184,30 @@
                     setTimeout(function(){
                         $(".tooltipTarget").tooltip({'trigger':'hover'});
                     },20);
-                }
+                },
+                addCarrier:function () {
+                    let $carrierAddingItem = $('#carrierAddingItem');
+                    let val=$carrierAddingItem.val();
+                    let carrierName = $carrierAddingItem.find("option:selected").text();
+                    let isLackOf=this.carriers.every(function(carrier){
+                        return carrier.id !== val;
+                    });
+                    if(isLackOf){
+                        this.carriers.push({id:val,name: carrierName})
+                    }else{
+                        tempTip.setDuration(1500);
+                        tempTip.show('已添加'+carrierName)
+                    }
+                },
+                clickingCarrierOption:function(e){
+                    let target = $(e.target);
+                    for (let i=0;i<this.carriers.length;i++){
+                        if(this.carriers[i].id===target.val()){
+                            this.carriers.splice(i,1);
+                            break;
+                        }
+                    }
+                },
             },
         });
 

+ 8 - 1
resources/views/maintenance/user/index.blade.php

@@ -16,6 +16,7 @@
                         <th>ID</th>
                         <th>用户名</th>
                         <th>角色</th>
+                        <th>配置承运商</th>
                         <th>邮件地址</th>
                         <th>创建时间</th>
                         <th>操作</th>
@@ -28,6 +29,12 @@
                             <ul v-if="user.roles.length>0" class="list-group">
                                 <li v-for="role in user.roles" style="list-style: none">@{{ role.name }}</li>
                             </ul>
+                        </td>
+                        <td>
+                            <ul v-if="user.carriers.length>0" class="list-group">
+                                <li v-for="carrier in user.carriers" style="list-style: none">@{{ carrier.name }}</li>
+                            </ul>
+                        </td>
                         <td>@{{user.email}}</td>
                         <td class="text-muted">@{{user.created_at}}</td>
                         <td>
@@ -53,7 +60,7 @@
                     @foreach( $users as $user )
                     {id:'{{$user->id}}',name:'{{$user->name}}',email:'{{$user->email}}',
                         isSuperAdmin:'{{$user->isSuperAdmin}}',
-                        roles:{!! $user->roles !!},created_at:'{{$user->created_at}}'},
+                        roles:{!! $user->roles !!},carriers:{!! $user->carriers !!},created_at:'{{$user->created_at}}'},
                     @endforeach
                 ],
             },

+ 9 - 0
routes/api.php

@@ -17,3 +17,12 @@ Route::prefix('log')->group(function(){ //日志记录
     Route::post('rejected/jianshang/send', "Api\jianshang\RejectedController@fakingReceiveSend");//faking rejected发给笕尚的退货信息
 });
 Route::post('rejected/jianshang/send', "Api\jianshang\RejectedController@fakingReceiveSend");//faking rejected发给笕尚的退货信息
+
+Route::any('wx/login', "Api\WxLoginController@login");//微信小程序登录验证
+
+
+Route::group(['middleware'=>'auth:api'],function(){
+    Route::get('wx/waybill', "Api\WxController@index");//微信小程序数据返回
+    Route::post('wx/waybill/show', "Api\WxController@show");//微信小程序数据返回
+    Route::post('wx/waybill/update', "Api\WxController@update");//微信小程序数据修改
+});