EmployeeService.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. <?php
  2. namespace App\Service;
  3. use App\Model\Depart;
  4. use App\Model\Employee;
  5. use App\Model\EmployeeDepartPermission;
  6. use App\Model\EmployeeManagerDepart;
  7. use App\Model\EmployeeMenuPermission;
  8. use App\Model\EmployeeRole;
  9. use App\Model\EmployeeTeamPermission;
  10. use App\Model\Role;
  11. use App\Model\SysMenu;
  12. use App\Model\Team;
  13. use Illuminate\Support\Facades\Cache;
  14. use Illuminate\Support\Facades\DB;
  15. use Illuminate\Support\Facades\Hash;
  16. /**
  17. * 人员相关
  18. * @package App\Models
  19. */
  20. class EmployeeService extends Service
  21. {
  22. public function employeeEdit($data,$user){
  23. list($status,$msg) = $this->employeeRule($data,false);
  24. if(!$status) return [$status,$msg];
  25. $model = new Employee();
  26. $model = $model->where('id',$data['id'])->first();
  27. $model->number = $data['number'] ;
  28. $model->mobile = $data['mobile'];
  29. $model->emp_name = $data['emp_name'];
  30. $model->is_admin = $data['is_admin'];
  31. if($model->is_admin == 1){
  32. $model->account = $data['account'];
  33. if($data['password'] !== '********'){
  34. $model->password = Hash::make($data['password']);
  35. }
  36. }
  37. $model->save();
  38. return [true,'保存成功!'];
  39. }
  40. public function employeeAdd($data,$user){
  41. list($status,$msg) = $this->employeeRule($data);
  42. if(!$status) return [$status,$msg];
  43. $model = new Employee();
  44. $model->number = $data['number'] ;
  45. $model->mobile = $data['mobile'];
  46. $model->emp_name = $data['emp_name'];
  47. $model->state = 1;
  48. $model->crt_id = $user['id'];
  49. $model->is_admin = $data['is_admin'];
  50. if($model->is_admin == 1){
  51. $model->account = $data['account'];
  52. $model->password = Hash::make($data['password']);
  53. }
  54. $model->save();
  55. return [true,'保存成功!'];
  56. }
  57. public function employeeDel($data){
  58. if($this->isEmpty($data,'id')) return [false,'ID必须!'];
  59. Employee::where('id',$data['id'])->update([
  60. 'del_time'=>time()
  61. ]);
  62. return [true,'删除成功'];
  63. }
  64. public function employeeList($data){
  65. $list = Employee::where('del_time',0)->select('emp_name','mobile','crt_time','account','is_admin','id')->orderBy('id','desc');
  66. $list = $this->limit($list,'',$data);
  67. return [200,$list];
  68. }
  69. public function employeeRule($data,$is_add = true){
  70. if($this->isEmpty($data,'number')) return [false,'工号不存在!'];
  71. if($this->isEmpty($data,'mobile')) return [false,'手机号不存在!'];
  72. if($this->isEmpty($data,'emp_name')) return [false,'姓名不存在!'];
  73. if(! $is_add){
  74. if($this->isEmpty($data,'id')) return [false,'ID不能为空!'];
  75. $bool = Employee::where('number',$data['number'])
  76. ->where('id','<>',$data['id'])
  77. ->where('del_time',0)->exists();
  78. $bool_account = Employee::where('account',$data['account'])
  79. ->where('id','<>',$data['id'])
  80. ->where('del_time',0)->exists();
  81. }else{
  82. $bool = Employee::where('number',$data['number'])
  83. ->where('del_time',0)->exists();
  84. $bool_account = Employee::where('account',$data['account'])
  85. ->where('del_time',0)->exists();
  86. }
  87. if($bool) return [false,'工号已存在!'];
  88. if($bool_account) return [false,'账号已存在!'];
  89. return [true,''];
  90. }
  91. public function roleEdit($data){
  92. list($status,$msg) = $this->roleRule($data);
  93. if(!$status) return [$status,$msg];
  94. $first = Role::where('title',$data['title'])->where('id','<>',$data['id'])->where('del_time',0)->first();
  95. if(!empty($first))return [false,'名称已存在!'];
  96. $model = new Role();
  97. $model = $model->where('id',$data['id'])->first();
  98. $model->title = $data['title'];
  99. $model->save();
  100. return [true,'保存成功!'];
  101. }
  102. public function roleAdd($data,$user){
  103. list($status,$msg) = $this->roleRule($data);
  104. if(!$status) return [$status,$msg];
  105. $first = Role::where('title',$data['title'])->where('del_time',0)->first();
  106. if(!empty($first))return [false,'名称已存在!'];
  107. $model = new Role();
  108. $model->title = $data['title'] ;
  109. $model->save();
  110. return [true,'保存成功!'];
  111. }
  112. public function roleDel($data){
  113. if($this->isEmpty($data,'id')) return [false,'ID必须!'];
  114. Role::where('id',$data['id'])->update([
  115. 'del_time'=>time()
  116. ]);
  117. return [true,'删除成功'];
  118. }
  119. public function roleList($data){
  120. $list = Role::where('del_time',0)->select('title','title','crt_time','id','upd_time')->orderBy('id','desc');
  121. $list = $this->limit($list,'',$data);
  122. return [200,$list];
  123. }
  124. public function roleRule($data){
  125. if($this->isEmpty($data,'title')) return [false,'名称不存在!'];
  126. return [true,''];
  127. }
  128. public function departEdit($data){
  129. list($status,$msg) = $this->departRule($data);
  130. if(!$status) return [$status,$msg];
  131. $first = Depart::where('title',$data['title'])->where('id','<>',$data['id'])->where('del_time',0)->first();
  132. if(!empty($first))return [false,'名称已存在!'];
  133. $model = new Depart();
  134. $model = $model->where('id',$data['id'])->first();
  135. $model->title = $data['title'];
  136. $model->code = $data['code']??'';
  137. $model->save();
  138. return [true,'保存成功!'];
  139. }
  140. public function departAdd($data,$user){
  141. // if($this->isEmpty($data,'title')) return [201,'名称不存在!'];
  142. list($status,$msg) = $this->departRule($data);
  143. if(!$status) return [$status,$msg];
  144. $first = Depart::where('title',$data['title'])->where('id','<>',$data['id'])->where('del_time',0)->first();
  145. if(!empty($first))return [false,'名称已存在!'];
  146. $model = new Depart();
  147. $model->title = $data['title'] ;
  148. $model->code = $data['code'] ?? '' ;
  149. $model->save();
  150. return [true,'保存成功!'];
  151. }
  152. public function departDel($data){
  153. if($this->isEmpty($data,'id')) return [false,'ID必须!'];
  154. Depart::where('id',$data['id'])->update([
  155. 'del_time'=>time()
  156. ]);
  157. return [true,'删除成功'];
  158. }
  159. public function departList($data){
  160. $list = Depart::where('del_time',0)->select('title','crt_time','id','upd_time','code')->orderBy('id','desc');
  161. $list = $this->limit($list,'',$data);
  162. return [200,$list];
  163. }
  164. public function departRule($data){
  165. if($this->isEmpty($data,'title')) return [false,'名称不存在!'];
  166. return [true,''];
  167. }
  168. public function employeeRole($data){
  169. $role_ids = [];
  170. $employee_ids = [];
  171. foreach ($data as $v){
  172. if(isset($v['role_id'])){
  173. if(!in_array($v['role_id'],$role_ids)){
  174. $role_ids[] = $v['role_id'];
  175. }
  176. }
  177. if(isset($v['employee_id'])){
  178. if(!in_array($v['employee_id'],$employee_ids)){
  179. $employee_ids[] = $v['employee_id'];
  180. }
  181. }
  182. }
  183. EmployeeMenuPermission::wherein('role_id',$role_ids)->delete();
  184. EmployeeMenuPermission::wherein('employee_id',$employee_ids)->delete();
  185. EmployeeMenuPermission::insert($data);
  186. return [200,'保存成功!'];
  187. }
  188. public function employeeDepart($data){
  189. if($this->isEmpty($data,'insert')) return [false,'数据不能为空!'];
  190. DB::beginTransaction();
  191. try {
  192. if($data['type'] == 1){
  193. EmployeeDepartPermission::whereIn('depart_id',$data['insert']['depart_id'])->delete();
  194. }else{
  195. EmployeeDepartPermission::whereIn('employee_id',$data['insert']['employee_id'])->delete();
  196. }
  197. $insert = [];
  198. foreach ($data['insert']['depart_id'] as $t){
  199. foreach ($data['insert']['employee_id'] as $e){
  200. $insert[] = [
  201. 'depart_id' => $t,
  202. 'employee_id' => $e
  203. ];
  204. }
  205. }
  206. EmployeeDepartPermission::insert($insert);
  207. DB::commit();
  208. }catch (\Throwable $exception){
  209. DB::rollBack();
  210. return [false,$exception->getMessage()];
  211. }
  212. return [true,'保存成功!'];
  213. }
  214. public function loginRule($data){
  215. if($this->isEmpty($data,'account')) return [false,'账号不能为空!'];
  216. if($this->isEmpty($data,'password')) return [false,'密码不能为空!'];
  217. if($this->isLoginlimitation($data['account'])) return [false,'账号密码输入错误过多,30分钟内限制登录!'];
  218. $res = Employee::where('del_time',0)
  219. ->where('account', $data['account'])
  220. ->get()->toArray();
  221. if(empty($res)) return [false,'账号不存在或已被删除!'];
  222. $res = reset($res);
  223. if($res['state'] == Employee::NOT_USE) return [false,'账号停用!'];
  224. if($res['is_admin'] != Employee::IS_ADMIN) return [false,'该账号不能登录!'];
  225. //密码校验
  226. if(! Hash::check($data['password'], $res['password'])) {
  227. $msg = $this->errorSetLogin($data['account']);
  228. return [false,$msg];
  229. }
  230. return [true, ['id'=>$res['id'], 'name'=>$res['emp_name'], 'account' => $res['account']]];
  231. }
  232. //设置登录错误次数(超过三次)
  233. public function errorSetLogin($cacheKey){
  234. if(Cache::has($cacheKey)){
  235. $num = Cache::get($cacheKey);
  236. $num++;
  237. Cache::put($cacheKey,$num,30);
  238. if($num >= 3){
  239. return ['账号密码输入错误3次,30分钟内限制登录!'];
  240. }else{
  241. return ['账号密码输入错误第'. $num .'次!'];
  242. }
  243. }else{
  244. Cache::add($cacheKey,1,30);
  245. return ['密码输入错误!'];
  246. }
  247. }
  248. //判断是否限制登录
  249. public function isLoginlimitation($cacheKey){
  250. if(Cache::has($cacheKey)){
  251. $num = Cache::get($cacheKey);
  252. if($num >= 3) return true;
  253. }
  254. return false;
  255. }
  256. public static function checkUser($userId){
  257. $res = Employee::where('id', $userId)
  258. ->where('del_time',0)
  259. ->where('is_admin',Employee::IS_ADMIN)
  260. ->where('state',Employee::USE)->get()->first();
  261. if(empty($res)) return [false, '该账号无法登录,请联系管理员!'];
  262. return [true, $res];
  263. }
  264. //获取登录账号的角色
  265. public static function getPersonRole($employee_id){
  266. if(empty($employee_id)) return [];
  267. $role = EmployeeRole::where('del_time',0)
  268. ->where('employee_id',$employee_id)
  269. ->select('role_id')
  270. ->get()->toArray();
  271. //组织
  272. $role_id = array_column($role,'role_id');
  273. asort($role_id);
  274. $role_id = array_values($role_id);
  275. return $role_id;
  276. }
  277. //获取登录账号的权限部门
  278. public static function getPersonDepart($employee_id){
  279. if(empty($employee_id)) return [];
  280. //admin账号
  281. if($employee_id == Employee::SPECIAL_ADMIN) return [Depart::RULE_DEPART];
  282. //操作人员直接绑定部门
  283. $employee_manager_depart = EmployeeManagerDepart::where('del_time',0)
  284. ->where('employee_id',$employee_id)
  285. ->select('depart_id')
  286. ->get()->toArray();
  287. //操作人员绑定角色
  288. $employee_role = EmployeeRole::from('employee_role as a')
  289. ->leftJoin('role_depart as b','b.role_id','a.role_id')
  290. ->select('b.depart_id','b.role_id')
  291. ->where('a.del_time',0)
  292. ->where('b.del_time',0)
  293. ->where('a.employee_id',$employee_id)
  294. ->get()->toArray();
  295. return array_filter(array_merge_recursive(array_column($employee_manager_depart,'depart_id'),array_column($employee_role,'depart_id')));
  296. }
  297. //人员直接绑定部门
  298. public function employeeManagerDepart($data,$user){
  299. if($user['id'] != Employee::SPECIAL_ADMIN) return [false,'非ADMIN账号不能操作'];
  300. if($this->isEmpty($data,'employee_id')) return [false,'请选择操作人员'];
  301. if($this->isEmpty($data,'depart_id')) return [false,'请选择部门'];
  302. EmployeeManagerDepart::where('employee_id',$data['employee_id'])->update([
  303. 'del_time' => time()
  304. ]);
  305. $insert = [];
  306. foreach ($data['depart_id'] as $value){
  307. $insert[] = [
  308. 'employee_id' => $data['employee_id'],
  309. 'depart_id' => $value,
  310. 'crt_time' => time(),
  311. 'upd_time' => time(),
  312. ];
  313. }
  314. EmployeeManagerDepart::insert($insert);
  315. return [true,''];
  316. }
  317. }