UserModel(); $model->load(Yii::$app->request->post(), ''); if (!$model->validate()) return $model->errors; $UserRow = $model->findUserName(); if ($UserRow->status != 1) return '该账号已被禁用,请联系管理员恢复。'; if ($UserRow == null) return '账号或密码错误!'; if ($UserRow->password != md5(md5($model->password))) { $pwd = $this->PasswordErrorNum($UserRow); if ($pwd === false) return '密码错误次数超过限制!'; return '你输入的密码有误,还可输入次数为:' . $pwd; } $tokenVal = $this->EditToken($UserRow); $UserRow->token = $tokenVal; $UserRow->login_time = $_SERVER['REQUEST_TIME']; $UserRow->login_num = $UserRow->login_num + 1; $UserRow->save(); Yii::$app->session['user_info'] = ['user' => $UserRow->user, 'token' => $tokenVal, 'uid' => $UserRow->uid, 'name' => $UserRow->name, 'status' => $UserRow->status ]; return true; } public function Signup() { $model = $this->UserModel(); $model->scenario = 'Signup'; $model->load(Yii::$app->request->post('data'), ''); if (!$model->validate()) return $model->errors; $model->token = md5(uniqid()); $model->password = md5(md5($model->password)); if ($model->insert(false) == true) { return true; } } /* * 密码错误次数记录 * */ protected function PasswordErrorNum($obj) { $cache = Yii::$app->cache; $cacheName = Yii::$app->params['cacheKey']['user_pwd'] . $obj->user; $num = 1; $UserCache = $cache->get($cacheName); if ($UserCache === false) { $data = ['num' => $num, 'time' => $_SERVER['REQUEST_TIME']]; //初始化错误次数 $cache->set($cacheName, $data, $this->loginErrorTime); } else { if ($UserCache['num'] == 5) return false; $UserCache['num'] = $UserCache['num'] + 1; $UserCache['time'] = $_SERVER['REQUEST_TIME']; $cache->set($cacheName, $UserCache, $this->loginErrorTime); $num = $UserCache['num']; } return $this->loginErrorNum - $num; } /* * 修改唯一登录标识 * */ protected function EditToken($obj) { $cache = Yii::$app->cache; $cacheName = Yii::$app->params['cacheKey']['user_token'] . $obj->user; $val = md5(uniqid()); $token = $cache->set($cacheName, $val); return $val; } public function Userlist() { $model = $this->UserModel(); $arr = []; $rows = $model->getList(Yii::$app->request->post()); if ($rows == null) return false; $arr['count'] = $model->Total(); foreach ($rows as &$val) { $val['create_at'] = date('Y-m-d H:i', $val['create_at']); $val['login_time'] = date('Y-m-d H:i', $val['login_time']); // $val['status'] = Yii::$app->params['state'][$val['status']]; } $arr['data'] = $rows; return $arr; } public function UserRoleFilter() { $role = new \common\models\UserRole(); $roleRows = $role->getList([]); $roleUser = new \common\models\UserUr(); $uid = $roleUser->getList(Yii::$app->request->get('uid')); if ($uid != null) { $uid = array_column($uid, 'rid'); foreach ($roleRows['data'] as &$val) { if (in_array($val['id'], $uid)) { $val['checked'] = 'checked'; } } } return $roleRows['data']; } //修改用户信息 public function UserSaveForm() { $model = $this->UserModel(); $row = $model->Authenticator(Yii::$app->request->post('data')); if (is_array($row)) return $row; $UserRow = $row->findById(Yii::$app->request->post('data')['uid']); if ($UserRow != null) { if ($UserRow->password != $row->password) { $row->password = md5(md5($row->password)); } $UserModel = \backend\base\Help::SetAttr(Yii::$app->request->post('data'), $row, $UserRow); if ($UserModel->save(false) == true) return true; } } //修改密码 public function PasswordSave() { $input = Yii::$app->request->post('data'); $arr = []; $model = $this->UserModel(); if (!preg_match(Yii::$app->params['match']['password'], $input['pwd'], $arr) || !preg_match(Yii::$app->params['match']['password'], $input['repassword'], $arr) || !preg_match(Yii::$app->params['match']['password'], $input['password'], $arr)) { // $model->addError('password',''); // return $model->errors; return '密码格式不正确'; } $userModel = $model->findById(Yii::$app->session['user_info']['uid']); if ($userModel != null) { if ($model->setPassword($input['password']) == $userModel->password) { return '旧密码不能和新密码一致'; } if ($model->setPassword($input['pwd']) == $userModel->password) { $userModel->password = $model->setPassword($input['password']); if ($userModel->update(false) == true) { unset(Yii::$app->session['user_info']); return true; } } else { return '请输入正确的旧密码'; } } } protected function UserModel() { return new Users(); } public function Usersetstatus() { $input = Yii::$app->request->post(); $model = $this->UserModel(); $rows = $model->findById($input['id']); $rows->status = $input['status']; if ($rows->update()) return true; return false; } }