Jelajahi Sumber

Android权限设置方式,跟随登录返回保存在android本地,添加迁移与映射路由

Zhouzhendong 4 tahun lalu
induk
melakukan
a2269eebb9

+ 1 - 0
app/Authority.php

@@ -22,6 +22,7 @@ class Authority extends Model
         1 => "POST",
         2 => "PUT",
         3 => "DELETE",
+        4 => "ANDROID"
     ];
 
     function roles(){

+ 16 - 12
app/Http/ApiControllers/LoginController.php

@@ -61,7 +61,8 @@ class LoginController
         }
 
         try {
-            $response["data"] = ["token"=>app("UserService")->getJWTToken($user,$privateKey)];
+            $response["data"] = ["token"=>app("UserService")->getJWTToken($user,$privateKey),
+                "menu"=>$this->getMenu($user)];
             app("UserService")->setOrRefreshCache($user);
             return response()->json($response);
         }catch (\Exception $e){
@@ -71,17 +72,20 @@ class LoginController
         }
     }
 
-    public function test()
+    private function getMenu($user)
     {
-        return response()->json([
-            "message" => "登陆成功",
-            "status_code"=>200,
-            "data"=>[
-                "param" => \request("test"),
-                "user" => Auth::id(),
-            ],
-        ]);
+        Auth::setUser($user);
+        $authorities = app("AuthorityService")->getUserAndroidAuthority();
+        $result = [];
+        foreach (app("AuthorityService")->format($authorities) as $authority){
+            foreach ($authority["child"] as $authorityOne){
+                $item = [];
+                foreach ($authorityOne["child"] as $authorityTwo){
+                    $item[] = $authorityTwo["name"];
+                }
+                $result[$authorityOne["name"]] = $item;
+            }
+        }
+        return json_encode($result,JSON_UNESCAPED_UNICODE);
     }
-
-
 }

+ 13 - 0
app/Http/ApiControllers/WaybillController.php

@@ -0,0 +1,13 @@
+<?php
+
+
+namespace App\Http\ApiControllers;
+
+
+class WaybillController
+{
+    public function getData()
+    {
+
+    }
+}

+ 3 - 0
app/Http/Controllers/TestController.php

@@ -48,6 +48,7 @@ use App\OrderPackageCountingRecord;
 use App\ProcurementCheckSheet;
 use App\RejectedBill;
 use App\SeeLog;
+use App\Services\AuthorityService;
 use App\Services\BatchService;
 use App\Services\CacheShelfService;
 use App\Services\ForeignHaiRoboticsService;
@@ -196,6 +197,8 @@ class TestController extends Controller
     }
     public function test()
     {
+        $a = new LoginController();
+        dd($a->getMenu());
         dd(config('app.env'));
         $url = config('api_logistic.collectUpload.ZTO.prod.url');
         $xAppKey =  config('api_logistic.collectUpload.ZTO.prod.x-appKey');

+ 16 - 3
app/Services/AuthorityService.php

@@ -34,6 +34,16 @@ class AuthorityService
         return Cache::tags($tag)->get($key);
     }
 
