Users.php 24KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Description: 系统员工
  4. // +----------------------------------------------------------------------
  5. // | Author: Michael_xu | gengxiaoxu@5kcrm.com
  6. // +----------------------------------------------------------------------
  7. namespace app\admin\controller;
  8. use app\admin\model\User;
  9. use think\Request;
  10. use think\Session;
  11. use think\Hook;
  12. use think\Cache;
  13. use think\Db;
  14. use app\admin\model\LoginRecord;
  15. use app\admin\model\User as UserModel;
  16. use app\admin\logic\UserLogic;
  17. use app\admin\model\Admin as AdminModel;
  18. use app\crm\traits\StarTrait;
  19. class Users extends ApiCommon
  20. {
  21. use StarTrait;
  22. /**
  23. * 用于判断权限
  24. * @permission 无限制
  25. * @allow 登录员工可访问
  26. * @other 其他根据系统设置
  27. **/
  28. public function _initialize()
  29. {
  30. $action = [
  31. 'permission' => ['exceldownload'],
  32. 'allow' => [
  33. 'index',
  34. 'update',
  35. 'updatepwd',
  36. 'read',
  37. 'updateimg',
  38. 'resetpassword',
  39. 'userlistbystructid',
  40. 'groups',
  41. 'groupsdel',
  42. 'tobeusers',
  43. 'structureuserlist',
  44. 'getuserlist',
  45. 'usernameedit',
  46. 'import',
  47. 'setparent',
  48. 'loginRecord',
  49. 'userstar',
  50. 'querylist',
  51. 'starlist',
  52. 'copyrole',
  53. 'subordinate'
  54. ]
  55. ];
  56. Hook::listen('check_auth',$action);
  57. $request = Request::instance();
  58. $a = strtolower($request->action());
  59. if (!in_array($a, $action['permission'])) {
  60. parent::_initialize();
  61. }
  62. }
  63. /**
  64. * 员工列表
  65. * @param
  66. * @return
  67. */
  68. public function index()
  69. {
  70. $userModel = model('User');
  71. $param = $this->param;
  72. $data = $userModel->getDataList($param);
  73. return resultArray(['data' => $data]);
  74. }
  75. /**
  76. * 员工详情
  77. * @param
  78. * @return
  79. */
  80. public function read()
  81. {
  82. $userModel = model('User');
  83. $param = $this->param;
  84. $userInfo = $this->userInfo;
  85. if (!$param['id']) $param['id'] = $userInfo['id'];
  86. $data = $userModel->getDataById($param['id']);
  87. if (!$data) {
  88. return resultArray(['error' => $userModel->getError()]);
  89. }
  90. return resultArray(['data' => $data]);
  91. }
  92. /**
  93. * 员工创建
  94. * @param
  95. * @return
  96. */
  97. public function save()
  98. {
  99. $userModel = model('User');
  100. $param = $this->param;
  101. $userInfo = $this->userInfo;
  102. $data = $userModel->createData($param);
  103. if (!$data) {
  104. return resultArray(['error' => $userModel->getError()]);
  105. }
  106. return resultArray(['data' => '添加成功']);
  107. }
  108. /**
  109. * 员工编辑
  110. * @param
  111. * @return
  112. */
  113. public function update()
  114. {
  115. $userModel = model('User');
  116. $param = $this->param;
  117. $userInfo = $this->userInfo;
  118. $userData = db('admin_user')->where(['id' => $param['id']])->find();
  119. if (!$param['id']) {
  120. //修改个人信息
  121. $param['user_id'] = $userInfo['id'];
  122. } else {
  123. //权限判断
  124. if (!checkPerByAction('admin', 'users', 'update')) {
  125. header('Content-Type:application/json; charset=utf-8');
  126. exit(json_encode(['code'=>102,'error'=>'无权操作']));
  127. }
  128. }
  129. unset($param['username']);
  130. $data = $userModel->updateDataById($param, $param['id']);
  131. if (!$data) {
  132. return resultArray(['error' => $userModel->getError()]);
  133. }
  134. $param['userInfo'] = $userData;
  135. $resSync = model('Sync')->syncData($param);
  136. return resultArray(['data' => '编辑成功']);
  137. }
  138. //批量设置密码
  139. public function updatePwd()
  140. {
  141. //权限判断
  142. if (!checkPerByAction('admin', 'users', 'update')) {
  143. header('Content-Type:application/json; charset=utf-8');
  144. exit(json_encode(['code'=>102,'error'=>'无权操作']));
  145. }
  146. $param = $this->param;
  147. if ($param['password'] && is_array($param['id'])) {
  148. $userModel = model('User');
  149. $ret = $userModel->updatePwdById($param);
  150. if ($ret) {
  151. return resultArray(['data'=>true]);
  152. } else {
  153. return resultArray(['error'=>$userModel->getError()]);
  154. }
  155. } else {
  156. return resultArray(['error'=>'参数错误']);
  157. }
  158. }
  159. /**
  160. * 员工状态
  161. * @param status 0禁用,1启用,2禁止登陆,3未激活
  162. * @return
  163. */
  164. public function enables()
  165. {
  166. $userModel = model('User');
  167. $param = $this->param;
  168. if (!is_array($param['id'])) {
  169. $ids[] = $param['id'];
  170. } else {
  171. $ids = $param['id'];
  172. }
  173. //顶级管理员不能修改
  174. foreach ($ids as $k=>$v) {
  175. if ((int)$v == 1 && $param['status'] == '0') {
  176. unset($ids[$k]);
  177. }
  178. }
  179. $data = $userModel->enableDatas($ids, $param['status']);
  180. if (!$data) {
  181. return resultArray(['error' => $userModel->getError()]);
  182. }
  183. return resultArray(['data' => '操作成功']);
  184. }
  185. /**
  186. * 获取权限范围内的员工数组
  187. * @param
  188. * @return
  189. */
  190. public function getUserList()
  191. {
  192. $userModel = model('User');
  193. $param = $this->param;
  194. $by = $param['by'] ? : '';
  195. $user_id = $param['user_id'] ? : '';
  196. $where = [];
  197. $belowIds = [];
  198. if ($param['m'] && $param['c'] && $param['a']) {
  199. if ($param['m'] == 'oa' && $param['c'] == 'task') {
  200. $belowIds = getSubUserId(true, 1);
  201. } else {
  202. $belowIds = $userModel->getUserByPer($param['m'], $param['c'], $param['a']);
  203. }
  204. $where['user.id'] = ['in',$belowIds];
  205. } else {
  206. if ($by == 'sub') {
  207. $userInfo = $this->userInfo;
  208. $adminIds = $userModel->getAdminId();
  209. if (in_array($userInfo['id'],$adminIds)) {
  210. $belowIds = getSubUserId(true, 1);
  211. } else {
  212. //下属id
  213. $belowIds = getSubUserId();
  214. }
  215. $where['user.id'] = ['in',$belowIds];
  216. } elseif ($by == 'parent') {
  217. if ($user_id == 1) {
  218. $where['user.id'] = 0;
  219. } else {
  220. $unUserId[] = $user_id;
  221. $subUserId = getSubUser($user_id);
  222. $unUserId = $subUserId ? array_merge($subUserId,$unUserId) : $unUserId;
  223. }
  224. $where['user.id'] = ['not in',$unUserId];
  225. } else {
  226. $belowIds = getSubUserId(true, 1);
  227. $where['user.id'] = ['in',$belowIds];
  228. }
  229. }
  230. $userList = db('admin_user')
  231. ->alias('user')
  232. ->where($where)
  233. ->where('user.status>0 and user.type=1')
  234. ->join('__ADMIN_STRUCTURE__ structure', 'structure.id = user.structure_id', 'LEFT')
  235. ->field('user.id,user.realname,user.thumb_img,structure.name as s_name')
  236. ->select();
  237. # 角色数据
  238. $groupList = db('admin_access')->alias('access')
  239. ->join('__ADMIN_GROUP__ group', 'group.id = access.group_id', 'LEFT')
  240. ->field('group.id, group.title, access.user_id')->select();
  241. $groupArray = [];
  242. foreach ($groupList AS $key => $value) {
  243. $groupArray[$value['user_id']]['roleId'][] = $value['id'];
  244. $groupArray[$value['user_id']]['roleName'][] = $value['title'];
  245. }
  246. foreach ($userList as $k=>$v) {
  247. $userList[$k]['username'] = $v['realname'];
  248. $userList[$k]['thumb_img'] = $v['thumb_img'] ? getFullPath($v['thumb_img']) : '';
  249. # 员工新增角色ID和角色名称字段
  250. $userList[$k]['roleId'] = !empty($groupArray[$v['id']]['roleId']) ? implode(',', $groupArray[$v['id']]['roleId']) : '';
  251. $userList[$k]['roleName'] = !empty($groupArray[$v['id']]['roleName']) ? implode(',', $groupArray[$v['id']]['roleName']) : '';
  252. # 单独处理admin管理员的角色ID和角色名称字段
  253. if ($v['id'] == 1 && (empty($groupArray[$v['id']]['roleId']) || empty($groupArray[$v['id']]['roleName']))) {
  254. $userList[$k]['roleId'] = '1';
  255. $userList[$k]['roleName'] = '超级管理员角色';
  256. }
  257. }
  258. return resultArray(['data' => $userList ? : []]);
  259. }
  260. /**
  261. * 修改头像
  262. * @param
  263. * @return
  264. */
  265. public function updateImg()
  266. {
  267. $fileModel = model('File');
  268. $param = $this->param;
  269. $userInfo = $this->userInfo;
  270. //处理图片
  271. header('Access-Control-Allow-Origin: *');
  272. header('Access-Control-Allow-Methods: POST');
  273. header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept");
  274. $param['file'] = request()->file('file');
  275. $resImg = $fileModel->updateByField($param['file'], 'User', $param['id'], 'img', 'thumb_img', 150, 150);
  276. if (!$resImg) {
  277. return resultArray(['error' => $fileModel->getError()]);
  278. }
  279. return resultArray(['data' => '上传成功']);
  280. }
  281. /**
  282. * 重置密码
  283. * @param
  284. * @return
  285. */
  286. public function resetPassword()
  287. {
  288. $param = $this->param;
  289. $userInfo = $this->userInfo;
  290. $userModel = model('User');
  291. if ($param['id'] && (int)$param['id'] !== $userInfo['id']) {
  292. //权限判断
  293. if (!checkPerByAction('admin', 'users', 'update')) {
  294. header('Content-Type:application/json; charset=utf-8');
  295. exit(json_encode(['code'=>102,'error'=>'无权操作']));
  296. }
  297. $user_id = $param['id'];
  298. if (!$param['new_pwd']) {
  299. $this->error = '请输入重置密码';
  300. return false;
  301. }
  302. $userInfo = $userModel->getDataById($user_id);
  303. if (user_md5($param['new_pwd'], $userInfo['salt'], $userInfo['username']) == $userInfo['password']) {
  304. $this->error = '密码没改变';
  305. return false;
  306. }
  307. if (db('admin_user')->where('id', $user_id)->setField('password', user_md5($param['new_pwd'], $userInfo['salt'], $userInfo['username']))) {
  308. $syncData = [];
  309. $syncModel = new \app\admin\model\Sync();
  310. $syncData['user_id'] = $userInfo['id'];
  311. $syncData['salt'] = $userInfo['salt'];
  312. $syncData['password'] = user_md5($param['new_pwd'], $userInfo['salt'], $userInfo['username']);
  313. $resSync = $syncModel->syncData($syncData);
  314. return resultArray(['data' => '密码重置成功']);
  315. } else {
  316. return resultArray(['error' => '密码重置失败,请重试']);
  317. }
  318. } else {
  319. $userModel = model('User');
  320. $old_pwd = $param['old_pwd'];
  321. $new_pwd = $param['new_pwd'];
  322. $data = $userModel->updatePaw($userInfo, $old_pwd, $new_pwd);
  323. if (!$data) {
  324. return resultArray(['error' => $userModel->getError()]);
  325. }
  326. return resultArray(['data' => $data]);
  327. }
  328. }
  329. /**
  330. * 员工角色关系
  331. * @param
  332. * @return
  333. */
  334. public function groups()
  335. {
  336. //权限判断
  337. if (!checkPerByAction('admin', 'groups', 'update')) {
  338. header('Content-Type:application/json; charset=utf-8');
  339. exit(json_encode(['code'=>102,'error'=>'无权操作']));
  340. }
  341. $param = $this->param;
  342. if (!$param['users'] && !$param['structures']) {
  343. return resultArray(['error' => '请选择员工']);
  344. }
  345. if (!$param['groups']) {
  346. return resultArray(['error' => '请选择角色']);
  347. }
  348. $userModel = model('User');
  349. //部门下所有员工
  350. $userArr = [];
  351. if (is_array($param['structures'])) {
  352. foreach ($param['structures'] as $v) {
  353. $userArr[] = $userModel->getSubUserByStr($v);
  354. }
  355. }
  356. if ($userArr) $userArr = call_user_func_array('array_merge', $userArr); //数组合并
  357. if ($userArr && $param['users']) {
  358. $userIds = array_merge($userArr, $param['users']);
  359. } elseif ($userArr) {
  360. $userIds = $userArr;
  361. } else {
  362. $userIds = $param['users'];
  363. }
  364. $userIds = array_unique($userIds);
  365. $groups = $param['groups'];
  366. $accessModel = model('Access');
  367. $resData = true;
  368. foreach ($userIds as $k=>$v) {
  369. //角色员工关系处理
  370. $res = $accessModel->userGroup($v, $param['groups']);
  371. if (!$res) {
  372. $resData = false;
  373. }
  374. }
  375. // if ($resData == false) {
  376. // return resultArray(['error' => '操作失败,请重试']);
  377. // }
  378. return resultArray(['data' => '创建成功']);
  379. }
  380. /**
  381. * 员工角色关系(删除)
  382. * @param
  383. * @return
  384. */
  385. public function groupsDel()
  386. {
  387. //权限判断
  388. if (!checkPerByAction('admin', 'groups', 'update')) {
  389. header('Content-Type:application/json; charset=utf-8');
  390. exit(json_encode(['code'=>102,'error'=>'无权操作']));
  391. }
  392. $param = $this->param;
  393. if (!$param['user_id']) {
  394. return resultArray(['error' => '请选择员工']);
  395. }
  396. if (!$param['group_id']) {
  397. return resultArray(['error' => '参数错误']);
  398. }
  399. # 员工至少保留一个角色
  400. $count = db('admin_access')->where(['user_id' => $param['user_id']])->count();
  401. if ($count == 1) return resultArray(['error' => '员工至少保留一个角色!']);
  402. $res = db('admin_access')->where(['user_id' => $param['user_id'],'group_id' => $param['group_id']])->delete();
  403. if (!$res) {
  404. return resultArray(['error' => '操作失败,请重试']);
  405. }
  406. return resultArray(['data' => '删除成功']);
  407. }
  408. /**
  409. * [structureUserList 部门员工混合数据]
  410. * @param
  411. * @return
  412. */
  413. public function structureUserList()
  414. {
  415. $structure_list = db('admin_structure')->select();
  416. $structureList = getSubObj(0, $structure_list, '', 1);
  417. foreach ($structureList as $k=>$v) {
  418. $userList = [];
  419. $userList = db('admin_user')->where(['structure_id' => $v['id'],'status' => array('in',['1','3'])])->field('id,realname')->select();
  420. $structureList[$k]['userList'] = $userList;
  421. }
  422. return $structureList;
  423. }
  424. //人资员工导入
  425. public function tobeusers(){
  426. $userModel = model('User');
  427. $param = $this->param;
  428. $flag = $userModel->beusers($param);
  429. if ($flag) {
  430. return resultArray(['data'=>$flag]);
  431. } else {
  432. return resultArray(['error'=>$userModel->getError()]);
  433. }
  434. }
  435. //根据部门ID获取员工列表
  436. public function userListByStructId()
  437. {
  438. $usermodel = model('User');
  439. $param = $this->param;
  440. $structure_id = $param['structure_id']?:'';
  441. $ret = $usermodel->getUserListByStructureId($structure_id) ? : [];
  442. return resultArray(['data'=>$ret]);
  443. }
  444. /**
  445. * 员工账号修改
  446. * @param
  447. * @return
  448. */
  449. public function usernameEdit()
  450. {
  451. //权限判断
  452. if (!checkPerByAction('admin', 'users', 'update')) {
  453. header('Content-Type:application/json; charset=utf-8');
  454. exit(json_encode(['code'=>102,'error'=>'无权操作']));
  455. }
  456. $param = $this->param;
  457. $userInfo = $this->userInfo;
  458. //权限判断
  459. if ($param['id'] == 1) {
  460. return resultArray(['error' => '管理员账号暂不能修改']);
  461. }
  462. $adminTypes = adminGroupTypes($userInfo['id']);
  463. if (!in_array(3,$adminTypes) && !in_array(1,$adminTypes) && !in_array(2,$adminTypes)) {
  464. header('Content-Type:application/json; charset=utf-8');
  465. exit(json_encode(['code'=>102,'error'=>'无权操作']));
  466. }
  467. if (!$param['id'] || !$param['username'] || !$param['password']) {
  468. return resultArray(['error' => '参数错误!']);
  469. }
  470. if (db('admin_user')->where(['id' => ['neq',$param['id']],'username' => $param['username']])->find()) {
  471. return resultArray(['error' => '手机号码已存在!']);
  472. }
  473. $userData = db('admin_user')->where(['id' => $param['id']])->field('username,salt,password')->find();
  474. $data = [];
  475. $data['username'] = $param['username'];
  476. $data['password'] = user_md5($param['password'], $userData['salt'], $param['username']);
  477. $data['userInfo'] = $userData;
  478. $resSync = model('Sync')->syncData($data);
  479. if ($resSync) {
  480. unset($data['userInfo']);
  481. $res = db('admin_user')->where(['id' => $param['id']])->update($data);
  482. return resultArray(['data' => '修改成功!']);
  483. } else {
  484. return resultArray(['error' => '修改失败,请重试!']);
  485. }
  486. }
  487. /**
  488. * 登录记录
  489. */
  490. public function loginRecord()
  491. {
  492. if (!checkPerByAction('admin', 'loginRecord', 'index')) {
  493. header('Content-Type:application/json; charset=utf-8');
  494. exit(json_encode(['code' => 102, 'error' => '无权操作']));
  495. }
  496. $loginRecordModel = new LoginRecord();
  497. $where = [];
  498. getWhereUserByParam($where, 'create_user_id');
  499. getWhereTimeByParam($where, 'create_time');
  500. $limit = $this->param['limit'] ?: 15;
  501. $data = $loginRecordModel
  502. ->where($where)
  503. ->order(['create_time' => 'DESC'])
  504. ->paginate($limit)
  505. ->each(function ($val) {
  506. $val['username'] = $val->create_user_info['realname'];
  507. $val['type_name'] = $val->type_name;
  508. })
  509. ->toArray();
  510. return resultArray([
  511. 'data' => [
  512. 'list' => $data['data'],
  513. 'dataCount' => $data['total']
  514. ],
  515. ]);
  516. }
  517. /**
  518. * 员工导入模板下载
  519. * @author Michael_xu
  520. * @param string $save_path 本地保存路径 用于错误数据导出,在 Admin\Model\Excel::batchImportData()调用
  521. * @return
  522. */
  523. public function excelDownload($save_path = '')
  524. {
  525. $param = $this->param;
  526. $userInfo = $this->userInfo;
  527. $excelModel = new \app\admin\model\Excel();
  528. // 导出的字段列表
  529. $field_list = UserModel::$import_field_list;
  530. $excelModel->excelImportDownload($field_list, 'admin_user', $save_path);
  531. }
  532. /**
  533. * 员工导入
  534. */
  535. public function import()
  536. {
  537. // 仅允许超管,系统管理员,部门与员工管理员 导入
  538. if (false === UserModel::checkUserGroup([1, 2, 3])) {
  539. return resultArray(['error' => '没有该权限']);
  540. }
  541. $param = $this->param;
  542. $userInfo = $this->userInfo;
  543. $excelModel = new \app\admin\model\Excel();
  544. $param['types'] = 'admin_user';
  545. $file = request()->file('file');
  546. $res = $excelModel->batchImportData($file, $param, $this);
  547. $list=[];
  548. $list[]=$excelModel->getError();
  549. $item=$list[0];
  550. if (!$res) {
  551. return resultArray(['data' => $item]);
  552. }
  553. Cache::clear('user_info');
  554. return resultArray(['data' => $item]);
  555. }
  556. /**
  557. * 批量设置直属上级
  558. *
  559. * @author Ymob
  560. * @datetime 2019-10-28 13:37:57
  561. */
  562. public function setParent()
  563. {
  564. // 仅允许超管,系统管理员,部门与员工管理员 批量设置
  565. if (false === UserModel::checkUserGroup([1, 2, 3])) {
  566. return resultArray(['error' => '没有该权限']);
  567. }
  568. $parent_id = (int) $this->param['parent_id'];
  569. $parent_user = UserModel::find($parent_id);
  570. if (!$parent_user) {
  571. return resultArray(['error' => '请选择直属上级']);
  572. }
  573. $user_id_list = (array) $this->param['id_list'];
  574. if (empty($user_id_list)) {
  575. return resultArray(['error' => '请选择员工']);
  576. }
  577. if (in_array(1, $user_id_list)) {
  578. return resultArray(['error' => '超级管理员不能设置上级']);
  579. }
  580. if (in_array($parent_id, $user_id_list)) {
  581. return resultArray(['error' => '直属上级不能为员工自己']);
  582. }
  583. foreach ($user_id_list as $val) {
  584. if (in_array($parent_id, getSubUserId(true, 0, (int) $val))) {
  585. return resultArray(['error' => '直属上级不能是自己下属(包含下属的下属)']);
  586. }
  587. }
  588. $a = new UserModel;
  589. if ($a->where(['id' => ['IN', $user_id_list]])->update(['parent_id' => $parent_id])) {
  590. Cache::clear('user_info');
  591. return resultArray(['data' => '员工直属上级设置成功']);
  592. } else {
  593. return resultArray(['error' => '员工直属上级设置失败' . $a->getError()]);
  594. }
  595. }
  596. /**
  597. * 通讯录列表
  598. * @return mixed
  599. */
  600. public function queryList(){
  601. $param = $this->param;
  602. $userInfo = $this->userInfo;
  603. $param['user_id']=$userInfo['id'];
  604. $userLogic=new UserLogic();
  605. $data=$userLogic->getDataList($param);
  606. return resultArray(['data' => $data]);
  607. }
  608. /**
  609. * 关注的通讯录列表
  610. * @return mixed
  611. */
  612. public function starList(){
  613. $param = $this->param;
  614. $userInfo = $this->userInfo;
  615. $param['user_id']=$userInfo['id'];
  616. $userLogic=new UserLogic();
  617. $data=$userLogic->queryList($param);
  618. return resultArray(['data' => $data]);
  619. }
  620. /**
  621. * 设置关注
  622. *
  623. * @return \think\response\Json
  624. * @throws \think\Exception
  625. * @throws \think\exception\PDOException
  626. */
  627. public function userStar()
  628. {
  629. $userInfo = $this->userInfo;
  630. $userId = $userInfo['id'];
  631. $targetId = $this->param['target_id'];
  632. $type = $this->param['type'];
  633. if (empty($userId) || empty($targetId) || empty($type)) return resultArray(['error' => '缺少必要参数!']);
  634. if (!$this->setStar($type, $userId, $targetId)) {
  635. return resultArray(['error' => '设置关注失败!']);
  636. }
  637. return resultArray(['data' => '设置关注成功!']);
  638. }
  639. /**
  640. * 复制员工角色
  641. *
  642. * @return \think\response\Json
  643. */
  644. public function copyRole()
  645. {
  646. $param = $this->param;
  647. if (empty($param['user_id']) && empty($param['structure_id'])) return resultArray(['error' => '请选择员工或部门!']);
  648. if (empty($param['group_id'])) return resultArray(['error' => '请选择角色!']);
  649. $userModel = new User();
  650. if (!$userModel->copyRole($param)) return resultArray(['error' => '操作失败!']);
  651. return resultArray(['data' => '操作成功!']);
  652. }
  653. /**
  654. * 获取下属(全部层级)
  655. *
  656. * @return \think\response\Json
  657. * @throws \think\db\exception\DataNotFoundException
  658. * @throws \think\db\exception\ModelNotFoundException
  659. * @throws \think\exception\DbException
  660. */
  661. public function subordinate()
  662. {
  663. $userId = $this->userInfo['id'];
  664. # 获取下属的ID
  665. $subIds = getSubUserId(false, 0, $userId);
  666. $data = Db::name('admin_user')->field(['id', 'realname', 'thumb_img as img'])->whereIn('id', $subIds)->select();
  667. foreach ($data AS $key => $value) {
  668. $data[$key]['img'] = !empty($data[$key]['img']) ? getFullPath($data[$key]['img']) : '';
  669. }
  670. return resultArray(['data' => $data]);
  671. }
  672. }