User.php 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930
  1. <?php
  2. namespace app\admin\controller\base\user;
  3. /**
  4. * @title: 用户
  5. */
  6. use app\admin\extend\enroll\UserRoleHandle;
  7. use Cassandra\Uuid;
  8. use daorui\platform\platformAuth;
  9. use think\Db;
  10. use app\admin\controller\Base;
  11. use app\admin\validate\base\user\User as UserValidate;
  12. use app\common\model\base\user\User as UserModel;
  13. use app\common\model\base\user\Token;
  14. use app\common\model\base\user\UserRole;
  15. use app\common\model\base\org\OrgRole;
  16. use app\common\model\base\org\Org;
  17. use app\common\model\base\menu\Menu;
  18. use think\facade\Cache;
  19. use app\common\model\base\msg\Sms as smsModel;
  20. class User extends Base
  21. {
  22. protected $userModel = null;
  23. protected $tokenModel = null;
  24. protected $orgRoleModel = null;
  25. protected $orgModel = null;
  26. protected $userRoleModel = null;
  27. protected $loginByName = false; // 是否允许非管理员使用名字登录
  28. protected $maxtrynum = 0; //密码错误最大尝试次数
  29. protected $noNeedLogin = [
  30. 'register', 'login', 'captcha', 'getSmsCode',
  31. 'checkLogin', 'logout', 'getQrcode', 'getTemplate', 'getFileRows', 'resetPwdByCode', 'ssologin', 'checkPwdComplexity',
  32. 'BatchCreateUser', 'BatchPower', 'resetPwdAll', 'exportNoChangePwd', 'resetPwd', 'socialLogin', 'accountLogin',
  33. ];
  34. protected $noNeedAuth = ['userInfo', 'getUserInfo', 'getList', 'getRouterList', 'socialLogin', 'accountLogin'];
  35. protected $noNeedValidate = ['doEdit'];
  36. //初始化
  37. public function initialize()
  38. {
  39. parent::initialize();
  40. $this->userModel = new UserModel;
  41. $this->tokenModel = new Token;
  42. $this->orgRoleModel = new OrgRole;
  43. $this->orgModel = new Org;
  44. $this->userRoleModel = new UserRole;
  45. $this->maxtrynum = sysconfig('account.PwdMaxTry');
  46. }
  47. /**
  48. * @title: 组装查询和导出的条件
  49. * @desc:
  50. * @param {*}
  51. * @return {*}
  52. * @Author: Rock
  53. * @Date: 2021-11-18 10:55:35
  54. * @LastEditTime: Do not edit
  55. */
  56. private function createWhere()
  57. {
  58. $data = $this->request->param();
  59. $keyword = $data['keyword'] ?? '';
  60. $status = $data['status'] ?? 0;
  61. $roleCode = $data['roleCode'] ?? '';
  62. $org_id = $data['org_id'] ?? 0;
  63. $ids = $data['ids'] ?? [];
  64. $where = [];
  65. $whereOr = [];
  66. // 有传IDS时
  67. if (!empty($ids)) {
  68. $where[] = ['user_id', 'IN', $ids];
  69. } else {
  70. //有传组织ID时
  71. if (!empty($org_id) && $org_id > 0) {
  72. $org_ids = $this->orgModel->getChildrenIds($org_id, true);
  73. $ids = $this->userRoleModel->where('org_id', 'IN', $org_ids)->column('user_id');
  74. $where[] = ['user_id', 'IN', $ids];
  75. }
  76. // else {
  77. // //组织ID限制
  78. //// $org_ids = $this->orgModel->getChildrenIds($this->userinfo['org_id'], true);
  79. //// $ids = $this->userRoleModel->where('org_id', 'IN', $org_ids)->column('user_id');
  80. //// $where[] = ['user_id', 'IN', array_unique($ids)];
  81. //// $whereOr[] = ['create_uid', '=', $this->userinfo['user_id']];
  82. // }
  83. //如果有传角色编码
  84. if (!empty($roleCode)) {
  85. $ids = $this->userRoleModel->where('role_code', 'LIKE', "%$roleCode%")->column('user_id');
  86. $where[] = ['user_id', 'IN', $ids];
  87. }
  88. // 如果有传状态
  89. if (!empty($status)) {
  90. $where[] = ['status', '=', $status];
  91. }
  92. // 如果是搜索
  93. if (!empty($keyword)) {
  94. $where[] = ["username|nickname|phone|idcard|name", "LIKE", "%$keyword%"];
  95. }
  96. }
  97. return ['where' => $where, 'whereOr' => $whereOr];
  98. }
  99. /**
  100. * @title: 获取用户列表
  101. * @param {int} {pageNo} {非必填,默认值为1} {页码}
  102. * @param {int} {pageSize} {非必填,默认值为10} {每页数量}
  103. * @param {int} {org_id} {非必填,自动获取} {组织id}
  104. * @param {array} {ids} {非必填} {id区间}
  105. * @param {string} {keyword} {非必填} {搜索关键词,帐号,姓名,电话,身份证号码}
  106. * @param {int} {status} {非必填} {状态筛选,1=正常,2=禁用}
  107. * @param {string} {roleCode} {非必填} {角色code搜索}
  108. * @return array
  109. * @Author: Rock
  110. */
  111. public function getList($pageNo = 1, $pageSize = 10)
  112. {
  113. $data = $this->request->param();
  114. $whereAry = $this->createWhere();
  115. $where = $whereAry['where'];
  116. $whereOr = $whereAry['whereOr'];
  117. $list = $this->userModel->where($where)->whereOr($whereOr)->with(['roles'])->append(['sex_txt', 'status_txt', 'is_ensure_txt'])->paginate(['page' => $pageNo, 'list_rows' => $pageSize]);
  118. $total = $list->total();
  119. $list = FieldConverList($list->items());
  120. return pageRes(1, "获取成功", $total, $list, $where);
  121. }
  122. /**
  123. * @title 用户登录
  124. * @desc 说明
  125. * @method GET/POST
  126. * @param {string} {username} {必填} {登录账户:账号或手机号或身份证号码}
  127. * @param {string} {password} {必填} {登录密码}
  128. * @param {string} {code} {必填} {图片验证码}
  129. * @param {string} {login_role} {必填,默认SUPERADMIN} {登录角色码,总后台只能SUPERADMIN登录}
  130. * @param {int} {login_org} {必填,默认1} {登录单位ID,总后台只能集团下的超级管理员登录}
  131. * @return array
  132. * @author Rock
  133. */
  134. public function login($username = '', $password = '', $code = '', $login_role = 'SUPERADMIN', $login_org = 1)
  135. {
  136. try {
  137. // 验证码
  138. if (!IsMobileAccess() && !parent::captcha_check($code)) {
  139. return Result(2, "验证码错误", $code);
  140. }
  141. $res = (new platformAuth())->interfaceRequest('accountLogin', ['username' => $username, 'password' => $password]);
  142. if (!$res['code']) return Result(2, $res['msg']);
  143. $fetchUser = $res['data'];
  144. $user = UserModel::where('uuid', $fetchUser['uuid'])->find();
  145. if (!$user->is_developer) {
  146. UserRoleHandle::handleRole($user->user_id);
  147. $userRole = UserRole::where('user_id', $user->user_id)->find();
  148. if ($userRole) {
  149. $login_org = $userRole['org_id'] ?? $login_org;
  150. $login_role = $this->orgRoleModel->where('role_id', $userRole['role_id'])->value('code');
  151. }
  152. }
  153. $token = $this->tokenModel->updatetoken($user->user_id, $login_org, $login_role);
  154. $log = slog(1, $user->username . "使用帐号密码登录成功");
  155. return Result(1, '登录成功', ['token' => $token, 'user_id' => $user->user_id, 'user' => $this->tokenModel->tokenUser($token), 'log' => $log]);
  156. // $res = $this->accountLogin($username, $password, $login_org, $login_role);
  157. // if ($res['code'] == 1) {
  158. // $token = $res['data']['token'];
  159. // $user_id = $res['data']['user_id'];
  160. // $data = [
  161. // 'token' => $token,
  162. // 'role' => $res['data']['user']['role'],
  163. // ];
  164. // }
  165. // return json($res);
  166. } catch (\Exception $e) {
  167. return res(2, '系统繁忙', $e->getFile() . '第' . $e->getLine() . '行:' . $e->getMessage(), $e->getTrace());
  168. }
  169. }
  170. /**
  171. * @title: 账号/手机号/身份证号密码登录
  172. * @param {string} {username} {必填} {登录账户:帐号/手机号}
  173. * @param {string} {password} {必填} {登录密码}
  174. * @param {int} {login_org} {必填} {登录单位}
  175. * @param {string} {login_role} {必填} {登录角色编码}
  176. * @return array
  177. * @Author: Rock
  178. */
  179. private function accountLogin($username, $password, $login_org, $login_role)
  180. {
  181. $where = [];
  182. try {
  183. // 查找用户信息
  184. $where[] = ['username|phone|idcard', '=', $username];
  185. $user = $this->userModel->where($where)->find();
  186. if (empty($user)) {
  187. return Result(0, '帐号或密码错误');
  188. }
  189. // 密码比对
  190. if (!CheckEncrypt($user->salt, $password, $user->password)) {
  191. // 密码不对,尝试次数+1
  192. $user->inc('trynum', 1)->update();
  193. // 剩余尝试次数
  194. $Surplus = $this->maxtrynum - $user->trynum;
  195. if ($user->trynum > $this->maxtrynum) {
  196. $user->save(['status' => 2]);
  197. slog(1, $user->username . "登录失败次数超过限制,账户已被禁用");
  198. return Result(0, '帐号或密码错误');
  199. }
  200. return Result(0, '帐号或密码错误,您还有' . $Surplus . '次机会。');
  201. }
  202. // 判断账号状态
  203. if ($user->status == 2) {
  204. return Result(0, '此帐号已被禁用');
  205. }
  206. // 判断是否是简单密码
  207. $PwdComplexity = UserModel::checkPwdComplexity($password);
  208. $complexity = $PwdComplexity['code'] == 1 ? 0 : 1;
  209. // 判断是否为默认密码
  210. if ($complexity == 0) {
  211. if (trim($password) == trim(sysconfig('account.default_pwd'))) {
  212. $complexity = 1;
  213. }
  214. }
  215. if (!$user->is_developer) {
  216. $userRole = UserRole::where('user_id', $user->user_id)->find();
  217. if ($userRole) {
  218. $login_org = $userRole['org_id'] ?? $login_org;
  219. $login_role = $this->orgRoleModel->where('role_id', $userRole['role_id'])->value('code');
  220. }
  221. }
  222. // 判断用户是否已绑定该角色
  223. $userRole = $this->userRoleModel->where('user_id', $user->user_id)->where('org_id', $login_org)->where('role_code', $login_role)->find();
  224. if (empty($userRole)) {
  225. return Result(0, "此账号未绑定对应角色");
  226. }
  227. $user->trynum = 0;
  228. $user->complexity = $complexity;
  229. $user->save();
  230. $token = $this->tokenModel->updatetoken($user->user_id, $userRole['org_id'], $login_role);
  231. $log = slog(1, $user->username . "使用帐号密码登录成功");
  232. return Result(1, '登录成功', ['token' => $token, 'user_id' => $user->user_id, 'user' => $this->tokenModel->tokenUser($token), 'log' => $log]);
  233. } catch (\Exception $e) {
  234. return Result(0, "登录失败", $e->getFile() . "第" . $e->getLine() . "行:" . $e->getMessage(), $e->getTrace());
  235. }
  236. }
  237. /**
  238. * Desc :第三方登录
  239. * User : zwq
  240. * Date : 2025-01-18 14:49
  241. */
  242. public function socialLogin()
  243. {
  244. $data = $this->request->param();
  245. if (empty($data['uuid'])) return res(0, '缺少参数');
  246. $uuid = $data['uuid'];
  247. $user = UserModel::where('uuid', $uuid)->find();
  248. if (empty($user)) {//没有账号
  249. //创建账号以及分配角色
  250. $user = $this->userModel->create_user($data);
  251. UserRole::create(['user_id' => $user['user_id'], 'role_id' => $data['role_id'], 'org_id' => $data['org_id'], 'role_code' => $data['role_code']]);
  252. $token = $this->tokenModel->updatetoken($user->user_id, $data['org_id'], $data['role_code']);
  253. } else {
  254. //更新账号信息
  255. $user->replace()->save($data);
  256. //更新用户绑定角色信息
  257. $token = $this->tokenModel->updatetoken($user->user_id, $data['org_id'], $data['role_code']);
  258. }
  259. return Result(1, '登录成功', ['token' => $token, 'user_id' => $user->user_id, 'user' => $this->tokenModel->tokenUser($token)]);
  260. }
  261. /**
  262. * @title: 获取指定用户信息
  263. * @param {int} {user_id} {必填} {用户id}
  264. * @return array
  265. * @Author: wangkewei
  266. * @Date: 2021/5/18 10:17
  267. */
  268. public function getUserInfo($user_id = 0)
  269. {
  270. if ($user_id) {
  271. $res = $this->userModel->where('user_id', $user_id)->hidden(['password', 'salt'])->append(['roles'])->find();
  272. $res->avatar_base64 = file2base64(public_path() . $res->avatar);
  273. } else {
  274. $tokenUser = $this->userinfo;
  275. $user_id = $this->userinfo['user_id'];
  276. if (empty($tokenUser)) {
  277. return res(2, "TOKEN验证失败");
  278. } else {
  279. $res = $tokenUser;
  280. $res['avatar'] = !empty($res['avatar']) ? $res['avatar'] : sysconfig('account.default_avatar');
  281. $res['avatar_base64'] = file2base64(public_path() . $res['avatar']);
  282. }
  283. }
  284. //todo 获取所有权限路径
  285. $res['permissions'] = \app\common\model\base\user\User::getUserRole($user_id, 'auth', $this->token) ?? [];
  286. // $res['permissions'] = UserModel::getReqAuth($user_id);
  287. return res(1, "获取成功", $res ?? []);
  288. }
  289. /**
  290. * @title: 新增/修改用户信息
  291. * @param array
  292. * @return array
  293. * @Author: wangkewei
  294. * @Date: 2021/5/18 10:18
  295. */
  296. public function doEdit()
  297. {
  298. $data = $this->request->param();
  299. $user_id = !empty($data['user_id']) ? $data['user_id'] : 0;
  300. $scene = !empty($user_id) ? 'edit' : 'add';//验证场景
  301. $data['avatar'] = !empty($data['avatar']) ? $data['avatar'] : sysconfig('account.default_avatar');
  302. //验证字段
  303. $check = validate(UserValidate::class)->scene($scene)->check($data);
  304. if (true !== $check) {
  305. return res(2, $check);
  306. }
  307. // 编辑
  308. if (!empty($user_id)) {
  309. // 判断是否有同一个手机号的帐号
  310. $phoneUsed = $this->userModel->where('phone', $data['phone'])->where('user_id', '<>', $user_id)->value('user_id');
  311. if (!empty($phoneUsed) && $phoneUsed != $user_id) {
  312. return res(2, "手机号已注册");
  313. }
  314. // 判断账号是否已被使用
  315. $accountUsed = $this->userModel->where('username', $data['username'])->where('user_id', '<>', $user_id)->value('user_id');
  316. if (!empty($accountUsed) && $accountUsed != $user_id) {
  317. return res(2, "账号已被使用");
  318. }
  319. if (isset($data['password'])) {
  320. unset($data['password']);
  321. }
  322. if (isset($data['salt'])) {
  323. unset($data['salt']);
  324. }
  325. $data['name'] = !empty($data['name']) ? $data['name'] : $data['nickname'];
  326. $info = $this->userModel->where('user_id', $user_id)->find();
  327. $params = [
  328. 'user' => [
  329. 'uuid' => $info['uuid'],
  330. 'name' => $data['name'],
  331. 'phone' => $data['phone'],
  332. 'sex' => $data['sex'],
  333. ],
  334. ];
  335. $res = (new platformAuth())->interfaceRequest('createAccount', $params);
  336. if (!$res['code']) return res(2, $res['msg']);
  337. $info->data($data, true);
  338. $info->save();
  339. // 如果是当前用户在修改自己的信息,则更新缓存中的用户信息
  340. $token = $this->token;
  341. if ($this->userinfo['user_id'] == $user_id) {
  342. Token::updateTokenUser($this->token);
  343. }
  344. } // 新增
  345. else {
  346. // 记录创建人
  347. $data['create_uid'] = $this->userinfo['user_id'];
  348. //生成uuid
  349. $data['uuid'] = Uuid::uuid4()->toString();
  350. // 判断是否有同一个手机号的帐号
  351. $phoneUsed = $this->userModel->where('phone', $data['phone'])->value('user_id');
  352. if (!empty($phoneUsed)) {
  353. return res(2, "手机号已注册");
  354. }
  355. $res = $this->userModel->create_user($data);
  356. if (is_string($res)) {
  357. return res(2, $res);
  358. }
  359. }
  360. // 同步到企业微信
  361. if (1 == sysconfig('account.sync_corp')) {
  362. //开发团队和外部用户不同步
  363. $developIds = $this->orgModel->where('org_type_code', 'in', ['SYSTEM', 'EXTERNAL'])->column('org_id');
  364. if (!in_array($data['org_id'], $developIds)) {
  365. $this->userModel->createCorpUser($user_id);
  366. }
  367. }
  368. if (empty($user_id)) {
  369. slog(1, "创建了用户" . $data['name']);
  370. } else {
  371. slog(1, '修改了用户' . $data['name']);
  372. }
  373. cache('USERLIST', null);
  374. return res(1, "保存成功", $data);
  375. }
  376. /**
  377. * @title: 退出登录
  378. * @return array
  379. * @Author: wangkewei
  380. * @Date: 2021/5/18 10:18
  381. */
  382. public function logout()
  383. {
  384. $token = $this->token;
  385. slog(1, "退出了系统");//记录日志需要获取当前用户,所以日志记录完后再销毁token
  386. $this->tokenModel->losetoken($token);
  387. return Res(1, "成功退出");
  388. }
  389. /**
  390. * @title: 重置密码
  391. * @param {int} {user_id} {必填} {用户id}
  392. * @return array
  393. * @Author: wangkewei
  394. * @Date: 2021/5/18 10:19
  395. */
  396. public function resetPwd($user_id = '')
  397. {
  398. if (empty($user_id)) {
  399. return res(2, "参数错误");
  400. }
  401. $info = $this->userModel->where('user_id', $user_id)->find();
  402. $res = $this->userModel->resetPwd($user_id);
  403. if ($res) {
  404. slog(1, "重置了" . $info->username . "的密码");
  405. return res(1, "操作成功,密码已被重置为默认密码");
  406. } else {
  407. slog(2, "重置" . $info->username . "的密码失败");
  408. return res(2, "操作失败");
  409. }
  410. }
  411. /**
  412. * @title: 修改自己的密码
  413. * @desc: 修改自己的密码
  414. * @param {string} {oldpwd} {必填} {原密码}
  415. * @param {string} {newpwd} {必填} {新密码}
  416. * @param {string} {renewPwd} {必填} {确认新密码}
  417. * @return {boolean} {} {} {修改结果}
  418. * @Author: Rock
  419. * @Date: 2021-06-05 09:43:13
  420. * @LastEditTime: Do not edit
  421. */
  422. public function changePwd($oldPwd = '', $newPwd = '', $renewPwd = '')
  423. {
  424. //检查密码复杂度
  425. $PwdComplexity = UserModel::checkPwdComplexity($newPwd);
  426. if ($PwdComplexity['code'] == 2) {
  427. return json($PwdComplexity);
  428. }
  429. $userInfo = UserModel::find($this->userinfo['user_id']);
  430. if (empty($oldPwd) || empty($newPwd)) {
  431. return res(2, '原密码和新密码都不能为空');
  432. } elseif (empty($renewPwd)) {
  433. return res(2, '确认密码不能为空');
  434. } elseif ($newPwd != $renewPwd) {
  435. return res(2, "确认密码与新密码不一致");
  436. } elseif (strlen($newPwd) < 6) {
  437. return res(2, '密码不能少于6个字符');
  438. }
  439. // $res = $this->userModel->resetPwd($userInfo->user_id, $newPwd);
  440. $params = [
  441. 'user' => [
  442. 'uuid' => $userInfo->uuid,
  443. 'password' => $oldPwd,
  444. 'newPwd' => $renewPwd,
  445. ],
  446. ];
  447. $res = (new platformAuth())->interfaceRequest('createAccount', $params);
  448. if (!$res['code']) return res(2, $res['msg']);
  449. return res(1, "密码修改成功");
  450. }
  451. /**
  452. * @title: 通过手机短信验证码重设密码
  453. * @desc:
  454. * @param {string} {mobile} {} {手机号}
  455. * @param {string} {newPassword} {} {新密码}
  456. * @param {string} {rePassword} {} {确认新密码}
  457. * @param {string} {code} {} {短信验证码}
  458. * @return {*}
  459. * @Author: Rock
  460. * @Date: 2021-12-03 15:07:31
  461. * @LastEditTime: Do not edit
  462. */
  463. public function resetPwdByCode(string $mobile, string $newPassword, string $rePassword, string $code)
  464. {
  465. $mobile = trim($mobile);
  466. $newPassword = trim($newPassword);
  467. $rePassword = trim($rePassword);
  468. $code = trim($code);
  469. $PwdComplexity = UserModel::checkPwdComplexity($newPassword);
  470. if ($PwdComplexity['code'] == 2) {
  471. return json($PwdComplexity);
  472. }
  473. if (empty($mobile) || empty($newPassword) || empty($rePassword) || empty($code)) {
  474. return res(2, "重设密码失败");
  475. }
  476. if (trim($newPassword) != trim($rePassword)) {
  477. return res(2, "两次密码输入不一致");
  478. }
  479. if (!smsModel::check($mobile, $code)) {
  480. return res(2, "验证码错误或失效");
  481. }
  482. $userList = $this->userModel->where('phone', $mobile)->select();
  483. foreach ($userList as $userInfo) {
  484. $res = $this->userModel->resetPwd($userInfo->user_id, $newPassword);
  485. slog(1, "修改了" . $userInfo->username . "的密码");
  486. if ($res) {
  487. wssend($userInfo->user_id, 'changepwd', "您的帐号密码已修改,请重新登录!");
  488. }
  489. }
  490. return res(1, "操作成功,手机号为$mobile 的所有帐号密码都已修改");
  491. }
  492. /**
  493. * @title: 修改自己绑定的电话号码
  494. * @param {string} {phone} {必填} {电话号码}
  495. * @return array
  496. * @Author: wangkewei
  497. * @Date: 2021/8/30 10:17
  498. */
  499. public function changeMobile()
  500. {
  501. $data = $this->request->param();
  502. $in = ['phone' => $data['phone']];
  503. if ($this->userinfo['username'] == $this->userinfo['mobile']) {
  504. $in['username'] = $data['phone'];
  505. }
  506. $this->userModel->where('user_id', $this->userinfo['user_id'])->save($in);
  507. return res(1, "操作成功");
  508. }
  509. /**
  510. * @title: 删除用户
  511. * @desc:
  512. * @param {int} {ids} {必填} {id}
  513. * @return array
  514. * @Author: Rock
  515. * @Date: 2021-05-07 15:48:37
  516. * @LastEditTime: Do not edit
  517. */
  518. public function doDelete($ids)
  519. {
  520. $where = [];
  521. if (!is_array($ids)) {
  522. $ids = explode(',', $ids);
  523. }
  524. $where[] = ['user_id', 'IN', $ids];
  525. if (in_array($this->userinfo['user_id'], $ids)) {
  526. return res(2, '无法删除自己');
  527. }
  528. $list = $this->userModel->where($where)->select();
  529. try {
  530. UserModel::startTrans();
  531. foreach ($list as $item) {
  532. if ($item->role_code == $this->userinfo['role_code']) {
  533. UserModel::rollback();
  534. return res(2, "无法删除同级账号");
  535. }
  536. $result = $this->userModel->deleteUser($item->user_id);
  537. $item->delete();
  538. slog(1, "删除了用户" . $item->username);
  539. }
  540. cache('USERLIST', null);
  541. UserModel::commit();
  542. return res(1, "删除成功");
  543. } catch (\Exception $e) {
  544. UserModel::rollback();
  545. return res(2, "删除失败", $e->getMessage());
  546. }
  547. }
  548. /**
  549. * @title: 封禁/启用帐号
  550. * @desc:
  551. * @param {mixed} {ids} {} {用户ID}
  552. * @return {*}
  553. * @Author: Rock
  554. * @Date: 2021-06-04 15:05:02
  555. * @LastEditTime: Do not edit
  556. * @throws \Exception
  557. */
  558. public function changeStatus($ids = [], $status = 0)
  559. {
  560. $where = [];
  561. if (empty($ids)) {
  562. return res(2, "参数错误");
  563. }
  564. if (is_string($ids)) {
  565. $ids = explode(',', $ids);
  566. } elseif (is_int($ids)) {
  567. $ids = [$ids];
  568. }
  569. $where[] = ['user_id', 'IN', $ids];
  570. if (empty($status)) {
  571. UserModel::where($where)->update(['status' => Db::raw('ABS(3 * `status` - 5)')]);
  572. } else {
  573. UserModel::where($where)->update(['status' => $status]);
  574. }
  575. // 获取被封禁的帐号发送禁用通知
  576. $userList = UserModel::where($where)->where('status', 2)->select();
  577. foreach ($userList as $item) {
  578. slog(1, "封禁了用户" . $item->username);
  579. }
  580. // 获取被激活的帐号并记录日志
  581. $userList = UserModel::where($where)->where('status', 1)->select();
  582. foreach ($userList as $info) {
  583. slog(1, "激活了用户" . $info->username);
  584. $info->trynum = 0;
  585. $info->save();
  586. }
  587. return res(1, "操作成功");
  588. }
  589. /**
  590. * @title: 检查登录状态
  591. * @desc:
  592. * @param {string} {token} {} {}
  593. * @return {*}
  594. * @Author: Rock
  595. * @Date: 2021-06-28 10:25:52
  596. * @LastEditTime: Do not edit
  597. */
  598. public function checkLogin($token = "")
  599. {
  600. $res = $this->checkToken($token);
  601. return res($res['code'], $res['msg']);
  602. }
  603. /**
  604. * @title: 清除缓存
  605. * @desc:
  606. * @param {*}
  607. * @return {*}
  608. * @Author: Rock
  609. * @Date: 2021-11-27 20:39:31
  610. * @LastEditTime: Do not edit
  611. */
  612. public function clearCache()
  613. {
  614. if (!$this->userinfo['is_developer']) {
  615. return res(2, "没有权限");
  616. }
  617. Cache::clear();
  618. return res(1, "清除成功");
  619. }
  620. /**
  621. * @title: 获取用户二维码
  622. * @param {int} {user_id} {必填} {用户id}
  623. * @return array
  624. * @Author: wangkewei
  625. * @Date: 2021/5/17 16:03
  626. */
  627. public function getQrcode($user_id = 0)
  628. {
  629. $info = $this->userModel->where('user_id', $user_id)->find();
  630. if (empty($info)) {
  631. return res(2, "用户不存在");
  632. }
  633. $basepath = "qrcode" . DS . "user" . DS;
  634. $savepath = public_path() . $basepath;
  635. $filename = "user_" . $user_id . '.png';
  636. $fullpath = $savepath . $filename;
  637. if (!is_file($fullpath)) {
  638. $data = $this->createQrcode($user_id, $info->name);
  639. } else {
  640. $data = [
  641. 'filename' => $filename,
  642. 'savepath' => DS . $basepath . $filename,
  643. 'url' => WEBURL . DS . $basepath . $filename,
  644. ];
  645. }
  646. return res(1, "获取成功", $data);
  647. }
  648. /**
  649. * @title: 生成用户二维码
  650. * @desc:
  651. * @param {int} {user_id} {} {用户ID}
  652. * @param {string} {name} {} {二维码下的文本}
  653. * @return {*}
  654. * @Author: Rock
  655. * @Date: 2021-12-03 11:12:56
  656. * @LastEditTime: Do not edit
  657. */
  658. public function createQrcode($user_id, $name = '')
  659. {
  660. $basepath = "qrcode" . DS . "user" . DS;
  661. $savepath = public_path() . $basepath;
  662. $filename = "user_" . $user_id . '.png';
  663. $fullpath = $savepath . $filename;
  664. if (!is_dir($savepath)) {
  665. mkdir($savepath, 0777, true);
  666. }
  667. //创建二维码
  668. include_once(root_path() . '/extend/phpqrcode/qrcode.php');
  669. $qrcode = CreateQRCode(WEBURL . '/index.php/index/pilot.user/info?user_id=' . $user_id, '400', $name, '', true, $fullpath);
  670. //在二维码上加入名字
  671. $data = [
  672. 'filename' => $filename,
  673. 'savepath' => DS . $basepath . $filename,
  674. 'url' => WEBURL . DS . $basepath . $filename,
  675. ];
  676. return $data;
  677. }
  678. /**
  679. * @title: 获取图形验证码(PHP直接输出)
  680. * @desc: 描述
  681. * @param {float} {timestamp} {} {随机小数}
  682. * @return {*}
  683. * @author: Rock
  684. * @method: POST
  685. * @Date: 2022-05-30 14:44:10
  686. */
  687. public function captcha()
  688. {
  689. $content = parent::captcha();
  690. return response($content, 200, ['Content-Length' => strlen($content)])->contentType('image/png');
  691. }
  692. /**
  693. * @title: 导出数据
  694. * @desc:
  695. * @param {*}
  696. * @return {*}
  697. * @Author: Rock
  698. * @Date: 2021-11-19 09:18:20
  699. * @LastEditTime: Do not edit
  700. */
  701. public function doExport()
  702. {
  703. $where = $this->createWhere();
  704. $list = $this->userModel->where($where)->with(['org'])->select();
  705. $rows = [];
  706. $header = array_values($this->importField);
  707. $rows[0] = $header;
  708. foreach ($list as $key => $item) {
  709. $index = $key + 1;
  710. foreach ($this->importField as $k => $v) {
  711. if ($k == 'role_id') {
  712. $rows[$index][] = $item['role']['name'];
  713. } elseif ($k == 'org_id') {
  714. $rows[$index][] = $item['org']['pathName'];
  715. } elseif ($k == 'sex') {
  716. $rows[$index][] = $item['sex_txt'];
  717. } else {
  718. $rows[$index][] = isset($item[$k . '_txt']) ? $item[$k . '_txt'] : $item[$k];
  719. }
  720. }
  721. }
  722. //创建文件夹
  723. $basepath = "uploads" . DS . "download" . DS . date('Ymd');
  724. $savepath = public_path() . $basepath;
  725. if (!is_dir($savepath)) {
  726. mkdir($savepath, 0777, true);
  727. }
  728. //保存文件
  729. require_once root_path() . "extend/excel/Excel.php";
  730. $filename = time() . GetRandStr() . ".xls";
  731. $fullpath = $savepath . DS . $filename;
  732. ArrayToXls($rows, $fullpath);
  733. //返回路径
  734. $returnpath = WEBURL . DS . $basepath . DS . $filename;
  735. slog(1, "导出了用户列表");
  736. return res(1, "获取成功", ['url' => $returnpath, 'name' => $filename]);
  737. }
  738. // 导出字段
  739. protected $importField = [
  740. 'org_id' => '所属组织',
  741. 'name' => '姓名',
  742. 'sex' => '性别',
  743. 'phone' => '手机号',
  744. 'role_id' => '角色',
  745. 'fax' => '传真',
  746. 'username' => '帐号',
  747. 'remark' => '备注',
  748. ];
  749. /**
  750. * @title: 保存用户签名图片
  751. * @desc:
  752. * @param {string} {sign_img} {} {签名图片}
  753. * @return {*}
  754. * @Author: Rock
  755. * @Date: 2022-01-25 09:33:58
  756. * @LastEditTime: Do not edit
  757. */
  758. public function saveSignImg($sign_img = '')
  759. {
  760. if (empty($sign_img)) {
  761. return res(2, "签名图片未上传");
  762. }
  763. if (strpos($sign_img, WEBURL)) {
  764. $sign_img = $sign_img;
  765. $old = public_path() . $this->userinfo['sign_img'];
  766. $new = public_path() . $sign_img;
  767. } else {
  768. $sign_img = str_replace(WEBURL, '', $sign_img);
  769. $old = public_path() . $this->userinfo['sign_img'];
  770. $new = public_path() . $sign_img;
  771. }
  772. if (is_file($new)) {
  773. $this->userinfo['sign_img'] = $sign_img;
  774. $this->userModel->replace()->save($this->userinfo);
  775. }
  776. if (is_file($old)) {
  777. unlink($old);
  778. }
  779. return res(1, "保存成功");
  780. }
  781. /**
  782. * @title: 检查密码复杂度
  783. * @desc: 描述
  784. * @param {string} {pwd} {} {密码}
  785. * @return {*}
  786. * @author: Rock
  787. * @method: POST
  788. * @Date: 2022-10-13 19:40:29
  789. */
  790. public function checkPwdComplexity(string $pwd = '')
  791. {
  792. $res = UserModel::checkPwdComplexity($pwd);
  793. return json($res);
  794. }
  795. /**
  796. * @title: 获取当前登录用户的权限
  797. * @desc: 描述
  798. * @return {*}
  799. * @author: Rock
  800. * @method: POST
  801. * @Date: 2023-02-02 14:39:05
  802. */
  803. public function getRouterList()
  804. {
  805. $fields = [
  806. 'menu_id',
  807. 'pid',
  808. 'name',
  809. 'title',
  810. 'path',
  811. 'component',
  812. 'redirect',
  813. 'icon',
  814. 'is_root',
  815. 'is_parent',
  816. 'affix',
  817. 'status',
  818. 'parent_path',
  819. 'sort',
  820. 'hidden',
  821. 'noKeepAlive',
  822. 'tabHidden',
  823. ];
  824. $where = [];
  825. $where[] = ['status', '=', 1];
  826. $whereOr = [];
  827. $whereOr[] = ['name', 'IN', ['Root', 'index', 'UserCenter', 'Homepage', 'Workbench', 'System']];
  828. if (!$this->userinfo['is_developer']) {
  829. $userInfo = $this->userinfo;
  830. $role_id = $userInfo['role_id'];
  831. $menu_ids = UserModel::getMenuIds($role_id);
  832. $where[] = ['menu_id', 'IN', $menu_ids];
  833. }
  834. $list = Menu::where($where)->whereOr($whereOr)->field($fields)->order('sort')->select()->toArray();
  835. foreach ($list as &$row) {
  836. $row['meta'] = [];
  837. $row['meta']['title'] = $row['title'];
  838. unset($row['title']);
  839. $row['meta']['icon'] = $row['icon'];
  840. unset($row['icon']);
  841. if (isset($row['hidden'])) {
  842. $row['meta']['hidden'] = $row['hidden'] == 1;
  843. unset($row['hidden']);
  844. }
  845. if (!empty($row['affix'])) {
  846. $row['meta']['affix'] = $row['affix'] == 1;
  847. unset($row['affix']);
  848. }
  849. if (!empty($row['dot'])) {
  850. $row['meta']['dot'] = $row['dot'] == 1;
  851. unset($row['dot']);
  852. }
  853. if (!empty($row['tabHidden'])) {
  854. $row['meta']['tabHidden'] = $row['tabHidden'] == 1;
  855. unset($row['tabHidden']);
  856. }
  857. if (!empty($row['noKeepAlive'])) {
  858. $row['meta']['noKeepAlive'] = $row['noKeepAlive'] == 1;
  859. unset($row['noKeepAlive']);
  860. }
  861. if (false !== strpos($row['path'], 'http')) {
  862. $row['meta']['target'] = '_blank';
  863. }
  864. }
  865. $list = array2tree($list, 'pid', 'menu_id');
  866. return res(1, '获取成功', $list);
  867. }
  868. /**
  869. * Notes:获取用户角色
  870. * User: zhang
  871. * Date: 2025/2/19
  872. * Time: 11:00
  873. */
  874. public function getUserRole($user_id = 0)
  875. {
  876. if (empty($user_id)) {
  877. $user_id = $this->userinfo['user_id'];
  878. }
  879. UserRoleHandle::handleRole($user_id);
  880. $list = $this->userRoleModel->where('user_id', $user_id)->select();
  881. return res(1, '获取成功', $list);
  882. }
  883. /**
  884. * Notes:变更组织重新颁发token
  885. * User: zhang
  886. * Date: 2025/2/20
  887. * Time: 14:40
  888. */
  889. public function changeOrg()
  890. {
  891. $org_id = input('org_id/d', "");
  892. $role_code = input('role_code/s', "");
  893. $userinfo = $this->userinfo;
  894. $token = $this->tokenModel->updatetoken($userinfo['user_id'], $org_id, $role_code);
  895. return res(1, '变更成功', $token);
  896. }
  897. }