User.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 2018/2/26/026
  6. * Time: 11:16
  7. */
  8. namespace backend\server;
  9. use yii\base\Model;
  10. use Yii;
  11. use common\models\User as Users;
  12. class User extends Model
  13. {
  14. public $loginErrorNum = 5; //错误最大次数
  15. public $loginErrorTime = 900; //密码输入错误缓存时间为900秒
  16. public function Login()
  17. {
  18. $model = $this->UserModel();
  19. $model->load(Yii::$app->request->post(), '');
  20. if (!$model->validate()) return $model->errors;
  21. $UserRow = $model->findUserName();
  22. if ($UserRow->status != 1) return '该账号已被禁用,请联系管理员恢复。';
  23. if ($UserRow == null) return '账号或密码错误!';
  24. if ($UserRow->password != md5(md5($model->password))) {
  25. $pwd = $this->PasswordErrorNum($UserRow);
  26. if ($pwd === false) return '密码错误次数超过限制!';
  27. return '你输入的密码有误,还可输入次数为:' . $pwd;
  28. }
  29. $tokenVal = $this->EditToken($UserRow);
  30. $UserRow->token = $tokenVal;
  31. $UserRow->login_time = $_SERVER['REQUEST_TIME'];
  32. $UserRow->login_num = $UserRow->login_num + 1;
  33. $UserRow->save();
  34. Yii::$app->session['user_info'] = ['user' => $UserRow->user,
  35. 'token' => $tokenVal,
  36. 'uid' => $UserRow->uid,
  37. 'name' => $UserRow->name,
  38. 'status' => $UserRow->status
  39. ];
  40. return true;
  41. }
  42. public function Signup()
  43. {
  44. $model = $this->UserModel();
  45. $model->scenario = 'Signup';
  46. $model->load(Yii::$app->request->post('data'), '');
  47. if (!$model->validate()) return $model->errors;
  48. $model->token = md5(uniqid());
  49. $model->password = md5(md5($model->password));
  50. if ($model->insert(false) == true) {
  51. return true;
  52. }
  53. }
  54. /*
  55. * 密码错误次数记录
  56. * */
  57. protected function PasswordErrorNum($obj)
  58. {
  59. $cache = Yii::$app->cache;
  60. $cacheName = Yii::$app->params['cacheKey']['user_pwd'] . $obj->user;
  61. $num = 1;
  62. $UserCache = $cache->get($cacheName);
  63. if ($UserCache === false) {
  64. $data = ['num' => $num, 'time' => $_SERVER['REQUEST_TIME']]; //初始化错误次数
  65. $cache->set($cacheName, $data, $this->loginErrorTime);
  66. } else {
  67. if ($UserCache['num'] == 5) return false;
  68. $UserCache['num'] = $UserCache['num'] + 1;
  69. $UserCache['time'] = $_SERVER['REQUEST_TIME'];
  70. $cache->set($cacheName, $UserCache, $this->loginErrorTime);
  71. $num = $UserCache['num'];
  72. }
  73. return $this->loginErrorNum - $num;
  74. }
  75. /*
  76. * 修改唯一登录标识
  77. * */
  78. protected function EditToken($obj)
  79. {
  80. $cache = Yii::$app->cache;
  81. $cacheName = Yii::$app->params['cacheKey']['user_token'] . $obj->user;
  82. $val = md5(uniqid());
  83. $token = $cache->set($cacheName, $val);
  84. return $val;
  85. }
  86. public function Userlist()
  87. {
  88. $model = $this->UserModel();
  89. $arr = [];
  90. $rows = $model->getList(Yii::$app->request->post());
  91. if ($rows == null) return false;
  92. $arr['count'] = $model->Total();
  93. foreach ($rows as &$val) {
  94. $val['create_at'] = date('Y-m-d H:i', $val['create_at']);
  95. $val['login_time'] = date('Y-m-d H:i', $val['login_time']);
  96. // $val['status'] = Yii::$app->params['state'][$val['status']];
  97. }
  98. $arr['data'] = $rows;
  99. return $arr;
  100. }
  101. public function UserRoleFilter()
  102. {
  103. $role = new \common\models\UserRole();
  104. $roleRows = $role->getList([]);
  105. $roleUser = new \common\models\UserUr();
  106. $uid = $roleUser->getList(Yii::$app->request->get('uid'));
  107. if ($uid != null) {
  108. $uid = array_column($uid, 'rid');
  109. foreach ($roleRows['data'] as &$val) {
  110. if (in_array($val['id'], $uid)) {
  111. $val['checked'] = 'checked';
  112. }
  113. }
  114. }
  115. return $roleRows['data'];
  116. }
  117. //修改用户信息
  118. public function UserSaveForm()
  119. {
  120. $model = $this->UserModel();
  121. $row = $model->Authenticator(Yii::$app->request->post('data'));
  122. if (is_array($row)) return $row;
  123. $UserRow = $row->findById(Yii::$app->request->post('data')['uid']);
  124. if ($UserRow != null) {
  125. if ($UserRow->password != $row->password) {
  126. $row->password = md5(md5($row->password));
  127. }
  128. $UserModel = \backend\base\Help::SetAttr(Yii::$app->request->post('data'), $row, $UserRow);
  129. if ($UserModel->save(false) == true) return true;
  130. }
  131. }
  132. //修改密码
  133. public function PasswordSave()
  134. {
  135. $input = Yii::$app->request->post('data');
  136. $arr = [];
  137. $model = $this->UserModel();
  138. 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)) {
  139. // $model->addError('password','');
  140. // return $model->errors;
  141. return '密码格式不正确';
  142. }
  143. $userModel = $model->findById(Yii::$app->session['user_info']['uid']);
  144. if ($userModel != null) {
  145. if ($model->setPassword($input['password']) == $userModel->password) {
  146. return '旧密码不能和新密码一致';
  147. }
  148. if ($model->setPassword($input['pwd']) == $userModel->password) {
  149. $userModel->password = $model->setPassword($input['password']);
  150. if ($userModel->update(false) == true) {
  151. unset(Yii::$app->session['user_info']);
  152. return true;
  153. }
  154. } else {
  155. return '请输入正确的旧密码';
  156. }
  157. }
  158. }
  159. protected function UserModel()
  160. {
  161. return new Users();
  162. }
  163. public function Usersetstatus()
  164. {
  165. $input = Yii::$app->request->post();
  166. $model = $this->UserModel();
  167. $rows = $model->findById($input['id']);
  168. $rows->status = $input['status'];
  169. if ($rows->update()) return true;
  170. return false;
  171. }
  172. }