+    public function getUserAndroidAuthority()
+    {
+        if (!Auth::user())return new Collection();
+        $key = "authorities:android:user_".Auth::id();
+        $isAdmin = $this->checkAdminIdentity();
+        $tag = $isAdmin ? "authorities:admin" : "authorities:user";
+        if (!Cache::tags($tag)->has($key))Cache::tags($tag)->forever($key,$this->getUserAuthorityData($isAdmin,true));
+        return Cache::tags($tag)->get($key);
+    }
+
     private function checkAdminIdentity():bool
     {
         if (!Auth::user())return false;
@@ -43,14 +53,17 @@ class AuthorityService
      * 即时的权限数据
      *
      * @param bool $isAdmin
+     * @param bool $isAndroid
      *
      * @return Collection
      */
-    public function getUserAuthorityData(bool $isAdmin = null):Collection
+    public function getUserAuthorityData(bool $isAdmin = null, bool $isAndroid = false):Collection
     {
+        $query = Authority::query();
+        if ($isAndroid)$query->whereNotNull("method");
         if ($isAdmin===null)$isAdmin = $this->checkAdminIdentity();
-        if ($isAdmin) return Authority::query()->get();
-        return Authority::query()->whereHas("roles",function (Builder $query){
+        if ($isAdmin) return $query->get();
+        return $query->whereHas("roles",function (Builder $query){
             $query->whereHas("users",function (Builder $query){
                 $query->where(DB::raw("users.id"),Auth::id());
             });

+ 3 - 2
app/Services/MaterialBoxService.php

@@ -37,9 +37,10 @@ class MaterialBoxService
                 ->whereNotIn("id",$blacklist)
                 ->where("id",">",$id)->where("code","like","IDE%")
                 /*->where("status",4)*/->limit(100)->orderBy("id")
-                ->whereNotIn("id",StationTaskMaterialBox::query()->select("material_box_id")
+                /*->whereNotIn("id",StationTaskMaterialBox::query()->select("material_box_id")
                     ->whereNotIn("status",["完成","取消"])
-                    ->where("material_box_model_id",$model->id)->groupBy("material_box_id"));
+                    ->groupBy("material_box_id"))*/
+                ->where("material_box_model_id",$model->id);
             $boxes = $boxes->get();
             if ($boxes->count()==0)break;
             $id = $boxes[count($boxes)-1]->id;

+ 1 - 0
app/Services/UserService.php

@@ -113,6 +113,7 @@ class UserService
     public function clearUserCache(User $user)
     {
         Cache::tags("authorities:user")->forget("authorities:user_".$user->id);
+        Cache::tags("authorities:user")->forget("authorities:android:user_".$user->id);
         Cache::forget("owners:user_".$user->id);
         Cache::forget("userWorkGroups:user_".$user->id);
         Cache::tags("AUTHORITY_MENU_MAPPING")->forget("am_mapping_".$user->id);

+ 81 - 0
database/migrations/2021_09_30_164937_change_authority_add_method_index.php

@@ -0,0 +1,81 @@
+<?php
+
+use Illuminate\Database\Migrations\Migration;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Support\Facades\Schema;
+
+class ChangeAuthorityAddMethodIndex extends Migration
+{
+    /**
+     * Run the migrations.
+     *
+     * @return void
+     */
+    public function up()
+    {
+        Schema::table('authorities', function (Blueprint $table) {
+            $table->index("method","auth_method_index");
+        });
+        $auth = \App\Authority::query()->create([
+            "name"          => "安卓端",
+            "parent_id"     => null,
+            'alias_name'    => \Faker\Provider\Uuid::uuid(),
+            "permission"    => "允许",
+            "route"         => null,
+            "method"        => 4,
+        ]);
+        $authWaybill = \App\Authority::query()->create([
+            "name"          => "运输管理",
+            "parent_id"     => $auth->id,
+            'alias_name'    => \Faker\Provider\Uuid::uuid(),
+            "permission"    => "允许",
+            "route"         => null,
+            "method"        => 4,
+        ]);
+        $authWaybillDispatch = \App\Authority::query()->create([
+            "name"          => "调度",
+            "parent_id"     => $authWaybill->id,
+            'alias_name'    => \Faker\Provider\Uuid::uuid(),
+            "permission"    => "允许",
+            "route"         => null,
+            "method"        => 4,
+        ]);
+        \App\Authority::query()->insert([
+            [
+                "name"          => "查看",
+                "parent_id"     => $authWaybillDispatch->id,
+                'alias_name'    => \Faker\Provider\Uuid::uuid(),
+                "permission"    => "允许",
+                "route"         => "waybill/dispatch",
+                "method"        => 0,
+            ],
+            [
+                "name"          => "编辑",
+                "parent_id"     => $authWaybillDispatch->id,
+                'alias_name'    => \Faker\Provider\Uuid::uuid(),
+                "permission"    => "允许",
+                "route"         => "waybill/dispatch",
+                "method"        => 1,
+            ],[
+                "name"          => "专线费",
+                "parent_id"     => $authWaybillDispatch->id,
+                'alias_name'    => \Faker\Provider\Uuid::uuid(),
+                "permission"    => "允许",
+                "route"         => "waybill/dispatch/dailyBilling",
+                "method"        => 1,
+            ]
+        ]);
+    }
+
+    /**
+     * Reverse the migrations.
+     *
+     * @return void
+     */
+    public function down()
+    {
+        Schema::table('authorities', function (Blueprint $table) {
+            $table->dropIndex("auth_method_index");
+        });
+    }
+}

+ 8 - 1
routes/api.php

@@ -22,7 +22,14 @@ Route::prefix("v1")->group(function (){
         });
     Route::middleware(['throttle:' . config('api.rate_limits.access'),"authorizing"])
         ->group(function () {
-            Route::POST('test', 'LoginController@test');
+            Route::prefix("waybill")->group(function () {
+                Route::prefix("dispatch")->group(function () {
+                    //登录
+                    Route::GET('/', 'WaybillController@getData');
+                    Route::POST('/', 'WaybillController@dispatch');
+                    Route::POST('dailyBilling', 'WaybillController@dailyBilling');
+                });
+            });
         });
 });