User.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. <?php
  2. namespace app\admin\controller\user;
  3. use app\admin\model\Admin;
  4. use think\facade\Cache;
  5. use Throwable;
  6. use ba\Random;
  7. use app\common\controller\Backend;
  8. use app\admin\model\User as UserModel;
  9. class User extends Backend
  10. {
  11. /**
  12. * @var object
  13. * @phpstan-var UserModel
  14. */
  15. protected object $model;
  16. protected array $withJoinTable = ['group'];
  17. // 排除字段
  18. protected string|array $preExcludeFields = ['last_login_time', 'login_failure', 'password', 'salt'];
  19. protected string|array $quickSearchField = ['username', 'nickname', 'id'];
  20. protected array $noNeedLogin = ['updatePassword'];
  21. protected array $noNeedPermission = ['updatePassword'];
  22. protected array $noNeedCheckPass = ['checkPassword'];
  23. public function initialize(): void
  24. {
  25. parent::initialize();
  26. $this->model = new UserModel();
  27. }
  28. /**
  29. * 查看
  30. * @throws Throwable
  31. */
  32. public function index(): void
  33. {
  34. if ($this->request->param('select')) {
  35. $this->select();
  36. }
  37. list($where, $alias, $limit, $order) = $this->queryBuilder();
  38. $res = $this->model
  39. ->withoutField('password,salt')
  40. ->withJoin($this->withJoinTable, $this->withJoinType)
  41. ->alias($alias)
  42. ->where($where)
  43. ->order($order)
  44. ->paginate($limit);
  45. $this->success('', [
  46. 'list' => $res->items(),
  47. 'total' => $res->total(),
  48. 'remark' => get_route_remark(),
  49. ]);
  50. }
  51. /**
  52. * 添加
  53. * @throws Throwable
  54. */
  55. public function add(): void
  56. {
  57. if ($this->request->isPost()) {
  58. $data = $this->request->post();
  59. if (!$data) {
  60. $this->error(__('Parameter %s can not be empty', ['']));
  61. }
  62. $salt = Random::build('alnum', 16);
  63. $passwd = encrypt_password($data['password'], $salt);
  64. $data = $this->excludeFields($data);
  65. $result = false;
  66. $this->model->startTrans();
  67. try {
  68. $data['salt'] = $salt;
  69. $data['password'] = $passwd;
  70. // 模型验证
  71. if ($this->modelValidate) {
  72. $validate = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  73. if (class_exists($validate)) {
  74. $validate = new $validate();
  75. if ($this->modelSceneValidate) $validate->scene('add');
  76. $validate->check($data);
  77. }
  78. }
  79. $result = $this->model->save($data);
  80. $this->model->commit();
  81. } catch (Throwable $e) {
  82. $this->model->rollback();
  83. $this->error($e->getMessage());
  84. }
  85. if ($result !== false) {
  86. $this->success(__('Added successfully'));
  87. } else {
  88. $this->error(__('No rows were added'));
  89. }
  90. }
  91. $this->error(__('Parameter error'));
  92. }
  93. /**
  94. * 编辑
  95. * @param string|int|null $id
  96. * @throws Throwable
  97. */
  98. public function edit(string|int $id = null): void
  99. {
  100. $row = $this->model->find($id);
  101. if (!$row) {
  102. $this->error(__('Record not found'));
  103. }
  104. if ($this->request->isPost()) {
  105. $password = $this->request->post('password', '');
  106. if ($password) {
  107. $this->model->resetPassword($id, $password);
  108. }
  109. parent::edit();
  110. }
  111. unset($row->salt);
  112. $row->password = '';
  113. $this->success('', [
  114. 'row' => $row
  115. ]);
  116. }
  117. /**
  118. * 重写select
  119. * @throws Throwable
  120. */
  121. public function select(): void
  122. {
  123. list($where, $alias, $limit, $order) = $this->queryBuilder();
  124. $res = $this->model
  125. ->withJoin($this->withJoinTable, $this->withJoinType)
  126. ->alias($alias)
  127. ->where($where)
  128. ->order($order)
  129. ->paginate($limit);
  130. foreach ($res as $re) {
  131. $re->nickname_text = $re->username . '(ID:' . $re->id . ')';
  132. }
  133. $this->success('', [
  134. 'list' => $res->items(),
  135. 'total' => $res->total(),
  136. 'remark' => get_route_remark(),
  137. ]);
  138. }
  139. public function resetPassword(): void
  140. {
  141. $id = $this->request->post('id', '');
  142. $row = Admin::where('id',$id)->find();
  143. if(empty($row))
  144. {
  145. $this->error('参数错误,人员无法找到');
  146. }
  147. $password = $row['username'].'@Zskk2024';
  148. $this->model->resetPassword($id, $password);
  149. $this->success('重置成功,初始密码为'.$password);
  150. }
  151. public function updatePassword(): void
  152. {
  153. $id = $this->request->post('id', '');
  154. $row = Admin::where('id',$id)->find();
  155. if(empty($row))
  156. {
  157. $this->error('参数错误,人员无法找到');
  158. }
  159. $oldPass = $this->request->post('oldPassword', '');
  160. if(encrypt_password($oldPass,$row['salt']) != $row['password'])
  161. {
  162. $this->error('旧密码输入错误');
  163. }
  164. $newPass = $this->request->post('newPassword', '');
  165. $check = $this->checkPass($newPass);
  166. if($newPass == $row['username'].'@Zskk2024')
  167. {
  168. $this->error('新密码不能与初始密码一致');
  169. }
  170. if(!$check)
  171. {
  172. $this->error('密码必须八位以上,且包含大小写+特殊字符+数字');
  173. }
  174. $repeatPass = $this->request->post('confirmPassword', '');
  175. if($newPass != $repeatPass)
  176. {
  177. $this->error('新密码不一致');
  178. }
  179. $salt = Random::build('alnum', 16);
  180. $passwd = encrypt_password($newPass, $salt);
  181. $oldPassArr = $this->makeOldPassArr($row['oldPassword'],$passwd,$salt);
  182. if(!$oldPassArr)
  183. {
  184. $this->error('您在最近的5次修改密码中使用过改密码,请更换新的密码进行修改');
  185. }
  186. Admin::where(['id' => $id])->update(['password' => $passwd, 'salt' => $salt,'oldPassword'=>$oldPassArr,'update_pass_time'=>date('Y-m-d H:i:s')]);
  187. $this->success('修改成功','');
  188. }
  189. public function makeOldPassArr($arr,$password,$salt): bool|string
  190. {
  191. $data = [];
  192. $i = 0;
  193. if(empty($arr))
  194. {
  195. $data[time()] = ['password'=>$password,'salt'=>$salt];
  196. }else{
  197. $arr = json_decode($arr,true);
  198. foreach ($arr as $v)
  199. {
  200. if($v['password'] == $password)
  201. {
  202. $i = 1;
  203. }
  204. }
  205. $arr[time()] = ['password'=>$password,'salt'=>$salt];
  206. krsort($arr);
  207. $data = array_slice($arr,0,5);
  208. }
  209. if($i == 1)
  210. {
  211. return false;
  212. }
  213. return json_encode($data);
  214. }
  215. public function checkPassword(): void
  216. {
  217. $id = $this->request->post('id', '');
  218. $pass = $this->request->post('pass', '');
  219. $row = Admin::where('id',$id)->find();
  220. $passwd = encrypt_password($pass, $row['salt']);
  221. if($row['password'] !== $passwd)
  222. {
  223. $this->error('密码错误');
  224. }
  225. $lastTime = Cache::get(get_auth_token());
  226. $this->success('校验成功');
  227. }
  228. }