Browse Source

多门店

cqpCow 1 year ago
parent
commit
adc6209840

+ 1 - 1
app/Http/Controllers/Api/LoginController.php

@@ -20,6 +20,6 @@ class LoginController extends BaseController
         //生成token
         $jwtToken = TokenService::getToken($return['id']);
 
-        return $this->json_return(200,'', ['id' => $return['id'],'token' => $jwtToken, 'emp_name'=>$return['name'],'is_main'=>$return['is_main'],'top_depart_id'=>$return['top_depart_id'],'top_depart_title'=>$return['top_depart_title'], 'all_top' => $return['all_top']]);
+        return $this->json_return(200,'', ['id' => $return['id'],'token' => $jwtToken, 'emp_name'=>$return['name'], 'all_top' => $return['all_top']]);
     }
 }

+ 9 - 1
app/Http/Middleware/CheckLogin.php

@@ -43,7 +43,13 @@ class CheckLogin
         //角色里所有菜单权限
         $data['role_authority'] = EmployeeService::getPersonRoleQx($data['role']);
 
-        $return = EmployeeService::getLoginDepart($result);
+        //当前门店
+        $userSetTopDepartId = $request->header('userSetTopDepartId');
+        if(empty($userSetTopDepartId)) return response()->json(['code'=>1,'msg'=>'当前门店信息缺失','data'=>null]);
+
+        //获取人员门店部门相关信息
+        $return = EmployeeService::getLoginDepart($result, $userSetTopDepartId);
+
         //所属部门
         $data['rule_depart'] = $return[0] ?? [];
         //顶级公司
@@ -58,6 +64,8 @@ class CheckLogin
         $data['head'] = $return[5] ?? [];
         //是否是总公司下的人
         $data['is_behind_main'] = $return[6] ?? 0;
+        //当前部门
+        $data['depart_select'] = $return[7] ?? [];
         //是否库存校验(默认校验)
         $my_top = $data['depart_top'][0] ?? [];
         $is_check_stock = empty($my_top['is_stock']) ? ProductInventorySet::type_one : $my_top['is_stock'];

+ 8 - 1
app/Http/Middleware/CheckWx.php

@@ -63,7 +63,12 @@ class CheckWx
             $data['role'] = EmployeeService::getPersonRole($employee_id);
             //角色里所有菜单权限
             $data['role_authority'] = EmployeeService::getPersonRoleQx($data['role']);
-            $return = EmployeeService::getLoginDepart($employee_id);
+
+            //当前门店
+            $userSetTopDepartId = $request->header('userSetTopDepartId');
+            if(empty($userSetTopDepartId)) return response()->json(['code'=>202,'msg'=>'当前门店信息缺失','data'=>null]);
+
+            $return = EmployeeService::getLoginDepart($employee_id, $userSetTopDepartId);
             //所属部门
             $data['rule_depart'] = $return[0] ?? [];
             //顶级公司
@@ -78,6 +83,8 @@ class CheckWx
             $data['head'] = $return[5] ?? [];
             //是否是总公司下的人
             $data['is_behind_main'] = $return[6] ?? 0;
+            //当前部门
+            $data['depart_select'] = $return[7] ?? [];
             //是否库存校验(默认校验)
             $my_top = $data['depart_top'][0] ?? [];
             $is_check_stock = empty($my_top['is_stock']) ? ProductInventorySet::type_one : $my_top['is_stock'];

+ 197 - 36
app/Service/EmployeeService.php

@@ -1100,14 +1100,17 @@ class EmployeeService extends Service
         if($res['is_admin'] != Employee::IS_ADMIN) return [false,'该账号不能登录!'];
         if($res['state'] == Employee::NOT_USE) return [false,'账号停用!'];
 
-        //总社分社信息
-        $return = EmployeeService::getLoginDepart($res['id']);
-        $is_main = $return[4] ?? 0;
-        $depart_top = $return[1] ?? [];
-        $depart_top_title = $depart_top[0]['title'] ?? "";
-        $depart_top_id = $depart_top[0]['depart_id'] ?? 0;
-
-        return [true, ['id'=>$res['id'], 'name'=>$res['emp_name'], 'is_main' => $is_main, 'top_depart_id' => $depart_top_id,'top_depart_title' => $depart_top_title,'all_top'=> $depart_top]];
+        //门店的信息(即将废弃)
+//        $return = EmployeeService::getLoginDepartfq($res['id']);
+//        $is_main = $return[4] ?? 0;
+//        $depart_top = $return[1] ?? [];
+//        $depart_top_title = $depart_top[0]['title'] ?? "";
+//        $depart_top_id = $depart_top[0]['depart_id'] ?? 0;
+
+        //门店的信息
+        $login_message = EmployeeService::getLoginMessage($res['id']);
+
+        return [true, ['id'=>$res['id'],'name'=>$res['emp_name'],'all_top'=> $login_message]];
     }
 
     /**
@@ -1296,7 +1299,7 @@ class EmployeeService extends Service
      * @param $employee_id
      * @return array|string[]
      */
