123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251 |
- <?php
- namespace app\admin\controller\user;
- use app\admin\model\Admin;
- use think\facade\Cache;
- use Throwable;
- use ba\Random;
- use app\common\controller\Backend;
- use app\admin\model\User as UserModel;
- class User extends Backend
- {
- /**
- * @var object
- * @phpstan-var UserModel
- */
- protected object $model;
- protected array $withJoinTable = ['group'];
- // 排除字段
- protected string|array $preExcludeFields = ['last_login_time', 'login_failure', 'password', 'salt'];
- protected string|array $quickSearchField = ['username', 'nickname', 'id'];
- protected array $noNeedLogin = ['updatePassword'];
- protected array $noNeedPermission = ['updatePassword'];
- protected array $noNeedCheckPass = ['checkPassword'];
- public function initialize(): void
- {
- parent::initialize();
- $this->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('校验成功');
- }
- }
|