瀏覽代碼

管理员的数据权限值

liugc 1 年之前
父節點
當前提交
fb3f3b3677

+ 1 - 2
app/adminapi/controller/auth/AdminController.php

@@ -114,8 +114,7 @@ class AdminController extends BaseAdminController
     public function mySelf()
     public function mySelf()
     {
     {
         $result = cache('mySelf');
         $result = cache('mySelf');
-        //if(empty($result)){
-        if(1){
+        if(empty($result) || !env('APP_DEBUG')){
             $result = AdminLogic::detail(['id' => $this->adminId], 'auth');
             $result = AdminLogic::detail(['id' => $this->adminId], 'auth');
             cache('mySelf',$result);
             cache('mySelf',$result);
         }
         }

+ 1 - 51
app/adminapi/controller/jobs_rules/JobsRulesController.php

@@ -108,57 +108,7 @@ class JobsRulesController extends BaseAdminController
     public function ruleData()
     public function ruleData()
     {
     {
         $params = request()->post();
         $params = request()->post();
-        $result = [];
-        /*1	省
-        2	市
-        3	销售组
-        4	后台管理员
-        5	代理人
-        6	销售*/
-        $province_ids = array_column($params['rules_data'], 'rule_value', 'rule_id');
-        $province_ids = $province_ids[1]??[];
-        switch ($params['id']) {
-            case 1:
-                $result = TableDataLogic::provinces();
-                // 数组的开始位置添加一个数组
-                array_unshift($result, [
-                    "id" => 'self',
-                    "name" => "管理员所属",
-                    "value" => "self",
-                    "type_value" => "data_table_provinces",
-                ]);
-                break;
-            case 2:
-                $result = TableDataLogic::citys($province_ids);
-                // 数组的开始位置添加一个数组
-                array_unshift($result, [
-                    "id" => 'self',
-                    "name" => "管理员所属",
-                    "value" => "self",
-                    "type_value" => "data_table_citys",
-                ]);
-                break;
-            case 3:
-                $result = TableDataLogic::saleGroup();
-                break;
-            case 4:
-                $result = TableDataLogic::admins();
-                // 数组的开始位置添加一个数组
-                array_unshift($result, [
-                    "id" => 'self',
-                    "name" => "管理员所属",
-                    "value" => "self",
-                    "type_value" => "data_table_admins",
-                ]);
-                break;
-            case 5:
-                $result = TableDataLogic::propertyHead();
-                break;
-            case 6:
-                $result = TableDataLogic::sale();
-                break;
-        }
         //provinces citys saleGroup admins propertyHead sale
         //provinces citys saleGroup admins propertyHead sale
-        return $this->data($result);
+        return $this->data(JobsRulesLogic::ruleData($params));
     }
     }
 }
 }

+ 35 - 0
app/adminapi/logic/auth/AdminLogic.php

@@ -14,6 +14,7 @@
 
 
 namespace app\adminapi\logic\auth;
 namespace app\adminapi\logic\auth;
 
 
+use app\adminapi\logic\jobs_rules\JobsRulesLogic;
 use app\common\cache\AdminAuthCache;
 use app\common\cache\AdminAuthCache;
 use app\common\enum\YesNoEnum;
 use app\common\enum\YesNoEnum;
 use app\common\logic\BaseLogic;
 use app\common\logic\BaseLogic;
@@ -23,6 +24,7 @@ use app\common\model\auth\AdminJobs;
 use app\common\model\auth\AdminRole;
 use app\common\model\auth\AdminRole;
 use app\common\model\auth\AdminSession;
 use app\common\model\auth\AdminSession;
 use app\common\cache\AdminTokenCache;
 use app\common\cache\AdminTokenCache;
+use app\common\model\dept\Jobs;
 use app\common\service\FileService;
 use app\common\service\FileService;
 use think\facade\Config;
 use think\facade\Config;
 use think\facade\Db;
 use think\facade\Db;
@@ -334,4 +336,37 @@ class AdminLogic extends BaseLogic
         }
         }
     }
     }
 
 
+    /**
+     * 获取当前登录管理员岗位数据权限信息
+     * @param $adminId
+     * @param $jobsIds
+     * @return void
+     * @throws \Exception
+     */
+    public static function getDataPermissions($adminId) : array
+    {
+        $jobsIds = AdminJobs::where('admin_id', $adminId)->column('jobs_id');
+        $rules = [];
+        if (!empty($jobsIds)) {
+            $list = Jobs::whereIn('id', $jobsIds)->where('status', YesNoEnum::YES)->select()->toArray();
+            foreach ($list as $item) {
+                //$item['rules_data']
+                /*[{"block_key":"1","rule_id":"1","value_lists":[],"rule_value":["self","37"]},
+                    {"block_key":"2","rule_id":"2","value_lists":[],"rule_value":["self","124"]},
+                    {"block_key":"3","rule_id":"4","value_lists":[],"rule_value":["self","4"]},
+                    {"block_key":"4","rule_id":"5","value_lists":[],"rule_value":["2","6","5"]}]*/
+                $rules_data = array_column($item['rules_data'], 'rule_value', 'rule_id');
+                foreach ($rules_data as $rule_id => $rule_value) {
+                    if(isset($rules[$rule_id])){
+                        $rules[$rule_id] = array_unique(array_merge($rules[$rule_id],$rule_value));
+                    }else{
+                        $rules[$rule_id] = $rule_value;
+                    }
+                }
+            }
+            $rules = JobsRulesLogic::ruleSelfValue($adminId,$rules);
+        }
+        return $rules??[];
+    }
+
 }
 }

+ 110 - 0
app/adminapi/logic/jobs_rules/JobsRulesLogic.php

