Explorar o código

公司初始化

cqp hai 1 mes
pai
achega
258dbb63d6

+ 0 - 13
app/Http/Controllers/Api/BIController.php

@@ -19,17 +19,4 @@ class BIController extends BaseController
             return $this->json_return(201,$data);
         }
     }
-
-    public function initializationCompany(Request $request)
-    {
-        $service = new BIService();
-        $user = $request->userData;
-        list($status,$data) = $service->initializationCompany($request->all(),$user);
-
-        if($status){
-            return $this->json_return(200,'',$data);
-        }else{
-            return $this->json_return(201,$data);
-        }
-    }
 }

+ 154 - 0
app/Http/Controllers/Api/MiddleGroundController.php

@@ -0,0 +1,154 @@
+<?php
+
+namespace App\Http\Controllers\Api;
+
+use App\Service\MiddleGroundService;
+use Illuminate\Http\Request;
+
+class MiddleGroundController extends BaseController
+{
+    public function initializationCompany(Request $request)
+    {
+        $service = new MiddleGroundService();
+        $user = $request->userData;
+        list($status,$data) = $service->initializationCompany($request->all(),$user);
+
+        if($status){
+            return $this->json_return(200,'',$data);
+        }else{
+            return $this->json_return(201,$data);
+        }
+    }
+
+    public function initializationCompanyList(Request $request)
+    {
+        $service = new MiddleGroundService();
+        $user = $request->userData;
+        list($status,$data) = $service->initializationCompanyList($request->all(),$user);
+
+        if($status){
+            return $this->json_return(200,'',$data);
+        }else{
+            return $this->json_return(201,$data);
+        }
+    }
+
+    public function roleEdit(Request $request)
+    {
+        $service = new MiddleGroundService();
+        $user = $request->userData;
+        list($status,$data) = $service->roleEdit($request->all(),$user);
+
+        if($status){
+            return $this->json_return(200,'',$data);
+        }else{
+            return $this->json_return(201,$data);
+        }
+    }
+
+    public function roleAdd(Request $request)
+    {
+        $service = new MiddleGroundService();
+        $user = $request->userData;
+        list($status,$data) = $service->roleAdd($request->all(),$user);
+
+        if($status){
+            return $this->json_return(200,'',$data);
+        }else{
+            return $this->json_return(201,$data);
+        }
+
+    }
+
+    public function roleDel(Request $request)
+    {
+        $service = new MiddleGroundService();
+        $user = $request->userData;
+        list($status,$data) = $service->roleDel($request->all(), $user);
+
+        if($status){
+            return $this->json_return(200,'',$data);
+        }else{
+            return $this->json_return(201,$data);
+        }
+
+    }
+
+    public function roleList(Request $request)
+    {
+        $service = new MiddleGroundService();
+        $user = $request->userData;
+        list($status,$data) = $service->roleList($request->all(),$user);
+
+        if($status){
+            return $this->json_return(200,'',$data);
+        }else{
+            return $this->json_return(201,$data);
+        }
+    }
+
+    public function roleMenu(Request $request)
+    {
+        $service = new MiddleGroundService();
+        $user = $request->userData;
+        list($status,$data) = $service->roleMenu($request->all());
+
+        if($status){
+            return $this->json_return(200,'',$data);
+        }else{
+            return $this->json_return(201,$data);
+        }
+    }
+
+    public function roleDetail(Request $request)
+    {
+        $service = new MiddleGroundService();
+        $user = $request->userData;
+        list($status,$data) = $service->roleDetail($request->all());
+
+        if($status){
+            return $this->json_return(200,'',$data);
+        }else{
+            return $this->json_return(201,$data);
+        }
+    }
+
+    public function menuList(Request $request)
+    {
+        $service = new MiddleGroundService();
+        $user = $request->userData;
+        list($status,$data) = $service->menuList($request->all(),$user);
+
+        if($status){
+            return $this->json_return(200,'',$data);
+        }else{
+            return $this->json_return(201,$data);
+        }
+    }
+
+    public function employeeList(Request $request)
+    {
+        $service = new MiddleGroundService();
+        $user = $request->userData;
+        list($status,$data) = $service->employeeList($request->all(),$user);
+
+        if($status){
+            return $this->json_return(200,'',$data);
+        }else{
+            return $this->json_return(201,$data);
+        }
+    }
+
+    public function employeeEdit(Request $request)
+    {
+        $service = new MiddleGroundService();
+        $user = $request->userData;
+        list($status,$data) = $service->employeeEdit($request->all(),$user);
+
+        if($status){
+            return $this->json_return(200,'',$data);
+        }else{
+            return $this->json_return(201,$data);
+        }
+    }
+}

