User.php 10 KB

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