Kaynağa Gözat

菜单权限变更记录

Zhouzhendong 4 yıl önce
ebeveyn
işleme
04286bdd5f

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

@@ -70,7 +70,7 @@ class AuthorityController extends Controller
     public function destroy()
     {
         $this->gate("权限-删除");
-        Authority::destroy(request("ids"));
+        Authority::query()->whereIn('id',request("ids"))->get()->each(function ($auth){$auth->delete();});
         app("AuthorityService")->removeAllAuth();//刷掉权限缓存
         $this->success();
     }

+ 10 - 7
app/Http/Controllers/MenuController.php

@@ -42,13 +42,16 @@ class MenuController extends Controller
         if (request()->has("font_style"))$update["font_style"] = request("font_style");
         if (request()->has("route"))$update["route"] = request("route");
         if (request()->has("diff")){
-            $diff = request("diff");
-            Menu::query()->whereIn("id",request("child"))->update(["level"=>DB::raw("level - {$diff}")]);
+            $diff = request("diff"); //TODO 此处需要查询出模型 ->get()->each()来进行update操作以确保观察者检测到 下面同理
+            Menu::query()->whereIn("id",request("child"))->get()->each(function ($menu)use($diff){
+               $menu->update(["level"=>DB::raw("level - {$diff}")]);
+            });
         }