+ 1 - 0
app/Http/Kernel.php

@@ -64,6 +64,7 @@ class Kernel extends HttpKernel
         'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
         'verified' => \Illuminate\Auth\Middleware\EnsureEmailIsVerified::class,
         'checkLogin' => \App\Http\Middleware\CheckLogin::class,
+        'checkCenterLogin' => \App\Http\Middleware\CheckCenterLogin::class,
         'OssFileDeal' => \App\Http\Middleware\OssFileDeal::class,
     ];
 

+ 33 - 0
app/Http/Middleware/CheckCenterLogin.php

@@ -0,0 +1,33 @@
+<?php
+
+namespace App\Http\Middleware;
+
+use App\Service\MiddleGroundService;
+use Closure;
+use App\Service\TokenService;
+
+class CheckCenterLogin
+{
+    public function handle($request, Closure $next)
+    {
+        $token = $request->header('Authorization');
+        if (empty($token)) return response()->json(['code' => 1,'msg' => '缺少登录凭证','data' => null]);
+
+        //校验token
+        list($bool, $return) = TokenService::verifyToken($token);
+        if(! $bool) return response()->json(['code' => 1,'msg' => $return,'data' => null]);
+
+        //人员在jwt里的信息
+        $e_array = $return;
+
+        //校验人员
+        $checkResult = MiddleGroundService::checkCenterUser($e_array);
+        list($state, $msg) = $checkResult;
+        if(! $state) return response()->json(['code' => 1,'msg' => $msg,'data' => null]);
+        $employee = $msg;
+        $employee['top_depart_id'] = 0;
+        $request->userData = $employee;
+
+        return $next($request);
+    }
+}

+ 2 - 2
app/Model/Employee.php

@@ -13,12 +13,12 @@ class Employee extends DataScopeBaseModel
     protected $dateFormat = 'U';
     public static $field = ['number','mobile','title','id','is_admin', 'account', 'crt_time', 'state', 'education', 'major', 'id_card','p_title','sex', 'entrust_type', 'man_type', 'position','employee_type'];
     public static $report_field = ['title','id', 'employee_type', 'education', 'major', 'id_card','p_title', 'position'];
-    const SPECIAL_ADMIN = 1;
     const USE = 1;
     const NOT_USE = 2;
     const IS_ADMIN_ZERO = 0;
     const IS_ADMIN_ONE = 1;
     const IS_ADMIN_TWO = 2;
