You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

276 lines
9.7 KiB

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