-        if ($update && Menu::query()->where("id",request("id"))->update($update)){
+        if ($update){
+            Menu::query()->where("id",request("id"))->first()->update($update);
             app("MenuService")->setMenu();//重建菜单缓存
             if ($authorityUpdate && ($mapping[request("id")] ?? null)){
-                Authority::query()->where("id",$mapping[request("id")])->update($authorityUpdate);
+                Authority::query()->where("id",$mapping[request("id")])->first()->update($authorityUpdate);
                 app("AuthorityService")->removeAllAuth();//移除所有用户权限缓存,这将在用户下次访问时重新建立
             }
         }
@@ -70,7 +73,7 @@ class MenuController extends Controller
         $mapping = app("MenuService")->getMenuAndAuthorityMapping();
         if ($id){
             $this->gate("基础设置-菜单-编辑");
-            Menu::query()->where("id",$id)->update([
+            Menu::query()->where("id",$id)->first()->update([
                 "name" => request("name"),
                 "route" => request("route"),
                 "font" => request("font"),
@@ -111,11 +114,11 @@ class MenuController extends Controller
         $mapping = app("MenuService")->getMenuAndAuthorityMapping();
         foreach ($ids as $id)if (isset($mapping[$id]))$authIds[] = $mapping[$id];
 
-        Menu::destroy($ids);
+        Menu::query()->whereIn("id",$ids)->get()->each(function ($menu){$menu->delete();});
         app("MenuService")->setMenu();//重建菜单缓存
         if ($authIds){
             DB::table("authority_role")->whereIn("id_authority",$authIds)->delete();
-            Authority::destroy($ids);
+            Authority::query()->whereIn("id",$authIds)->get()->each(function ($auth){$auth->delete();});
             app("AuthorityService")->removeAllAuth();//移除所有用户权限缓存,这将在用户下次访问时重新建立
         }
         $this->success();

+ 35 - 2
app/Http/Controllers/TestController.php

@@ -155,10 +155,43 @@ class TestController extends Controller
         dd("OK");
     }
 
+    public function test2()
+    {
+        ini_set('max_execution_time',-1);
+        $myfile = fopen(base_path()."\\database\\data\\menus.data", "a+");
+        foreach (Menu::query()->get() as $menu){
+            $txt = "INSERT INTO menus";
+            $columns = "(";
+            $values = "(";
+            foreach ($menu->toArray() as $col=>$val){
+                $columns .= "{$col},";
+                $values .= "'{$val}',";
+            }
+            $columns = mb_substr($columns,0,-1);
+            $values = mb_substr($values,0,-1);
+            $txt .= "{$columns}) VALUES{$values});\r\n";
+            fwrite($myfile, $txt);
+        }
+        fclose($myfile);
+
+        $myfile = fopen(base_path()."\\database\\data\\authorities.data", "a+");
+        foreach (Authority::query()->get() as $authority){
+            $columns = "(";
+            $values = "(";
+            foreach ($authority->toArray() as $col=>$val){
+                $columns .= "{$col},";
+                $values .= "'{$val}',";
+            }
+            $columns = mb_substr($columns,0,-1);
+            $values = mb_substr($values,0,-1);
+            $txt .= "{$columns}) VALUES{$values});\r\n";
+            fwrite($myfile, $txt);
+        }
+        fclose($myfile);
+        dd("OK");
+    }
     public function test()
     {
-        dd(app("MenuService")->getVisibleFunctionList());
-        ini_set('max_execution_time',9999);
         DB::beginTransaction();
         try{
             $mapping = [

+ 66 - 0
app/Observers/AuthorityObserver.php

@@ -0,0 +1,66 @@
+<?php
+
+namespace App\Observers;
+
+use App\Authority;
+
+class AuthorityObserver
+{
+    /**
+     * Handle the authority "created" event.
+     *
+     * @param  \App\Authority  $authority
+     * @return void
+     */
+    public function created(Authority $authority)
+    {
+        if (env('APP_ENV')!='production')return;
+        $txt = "INSERT INTO authorities";
+        $columns = "(";
+        $values = "(";
+        foreach ($authority->toArray() as $col=>$val){
+            $columns .= "{$col},";
+            $values .= "'{$val}',";
+        }
+        $columns = mb_substr($columns,0,-1);
+        $values = mb_substr($values,0,-1);
+        $txt .= "{$columns}) VALUES{$values});\r\n";
+        $this->write($txt);
+    }
+
+    /**
+     * Handle the authority "updated" event.
+     *
+     * @param  \App\Authority  $authority
+     * @return void
+     */
+    public function updated(Authority $authority)
+    {
+        if (env('APP_ENV')!='production')return;
+        $txt = "UPDATE authorities SET ";
+        foreach ($authority->toArray() as $col=>$val)$txt .= "{$col}='{$val}',";
+        $txt = mb_substr($txt,0,-1);
+        $txt .= " WHERE id = {$authority->id};\r\n";
+        $this->write($txt);
+    }
+
+    /**
+     * Handle the authority "deleted" event.
+     *
+     * @param  \App\Authority  $authority
+     * @return void
+     */
+    public function deleted(Authority $authority)
+    {
+        if (env('APP_ENV')!='production')return;
+        $txt = "DELETE FROM authorities WHERE id = {$authority->id};\r\n";
+        $this->write($txt);
+    }
+
+    private function write($txt)
+    {
+        $myfile = fopen(base_path()."\\database\\data\\authorities.data", "a+");
+        fwrite($myfile, $txt);
+        fclose($myfile);
+    }
+}

+ 66 - 0
app/Observers/MenuObserver.php

@@ -0,0 +1,66 @@
+<?php
+
+namespace App\Observers;
+
+use App\Menu;
+
+class MenuObserver
+{
+    /**
+     * Handle the menu "created" event.
+     *
+     * @param  \App\Menu  $menu
+     * @return void
+     */
+    public function created(Menu $menu)
+    {
+        if (env('APP_ENV')!='production')return;
+        $txt = "INSERT INTO menus";
+        $columns = "(";
+        $values = "(";
+        foreach ($menu->toArray() as $col=>$val){
+            $columns .= "{$col},";
+            $values .= "'{$val}',";
+        }
+        $columns = mb_substr($columns,0,-1);
+        $values = mb_substr($values,0,-1);
+        $txt .= "{$columns}) VALUES{$values});\r\n";
+        $this->write($txt);
+    }
+
+    /**
+     * Handle the menu "updated" event.
+     *
+     * @param  \App\Menu  $menu
+     * @return void
+     */
+    public function updated(Menu $menu)
+    {
+        if (env('APP_ENV')!='production')return;
+        $txt = "UPDATE menus SET ";
+        foreach ($menu->toArray() as $col=>$val)$txt .= "{$col}='{$val}',";
+        $txt = mb_substr($txt,0,-1);
+        $txt .= " WHERE id = {$menu->id};\r\n";
+        $this->write($txt);
+    }
+
+    /**
+     * Handle the menu "deleted" event.
+     *
+     * @param  \App\Menu  $menu
+     * @return void
+     */
+    public function deleted(Menu $menu)
+    {
+        if (env('APP_ENV')!='production')return;
+        $txt = "DELETE FROM menus WHERE id = {$menu->id};\r\n";
+        $this->write($txt);
+    }
+
+    private function write($txt)
+    {
+        $myfile = fopen(base_path()."\\database\\data\\menus.data", "a+");
+        fwrite($myfile, $txt);
+        fclose($myfile);
+    }
+}

+ 2 - 6
app/Observers/OwnerObserver.php

@@ -15,16 +15,12 @@ class OwnerObserver
     public function created(Owner $owner)
     {
         app("OwnerAreaReportService")->notExistToInsert([$owner]);
-        if(env('APP_ENV')=='production')
-            app("OwnerService")->syncPush($owner);
-            //app("OwnerService")->createAuthority($owner);
+        if(env('APP_ENV')=='production')app("OwnerService")->syncPush($owner);
     }
 
     public function updated(Owner $owner)
     {
-        if(env('APP_ENV')=='production'){
-            app("OwnerService")->syncUpdate($owner);
-        }
+        if(env('APP_ENV')=='production')app("OwnerService")->syncUpdate($owner);
     }
 
     public function deleted(Owner $owner)

+ 6 - 0
app/Providers/AppServiceProvider.php

@@ -2,11 +2,15 @@
 
 namespace App\Providers;
 
+use App\Authority;
 use App\Http\Controllers\Controller;
 use App\Jobs\LogisticSFSync;
 use App\Jobs\LogisticYDSync;
 use App\Jobs\LogisticYTOSync;
 use App\Jobs\LogisticZopSync;
+use App\Menu;
+use App\Observers\AuthorityObserver;
+use App\Observers\MenuObserver;
 use App\Observers\OwnerObserver;
 use App\Observers\UserWorkGroupObserver;
 use App\Owner;
@@ -344,5 +348,7 @@ class AppServiceProvider extends ServiceProvider
     {
         Owner::observe(OwnerObserver::class);
         UserWorkgroup::observe(UserWorkGroupObserver::class);
+        Menu::observe(MenuObserver::class);
+        Authority::observe(AuthorityObserver::class);
     }
 }

+ 5 - 0
app/Services/common/BatchUpdateService.php

@@ -70,6 +70,11 @@ class BatchUpdateService
             // DB::update
             $bool = DB::connection($connection)->update($updateSql, $bindings);
             app('LogService')->log(__METHOD__, __FUNCTION__, '批量更新:' . $tableName . ' | '.json_encode($multipleData).' | 溯源:'.json_encode($debug));
+            if (($tableName == 'authorities' || $tableName == 'menus') && env('APP_ENV')=='production'){
+                $myfile = fopen(base_path()."\\database\\data\\{$tableName}.data", "a+");
+                fwrite($myfile, vsprintf(str_replace('?',"'%s'",$updateSql), $bindings).";\r\n");
+                fclose($myfile);
+            }
             return $bool;
         } catch (\Exception $e) {
             app('LogService')->log(__METHOD__, __FUNCTION__, '批量更新失败' . $tableName . ' | '.json_encode($multipleData).' | 溯源:'.json_encode($debug) . $e->getMessage() . $e->getTraceAsString());

+ 0 - 0
database/data/authorities.data


+ 0 - 0
database/data/menus.data