+    const IS_ADMIN_THREE = 3; // 中台管理员
     const IS_ADMIN_TITLE_SIMPLE = [
         self::IS_ADMIN_ZERO => '普通账号',
         self::IS_ADMIN_ONE => '后台用户',
@@ -26,7 +26,7 @@ class Employee extends DataScopeBaseModel
     const IS_ADMIN_TITLE = [
         self::IS_ADMIN_ZERO => '普通账号(限制后台登录)',
         self::IS_ADMIN_ONE => '后台用户',
-        self::IS_ADMIN_TWO => '管理员',
+        self::IS_ADMIN_TWO => '后台管理员',
     ];
 
     const AUTH_ONE = 1; // 我的

+ 0 - 103
app/Service/BIService.php

@@ -4,9 +4,6 @@ namespace App\Service;
 
 use App\Model\DailyPwOrder;
 use App\Model\DailyPwOrderDetails;
-use App\Model\Depart;
-use App\Model\Employee;
-use App\Model\EmployeeDepartPermission;
 use App\Model\ExpenseClaims;
 use App\Model\ExpenseClaimsDetails;
 use App\Model\Fee;
@@ -17,9 +14,6 @@ use App\Model\MonthlyPsOrder;
 use App\Model\MonthlyPsOrderDetails;
 use App\Model\PLeaveOverOrder;
 use App\Model\PLeaveOverOrderDetails;
-use App\Model\WorkRangeDetails;
-use Illuminate\Support\Facades\DB;
-use Illuminate\Support\Facades\Hash;
 
 class BIService extends Service
 {
@@ -361,101 +355,4 @@ class BIService extends Service
 
         return $return;
     }
-
-    public function initializationCompany($data, $user){
-        if(empty($data['title'])) return [false, '公司名称不能为空'];
-        if(empty($data['code'])) return [false, '公司代码不能为空'];
-
-        // ^[A-Za-z]+$  表示从头到尾只能是英文字母(不区分大小写)
-        if (!preg_match('/^[A-Za-z]+$/', $data['code']) || mb_strlen($data['code']) < 4) return [false, "公司代码必须全为英文且长度需大于等于 4 位"];
-
-        $exists = Depart::where('parent_id', 0)
-            ->where('del_time', 0)
-            ->where(function($query) use ($data) {
-                $query->where('title', $data['title'])
-                    ->orWhere('code', $data['code']);
-            })
-            ->exists();
-        if($exists) return [false, '公司名称或公司代码已存在,新增失败'];
-
-        $account = $data['code'] . "_" . 'admin';
-        $exists = Employee::where('del_time', 0)
-            ->where('account', $account)
-            ->exists();
-        if($exists) return [false, '创建账号已存在,新增失败'];
-
-        try {
-            DB::beginTransaction();
-
-            //创建公司
-            $model = new Depart();
-            $model->parent_id = 0;
-            $model->title = $data['title'];
-            $model->code = $data['code'];
-            $model->top_depart_id = 0;
-            $model->save();
-            //公司ID
-            $top_depart_id = $model->id;
-
-            $password = $this->generateAt8CharPassword();
-            //创建账号
-            $model_2 = new Employee();
-            $model_2->title = $data['title'];
-            $model_2->code = $data['code'];
-            $model_2->account = $account;
-            $model_2->password = Hash::make($password);
-            $model_2->is_admin = Employee::IS_ADMIN_TWO;
-            $model_2->top_depart_id = $top_depart_id;
-            $model_2->state = Employee::TYPE_ONE;
-            $model_2->save();
-            $employee_id = $model_2->id;
-
-            //关联人员的部门初始信息
-            $model_3 = new EmployeeDepartPermission();
-            $model_3->employee_id = $employee_id;
-            $model_3->depart_id = 0;
-            $model_3->top_depart_id = $top_depart_id;
-            $model_3->save();
-
-            //公司上班时段
-            $work_range[] = [
-                'top_depart_id' => $top_depart_id,
-                'start_time_hour' => 9,
-                'start_time_min' => 0,
-                'end_time_hour' => 12,
-                'end_time_min' => 0,
-                'total_work_min' => 180,
-            ];
-            $work_range[] = [
-                'top_depart_id' => $top_depart_id,
-                'start_time_hour' => 13,
-                'start_time_min' => 0,
-                'end_time_hour' => 18,
-                'end_time_min' => 0,
-                'total_work_min' => 300,
-            ];
-            WorkRangeDetails::insert($work_range);
-
-            DB::commit();
-        }catch (\Throwable $exception){
-            DB::rollBack();
-            return [false, $exception->getMessage()];
-        }
-
-        return [true, ['account' => $account, 'password' => $password]];
-    }
-
-    function generateAt8CharPassword() {
-        $lettersNumbers = 'abcdefghijkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ23456789';
-
-        $password = '@'; // 1. 先把固定的 @ 放进去
-
-        // 2. 随机抽取 7 位英文或数字
-        for ($i = 0; $i < 7; $i++) {
-            $password .= $lettersNumbers[random_int(0, strlen($lettersNumbers) - 1)];
-        }
-
-        // 3. 打乱顺序,让 @ 的位置不固定
-        return str_shuffle($password);
-    }
 }

+ 1 - 3
app/Service/LoginService.php

@@ -47,15 +47,13 @@ class LoginService extends Service
             ->first();
         if(empty($result)) return [false, '账号不存在或已被删除'];
         $result = $result->toArray();
