User.php 9.1 KB

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