User.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. <?php
  2. namespace app\index\controller;
  3. use app\common\controller\Frontend;
  4. use think\Config;
  5. use think\Cookie;
  6. use think\Hook;
  7. use think\Session;
  8. use think\Validate;
  9. /**
  10. * 会员中心
  11. */
  12. class User extends Frontend
  13. {
  14. protected $layout = 'default';
  15. protected $noNeedLogin = ['login', 'register', 'third'];
  16. protected $noNeedRight = ['*'];
  17. public function _initialize()
  18. {
  19. parent::_initialize();
  20. $auth = $this->auth;
  21. if (!Config::get('fastadmin.usercenter')) {
  22. $this->error(__('User center already closed'));
  23. }
  24. //监听注册登录注销的事件
  25. Hook::add('user_login_successed', function ($user) use ($auth) {
  26. $expire = input('post.keeplogin') ? 30 * 86400 : 0;
  27. Cookie::set('uid', $user->id, $expire);
  28. Cookie::set('token', $auth->getToken(), $expire);
  29. });
  30. Hook::add('user_register_successed', function ($user) use ($auth) {
  31. Cookie::set('uid', $user->id);
  32. Cookie::set('token', $auth->getToken());
  33. });
  34. Hook::add('user_delete_successed', function ($user) use ($auth) {
  35. Cookie::delete('uid');
  36. Cookie::delete('token');
  37. });
  38. Hook::add('user_logout_successed', function ($user) use ($auth) {
  39. Cookie::delete('uid');
  40. Cookie::delete('token');
  41. });
  42. }
  43. /**
  44. * 空的请求
  45. * @param $name
  46. * @return mixed
  47. */
  48. public function _empty($name)
  49. {
  50. $data = Hook::listen("user_request_empty", $name);
  51. foreach ($data as $index => $datum) {
  52. $this->view->assign($datum);
  53. }
  54. return $this->view->fetch('user/' . $name);
  55. }
  56. /**
  57. * 会员中心
  58. */
  59. public function index()
  60. {
  61. $this->view->assign('title', __('User center'));
  62. return $this->view->fetch();
  63. }
  64. /**
  65. * 注册会员
  66. */
  67. public function register()
  68. {
  69. $url = $this->request->request('url');
  70. if ($this->auth->id) {
  71. $this->success(__('You\'ve logged in, do not login again'), $url ? $url : url('user/index'));
  72. }
  73. if ($this->request->isPost()) {
  74. $username = $this->request->post('username');
  75. $password = $this->request->post('password');
  76. $email = $this->request->post('email');
  77. $mobile = $this->request->post('mobile', '');
  78. $captcha = $this->request->post('captcha');
  79. $token = $this->request->post('__token__');
  80. $rule = [
  81. 'username' => 'require|length:3,30',
  82. 'password' => 'require|length:6,30',
  83. 'email' => 'require|email',
  84. 'mobile' => 'regex:/^1\d{10}$/',
  85. 'captcha' => 'require|captcha',
  86. '__token__' => 'token',
  87. ];
  88. $msg = [
  89. 'username.require' => 'Username can not be empty',
  90. 'username.length' => 'Username must be 3 to 30 characters',
  91. 'password.require' => 'Password can not be empty',
  92. 'password.length' => 'Password must be 6 to 30 characters',
  93. 'captcha.require' => 'Captcha can not be empty',
  94. 'captcha.captcha' => 'Captcha is incorrect',
  95. 'email' => 'Email is incorrect',
  96. 'mobile' => 'Mobile is incorrect',
  97. ];
  98. $data = [
  99. 'username' => $username,
  100. 'password' => $password,
  101. 'email' => $email,
  102. 'mobile' => $mobile,
  103. 'captcha' => $captcha,
  104. '__token__' => $token,
  105. ];
  106. $validate = new Validate($rule, $msg);
  107. $result = $validate->check($data);
  108. if (!$result) {
  109. $this->error(__($validate->getError()), null, ['token' => $this->request->token()]);
  110. }
  111. if ($this->auth->register($username, $password, $email, $mobile)) {
  112. $this->success(__('Sign up successful'), $url ? $url : url('user/index'));
  113. } else {
  114. $this->error($this->auth->getError(), null, ['token' => $this->request->token()]);
  115. }
  116. }
  117. //判断来源
  118. $referer = $this->request->server('HTTP_REFERER');
  119. if (!$url && (strtolower(parse_url($referer, PHP_URL_HOST)) == strtolower($this->request->host()))
  120. && !preg_match("/(user\/login|user\/register)/i", $referer)) {
  121. $url = $referer;
  122. }
  123. $this->view->assign('url', $url);
  124. $this->view->assign('title', __('Register'));
  125. return $this->view->fetch();
  126. }
  127. /**
  128. * 会员登录
  129. */
  130. public function login()
  131. {
  132. $url = $this->request->request('url');
  133. if ($this->auth->id) {
  134. $this->success(__('You\'ve logged in, do not login again'), $url ? $url : url('user/index'));
  135. }
  136. if ($this->request->isPost()) {
  137. $account = $this->request->post('account');
  138. $password = $this->request->post('password');
  139. $keeplogin = (int)$this->request->post('keeplogin');
  140. $token = $this->request->post('__token__');
  141. $rule = [
  142. 'account' => 'require|length:3,50',
  143. 'password' => 'require|length:6,30',
  144. '__token__' => 'token',
  145. ];
  146. $msg = [
  147. 'account.require' => 'Account can not be empty',
  148. 'account.length' => 'Account must be 3 to 50 characters',
  149. 'password.require' => 'Password can not be empty',
  150. 'password.length' => 'Password must be 6 to 30 characters',
  151. ];
  152. $data = [
  153. 'account' => $account,
  154. 'password' => $password,
  155. '__token__' => $token,
  156. ];
  157. $validate = new Validate($rule, $msg);
  158. $result = $validate->check($data);
  159. if (!$result) {
  160. $this->error(__($validate->getError()), null, ['token' => $this->request->token()]);
  161. return false;
  162. }
  163. if ($this->auth->login($account, $password)) {
  164. $this->success(__('Logged in successful'), $url ? $url : url('user/index'));
  165. } else {
  166. $this->error($this->auth->getError(), null, ['token' => $this->request->token()]);
  167. }
  168. }
  169. //判断来源
  170. $referer = $this->request->server('HTTP_REFERER');
  171. if (!$url && (strtolower(parse_url($referer, PHP_URL_HOST)) == strtolower($this->request->host()))
  172. && !preg_match("/(user\/login|user\/register)/i", $referer)) {
  173. $url = $referer;
  174. }
  175. $this->view->assign('url', $url);
  176. $this->view->assign('title', __('Login'));
  177. return $this->view->fetch();
  178. }
  179. /**
  180. * 注销登录
  181. */
  182. public function logout()
  183. {
  184. //注销本站
  185. $this->auth->logout();
  186. $this->success(__('Logout successful'), url('user/index'));
  187. }
  188. /**
  189. * 个人信息
  190. */
  191. public function profile()
  192. {
  193. $this->view->assign('title', __('Profile'));
  194. return $this->view->fetch();
  195. }
  196. /**
  197. * 修改密码
  198. */
  199. public function changepwd()
  200. {
  201. if ($this->request->isPost()) {
  202. $oldpassword = $this->request->post("oldpassword");
  203. $newpassword = $this->request->post("newpassword");
  204. $renewpassword = $this->request->post("renewpassword");
  205. $token = $this->request->post('__token__');
  206. $rule = [
  207. 'oldpassword' => 'require|length:6,30',
  208. 'newpassword' => 'require|length:6,30',
  209. 'renewpassword' => 'require|length:6,30|confirm:newpassword',
  210. '__token__' => 'token',
  211. ];
  212. $msg = [
  213. ];
  214. $data = [
  215. 'oldpassword' => $oldpassword,
  216. 'newpassword' => $newpassword,
  217. 'renewpassword' => $renewpassword,
  218. '__token__' => $token,
  219. ];
  220. $field = [
  221. 'oldpassword' => __('Old password'),
  222. 'newpassword' => __('New password'),
  223. 'renewpassword' => __('Renew password')
  224. ];
  225. $validate = new Validate($rule, $msg, $field);
  226. $result = $validate->check($data);
  227. if (!$result) {
  228. $this->error(__($validate->getError()), null, ['token' => $this->request->token()]);
  229. return false;
  230. }
  231. $ret = $this->auth->changepwd($newpassword, $oldpassword);
  232. if ($ret) {
  233. $this->success(__('Reset password successful'), url('user/login'));
  234. } else {
  235. $this->error($this->auth->getError(), null, ['token' => $this->request->token()]);
  236. }
  237. }
  238. $this->view->assign('title', __('Change password'));
  239. return $this->view->fetch();
  240. }
  241. }