-        if($result['is_admin'] == Employee::IS_ADMIN_ZERO) return [false, '账号限制登录'];
+        if($result['is_admin'] == Employee::IS_ADMIN_ZERO || $result['is_admin'] == Employee::IS_ADMIN_THREE) return [false, '账号限制登录'];
         if($result['p_version'] != $employee['p_version']) return [false, '请重新登录'];
 
         return [true, $result];
     }
 
     public static function checkCompany($employee){
-        $employee_id = $employee['id'];
-//        if ($employee_id == Employee::SPECIAL_ADMIN) return [true, ''];
         $top_depart_id = $employee['top_depart_id'];
         if(empty($top_depart_id)) return [false, '公司信息不存在'];
 

+ 499 - 0
app/Service/MiddleGroundService.php

@@ -0,0 +1,499 @@
+<?php
+
+namespace App\Service;
+
+use App\Model\Depart;
+use App\Model\Employee;
+use App\Model\EmployeeDepartPermission;
+use App\Model\EmployeeRole;
+use App\Model\Role;
+use App\Model\RoleMenu;
+use App\Model\RoleMenuButton;
+use App\Model\SysMenu;
+use App\Model\SysMenuButton;
+use App\Model\WorkRangeDetails;
+use Illuminate\Support\Facades\DB;
+use Illuminate\Support\Facades\Hash;
+
+class MiddleGroundService extends Service
+{
+    // 角色 ---------------------------------------
+    public function roleEdit($data,$user){
+        list($status,$msg) = $this->roleRule($data,$user, false);
+        if(!$status) return [$status,$msg];
+
+        $model = new Role();
+        $model = $model->where('id',$data['id'])->first();
+        $model->title = $data['title'];
+        $model->save();
+
+        return [true,''];
+    }
+
+    public function roleAdd($data,$user){
+        list($status,$msg) = $this->roleRule($data,$user);
+        if(!$status) return [$status,$msg];
+
+        $model = new Role();
+        $model->title = $data['title'] ;
+        $model->top_depart_id = $user['top_depart_id'];
+        $model->save();
+
+        return [true,''];
+    }
+
+    public function roleDel($data, $user){
+        if($this->isEmpty($data,'id')) return [false,'ID必须!'];
+
+        $bool = EmployeeRole::where('del_time',0)
+            ->whereIn('role_id',$data['id'])
+            ->exists();
+        if($bool) return [false,'角色已绑定人员'];
+
+        try {
+            DB::beginTransaction();
+            $time = time();
+
+            Role::where('id',$data['id'])->update([
+                'del_time' => $time
+            ]);
+
+            RoleMenu::where('del_time',0)->where('role_id',$data['id'])->update([
+                'del_time' => $time
+            ]);
+
+            RoleMenuButton::where('del_time',0)->where('role_id',$data['id'])->update([
+                'del_time' => $time
+            ]);
+
+            DB::commit();
+        }catch (\Exception $exception){
+            DB::rollBack();
+            return [false,$exception->getMessage()];
+        }
+
+        return [true, ''];
+    }
+
+    public function roleList($data,$user){
+        $model = Role::TopClear($user,$data);
+        $model = $model->where('del_time',0)
+            ->select('title','crt_time','id','upd_time')
+            ->orderBy('id','desc');
+        if(! empty($data['title'])) $model->where('title', 'LIKE', '%' . $data['title'] . '%');
+
+        $list = $this->limit($model,'',$data);
+
+        return [true, $list];
+    }
+
+    public function roleRule(&$data,$user, $is_check = true){
+        if($this->isEmpty($data,'title')) return [false,'名称不能为空'];
+
+        if($is_check){
+            $bool = Role::where('title',$data['title'])
+                ->where('top_depart_id', $user['top_depart_id'])
+                ->where('del_time',0)
+                ->exists();
+            if($bool) return [false,'角色名称已存在'];
+        }else{
+            if($this->isEmpty($data,'id')) return [false,'ID不能为空'];
+            $top_depart_id = Role::where('id',$data['id'])->value('top_depart_id');
+            $bool = Role::where('title',$data['title'])
+                ->where('top_depart_id',$top_depart_id)
+                ->where('id','<>',$data['id'])
+                ->where('del_time',0)
+                ->exists();
+            if($bool) return [false,'角色名称已存在'];
+        }
+
+        return [true, ''];
+    }
+
+    public function roleMenu($data){
+        if(empty($data['role_id']))  return [false,'角色不能为空!'];
+        if(empty($data['menu'])) return [false,'菜单数据不能为空!'];
+
+        DB::beginTransaction();
+        try {
+            RoleMenu::where('del_time',0)->where('role_id',$data['role_id'])->update(['del_time' => time()]);
+            RoleMenuButton::where('del_time',0)->where('role_id',$data['role_id'])->update(['del_time' => time()]);
+
+            $insert = $insert2 = [];
+            foreach ($data['menu'] as $t){
+                $insert[] = [
+                    'role_id' => $data['role_id'],
+                    'menu_id' => $t['menu_id'],
+                    'type' => $t['type'],
+                    'crt_time' => time()
+                ];
+                if(! empty($t['button'])){
+                    foreach ($t['button'] as $b){
+                        $insert2[] = [
+                            'role_id' => $data['role_id'],
+                            'menu_id' => $t['menu_id'],
+                            'button_id' => $b,
+                            'crt_time' => time()
+                        ];
+                    }
+                RoleMenuButton::insert($insert2);
+                }
+            }
+            RoleMenu::insert($insert);
+
+            DB::commit();
+        }catch (\Throwable $exception){
+            DB::rollBack();
+            return [false,$exception->getMessage()];
+        }
+
+        return [true, ''];
+    }
+
+    public function roleDetail($data){
+        if(empty($data['role_id'])) return [false,'请选择角色'];
+
+        $role = Role::where('id',$data['role_id'])
+            ->where('del_time',0)
+            ->select('id','title')
+            ->first();
+        if(empty($role)) return [false,'角色不存在或已被删除'];
+        $role = $role->toArray();
+
+        $menu = RoleMenu::where('role_id',$data['role_id'])
+            ->where('del_time',0)
+            ->select('menu_id','type')
+            ->get()->toArray();
+        $button = $this->fillRoleButton([$data['role_id']]);
+        foreach ($menu as $key => $value){
+            $menu[$key]['button'] = $button[$value['menu_id']] ?? [];
+        }
+        $role['menu'] = $menu;
+
+        return [true, $role];
+    }
+
+    public function fillRoleButton($role_id){
+        $button = RoleMenuButton::whereIn('role_id',$role_id)
+            ->where('del_time',0)
+            ->select('menu_id','button_id')
+            ->get()->toArray();
+        $button_map = [];
+        foreach ($button as $value){
+            if(! isset($button_map[$value['menu_id']])){
+                $button_map[$value['menu_id']][] = $value['button_id'];
+            }else{
+                if(! in_array($value['button_id'], $button_map[$value['menu_id']])) $button_map[$value['menu_id']][] = $value['button_id'];
+            }
+        }
+
+        return $button_map;
+    }
+
+    public function initializationCompanyList($data, $user){
+        $model = Depart::where('del_time',0)
+            ->where('parent_id', 0)
+            ->select('*')
+            ->orderby('id', 'desc');
+
+        if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
+        if(! empty($data['code'])) $model->where('code', 'LIKE', '%'.$data['code'].'%');
+
+        $list = $this->limit($model,'',$data);
+
+        return [true, $list];
+    }
+
+    // 公司初始化 返回 公司后台管理员账户 密码
+    public function initializationCompany($data, $user){
+        if(empty($data['title'])) return [false, '公司名称不能为空'];
+        if(empty($data['code'])) return [false, '公司代码不能为空'];
+
+        // ^[A-Za-z]+$  表示从头到尾只能是英文字母(不区分大小写)
+        if (! preg_match('/^[A-Za-z]+$/', $data['code']) || mb_strlen($data['code']) < 4) return [false, "公司代码必须全为英文且长度需大于等于 4 位"];
+
+        $exists = Depart::where('parent_id', 0)
+            ->where('del_time', 0)
+            ->where(function($query) use ($data) {
+                $query->where('title', $data['title'])
+                    ->orWhere('code', $data['code']);
+            })
+            ->exists();
+        if($exists) return [false, '公司名称或公司代码已存在,新增失败'];
+
+        $account = $data['code'] . "_" . 'admin';
+        $exists = Employee::where('del_time', 0)
+            ->where('account', $account)
+            ->exists();
+        if($exists) return [false, '创建账号已存在,新增失败'];
+
+        try {
+            DB::beginTransaction();
+
+            //创建公司
+            $model = new Depart();
+            $model->parent_id = 0;
+            $model->title = $data['title'];
+            $model->code = $data['code'];
+            $model->top_depart_id = 0;
+            $model->save();
+            //公司ID
+            $top_depart_id = $model->id;
+
+            $password = $this->generateAt8CharPassword();
+            //创建账号
+            $model_2 = new Employee();
+            $model_2->title = $data['title'];
+            $model_2->code = $data['code'];
+            $model_2->account = $account;
+            $model_2->password = Hash::make($password);
+            $model_2->is_admin = Employee::IS_ADMIN_TWO;
+            $model_2->top_depart_id = $top_depart_id;
+            $model_2->state = Employee::TYPE_ONE;
+            $model_2->save();
+            $employee_id = $model_2->id;
+
+            //关联人员的部门初始信息
+            $model_3 = new EmployeeDepartPermission();
+            $model_3->employee_id = $employee_id;
+            $model_3->depart_id = 0;
+            $model_3->top_depart_id = $top_depart_id;
+            $model_3->save();
+
+            //公司上班时段
+            $work_range[] = [
+                'top_depart_id' => $top_depart_id,
+                'start_time_hour' => 9,
+                'start_time_min' => 0,
+                'end_time_hour' => 12,
+                'end_time_min' => 0,
+                'total_work_min' => 180,
+            ];
+            $work_range[] = [
+                'top_depart_id' => $top_depart_id,
+                'start_time_hour' => 13,
+                'start_time_min' => 0,
+                'end_time_hour' => 18,
+                'end_time_min' => 0,
+                'total_work_min' => 300,
+            ];
+            WorkRangeDetails::insert($work_range);
+
+            DB::commit();
+        }catch (\Throwable $exception){
+            DB::rollBack();
+            return [false, $exception->getMessage()];
+        }
+
+        return [true, ['account' => $account, 'password' => $password]];
+    }
+
+    function generateAt8CharPassword() {
+        $lettersNumbers = 'abcdefghijkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ23456789';
+
+        $password = '@'; // 1. 先把固定的 @ 放进去
+
+        // 2. 随机抽取 6 位英文或数字
+        for ($i = 0; $i < 6; $i++) {
+            $password .= $lettersNumbers[random_int(0, strlen($lettersNumbers) - 1)];
+        }
+
+        // 3. 打乱顺序,让 @ 的位置不固定
+        return str_shuffle($password);
+    }
+
+    // 菜单
+    public function menuList($data, $user){
+        $sysList = SysMenu::where('del_time',0)
+            ->select('title','icon','uri','parent_id','sort','id','is_authority', 'state')
+            ->orderBy('sort','desc')
+            ->get()
+            ->toArray();
+        $button = SysMenuButton::where('del_time',0)
+            ->select('id','title','sort','func','menu_id')
+            ->get()->toArray();
+
+        $return = $this->fillMenuButton($button,$sysList, $user);
+
+        return [true, $return];
+    }
+
+    public function fillMenuButton($button, $menu, $user)
+    {
+        // 1. 预处理所有按钮池(合并配置中的特殊按钮)
+        $allButtons = collect($button)->concat(config('specialButton') ?? [])->groupBy('menu_id');
+
+        // 2. 获取权限基础数据
+        $isSuper = ($user['is_admin'] == Employee::IS_ADMIN_THREE);
+
+        $result = [];
+
+        foreach ($menu as $m) {
+            $menuId = $m['id'];
+
+            // 权限过滤:非超管
+            if (!$isSuper) continue;
+
+            // 计算当前菜单拥有的按钮
+            $btns = $allButtons->get($menuId) ?? [];
+
+            $m['button'] = $btns;
+
+            $result[] = $m;
+        }
+
+        return $result;
+    }
+
+    // 管理员
+    public function employeeCommon($data,$user){
+        $model = Employee::where('del_time',0)
+            ->where('is_admin', Employee::IS_ADMIN_TWO)
+            ->select('id', 'account', 'crt_time', 'top_depart_id')
+            ->orderBy('id','desc');
+
+        if(! empty($data['id'])) $model->whereIn('id', $data['id']);
+        if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
+        if(! empty($data['mobile'])) $model->where('mobile', 'LIKE', '%'.$data['mobile'].'%');
+        if(! empty($data['role'])) {
+            $emp = EmployeeRole::where('role_id',$data['role'])
+                ->where('del_time',0)
+                ->select('employee_id')->get()->toArray();
+            $model->whereIn('id',array_column($emp,'employee_id'));
+        }
+
+        return $model;
+    }
+
+    public function employeeList($data,$user){
+        $model = $this->employeeCommon($data, $user);
+        $list = $this->limit($model,'',$data);
+        $list = $this->organizationEmployeeData($list, $data, $user);
+
+        return [true, $list];
+    }
+
+    public function organizationEmployeeData($data, $ergs, $user)
+    {
+        if (empty($data['data'])) return $data;
+
+        // 获取员工ID并查询扩展数据
+        $employee_ids = array_column($data['data'], 'id');
+        list($status, $extraMap) = $this->getEmployee($employee_ids);
+
+        $depart_map = Depart::whereIn('id', array_unique(array_column($data['data'],'top_depart_id')))
+            ->pluck('title', 'id')
+            ->toArray();
+        foreach ($data['data'] as &$item) {
+            $id = $item['id'];
+            $extra = $extraMap[$id] ?? null;
+
+            $item['role']         = $extra['role_ids'] ?? [];
+            $item['role_name']    = isset($extra['role_names']) ? implode(',', $extra['role_names']) : '';
+            $item['top_depart_title'] = $depart_map[$item['top_depart_id']];
+
+            $item['crt_time']       = !empty($item['crt_time']) ? date("Y-m-d", $item['crt_time']) : "";
+        }
+
+        return $data;
+    }
+
+    public function getEmployee(array $employee_ids)
+    {
+        if (empty($employee_ids)) return [false, []];
+
+        // 1. 一次性获取所有角色
+        $roles = DB::table('employee_role as a')
+            ->join('role as b', 'a.role_id', '=', 'b.id')
+            ->where('a.del_time', 0)
+            ->where('b.del_time', 0)
+            ->whereIn("a.employee_id", $employee_ids)
+            ->select('a.employee_id', 'b.title', 'b.id')
+            ->get();
+        $resultMap = [];
+
+        foreach ($roles as $r) {
+            $resultMap[$r->employee_id]['role_ids'][] = $r->id;
+            $resultMap[$r->employee_id]['role_names'][] = $r->title;
+        }
+
+        return [true, $resultMap];
+    }
+
+    public function employeeEdit($data,$user){
+        list($status,$msg) = $this->employeeRule($data,$user,false);
+        if(!$status) return [$status,$msg];
+
+        try {
+            DB::beginTransaction();
+            $model = new Employee();
+            $model = $model->where('id',$data['id'])->first();
+            $model->title = $data['title'] ?? "";
+            if($model->is_admin && $data['password'] !== '******') {
+                $model->password = Hash::make($data['password']);
+                $model->p_version = $model->p_version + 1;
+            }
+            $model->save();
+
+            $time = time();
+
+            EmployeeRole::where('employee_id',$data['id'])->update([
+                'del_time' => $time
+            ]);
+
+            if(! empty($data['role'])){
+                $insert = [];
+                foreach ($data['role'] as $value){
+                    $insert[] = [
+                        'employee_id' => $model->id,
+                        'role_id' => $value,
+                        'crt_time' => $time,
+                    ];
+                }
+                EmployeeRole::insert($insert);
+            }
+
+            DB::commit();
+        }catch (\Exception $exception){
+            DB::rollBack();
+            return [false, $exception->getMessage()];
+        }
+
+        return [true, ''];
+    }
+
+    public function employeeRule(&$data, $user, $is_add = true){
+        if(empty($data['id'])) return [false, '管理员ID不能为空'];
+        $bool = Employee::where('del_time',0)
+            ->where('id', $data['id'])
+            ->exists();
+        if(! $bool) return [false, '管理员账户不存在或已被删除'];
+
+        if(empty($data['title'])) return [false, '公司名称不能为空'];
+        $exists = Depart::where('parent_id', 0)
+            ->where('del_time', 0)
+            ->where('title', $data['title'])
+            ->where('id', '<>', $data['id'])
+            ->exists();
+        if($exists) return [false, '公司名称已存在,编辑失败'];
+
+        if(empty($data['password'])) return [false, '密码不能为空'];
+        if(mb_strlen($data['password']) < 6) return [false, '密码长度不得小于6位长度'];
+
+        return [true, ''];
+    }
+
+    public static function checkCenterUser($employee){
+        if(empty($employee['employee_id'])) return [false, 'token错误'];
+        if(! isset($employee['p_version'])) return [false, 'token错误'];
+        $result = Employee::where('id', $employee['employee_id'])
+            ->where('del_time',0)
+            ->first();
+        if(empty($result)) return [false, '账号不存在或已被删除'];
+        $result = $result->toArray();
+        if($result['is_admin'] != Employee::IS_ADMIN_THREE) return [false, '账号限制登录'];
+        if($result['p_version'] != $employee['p_version']) return [false, '请重新登录'];
+
+        return [true, $result];
+    }
+}

+ 0 - 81
app/Service/SysMenuService.php

@@ -306,87 +306,6 @@ class SysMenuService extends Service
         return $result;
     }
 
