User.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. <?php
  2. namespace app\index\controller;
  3. use app\common\controller\Frontend;
  4. use think\Cookie;
  5. use think\Hook;
  6. use think\Session;
  7. use think\Validate;
  8. /**
  9. * 会员中心
  10. */
  11. class User extends Frontend
  12. {
  13. protected $layout = 'default';
  14. protected $noNeedLogin = ['login', 'register', 'third'];
  15. protected $noNeedRight = ['*'];
  16. public function _initialize()
  17. {
  18. parent::_initialize();
  19. $auth = $this->auth;
  20. $ucenter = get_addon_info('ucenter');
  21. if ($ucenter && $ucenter['state'])
  22. {
  23. include ADDON_PATH . 'ucenter' . DS . 'uc.php';
  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. */
  47. public function index()
  48. {
  49. $this->view->assign('title', __('User center'));
  50. return $this->view->fetch();
  51. }
  52. /**
  53. * 注册会员
  54. */
  55. public function register()
  56. {
  57. $url = $this->request->request('url', url('user/index'));
  58. if ($this->auth->id)
  59. $this->success(__('You\'ve logged in, do not login again'), $url);
  60. if ($this->request->isPost())
  61. {
  62. $username = $this->request->post('username');
  63. $password = $this->request->post('password');
  64. $email = $this->request->post('email');
  65. $mobile = $this->request->post('mobile', '');
  66. $captcha = $this->request->post('captcha');
  67. $token = $this->request->post('__token__');
  68. $rule = [
  69. 'username' => 'require|length:3,30',
  70. 'password' => 'require|length:6,30',
  71. 'email' => 'require|email',
  72. 'mobile' => 'regex:/^1\d{10}$/',
  73. 'captcha' => 'require|captcha',
  74. '__token__' => 'token',
  75. ];
  76. $msg = [
  77. 'username.require' => 'Username can not be empty',
  78. 'username.length' => 'Username must be 3 to 30 characters',
  79. 'password.require' => 'Password can not be empty',
  80. 'password.length' => 'Password must be 6 to 30 characters',
  81. 'captcha.require' => 'Captcha can not be empty',
  82. 'captcha.captcha' => 'Captcha is incorrect',
  83. 'email' => 'Email is incorrect',
  84. 'mobile' => 'Mobile is incorrect',
  85. ];
  86. $data = [
  87. 'username' => $username,
  88. 'password' => $password,
  89. 'email' => $email,
  90. 'mobile' => $mobile,
  91. 'captcha' => $captcha,
  92. '__token__' => $token,
  93. ];
  94. $validate = new Validate($rule, $msg);
  95. $result = $validate->check($data);
  96. if (!$result)
  97. {
  98. $this->error(__($validate->getError()));
  99. }
  100. if ($this->auth->register($username, $password, $email, $mobile))
  101. {
  102. $synchtml = '';
  103. ////////////////同步到Ucenter////////////////
  104. if (defined('UC_STATUS') && UC_STATUS)
  105. {
  106. $uc = new \addons\ucenter\library\client\Client();
  107. $synchtml = $uc->uc_user_synregister($this->auth->id, $password);
  108. }
  109. $this->success(__('Sign up successful') . $synchtml, $url);
  110. }
  111. else
  112. {
  113. $this->error($this->auth->getError());
  114. }
  115. }
  116. Session::set('redirect_url', $url);
  117. $this->view->assign('title', __('Register'));
  118. return $this->view->fetch();
  119. }
  120. /**
  121. * 会员登录
  122. */
  123. public function login()
  124. {
  125. $url = $this->request->request('url', url('user/index'));
  126. if ($this->auth->id)
  127. $this->success(__('You\'ve logged in, do not login again'), $url);
  128. if ($this->request->isPost())
  129. {
  130. $account = $this->request->post('account');
  131. $password = $this->request->post('password');
  132. $keeplogin = (int) $this->request->post('keeplogin');
  133. $token = $this->request->post('__token__');
  134. $rule = [
  135. 'account' => 'require|length:3,50',
  136. 'password' => 'require|length:6,30',
  137. '__token__' => 'token',
  138. ];
  139. $msg = [
  140. 'account.require' => 'Account can not be empty',
  141. 'account.length' => 'Account must be 3 to 50 characters',
  142. 'password.require' => 'Password can not be empty',
  143. 'password.length' => 'Password must be 6 to 30 characters',
  144. ];
  145. $data = [
  146. 'account' => $account,
  147. 'password' => $password,
  148. '__token__' => $token,
  149. ];
  150. $validate = new Validate($rule, $msg);
  151. $result = $validate->check($data);
  152. if (!$result)
  153. {
  154. $this->error(__($validate->getError()));
  155. return FALSE;
  156. }
  157. if ($this->auth->login($account, $password))
  158. {
  159. $synchtml = '';
  160. ////////////////同步到Ucenter////////////////
  161. if (defined('UC_STATUS') && UC_STATUS)
  162. {
  163. $uc = new \addons\ucenter\library\client\Client();
  164. $synchtml = $uc->uc_user_synlogin($this->auth->id);
  165. }
  166. $this->success(__('Logged in successful') . $synchtml, $url);
  167. }
  168. else
  169. {
  170. $this->error($this->auth->getError());
  171. }
  172. }
  173. $this->view->assign('title', __('Login'));
  174. return $this->view->fetch();
  175. }
  176. /**
  177. * 注销登录
  178. */
  179. function logout()
  180. {
  181. //注销本站
  182. $this->auth->logout();
  183. $synchtml = '';
  184. ////////////////同步到Ucenter////////////////
  185. if (defined('UC_STATUS') && UC_STATUS)
  186. {
  187. $uc = new \addons\ucenter\library\client\Client();
  188. $synchtml = $uc->uc_user_synlogout();
  189. }
  190. $this->success(__('Logout successful') . $synchtml, 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. {
  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. {
  234. $this->error(__($validate->getError()));
  235. return FALSE;
  236. }
  237. $ret = $this->auth->changepwd($newpassword, $oldpassword);
  238. if ($ret)
  239. {
  240. $synchtml = '';
  241. ////////////////同步到Ucenter////////////////
  242. if (defined('UC_STATUS') && UC_STATUS)
  243. {
  244. $uc = new \addons\ucenter\library\client\Client();
  245. $synchtml = $uc->uc_user_synlogout();
  246. }
  247. $this->success(__('Reset password successful') . $synchtml, url('user/login'));
  248. }
  249. else
  250. {
  251. $this->error($this->auth->getError());
  252. }
  253. }
  254. $this->view->assign('title', __('Change password'));
  255. return $this->view->fetch();
  256. }
  257. }