123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203 |
- <?php
- /**
- * Created by PhpStorm.
- * User: Administrator
- * Date: 2018/2/26/026
- * Time: 11:16
- */
- namespace backend\server;
- use yii\base\Model;
- use Yii;
- use common\models\User as Users;
- class User extends Model
- {
- public $loginErrorNum = 5; //错误最大次数
- public $loginErrorTime = 900; //密码输入错误缓存时间为900秒
- public function Login()
- {
- $model = $this->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;
- }
- }
|