-    public function fillMenuButton1($button, $menu, $user, $type = 1){
-        $button_map = [];
-        $special_button = config('specialButton') ?? [];
-        foreach ($special_button as $value){
-            $button[] = $value;
-        }
-        foreach ($button as $value){
-            $button_map[$value['menu_id']][] = $value;
-        }
-
-        if($type == 1){
-            if($user['is_admin'] == Employee::IS_ADMIN_TWO){
-                //管理员
-                foreach ($menu as $key => $value){
-                    $menu[$key]['button'] = $button_map[$value['id']] ?? [];
-                }
-                return $menu;
-            }else{
-                //人员所拥有的菜单ID
-                $menu_id = $this->getMenuIDByRoleInList($user);
-                //角色
-                $role_id = $user['role_ids'] ?? [];
-                //角色下拥有的菜单里按钮
-                $role_button = (new EmployeeService())->fillRoleButton($role_id);
-                foreach ($menu as $key => $value){
-                    $result = [];
-                    if(! in_array($value['id'], $menu_id)) {
-                        unset($menu[$key]);
-                        continue;
-                    }
-                    $bt = $role_button[$value['id']] ?? [];
-                    $button_tmp = $button_map[$value['id']] ?? [];
-                    foreach ($bt as $b){
-                        if(isset($button_tmp[$b])) $result[] = $button_tmp[$b];
-                    }
-                    $menu[$key]['button'] = $result;
-                }
-                return array_values($menu);
-            }
-        }else{
-            $object = [];
-
-            if($user['is_admin'] == Employee::IS_ADMIN_TWO){
-                //管理员
-                foreach ($menu as $value){
-                    $object[] = [
-                        'id' => $value['id'],
-                        'uri' => $value['uri'],
-                        'button' => $button_map[$value['id']] ?? [],
-                    ];
-                }
-            }else{
-                //人员所拥有的菜单数据
-                $role_menu = $this->getMenuDataByRoleInList($user);
-                if(empty($role_menu)) return $object;
-                $menu_map = array_column($menu,'uri','id');
-                //角色
-                $role_id = $user['role_ids'] ?? [];
-                //角色下拥有的菜单里按钮
-                $role_button = (new EmployeeService())->fillRoleButton($role_id);
-                foreach ($role_menu as $value){
-                    $result = [];
-                    $bt = $role_button[$value['menu_id']] ?? [];
-                    $button_tmp = $button_map[$value['menu_id']] ?? [];
-                    $button_tmp = array_column($button_tmp,null,'id');
-                    foreach ($bt as $b){
-                        if(isset($button_tmp[$b])) $result[] = $button_tmp[$b];
-                    }
-
-                    $object[] = [
-                        'id' => $value['menu_id'],
-                        'uri' => $menu_map[$value['menu_id']] ?? '',
-                        'button' => $result,
-                    ];
-                }
-            }
-
-            return $object;
-        }
-    }
-
     private function returnTwo($sysList, $user){
         //人员所拥有的菜单
         $menu = $this->getMenuIDByRoleInList($user);

+ 35 - 2
routes/api.php

@@ -92,6 +92,21 @@ Route::group(['middleware'=> ['checkLogin']],function ($route){
     $route->any('itemDel', 'Api\ItemController@itemDel');
     $route->any('itemDetail', 'Api\ItemController@itemDetail');
 
+    //中台新建角色 以及分配菜单权限
+    $route->any('roleMiddleGroundAdd', 'Api\RoleMiddleGroudController@roleAdd');
+    $route->any('roleMiddleGroundEdit', 'Api\RoleMiddleGroudController@roleEdit');
+    $route->any('roleMiddleGroundDel', 'Api\RoleMiddleGroudController@roleDel');
+    $route->any('roleMiddleGroundList', 'Api\RoleMiddleGroudController@roleList');
+    $route->any('roleMiddleGroundDetail', 'Api\RoleMiddleGroudController@roleDetail');
+    $route->any('roleMiddleGroundMenu', 'Api\RoleMiddleGroudController@roleMenu');
+
+    //优先级
+    $route->any('priorityList', 'Api\PriorityController@priorityList');
+    $route->any('priorityEdit', 'Api\PriorityController@priorityEdit');
+    $route->any('priorityAdd', 'Api\PriorityController@priorityAdd');
+    $route->any('priorityDel', 'Api\PriorityController@priorityDel');
+    $route->any('priorityDetail', 'Api\PriorityController@priorityDetail');
+
     //费用类型
     $route->any('feeAdd', 'Api\FeeController@feeAdd')->name('fee.add');
     $route->any('feeEdit', 'Api\FeeController@feeEdit')->name('fee.edit');
@@ -220,11 +235,29 @@ Route::group(['middleware'=> ['checkLogin']],function ($route){
     $route->any('enterpriseRdItemStatistic', 'Api\StatisticController@enterpriseRdItemStatistic');
 
 
-
     //首页BI数据
     $route->any('homePageData', 'Api\BIController@homePageData');
+});
+
+//中台路由
+Route::group(['middleware'=> ['checkCenterLogin']],function ($route){
+    $route->any('roleMiddleGroundAdd', 'Api\MiddleGroundController@roleAdd');
+    $route->any('roleMiddleGroundEdit', 'Api\MiddleGroundController@roleEdit');
+    $route->any('roleMiddleGroundDel', 'Api\MiddleGroundController@roleDel');
+    $route->any('roleMiddleGroundList', 'Api\MiddleGroundController@roleList');
+    $route->any('roleMiddleGroundDetail', 'Api\MiddleGroundController@roleDetail');
+    $route->any('roleMiddleGroundMenu', 'Api\MiddleGroundController@roleMenu');
 
     //创建公司账号 工时设置 初始化
-    $route->any('initializationCompany', 'Api\BIController@initializationCompany');
+    $route->any('initializationCompany', 'Api\MiddleGroundController@initializationCompany');
+    //公司列表
+    $route->any('initializationCompanyList', 'Api\MiddleGroundController@initializationCompanyList');
+
+    //菜单列表 为了设置角色权限用的
+    $route->any('menuMiddleGroundList', 'Api\MiddleGroundController@menuList');
+
+    //管理员
+    $route->any('employeeMiddleGroundList', 'Api\MiddleGroundController@employeeList');
+    $route->any('employeeMiddleGroundEdit', 'Api\MiddleGroundController@employeeEdit');
 });