@@ -15,6 +15,8 @@
 namespace app\adminapi\logic\jobs_rules;
 namespace app\adminapi\logic\jobs_rules;
 
 
 
 
+use app\common\logic\TableDataLogic;
+use app\common\model\auth\Admin;
 use app\common\model\jobs_rules\JobsRules;
 use app\common\model\jobs_rules\JobsRules;
 use app\common\logic\BaseLogic;
 use app\common\logic\BaseLogic;
 use think\facade\Db;
 use think\facade\Db;
@@ -109,4 +111,112 @@ class JobsRulesLogic extends BaseLogic
     {
     {
         return JobsRules::findOrEmpty($params['id'])->toArray();
         return JobsRules::findOrEmpty($params['id'])->toArray();
     }
     }
+
+    /**
+     * @notes 获取规则数据
+     * @param $params
+     * @return array
+     * @date 2024/12/31 13:50
+     */
+    public static function ruleData($params)
+    {
+        $result = [];
+        /*1	省
+        2	市
+        3	销售组
+        4	后台管理员
+        5	代理人
+        6	销售*/
+        $province_ids = array_column($params['rules_data'], 'rule_value', 'rule_id');
+        $province_ids = $province_ids[1]??[];
+        switch ($params['id']) {
+            case 1:
+                $result = TableDataLogic::provinces();
+                // 数组的开始位置添加一个数组
+                array_unshift($result, [
+                    "id" => 'self',
+                    "name" => "管理员所属",
+                    "value" => "self",
+                    "type_value" => "data_table_provinces",
+                ]);
+                break;
+            case 2:
+                $result = TableDataLogic::citys($province_ids);
+                // 数组的开始位置添加一个数组
+                array_unshift($result, [
+                    "id" => 'self',
+                    "name" => "管理员所属",
+                    "value" => "self",
+                    "type_value" => "data_table_citys",
+                ]);
+                break;
+            case 3:
+                $result = TableDataLogic::saleGroup();
+                break;
+            case 4:
+                $result = TableDataLogic::admins();
+                // 数组的开始位置添加一个数组
+                array_unshift($result, [
+                    "id" => 'self',
+                    "name" => "管理员所属",
+                    "value" => "self",
+                    "type_value" => "data_table_admins",
+                ]);
+                break;
+            case 5:
+                $result = TableDataLogic::propertyHead();
+                break;
+            case 6:
+                $result = TableDataLogic::sale();
+                break;
+        }
+        //provinces citys saleGroup admins propertyHead sale
+        return $result?:[];
+    }
+
+    /**
+     * 替换规则中的self为管理员所属的值
+     * @param $adminId
+     * @param $rules
+     * @return array
+     * @throws \think\db\exception\DataNotFoundException
+     * @throws \think\db\exception\DbException
+     * @throws \think\db\exception\ModelNotFoundException
+     */
+    public static function ruleSelfValue($adminId,$rules)
+    {
+        $admin = Admin::where('admin_id', $adminId)->where('root','=',0)->find()->toArray();
+        function replace_value($rule,$replace_value) {
+            array_walk($rule,function (&$value,$key) use($replace_value) {
+                if ($value == 'self'){
+                    $value = $replace_value;
+                }
+            });
+            return $rule;
+        }
+        //[1=>[,,,,,],2=>[,,,,,]]
+        //provinces citys saleGroup admins propertyHead sale
+        foreach ($rules as $key => &$rule) {
+            switch ($key) {
+                case 1:
+                    $rule = replace_value($rule,$admin['province']);
+                    break;
+                case 2:
+                    $rule = replace_value($rule,$admin['city']);
+                    break;
+                case 4:
+                    $rule = replace_value($rule,$admin['id']);
+                    break;
+
+                case 3:
+                case 5:
+                case 6:
+                    break;
+            }
+        }
+        return $rules?:[];
+    }
+
+
+
 }
 }

+ 5 - 0
app/common/cache/AdminTokenCache.php

@@ -16,6 +16,7 @@
 namespace app\common\cache;
 namespace app\common\cache;
 
 
 
 
+use app\adminapi\logic\auth\AdminLogic;
 use app\common\model\auth\Admin;
 use app\common\model\auth\Admin;
 use app\common\model\auth\AdminSession;
 use app\common\model\auth\AdminSession;
 use app\common\model\auth\SystemRole;
 use app\common\model\auth\SystemRole;
@@ -87,6 +88,9 @@ class AdminTokenCache extends BaseCache
             $roleName = trim($roleName, '/');
             $roleName = trim($roleName, '/');
         }
         }
 
 
+        // 获取数据权限信息
+        $data_rules = AdminLogic::getDataPermissions($admin->id);
+
         $adminInfo = [
         $adminInfo = [
             'admin_id' => $admin->id,
             'admin_id' => $admin->id,
             'root' => $admin->root,
             'root' => $admin->root,
@@ -98,6 +102,7 @@ class AdminTokenCache extends BaseCache
             'terminal' => $adminSession->terminal,
             'terminal' => $adminSession->terminal,
             'expire_time' => $adminSession->expire_time,
             'expire_time' => $adminSession->expire_time,
             'login_ip' => request()->ip(),
             'login_ip' => request()->ip(),
+            'data_rules' => $data_rules,
         ];
         ];
         $this->set($this->prefix . $token, $adminInfo, new \DateTime(Date('Y-m-d H:i:s', $adminSession->expire_time)));
         $this->set($this->prefix . $token, $adminInfo, new \DateTime(Date('Y-m-d H:i:s', $adminSession->expire_time)));
         return $this->getAdminInfo($token);
         return $this->getAdminInfo($token);