-    public static function getLoginDepart($employee_id){
+    public static function getLoginDepart($employee_id, $top_depart_id = 0){
         if(empty($employee_id)) return [];
 
         //自己绑定的部门 启用的部门
@@ -1311,6 +1314,8 @@ class EmployeeService extends Service
             ->get()->toArray();
 
         $top = $map = $rule = $head = [];
+        $res_data = [];//获取当前所在部门(也可能是门店 为了 depart_id)
+        $my_depart = [];//当前门店下 我勾选的门店以及部门信息
         $is_all_depart = $is_behind_main = 0;
         if(! empty($depart)){
             //库存校验
@@ -1319,8 +1324,9 @@ class EmployeeService extends Service
             //所有部门
             $list = Depart::where('del_time',0)->get()->toArray();
             $depart_map = array_column($list,null,'id');
+
             foreach ($depart as $key => $value){
-                if($value['parent_id'] == 0){//顶级
+                if($value['parent_id'] == 0){
                     $is_stock = $set_map[$value['depart_id']] ?? 1;
                     $depart[$key]['is_stock'] = $is_stock;
                     $top[$value['depart_id']] = [
@@ -1331,30 +1337,69 @@ class EmployeeService extends Service
                         'is_stock' => $is_stock,
                     ];
                     $map[$value['depart_id']] = $value['depart_id'];
-                    if(! empty($value['is_main']) && ! $is_all_depart) $is_all_depart = 1;
                     if(! empty($value['is_main']) && ! $is_behind_main) $is_behind_main = 1;
+
+                    if($value['depart_id'] == $top_depart_id) {
+                        $my_depart[] = $depart[$key];
+                        if(empty($res_data)){
+                            $res_data = $depart[$key];
+                        }else{
+                            if($value['depart_id'] >= $res_data['depart_id']) $res_data = $depart[$key];
+                        }
+                    }
                 }else{
                     $t = self::getTopParentId($value['depart_id'],$list);
                     if($t && isset($depart_map[$t])) {
                         $is_stock = $set_map[$t] ?? 1;
                         $depart[$key]['is_stock'] = $is_stock;
                         $t_tmp = $depart_map[$t] ?? [];
-                        $top[$t_tmp['id']] = [
-                            'depart_id' => $t_tmp['id'],
-                            'is_main' => $t_tmp['is_main'],
-                            'basic_type_id' => $t_tmp['basic_type_id'],
-                            'title' => $t_tmp['title'],
-                            'is_stock' => $set_map[$t] ?? 1,
-                        ];
+                        if( ! isset($top[$t_tmp['id']])){
+                            $top[$t_tmp['id']] = [
+                                'depart_id' => $t_tmp['id'],
+                                'is_main' => $t_tmp['is_main'],
+                                'basic_type_id' => $t_tmp['basic_type_id'],
+                                'title' => $t_tmp['title'],
+                                'is_stock' => $set_map[$t] ?? 1,
+                            ];
+                        }
                         $map[$value['depart_id']] = $t;
-                        if(! empty($t_tmp['is_main']) && $value['is_main'] && ! $is_all_depart) $is_all_depart = 1;
                         if(! empty($t_tmp['is_main']) && ! $is_behind_main) $is_behind_main = 1;
                     }
+                    if($t == $top_depart_id){
+                        $my_depart[] = $depart[$key];
+                        if(empty($res_data)){
+                            $res_data = $depart[$key];
+                        }else{
+                            if($value['depart_id'] >= $res_data['depart_id']) $res_data = $depart[$key];
+                        }
+                    }
                 }
             }
-            foreach ($depart as $value){
+
+            //-------------重组当前所在门店
+            $top = array_values($top);
+            usort($top, function($a, $b) {
+                return $b['is_main'] - $a['is_main'];
+            });
+            if(! empty($top_depart_id)){
+                $targetArray = null;
+                foreach ($top as $key => $value) {
+                    if ($value['depart_id'] === $top_depart_id) {
+                        $targetArray = $value;
+                        unset($top[$key]); // 从原数组中移除目标数组
+                        break; // 找到后跳出循环
+                    }
+                }
+                if ($targetArray) array_unshift($top, $targetArray);
+            }
+            //-------------重组当前所在门店
+
+            //-------------拥有的数据权限(门店、部门)
+            $my_top = $top[0] ?? [];
+            foreach ($my_depart as $value){
                 if(in_array($value['depart_id'],$rule)) continue;
                 if(! $value['parent_id']){ //顶级
+                    if(! empty($value['is_main']) && ! $is_all_depart) $is_all_depart = 1;
                     if($value['is_main']) {//是总公司
                         //所有部门都有
                         $rule = array_column($list,'id');
@@ -1364,6 +1409,8 @@ class EmployeeService extends Service
                         $rule = array_merge_recursive($rule,$depart_id);
                     }
                 }else{//非顶级
+                    if(! empty($my_top['is_main']) && $value['is_main'] && ! $is_all_depart) $is_all_depart = 1;
+
                     if($value['is_main']) {//是总社
                         $top_tmp = $map[$value['depart_id']];
                         if(! empty($depart_map[$top_tmp]['is_main'])){
@@ -1379,55 +1426,169 @@ class EmployeeService extends Service
                     }
                 }
             }
+            //-------------拥有的数据权限(门店、部门)
+
+            //总店
             foreach ($list as $value){
                 if(empty($value['parent_id']) && ! empty($value['is_main'])) $head = $value;
             }
         }
-        $top = array_values($top);
+
         $rule = array_unique($rule);
 
-        return [$depart, $top, $map, $rule, $is_all_depart, $head, $is_behind_main];
+        return [$depart,$top,$map,$rule,$is_all_depart,$head,$is_behind_main,$res_data];
     }
 
-    //判断是否总公司
-    public static function isMain($employee_id){
-        $is_main = 0;
-        if(empty($employee_id)) return $is_main;
+    //获取用户的所属门店信息
+    public static function getLoginMessage($employee_id){
+        if(empty($employee_id)) return [];
+
+        //自己绑定的部门 启用的部门
+        $depart = EmployeeDepartPermission::from('employee_depart_permission as a')
+            ->join('depart as b','b.id','a.depart_id')
+            ->where('a.employee_id',$employee_id)
+            ->where('b.is_use',Depart::IS_UES)
+            ->select('a.depart_id','b.is_main','b.parent_id','b.basic_type_id','b.title')
+            ->orderBy('b.parent_id','asc')
+            ->orderBy('b.is_main','desc')
+            ->orderBy('a.depart_id','asc')
+            ->get()->toArray();
+
+        $top = [];
+        if(! empty($depart)){
+            //所有部门
+            $list = Depart::where('del_time',0)->get()->toArray();
+            $depart_map = array_column($list,null,'id');
+            foreach ($depart as $value){
+                if($value['parent_id'] == 0){//顶级
+                    if(! isset($top[$value['depart_id']])){
+                        $top[$value['depart_id']] = [
+                            'depart_id' => $value['depart_id'],
+                            'is_main' => $value['is_main'],
+                            'title' => $value['title'],
+                        ];
+                    }
+                }else{
+                    //门店
+                    $t = self::getTopParentId($value['depart_id'],$list);
 
-        //admin账号
-        if($employee_id == Employee::SPECIAL_ADMIN) {
-            $is_main = 1;
-            return $is_main;
+                    if($t && isset($depart_map[$t])) {
+                        $t_tmp = $depart_map[$t] ?? [];
+                        $is_all_depart = 0;
+                        if(! empty($t_tmp['is_main']) && $value['is_main']) $is_all_depart = 1;
+                        if(! isset($top[$t_tmp['id']])){
+                            $top[$t_tmp['id']] = [
+                                'depart_id' => $t_tmp['id'],
+                                'is_main' => $is_all_depart,
+                                'title' => $t_tmp['title'],
+                            ];
+                        }else{
+                            if(! empty($is_all_depart)) $top[$t_tmp['id']]['is_main'] = $is_all_depart;
+                        }
+                    }
+                }
+            }
         }
 
+        $top = array_values($top);
+        usort($top, function($a, $b) {
+            return $b['is_main'] - $a['is_main'];
+        });
+
+        return $top;
+    }
+
+    public static function getLoginDepartfq($employee_id){
+        if(empty($employee_id)) return [];
+
         //自己绑定的部门 启用的部门
         $depart = EmployeeDepartPermission::from('employee_depart_permission as a')
             ->join('depart as b','b.id','a.depart_id')
             ->where('a.employee_id',$employee_id)
             ->where('b.is_use',Depart::IS_UES)
-            ->select('a.depart_id','b.is_main','b.parent_id')
+            ->select('a.depart_id','b.is_main','b.parent_id','b.basic_type_id','b.title')
             ->orderBy('b.parent_id','asc')
             ->orderBy('b.is_main','desc')
             ->orderBy('a.depart_id','asc')
             ->get()->toArray();
 
+        $top = $map = $rule = $head = [];
+        $is_all_depart = $is_behind_main = 0;
         if(! empty($depart)){
+            //库存校验
+            $set_map = ProductInventorySet::where('del_time',0)->pluck('param_one','top_depart_id')->toArray();
+
+            //所有部门
             $list = Depart::where('del_time',0)->get()->toArray();
             $depart_map = array_column($list,null,'id');
-            foreach ($depart as $value){
+            foreach ($depart as $key => $value){
                 if($value['parent_id'] == 0){//顶级
-                    if(! empty($value['is_main']) && ! $is_main) $is_main = 1;
+                    $is_stock = $set_map[$value['depart_id']] ?? 1;
+                    $depart[$key]['is_stock'] = $is_stock;
+                    $top[$value['depart_id']] = [
+                        'depart_id' => $value['depart_id'],
+                        'is_main' => $value['is_main'],
+                        'basic_type_id' => $value['basic_type_id'],
+                        'title' => $value['title'],
+                        'is_stock' => $is_stock,
+                    ];
+                    $map[$value['depart_id']] = $value['depart_id'];
+                    if(! empty($value['is_main']) && ! $is_all_depart) $is_all_depart = 1;
+                    if(! empty($value['is_main']) && ! $is_behind_main) $is_behind_main = 1;
                 }else{
                     $t = self::getTopParentId($value['depart_id'],$list);
                     if($t && isset($depart_map[$t])) {
-                        $tmp_is_main = $depart_map[$t]['is_main'] ?? 0;
-                        if(! empty($tmp_is_main) && ! $is_main) $is_main = 1;
+                        $is_stock = $set_map[$t] ?? 1;
+                        $depart[$key]['is_stock'] = $is_stock;
+                        $t_tmp = $depart_map[$t] ?? [];
+                        $top[$t_tmp['id']] = [
+                            'depart_id' => $t_tmp['id'],
+                            'is_main' => $t_tmp['is_main'],
+                            'basic_type_id' => $t_tmp['basic_type_id'],
+                            'title' => $t_tmp['title'],
+                            'is_stock' => $set_map[$t] ?? 1,
+                        ];
+                        $map[$value['depart_id']] = $t;
+                        if(! empty($t_tmp['is_main']) && $value['is_main'] && ! $is_all_depart) $is_all_depart = 1;
+                        if(! empty($t_tmp['is_main']) && ! $is_behind_main) $is_behind_main = 1;
+                    }
+                }
+            }
+            foreach ($depart as $value){
+                if(in_array($value['depart_id'],$rule)) continue;
+                if(! $value['parent_id']){ //顶级
+                    if($value['is_main']) {//是总公司
+                        //所有部门都有
+                        $rule = array_column($list,'id');
+                    }else{//不是总公司
+                        //自己以及子部门
+                        $depart_id = array_merge(self::getAllIds($list,$map[$value['depart_id']]),[$map[$value['depart_id']]]);
+                        $rule = array_merge_recursive($rule,$depart_id);
+                    }
+                }else{//非顶级
+                    if($value['is_main']) {//是总社
+                        $top_tmp = $map[$value['depart_id']];
+                        if(! empty($depart_map[$top_tmp]['is_main'])){
+                            //顶级公司是总公司 所有部门都有
+                            $rule = array_column($list,'id');
+                        }else{
+                            //顶级公司是分公司 分公司所有部门
+                            $depart_id = array_merge(self::getAllIds($list,$top_tmp),[$top_tmp]);
+                            $rule = array_merge_recursive($rule,$depart_id);
+                        }
+                    }else{//不是总社
+                        $rule = array_merge($rule,[$value['depart_id']]);
                     }
                 }
             }
+            foreach ($list as $value){
+                if(empty($value['parent_id']) && ! empty($value['is_main'])) $head = $value;
+            }
         }
+        $top = array_values($top);
+        $rule = array_unique($rule);
 
-        return $is_main;
+        return [$depart, $top, $map, $rule, $is_all_depart, $head, $is_behind_main];
     }
 
     /**

+ 1 - 2
app/Service/Service.php

@@ -389,8 +389,7 @@ class Service
 
     public function getDepart($user){
         if(empty($user)) return 0;
-        $depart = array_shift($user['rule_depart']);
-        return $depart['depart_id'];
+        return $user['depart_select']['depart_id'];
     }
 
     public function getTopId($id, $data) {