User.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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. $code = $this->request->post('code');
  81. $token = $this->request->post('__token__');
  82. $rule = [
  83. 'username' => 'require|length:3,30',
  84. 'password' => 'require|length:6,30',
  85. 'email' => 'require|email',
  86. 'mobile' => 'regex:/^1\d{10}$/',
  87. '__token__' => 'require|token',
  88. ];
  89. $msg = [
  90. 'username.require' => 'Username can not be empty',
  91. 'username.length' => 'Username must be 3 to 30 characters',
  92. 'password.require' => 'Password can not be empty',
  93. 'password.length' => 'Password must be 6 to 30 characters',
  94. //'captcha.require' => 'Captcha can not be empty',
  95. //'captcha.captcha' => 'Captcha is incorrect',
  96. 'email' => 'Email is incorrect',
  97. 'mobile' => 'Mobile is incorrect',
  98. ];
  99. $data = [
  100. 'username' => $username,
  101. 'password' => $password,
  102. 'email' => $email,
  103. 'mobile' => $mobile,
  104. //'captcha' => $captcha,
  105. '__token__' => $token,
  106. ];
  107. $ret = Sms::check($mobile, $code, 'register');
  108. if (!$ret) {
  109. $this->error(__('Captcha is incorrect'));
  110. }
  111. $validate = new Validate($rule, $msg);
  112. $result = $validate->check($data);
  113. if (!$result) {
  114. $this->error(__($validate->getError()), null, ['token' => $this->request->token()]);
  115. }
  116. if ($this->auth->register($username, $password, $email, $mobile)) {
  117. $this->success(__('Sign up successful'), $url ? $url : url('user/index'));
  118. } else {
  119. $this->error($this->auth->getError(), null, ['token' => $this->request->token()]);
  120. }
  121. }
  122. //判断来源
  123. $referer = $this->request->server('HTTP_REFERER');
  124. if (!$url && (strtolower(parse_url($referer, PHP_URL_HOST)) == strtolower($this->request->host()))
  125. && !preg_match("/(user\/login|user\/register|user\/logout)/i", $referer)) {
  126. $url = $referer;
  127. }
  128. $this->view->assign('url', $url);
  129. $this->view->assign('title', __('Register'));
  130. return $this->view->fetch();
  131. }
  132. /**
  133. * 会员登录
  134. */
  135. public function login()
  136. {
  137. $url = $this->request->request('url', '', 'trim');
  138. if ($this->auth->id) {
  139. $this->success(__('You\'ve logged in, do not login again'), $url ? $url : url('user/index'));
  140. }
  141. if ($this->request->isPost()) {
  142. $account = $this->request->post('account');
  143. $password = $this->request->post('password');
  144. $keeplogin = (int)$this->request->post('keeplogin');
  145. $token = $this->request->post('__token__');
  146. $rule = [
  147. 'account' => 'require|length:3,50',
  148. 'password' => 'require|length:6,30',
  149. '__token__' => 'require|token',
  150. ];
  151. $msg = [
  152. 'account.require' => 'Account can not be empty',
  153. 'account.length' => 'Account must be 3 to 50 characters',
  154. 'password.require' => 'Password can not be empty',
  155. 'password.length' => 'Password must be 6 to 30 characters',
  156. ];
  157. $data = [
  158. 'account' => $account,
  159. 'password' => $password,
  160. '__token__' => $token,
  161. ];
  162. $validate = new Validate($rule, $msg);
  163. $result = $validate->check($data);
  164. if (!$result) {
  165. $this->error(__($validate->getError()), null, ['token' => $this->request->token()]);
  166. return false;
  167. }
  168. if ($this->auth->login($account, $password)) {
  169. $this->success(__('Logged in successful'), $url ? $url : url('user/index'));
  170. } else {
  171. $this->error($this->auth->getError(), null, ['token' => $this->request->token()]);
  172. }
  173. }
  174. //判断来源
  175. $referer = $this->request->server('HTTP_REFERER');
  176. if (!$url && (strtolower(parse_url($referer, PHP_URL_HOST)) == strtolower($this->request->host()))
  177. && !preg_match("/(user\/login|user\/register|user\/logout)/i", $referer)) {
  178. $url = $referer;
  179. }
  180. $this->view->assign('url', $url);
  181. $this->view->assign('title', __('Login'));
  182. return $this->view->fetch();
  183. }
  184. /**
  185. * 注销登录
  186. */
  187. public function logout()
  188. {
  189. //注销本站
  190. $this->auth->logout();
  191. $this->success(__('Logout successful'), url('user/index'));
  192. }
  193. /**
  194. * 个人信息
  195. */
  196. public function profile()
  197. {
  198. $this->view->assign('title', __('Profile'));
  199. return $this->view->fetch();
  200. }
  201. /**
  202. * 修改密码
  203. */
  204. public function changepwd()
  205. {
  206. if ($this->request->isPost()) {
  207. $oldpassword = $this->request->post("oldpassword");
  208. $newpassword = $this->request->post("newpassword");
  209. $renewpassword = $this->request->post("renewpassword");
  210. $token = $this->request->post('__token__');
  211. $rule = [
  212. 'oldpassword' => 'require|length:6,30',
  213. 'newpassword' => 'require|length:6,30',
  214. 'renewpassword' => 'require|length:6,30|confirm:newpassword',
  215. '__token__' => 'token',
  216. ];
  217. $msg = [
  218. ];
  219. $data = [
  220. 'oldpassword' => $oldpassword,
  221. 'newpassword' => $newpassword,
  222. 'renewpassword' => $renewpassword,
  223. '__token__' => $token,
  224. ];
  225. $field = [
  226. 'oldpassword' => __('Old password'),
  227. 'newpassword' => __('New password'),
  228. 'renewpassword' => __('Renew password')
  229. ];
  230. $validate = new Validate($rule, $msg, $field);
  231. $result = $validate->check($data);
  232. if (!$result) {
  233. $this->error(__($validate->getError()), null, ['token' => $this->request->token()]);
  234. return false;
  235. }
  236. $ret = $this->auth->changepwd($newpassword, $oldpassword);
  237. if ($ret) {
  238. $this->success(__('Reset password successful'), url('user/login'));
  239. } else {
  240. $this->error($this->auth->getError(), null, ['token' => $this->request->token()]);
  241. }
  242. }
  243. $this->view->assign('title', __('Change password'));
  244. return $this->view->fetch();
  245. }
  246. }