model = new UserModel(); } /** * 查看 * @throws Throwable */ public function index(): void { if ($this->request->param('select')) { $this->select(); } list($where, $alias, $limit, $order) = $this->queryBuilder(); $res = $this->model ->withoutField('password,salt') ->withJoin($this->withJoinTable, $this->withJoinType) ->alias($alias) ->where($where) ->order($order) ->paginate($limit); $this->success('', [ 'list' => $res->items(), 'total' => $res->total(), 'remark' => get_route_remark(), ]); } /** * 添加 * @throws Throwable */ public function add(): void { if ($this->request->isPost()) { $data = $this->request->post(); if (!$data) { $this->error(__('Parameter %s can not be empty', [''])); } $salt = Random::build('alnum', 16); $passwd = encrypt_password($data['password'], $salt); $data = $this->excludeFields($data); $result = false; $this->model->startTrans(); try { $data['salt'] = $salt; $data['password'] = $passwd; // 模型验证 if ($this->modelValidate) { $validate = str_replace("\\model\\", "\\validate\\", get_class($this->model)); if (class_exists($validate)) { $validate = new $validate(); if ($this->modelSceneValidate) $validate->scene('add'); $validate->check($data); } } $result = $this->model->save($data); $this->model->commit(); } catch (Throwable $e) { $this->model->rollback(); $this->error($e->getMessage()); } if ($result !== false) { $this->success(__('Added successfully')); } else { $this->error(__('No rows were added')); } } $this->error(__('Parameter error')); } /** * 编辑 * @param string|int|null $id * @throws Throwable */ public function edit(string|int $id = null): void { $row = $this->model->find($id); if (!$row) { $this->error(__('Record not found')); } if ($this->request->isPost()) { $password = $this->request->post('password', ''); if ($password) { $this->model->resetPassword($id, $password); } parent::edit(); } unset($row->salt); $row->password = ''; $this->success('', [ 'row' => $row ]); } /** * 重写select * @throws Throwable */ public function select(): void { list($where, $alias, $limit, $order) = $this->queryBuilder(); $res = $this->model ->withJoin($this->withJoinTable, $this->withJoinType) ->alias($alias) ->where($where) ->order($order) ->paginate($limit); foreach ($res as $re) { $re->nickname_text = $re->username . '(ID:' . $re->id . ')'; } $this->success('', [ 'list' => $res->items(), 'total' => $res->total(), 'remark' => get_route_remark(), ]); } public function resetPassword(): void { $id = $this->request->post('id', ''); $row = Admin::where('id',$id)->find(); if(empty($row)) { $this->error('参数错误,人员无法找到'); } $password = $row['username'].'@Zskk2024'; $this->model->resetPassword($id, $password); $this->success('重置成功,初始密码为'.$password); } public function updatePassword(): void { $id = $this->request->post('id', ''); $row = Admin::where('id',$id)->find(); if(empty($row)) { $this->error('参数错误,人员无法找到'); } $oldPass = $this->request->post('oldPassword', ''); if(encrypt_password($oldPass,$row['salt']) != $row['password']) { $this->error('旧密码输入错误'); } $newPass = $this->request->post('newPassword', ''); $check = $this->checkPass($newPass); if($newPass == $row['username'].'@Zskk2024') { $this->error('新密码不能与初始密码一致'); } if(!$check) { $this->error('密码必须八位以上,且包含大小写+特殊字符+数字'); } $repeatPass = $this->request->post('confirmPassword', ''); if($newPass != $repeatPass) { $this->error('新密码不一致'); } $salt = Random::build('alnum', 16); $passwd = encrypt_password($newPass, $salt); $oldPassArr = $this->makeOldPassArr($row['oldPassword'],$passwd,$salt); if(!$oldPassArr) { $this->error('您在最近的5次修改密码中使用过改密码,请更换新的密码进行修改'); } Admin::where(['id' => $id])->update(['password' => $passwd, 'salt' => $salt,'oldPassword'=>$oldPassArr,'update_pass_time'=>date('Y-m-d H:i:s')]); $this->success('修改成功',''); } public function makeOldPassArr($arr,$password,$salt): bool|string { $data = []; $i = 0; if(empty($arr)) { $data[time()] = ['password'=>$password,'salt'=>$salt]; }else{ $arr = json_decode($arr,true); foreach ($arr as $v) { if($v['password'] == $password) { $i = 1; } } $arr[time()] = ['password'=>$password,'salt'=>$salt]; krsort($arr); $data = array_slice($arr,0,5); } if($i == 1) { return false; } return json_encode($data); } public function checkPassword(): void { $id = $this->request->post('id', ''); $pass = $this->request->post('pass', ''); $row = Admin::where('id',$id)->find(); $passwd = encrypt_password($pass, $row['salt']); if($row['password'] !== $passwd) { $this->error('密码错误'); } $lastTime = Cache::get(get_auth_token()); $this->success('校验成功'); } }