Users.php 30KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884
  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. 'countnumofuser',
  55. 'setuserdept'
  56. ]
  57. ];
  58. Hook::listen('check_auth', $action);
  59. $request = Request::instance();
  60. $a = strtolower($request->action());
  61. if (!in_array($a, $action['permission'])) {
  62. parent::_initialize();
  63. }
  64. }
  65. /**
  66. * 员工列表
  67. * @param
  68. * @return
  69. */
  70. public function index()
  71. {
  72. $userModel = model('User');
  73. $param = $this->param;
  74. $data = $userModel->getDataList($param);
  75. return resultArray(['data' => $data]);
  76. }
  77. /**
  78. * 员工详情
  79. * @param
  80. * @return
  81. */
  82. public function read()
  83. {
  84. $userModel = model('User');
  85. $param = $this->param;
  86. $userInfo = $this->userInfo;
  87. if (!$param['id']) $param['id'] = $userInfo['id'];
  88. $data = $userModel->getDataById($param['id']);
  89. if (!$data) {
  90. return resultArray(['error' => $userModel->getError()]);
  91. }
  92. $serverUserInfo = $this->queryLoginUser();
  93. if (!empty($serverUserInfo)) $data['serverUserInfo'] = $serverUserInfo;
  94. # 角色权限查看 配置范围对应id
  95. $rule_authority_id = db('admin_rule')->where(['title' => '角色权限查看', 'name' => 'ruleList'])->value('id');
  96. $data['rule_authority_id'] = $rule_authority_id;
  97. # 数据库更新 todo 在线升级正常使用后删除
  98. $updateStatus = $this->executeUpdateSql();
  99. if (empty($updateStatus['status'])) return resultArray(['error' => $updateStatus['message']]);
  100. return resultArray(['data' => $data]);
  101. }
  102. /**
  103. * 更新SQL
  104. *
  105. * @author fanqi
  106. * @since 2021-05-08
  107. */
  108. public function executeUpdateSql()
  109. {
  110. // 表前缀
  111. $prefix = config('database.prefix');
  112. // 检查更新记录表是否存在
  113. if (!db()->query("SHOW TABLES LIKE '".$prefix."admin_upgrade_record'")) {
  114. db()->query("
  115. CREATE TABLE `".$prefix."admin_upgrade_record` (
  116. `version` int(10) unsigned DEFAULT NULL COMMENT '版本号',
  117. UNIQUE KEY `version` (`version`) USING BTREE
  118. ) ENGINE = InnoDB DEFAULT CHARSET = utf8 COMMENT = 'SQL更新记录,用于防止重复执行更新。'
  119. ");
  120. }
  121. // 检查是否执行过11.0.3版本的更新
  122. if (!db('admin_upgrade_record')->where('version', 1103)->value('version')) {
  123. // 添加跟进记录导入导出权限数据
  124. UpdateSql::addFollowRuleData();
  125. // 添加公海默认数据
  126. $poolStatus = UpdateSql::addPoolDefaultData();
  127. if (!$poolStatus) return ['status' => false, 'message' => '添加公海默认配置失败,请在后台手动添加!'];
  128. // 添加此次升级标记
  129. db('admin_upgrade_record')->insert(['version' => 1103]);
  130. return ['status' => true, 'message' => '更新完成!'];
  131. }
  132. // 检查是否执行过11.0.4版本的更新
  133. if (!db('admin_upgrade_record')->where('version', 1104)->value('version')) {
  134. // 处理11.0.3升级时,没有处理旧公海数据的问题
  135. UpdateSql::SynchronizationCustomerToPool();
  136. // 发票导出权限
  137. UpdateSql::createInvoiceExportRule();
  138. // 修改数字字段类型
  139. UpdateSql::updateFieldNumberType();
  140. // 添加此次升级标记
  141. db('admin_upgrade_record')->insert(['version' => 1104]);
  142. }
  143. return ['status' => true, 'message' => '没有可用更新!'];
  144. }
  145. /**
  146. * 员工创建
  147. * @param
  148. * @return
  149. */
  150. public function save()
  151. {
  152. $userModel = model('User');
  153. $param = $this->param;
  154. $userInfo = $this->userInfo;
  155. $param['userId']=$userInfo['id'];
  156. $data = $userModel->createData($param);
  157. if (!$data) {
  158. return resultArray(['error' => $userModel->getError()]);
  159. }
  160. return resultArray(['data' => '添加成功']);
  161. }
  162. /**
  163. * 员工编辑
  164. * @param
  165. * @return
  166. */
  167. public function update()
  168. {
  169. $userModel = model('User');
  170. $param = $this->param;
  171. $userInfo = $this->userInfo;
  172. $userData = db('admin_user')->where(['id' => $param['id']])->find();
  173. if (!$param['id']) {
  174. //修改个人信息
  175. $param['user_id'] = $userInfo['id'];
  176. } else {
  177. //权限判断
  178. if (!checkPerByAction('admin', 'users', 'update')) {
  179. header('Content-Type:application/json; charset=utf-8');
  180. exit(json_encode(['code' => 102, 'error' => '无权操作']));
  181. }
  182. $param['userId']=$userInfo['id'];
  183. }
  184. unset($param['username']);
  185. $data = $userModel->updateDataById($param, $param['id']);
  186. if (!$data) {
  187. return resultArray(['error' => $userModel->getError()]);
  188. }
  189. $param['userInfo'] = $userData;
  190. $resSync = model('Sync')->syncData($param);
  191. return resultArray(['data' => '编辑成功']);
  192. }
  193. //批量设置密码
  194. public function updatePwd()
  195. {
  196. //权限判断
  197. if (!checkPerByAction('admin', 'users', 'update')) {
  198. header('Content-Type:application/json; charset=utf-8');
  199. exit(json_encode(['code' => 102, 'error' => '无权操作']));
  200. }
  201. $param = $this->param;
  202. $userInfo=$this->userInfo;
  203. $param['user_id']=$userInfo['id'];
  204. if ($param['password'] && is_array($param['id'])) {
  205. $userModel = model('User');
  206. $ret = $userModel->updatePwdById($param);
  207. if ($ret) {
  208. return resultArray(['data' => true]);
  209. } else {
  210. return resultArray(['error' => $userModel->getError()]);
  211. }
  212. } else {
  213. return resultArray(['error' => '参数错误']);
  214. }
  215. }
  216. /**
  217. * 员工状态
  218. * @param status 0禁用,1启用,2禁止登陆,3未激活
  219. * @return
  220. */
  221. public function enables()
  222. {
  223. $userModel = model('User');
  224. $param = $this->param;
  225. if (!is_array($param['id'])) {
  226. $ids[] = $param['id'];
  227. } else {
  228. $ids = $param['id'];
  229. }
  230. //顶级管理员不能修改
  231. foreach ($ids as $k => $v) {
  232. if ((int)$v == 1 && $param['status'] == '0') {
  233. unset($ids[$k]);
  234. }
  235. }
  236. $data = $userModel->enableDatas($ids, $param['status']);
  237. if (!$data) {
  238. return resultArray(['error' => $userModel->getError()]);
  239. }
  240. # 添加记录
  241. if ($param['status'] == 0) {
  242. $content = '禁用了:';
  243. } elseif ($param['status'] == 1) {
  244. $content = '激活了:';
  245. }
  246. $user = new ApiCommon();
  247. $userInfo = $user->userInfo;
  248. foreach ($ids as $key => $val) {
  249. $dataInfo = db('admin_user')->where('id', $val)->find();
  250. SystemActionLog($userInfo['id'], 'admin_user', 'employee', $val, 'update', '员工状态', '', '', $content . $dataInfo['realname']);
  251. }
  252. return resultArray(['data' => '操作成功']);
  253. }
  254. /**
  255. * 获取权限范围内的员工数组
  256. * @param
  257. * @return
  258. */
  259. public function getUserList()
  260. {
  261. $userModel = model('User');
  262. $param = $this->param;
  263. $by = $param['by'] ?: '';
  264. $user_id = $param['user_id'] ?: '';
  265. $where = [];
  266. $belowIds = [];
  267. if ($param['m'] && $param['c'] && $param['a']) {
  268. if ($param['m'] == 'oa' && $param['c'] == 'task') {
  269. $belowIds = getSubUserId(true, 1);
  270. } else {
  271. $belowIds = $userModel->getUserByPer($param['m'], $param['c'], $param['a']);
  272. }
  273. $where['user.id'] = ['in', $belowIds];
  274. } else {
  275. if ($by == 'sub') {
  276. $userInfo = $this->userInfo;
  277. $adminIds = $userModel->getAdminId();
  278. if (in_array($userInfo['id'], $adminIds)) {
  279. $belowIds = getSubUserId(true, 1);
  280. } else {
  281. //下属id
  282. $belowIds = getSubUserId();
  283. }
  284. $where['user.id'] = ['in', $belowIds];
  285. } elseif ($by == 'parent') {
  286. if ($user_id == 1) {
  287. $where['user.id'] = 0;
  288. } else {
  289. $unUserId[] = $user_id;
  290. $subUserId = getSubUser($user_id);
  291. $unUserId = $subUserId ? array_merge($subUserId, $unUserId) : $unUserId;
  292. }
  293. $where['user.id'] = ['not in', $unUserId];
  294. } else {
  295. $belowIds = getSubUserId(true, 1);
  296. $where['user.id'] = ['in', $belowIds];
  297. }
  298. }
  299. $userList = db('admin_user')
  300. ->alias('user')
  301. ->where($where)
  302. ->where('user.status>0 and user.type=1')
  303. ->join('__ADMIN_STRUCTURE__ structure', 'structure.id = user.structure_id', 'LEFT')
  304. ->field('user.id,user.realname,user.thumb_img,structure.name as s_name')
  305. ->select();
  306. # 角色数据
  307. $groupList = db('admin_access')->alias('access')
  308. ->join('__ADMIN_GROUP__ group', 'group.id = access.group_id', 'LEFT')
  309. ->field('group.id, group.title, access.user_id')->select();
  310. $groupArray = [];
  311. foreach ($groupList as $key => $value) {
  312. $groupArray[$value['user_id']]['roleId'][] = $value['id'];
  313. $groupArray[$value['user_id']]['roleName'][] = $value['title'];
  314. }
  315. foreach ($userList as $k => $v) {
  316. $userList[$k]['username'] = $v['realname'];
  317. $userList[$k]['thumb_img'] = $v['thumb_img'] ? getFullPath($v['thumb_img']) : '';
  318. # 员工新增角色ID和角色名称字段
  319. $userList[$k]['roleId'] = !empty($groupArray[$v['id']]['roleId']) ? implode(',', $groupArray[$v['id']]['roleId']) : '';
  320. $userList[$k]['roleName'] = !empty($groupArray[$v['id']]['roleName']) ? implode(',', $groupArray[$v['id']]['roleName']) : '';
  321. # 单独处理admin管理员的角色ID和角色名称字段
  322. if ($v['id'] == 1 && (empty($groupArray[$v['id']]['roleId']) || empty($groupArray[$v['id']]['roleName']))) {
  323. $userList[$k]['roleId'] = '1';
  324. $userList[$k]['roleName'] = '超级管理员角色';
  325. }
  326. }
  327. return resultArray(['data' => $userList ?: []]);
  328. }
  329. /**
  330. * 修改头像
  331. * @param
  332. * @return
  333. */
  334. public function updateImg()
  335. {
  336. $fileModel = model('File');
  337. $param = $this->param;
  338. $userInfo = $this->userInfo;
  339. //处理图片
  340. header('Access-Control-Allow-Origin: *');
  341. header('Access-Control-Allow-Methods: POST');
  342. header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept");
  343. $param['file'] = request()->file('file');
  344. $resImg = $fileModel->updateByField($param['file'], 'User', $param['id'], 'img', 'thumb_img', 150, 150);
  345. if (!$resImg) {
  346. return resultArray(['error' => $fileModel->getError()]);
  347. }
  348. return resultArray(['data' => '上传成功']);
  349. }
  350. /**
  351. * 重置密码
  352. * @param
  353. * @return
  354. */
  355. public function resetPassword()
  356. {
  357. $param = $this->param;
  358. $userInfo = $this->userInfo;
  359. $userModel = model('User');
  360. $userId=$userInfo['id'];
  361. if (empty($param['new_pwd']) || empty($param['old_pwd'])) return resultArray(['error' => '密码不能为空!']);
  362. if ($param['id'] && (int)$param['id'] !== $userInfo['id']) {
  363. //权限判断
  364. if (!checkPerByAction('admin', 'users', 'update')) {
  365. header('Content-Type:application/json; charset=utf-8');
  366. exit(json_encode(['code' => 102, 'error' => '无权操作']));
  367. }
  368. $user_id = $param['id'];
  369. if (!$param['new_pwd']) {
  370. $this->error = '请输入重置密码';
  371. return false;
  372. }
  373. $userInfo = $userModel->getDataById($user_id);
  374. if (user_md5($param['new_pwd'], $userInfo['salt'], $userInfo['username']) == $userInfo['password']) {
  375. $this->error = '密码没改变';
  376. return false;
  377. }
  378. if (db('admin_user')->where('id', $user_id)->setField('password', user_md5($param['new_pwd'], $userInfo['salt'], $userInfo['username']))) {
  379. $syncData = [];
  380. $syncModel = new \app\admin\model\Sync();
  381. $syncData['user_id'] = $userInfo['id'];
  382. $syncData['salt'] = $userInfo['salt'];
  383. $syncData['password'] = user_md5($param['new_pwd'], $userInfo['salt'], $userInfo['username']);
  384. $resSync = $syncModel->syncData($syncData);
  385. # 添加记录
  386. SystemActionLog($userInfo['id'], 'admin_user', 'employee', $userInfo['id'], 'update', $userInfo['realname'], '', '', '重置了密码:' . $userInfo['realname']);
  387. return resultArray(['data' => '密码重置成功']);
  388. } else {
  389. return resultArray(['error' => '密码重置失败,请重试']);
  390. }
  391. } else {
  392. $userModel = model('User');
  393. $old_pwd = $param['old_pwd'];
  394. $new_pwd = $param['new_pwd'];
  395. $data = $userModel->updatePaw($userInfo, $old_pwd, $new_pwd,$userId);
  396. if (!$data) {
  397. return resultArray(['error' => $userModel->getError()]);
  398. }
  399. return resultArray(['data' => $data]);
  400. }
  401. }
  402. /**
  403. * 员工角色关系
  404. * @param
  405. * @return
  406. */
  407. public function groups()
  408. {
  409. //权限判断
  410. if (!checkPerByAction('admin', 'groups', 'update')) {
  411. header('Content-Type:application/json; charset=utf-8');
  412. exit(json_encode(['code' => 102, 'error' => '无权操作']));
  413. }
  414. $param = $this->param;
  415. if (!$param['users'] && !$param['structures']) {
  416. return resultArray(['error' => '请选择员工']);
  417. }
  418. if (!$param['groups']) {
  419. return resultArray(['error' => '请选择角色']);
  420. }
  421. $userModel = model('User');
  422. //部门下所有员工
  423. $userArr = [];
  424. if (is_array($param['structures'])) {
  425. foreach ($param['structures'] as $v) {
  426. $userArr[] = $userModel->getSubUserByStr($v);
  427. }
  428. }
  429. if ($userArr) $userArr = call_user_func_array('array_merge', $userArr); //数组合并
  430. if ($userArr && $param['users']) {
  431. $userIds = array_merge($userArr, $param['users']);
  432. } elseif ($userArr) {
  433. $userIds = $userArr;
  434. } else {
  435. $userIds = $param['users'];
  436. }
  437. $userIds = array_unique($userIds);
  438. $groups = $param['groups'];
  439. $accessModel = model('Access');
  440. $resData = true;
  441. $user_id = $this->userInfo;
  442. foreach ($userIds as $k => $v) {
  443. //角色员工关系处理
  444. $res = $accessModel->userGroup($v, $param['groups']);
  445. if (!$res) {
  446. $resData = false;
  447. }
  448. $userInfo = Db::name('admin_user')->where('id', $v)->find();
  449. $user_id=$this->userInfo;
  450. SystemActionLog($user_id['id'], 'admin_user', 'employee', $v, 'update', $userInfo['realname'], '', '', '员工关联了角色:' . $userInfo['realname']);
  451. }
  452. // if ($resData == false) {
  453. // return resultArray(['error' => '操作失败,请重试']);
  454. // }
  455. return resultArray(['data' => '创建成功']);
  456. }
  457. /**
  458. * 员工角色关系(删除)
  459. * @param
  460. * @return
  461. */
  462. public function groupsDel()
  463. {
  464. //权限判断
  465. if (!checkPerByAction('admin', 'groups', 'update')) {
  466. header('Content-Type:application/json; charset=utf-8');
  467. exit(json_encode(['code' => 102, 'error' => '无权操作']));
  468. }
  469. $param = $this->param;
  470. if (!$param['user_id']) {
  471. return resultArray(['error' => '请选择员工']);
  472. }
  473. if (!$param['group_id']) {
  474. return resultArray(['error' => '参数错误']);
  475. }
  476. # 员工至少保留一个角色
  477. $count = db('admin_access')->where(['user_id' => $param['user_id']])->count();
  478. if ($count == 1) return resultArray(['error' => '员工至少保留一个角色!']);
  479. $res = db('admin_access')->where(['user_id' => $param['user_id'], 'group_id' => $param['group_id']])->delete();
  480. if (!$res) {
  481. return resultArray(['error' => '操作失败,请重试']);
  482. }
  483. return resultArray(['data' => '删除成功']);
  484. }
  485. /**
  486. * [structureUserList 部门员工混合数据]
  487. * @param
  488. * @return
  489. */
  490. public function structureUserList()
  491. {
  492. $structure_list = db('admin_structure')->select();
  493. $structureList = getSubObj(0, $structure_list, '', 1);
  494. foreach ($structureList as $k => $v) {
  495. $userList = [];
  496. $userList = db('admin_user')->where(['structure_id' => $v['id'], 'status' => array('in', ['1', '3'])])->field('id,realname')->select();
  497. $structureList[$k]['userList'] = $userList;
  498. }
  499. return $structureList;
  500. }
  501. //人资员工导入
  502. public function tobeusers()
  503. {
  504. $userModel = model('User');
  505. $param = $this->param;
  506. $flag = $userModel->beusers($param);
  507. if ($flag) {
  508. return resultArray(['data' => $flag]);
  509. } else {
  510. return resultArray(['error' => $userModel->getError()]);
  511. }
  512. }
  513. //根据部门ID获取员工列表
  514. public function userListByStructId()
  515. {
  516. $usermodel = model('User');
  517. $param = $this->param;
  518. $structure_id = $param['structure_id'] ?: '';
  519. $ret = $usermodel->getUserListByStructureId($structure_id) ?: [];
  520. return resultArray(['data' => $ret]);
  521. }
  522. /**
  523. * 员工账号修改
  524. * @param
  525. * @return
  526. */
  527. public function usernameEdit()
  528. {
  529. //权限判断
  530. if (!checkPerByAction('admin', 'users', 'update')) {
  531. header('Content-Type:application/json; charset=utf-8');
  532. exit(json_encode(['code' => 102, 'error' => '无权操作']));
  533. }
  534. $param = $this->param;
  535. $userInfo = $this->userInfo;
  536. //权限判断
  537. if ($param['id'] == 1) {
  538. return resultArray(['error' => '管理员账号暂不能修改']);
  539. }
  540. $adminTypes = adminGroupTypes($userInfo['id']);
  541. if (!in_array(3, $adminTypes) && !in_array(1, $adminTypes) && !in_array(2, $adminTypes)) {
  542. header('Content-Type:application/json; charset=utf-8');
  543. exit(json_encode(['code' => 102, 'error' => '无权操作']));
  544. }
  545. if (!$param['id'] || !$param['username'] || !$param['password']) {
  546. return resultArray(['error' => '参数错误!']);
  547. }
  548. if (db('admin_user')->where(['id' => ['neq', $param['id']], 'username' => $param['username']])->find()) {
  549. return resultArray(['error' => '手机号码已存在!']);
  550. }
  551. $userData = db('admin_user')->where(['id' => $param['id']])->field('username,salt,password,realname')->find();
  552. $data = [];
  553. $data['username'] = $param['username'];
  554. $data['password'] = user_md5($param['password'], $userData['salt'], $param['username']);
  555. $data['userInfo'] = $userData;
  556. $data['mobile'] = $param['username'];
  557. $resSync = model('Sync')->syncData($data);
  558. if ($resSync) {
  559. unset($data['userInfo']);
  560. $res = db('admin_user')->where(['id' => $param['id']])->update($data);
  561. SystemActionLog($userInfo['id'], 'admin_user', 'employee', $param['id'], 'update', $userData['realname'], '', '', '员工账号修改:' . $userData['realname']);
  562. return resultArray(['data' => '修改成功!']);
  563. } else {
  564. return resultArray(['error' => '修改失败,请重试!']);
  565. }
  566. }
  567. /**
  568. * 登录记录
  569. */
  570. public function loginRecord()
  571. {
  572. if (!checkPerByAction('admin', 'loginRecord', 'index')) {
  573. header('Content-Type:application/json; charset=utf-8');
  574. exit(json_encode(['code' => 102, 'error' => '无权操作']));
  575. }
  576. $loginRecordModel = new LoginRecord();
  577. $where = [];
  578. getWhereUserByParam($where, 'create_user_id');
  579. getWhereTimeByParam($where, 'create_time');
  580. $limit = $this->param['limit'] ?: 15;
  581. $data = $loginRecordModel
  582. ->where($where)
  583. ->order(['create_time' => 'DESC'])
  584. ->paginate($limit)
  585. ->each(function ($val) {
  586. $val['username'] = $val->create_user_info['realname'];
  587. $val['type_name'] = $val->type_name;
  588. })
  589. ->toArray();
  590. return resultArray([
  591. 'data' => [
  592. 'list' => $data['data'],
  593. 'dataCount' => $data['total']
  594. ],
  595. ]);
  596. }
  597. /**
  598. * 员工导入模板下载
  599. * @param string $save_path 本地保存路径 用于错误数据导出,在 Admin\Model\Excel::batchImportData()调用
  600. * @return
  601. * @author Michael_xu
  602. */
  603. public function excelDownload($save_path = '')
  604. {
  605. $param = $this->param;
  606. $userInfo = $this->userInfo;
  607. $excelModel = new \app\admin\model\Excel();
  608. // 导出的字段列表
  609. $field_list = UserModel::$import_field_list;
  610. $excelModel->excelImportDownload($field_list, 'admin_user', $save_path);
  611. }
  612. /**
  613. * 员工导入
  614. */
  615. public function import()
  616. {
  617. // 仅允许超管,系统管理员,部门与员工管理员 导入
  618. if (false === UserModel::checkUserGroup([1, 2, 3])) {
  619. return resultArray(['error' => '没有该权限']);
  620. }
  621. $param = $this->param;
  622. $userInfo = $this->userInfo;
  623. $excelModel = new \app\admin\model\Excel();
  624. $param['types'] = 'admin_user';
  625. $file = request()->file('file');
  626. $res = $excelModel->batchImportData($file, $param, $this);
  627. $list = [];
  628. $list[] = $excelModel->getError();
  629. $item = $list[0];
  630. if (!$res) {
  631. return resultArray(['data' => $item]);
  632. }
  633. Cache::clear('user_info');
  634. return resultArray(['data' => $item]);
  635. }
  636. /**
  637. * 批量设置直属上级
  638. *
  639. * @author Ymob
  640. * @datetime 2019-10-28 13:37:57
  641. */
  642. public function setParent()
  643. {
  644. // 仅允许超管,系统管理员,部门与员工管理员 批量设置
  645. if (false === UserModel::checkUserGroup([1, 2, 3])) {
  646. return resultArray(['error' => '没有该权限']);
  647. }
  648. $parent_id = (int)$this->param['parent_id'];
  649. $parent_user = UserModel::find($parent_id);
  650. if (!$parent_user) {
  651. return resultArray(['error' => '请选择直属上级']);
  652. }
  653. $user_id_list = (array)$this->param['id_list'];
  654. if (empty($user_id_list)) {
  655. return resultArray(['error' => '请选择员工']);
  656. }
  657. if (in_array(1, $user_id_list)) {
  658. return resultArray(['error' => '超级管理员不能设置上级']);
  659. }
  660. if (in_array($parent_id, $user_id_list)) {
  661. return resultArray(['error' => '直属上级不能为员工自己']);
  662. }
  663. foreach ($user_id_list as $val) {
  664. if (in_array($parent_id, getSubUserId(true, 0, (int)$val))) {
  665. return resultArray(['error' => '直属上级不能是自己下属(包含下属的下属)']);
  666. }
  667. }
  668. $a = new UserModel;
  669. if ($a->where(['id' => ['IN', $user_id_list]])->update(['parent_id' => $parent_id])) {
  670. Cache::clear('user_info');
  671. return resultArray(['data' => '员工直属上级设置成功']);
  672. } else {
  673. return resultArray(['error' => '员工直属上级设置失败' . $a->getError()]);
  674. }
  675. }
  676. /**
  677. * 通讯录列表
  678. * @return mixed
  679. */
  680. public function queryList()
  681. {
  682. $param = $this->param;
  683. $userInfo = $this->userInfo;
  684. $param['user_id'] = $userInfo['id'];
  685. $userLogic = new UserLogic();
  686. $data = $userLogic->getDataList($param);
  687. return resultArray(['data' => $data]);
  688. }
  689. /**
  690. * 关注的通讯录列表
  691. * @return mixed
  692. */
  693. public function starList()
  694. {
  695. $param = $this->param;
  696. $userInfo = $this->userInfo;
  697. $param['user_id'] = $userInfo['id'];
  698. $userLogic = new UserLogic();
  699. $data = $userLogic->queryList($param);
  700. return resultArray(['data' => $data]);
  701. }
  702. /**
  703. * 设置关注
  704. *
  705. */
  706. public function userStar()
  707. {
  708. $userInfo = $this->userInfo;
  709. $userId = $userInfo['id'];
  710. $targetId = $this->param['target_id'];
  711. $type = $this->param['type'];
  712. if (empty($userId) || empty($targetId) || empty($type)) return resultArray(['error' => '缺少必要参数!']);
  713. if (!$this->setStar($type, $userId, $targetId)) {
  714. return resultArray(['error' => '设置关注失败!']);
  715. }
  716. return resultArray(['data' => '设置关注成功!']);
  717. }
  718. /**
  719. * 复制员工角色
  720. *
  721. * @return \think\response\Json
  722. */
  723. public function copyRole()
  724. {
  725. $param = $this->param;
  726. if (empty($param['user_id']) && empty($param['structure_id'])) return resultArray(['error' => '请选择员工或部门!']);
  727. if (empty($param['group_id'])) return resultArray(['error' => '请选择角色!']);
  728. $userModel = new User();
  729. $userInfo=$this->userInfo;
  730. $param['userId']=$userInfo['id'];
  731. if (!$userModel->copyRole($param)) return resultArray(['error' => '操作失败!']);
  732. return resultArray(['data' => '操作成功!']);
  733. }
  734. /**
  735. * 获取下属(全部层级)
  736. *
  737. */
  738. public function subordinate()
  739. {
  740. $userId = $this->userInfo['id'];
  741. # 获取下属的ID
  742. $subIds = getSubUserId(false, 0, $userId);
  743. $data = Db::name('admin_user')->field(['id', 'realname', 'thumb_img as img'])->whereIn('id', $subIds)->select();
  744. foreach ($data as $key => $value) {
  745. $data[$key]['img'] = !empty($data[$key]['img']) ? getFullPath($data[$key]['img']) : '';
  746. }
  747. return resultArray(['data' => $data]);
  748. }
  749. /**
  750. * 获取当前登录人信息
  751. *
  752. */
  753. public function queryLoginUser()
  754. {
  755. $resData = [];
  756. $wkcode = file_get_contents(CONF_PATH . 'license.dat');
  757. if ($wkcode) {
  758. $resCheckData = checkWkCode($wkcode);
  759. if ($resCheckData) {
  760. $resData = object_to_array(json_decode($resCheckData));
  761. }
  762. }
  763. return $resData;
  764. }
  765. /**
  766. * 批量重设部门
  767. *
  768. * @author alvin guogaobo
  769. * @version 1.0 版本号
  770. * @since 2021/4/15 0015 16:37
  771. */
  772. public function setUserDept()
  773. {
  774. //权限判断
  775. // 仅允许超管,系统管理员,部门与员工管理员 导入
  776. if (false === UserModel::checkUserGroup([1, 2, 3])) {
  777. return resultArray(['error' => '没有该权限']);
  778. }
  779. $userId = $this->userInfo['id'];
  780. $userModel = model('User');
  781. $param = $this->param;
  782. $param['user_id']=$userId;
  783. if (!is_array($param['id'])) {
  784. $ids[] = $param['id'];
  785. } else {
  786. $ids = $param['id'];
  787. }
  788. $data = $userModel->setUserDept($ids, $param);
  789. if (!$data) {
  790. return resultArray(['error' => $userModel->getError()]);
  791. }
  792. # 添加记录
  793. $userInfo = $this->userInfo;
  794. foreach ($ids as $key => $val) {
  795. $dataInfo = db('admin_user')->where('id', $val)->find();
  796. SystemActionLog($userInfo['id'], 'admin_user', 'employee', $val, 'update', $dataInfo['realname'], '', '','重置了部门: ' . $dataInfo['realname']);
  797. }
  798. return resultArray(['data' => '操作成功']);
  799. }
  800. /**
  801. * 员工分类后面跟的数据
  802. * @author alvin guogaobo
  803. * @version 1.0 版本号
  804. * @since 2021/4/24 0024 14:42
  805. */
  806. public function countNumOfUser(){
  807. $userModel = model('User');
  808. $data=$userModel->countNumOfUser();
  809. return resultArray(['data' => $data['list']]);
  810. }
  811. }