lip 3 ماه پیش
والد
کامیت
4b4622f521
3فایلهای تغییر یافته به همراه58 افزوده شده و 26 حذف شده
  1. 6 17
      app/admin/controller/Department.php
  2. 36 0
      app/admin/model/Department.php
  3. 16 9
      app/enterprise/model/User.php

+ 6 - 17
app/admin/controller/Department.php

@@ -106,15 +106,14 @@ class Department extends BaseController
             if (!$department) {
                 return $this->error('部门不存在');
             }
-            //查询所有下级部门
-            $list = DepartmentModel::order('parent_id', 'asc')
-                ->order('weight', 'desc')
-                ->select();   
-            $tree = linear_to_tree($list, 'children', 'id', 'parent_id', $id);
             
             DB::startTrans();
+
+            //查询所有下级部门
+            $tree = DepartmentModel::getChildrenTree($id);
+
             //获取所有下级的ID
-            $ids = $this->getChildrenIds($tree);
+            $ids = DepartmentModel::getChildrenIds($tree);
             $ids[] = $id;
             $exists = Admin::whereIn('department_id', $ids)->count();
             if ($exists) {
@@ -129,15 +128,5 @@ class Department extends BaseController
         }
         return $this->success([], '删除成功');
     }
-    //获取所有下级的ID
-    private function getChildrenIds($sub) {
-        $ids = [];
-        foreach ($sub as $key => $value) {
-            $ids[] = $value['id'];
-            if (!empty($value['children'])) {
-                $ids = array_merge($ids, $this->getChildrenIds($value['children']));
-            }
-        }
-        return $ids;
-    }
+    
 }

+ 36 - 0
app/admin/model/Department.php

@@ -10,4 +10,40 @@ class Department extends BaseModel
     protected $autoWriteTimestamp = true;
     protected $createTime = 'created_at';
     protected $updateTime = 'updated_at';
+
+    //获取指定部门的下级树形结构
+    public static function getChildrenTree($department_id) {
+        //查询所有下级部门
+        $list = self::order('parent_id', 'asc')
+            ->order('weight', 'desc')
+            ->select();   
+        $tree = linear_to_tree($list, 'children', 'id', 'parent_id', $department_id);
+        return $tree;
+    }
+
+    //获取所有下级的ID
+    public static function getChildrenIds($children) {
+        $ids = [];
+        foreach ($children as $value) {
+            $ids[] = $value['id'];
+            if (!empty($value['children'])) {
+                $ids = array_merge($ids, self::getChildrenIds($value['children']));
+            }
+        }
+        return $ids;
+    }
+
+    //获取部门下级的所有客服ID
+    public static function getDepartmentCsUids($admin_id) { 
+        $department_id = Admin::where('id', $admin_id)->value('department_id');
+        $tree = self::getChildrenTree($department_id);
+        $department_ids = self::getChildrenIds($tree);
+
+        $cs_uids = Admin::alis('admin')->join('user', 'admin.id=user.uid','left')
+            ->whereIn('admin.department_id', $department_ids)
+            ->where('user.role', '>', 0)
+            ->column('user.user_id');
+        return $cs_uids;
+    }
+
 }

+ 16 - 9
app/enterprise/model/User.php

@@ -10,13 +10,13 @@ namespace app\enterprise\model;
 use GatewayClient\Gateway;
 use app\BaseModel;
 use think\facade\Db;
-use think\facade\Request;
 use think\model\concern\SoftDelete;
 use app\manage\model\Config;
 use thans\jwt\facade\JWTAuth;
 use app\admin\model\Sign;
 use app\admin\model\KefuWork;
 use app\admin\model\KefuTime;
+use app\admin\model\Department;
 
 class User extends BaseModel
 {
@@ -132,19 +132,26 @@ class User extends BaseModel
             $list = self::where($map)->field($field)->select();
          }
       }else{
-         $friendList = Friend::getFriend(['create_user' => $user_id,'status'=>1]);
-         $userList = array_keys($friendList);
+         $userList = [];
          // 将专属客服设置为好友
          $csUid=request()->userInfo['cs_uid'] ?? 0;
          if($csUid){
-            // $userList[]=$csUid;
-            $userList = [$csUid];
+            $userList[]=$csUid;
          }
-         // 如果我有客服权限,就查询客服的好友
-         $cus=self::where(['cs_uid'=>$user_id])->column('user_id');
-         if($cus){
-            $userList=array_merge($userList,$cus);
+         // 如果是管理员,就查询整个部门的客服好友
+         if (request()->userInfo['role'] > 0) {
+             $friendList = Friend::getFriend(['create_user' => $user_id,'status'=>1]);//管理员自己的好友
+             $userList = array_merge($userList, array_keys($friendList));
+             
+             $admin_id = request()->userInfo['uid'];
+             $department_user_ids = Department::getDepartmentCsUids($admin_id);//整个部门的用户
+             $department_user_ids[] = $user_id;
+             $cus=self::whereIn('cs_uid',$department_user_ids)->column('user_id');
+             if($cus){
+                $userList=array_merge($userList,$cus);
+             }
          }
+         
          $userList = array_unique($userList);
          $map[] = ['user_id','in',$userList];
          $list = self::where($map)->whereOr(function ($query) use ($role) {