Просмотр исходного кода

动态菜单第一版(大更新,将会涉及所有模块,所有页面)

Zhouzhendong 4 лет назад
Родитель
Сommit
78796d445b

+ 1 - 0
app/Authority.php

@@ -8,6 +8,7 @@ use App\Traits\ModelTimeFormat;
 
 use App\Traits\ModelLogChanging;
 use Illuminate\Database\Eloquent\SoftDeletes;
+use Illuminate\Support\Facades\DB;
 
 class Authority extends Model
 {

+ 5 - 0
app/Http/Controllers/MenuController.php

@@ -113,4 +113,9 @@ class MenuController extends Controller
         }
         $this->success();
     }
+
+    public function get()
+    {
+        return app("MenuService")->getVisibleFunctionList();
+    }
 }

+ 57 - 33
app/Http/Controllers/TestController.php

@@ -82,6 +82,7 @@ use App\Store;
 use App\StationTaskBatch;
 use App\StoreItem;
 use App\Unit;
+use App\User;
 use App\UserDetail;
 use App\UserDutyCheck;
 use App\ValueStore;
@@ -131,44 +132,67 @@ class TestController extends Controller
     public function test()
     {
         ini_set('max_execution_time',9999);
-        $mapping = [
-            "43"=>[647],"40"=>[648,649,650],
-            "612"=>[651],"210"=>[652],"274" => [653],"314"=>[654],"327"=>[655],
-            "329"=>[656],"344"=>[657],"159"=>[646,658],"164"=>[660],
-            "206"=>[661,662],"205"=>[663,664,665],"272"=>[666,667],
-            "320"=>[668],"220"=>[669],"223"=>[670],"616"=>[671],"310"=>[672],
-            "348"=>[673],"538"=>[674,675],"311"=>[676],"16"=>[677],"133"=>[678,679],
-            "488"=>[688],"557"=>[689,690]
-
-        ];
-        foreach ($mapping as $key=>$arr){
-            $roles = DB::select(DB::raw("SELECT * FROM authority_role WHERE id_authority = ?"),[$key]);
-            if (!$roles)continue;
-            foreach ($roles as $role){
-                foreach ($arr as $aId){
-                    DB::insert(DB::raw("INSERT INTO authority_role(id_authority,id_role) values (?,?)"),[$aId,$role->id_role]);
+        DB::beginTransaction();
+        try{
+            $mapping = [
+                "43"=>[647],"40"=>[648,649,650],
+                "612"=>[651],"210"=>[652],"274" => [653],"314"=>[654],"327"=>[655],
+                "329"=>[656],"344"=>[657],"159"=>[646,658],"164"=>[660],
+                "206"=>[661,662],"205"=>[663,664,665],"272"=>[666,667],
+                "320"=>[668],"220"=>[669],"223"=>[670],"616"=>[671],"310"=>[672],
+                "348"=>[673],"538"=>[674,675],"311"=>[676],"16"=>[677],"133"=>[678,679],
+                "488"=>[688],"557"=>[689,690]
+            ];
+            foreach ($mapping as $key=>$arr){
+                $roles = DB::select(DB::raw("SELECT * FROM authority_role WHERE id_authority = ?"),[$key]);
+                if (!$roles)continue;
+                foreach ($roles as $role){
+                    foreach ($arr as $aId){
+                        DB::insert(DB::raw("INSERT INTO authority_role(id_authority,id_role) values (?,?)"),[$aId,$role->id_role]);
+                    }
                 }
             }
-        }
-        foreach (Authority::query()->where("type","工作组")->get() as $authority){
-            $usId = $authority->relevance;
-            $auId = $authority->id;
-            $roles = DB::select(DB::raw("SELECT * FROM authority_role WHERE id_authority = ?"),[$auId]);
-            if (!$roles)continue;
-            foreach ($roles as $role){
-                DB::insert(DB::raw("INSERT INTO role_user_work_group(role_id,user_work_group_id) values (?,?)"),[$role->id_role,$usId]);
+            $re = [
+                257=>1,
+                258=>2,
+                259=>3,
+                260=>4,
+                261=>5,
+                262=>6,
+                263=>7,
+                264=>8,
+                265=>9,
+                266=>10,
+                312=>11,
+                313=>12,
+                510=>13,
+            ];
+            foreach ($re as $key=>$val){
+                $usId = $val;
+                $auId = $key;
+                $roles = DB::select(DB::raw("SELECT * FROM authority_role WHERE id_authority = ?"),[$auId]);
+                if (!$roles)continue;
+                foreach ($roles as $role){
+                    DB::insert(DB::raw("INSERT INTO role_user_work_group(role_id,user_work_group_id) values (?,?)"),[$role->id_role,$usId]);
+                }
             }
-        }
-        $owners = Authority::query()->where("name","like","\_%")->get();
-        foreach ($owners as $owner){
-            $owId = ltrim($owner->name,"_");
-            $auId = $owner->id;
-            $roles = DB::select(DB::raw("SELECT * FROM authority_role WHERE id_authority = ?"),[$auId]);
-            if (!$roles)continue;
-            foreach ($roles as $role){
-                DB::insert(DB::raw("INSERT INTO owner_role(owner_id,role_id) values (?,?)"),[$owId,$role->id_role]);
+            $owners = Authority::query()->where("name","like","\_%")->get();
+            foreach ($owners as $owner){
+                $owId = ltrim($owner->name,"_");
+                $auId = $owner->id;
+                $roles = DB::select(DB::raw("SELECT * FROM authority_role WHERE id_authority = ?"),[$auId]);
+                if (!$roles)continue;
+                foreach ($roles as $role){
+                    DB::insert(DB::raw("INSERT INTO owner_role(owner_id,role_id) values (?,?)"),[$owId,$role->id_role]);
+                }
             }
+            DB::commit();
+        }catch (\Exception $e){
+            DB::rollBack();
+            dd($e->getMessage());
         }
+        app("MenuService")->setMenu();//重建菜单缓存
+        app("AuthorityService")->removeAllAuth();//移除所有用户权限缓存,这将在用户下次访问时重新建立
     }
     public function orderCreateBill()
     {

+ 1 - 1
app/Http/Controllers/WaybillController.php

@@ -53,7 +53,7 @@ class WaybillController extends Controller
      */
     public function index(Request $request,OwnerService $ownerService,LogisticService $logisticService)
     {
-        if(!Gate::allows('运输管理-运单-查询')){ return view("denied");  }
+        if(!Gate::allows('运输管理-运单-查询')){ return view("exception.authority");  }
         $paginateParams = $request->input();
         $waybills=app('waybillService')->paginate($request->input());
         return view('transport.waybill.index', [

+ 8 - 4
app/User.php

@@ -8,6 +8,7 @@ use Illuminate\Notifications\Notifiable;
 use Illuminate\Foundation\Auth\User as Authenticatable;
 use Illuminate\Support\Collection;
 use Illuminate\Support\Facades\Cache;
+use Illuminate\Support\Facades\DB;
 use Illuminate\Support\Facades\Gate;
 use App\Traits\ModelTimeFormat;
 
@@ -118,11 +119,12 @@ class User extends Authenticatable
             });
             return $ownerIds;
         }
-        $this->authorities()->each(function(Authority $authority)use(&$ownerIds){
+        return array_column(DB::table("owner_role")->whereIn("role_id",array_column($this->roles->toArray(),"id"))->get()->toArray(),"owner_id");
+        /*$this->authorities()->each(function(Authority $authority)use(&$ownerIds){
             $ownerId=$authority->getOwnerIdAttribute();
             if($ownerId){array_push($ownerIds,$ownerId);}
         });
-        return array_unique($ownerIds);
+        return array_unique($ownerIds);*/
     }
     function getPermittingWorkgroupIds($allowAll=false){
         $workgroupIds=[];
@@ -132,9 +134,11 @@ class User extends Authenticatable
                 array_push($workgroupIds,$workgroup['id']);
             });
         }else{
-            $this->authorities()->each(function(Authority $authority)use(&$workgroupIds){
+            $workgroupIds = array_column(DB::table("role_user_work_group")
+                ->whereIn("role_id",array_column($this->roles->toArray(),"id"))->get()->toArray(),"user_work_group_id");
+            /*$this->authorities()->each(function(Authority $authority)use(&$workgroupIds){
                 if($authority->type=="工作组"){array_push($workgroupIds,$authority->relevance);}
-            });
+            });*/
         }
         return $workgroupIds;
     }

+ 12 - 1
resources/views/layouts/app.blade.php

@@ -38,7 +38,9 @@
 @yield('script')
 {{-- 必须在app.js前边--}}
 <script type="text/javascript">
-    let menuList = JSON.parse(localStorage.getItem("menus"));
+    let menus = localStorage.getItem("menus");
+    if (menus===null)menus = getMenu();
+    let menuList = JSON.parse(menus);
     let baseUrl = "{{url('')}}";
     let currentUri = window.location.href.slice(baseUrl.length+1);
     let relativeUrl = currentUri.split(/[/?]/);
@@ -138,6 +140,15 @@
                 return li;
         }
     }
+    function getMenu() {
+        let xhr = new XMLHttpRequest();
+        let url = "{{url('getMenu')}}";
+        xhr.open('POST', url, false);
+        xhr.setRequestHeader('Content-Type', 'application/json');
+        xhr.setRequestHeader('X-CSRF-Token', "{{ csrf_token() }}");
+        xhr.send('');
+        return xhr.responseText;
+    }
 </script>
 <script src="{{ mix('js/app.js') }}"></script>
 <script>

+ 1 - 0
routes/web.php

@@ -36,6 +36,7 @@ Route::group(['prefix'=>'password'],function(){
 
 /** 全局 */
 Route::get('denied',function (){return view('exception.authority');});
+Route::post('getMenu','MenuController@get');
 /** 基础设置 */
 Route::group(['prefix'=>'maintenance'],function(){
     /** 菜单 */