User.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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', url('user/index'));
  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()));
  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);
  109. } else {
  110. $this->error($this->auth->getError());
  111. }
  112. }
  113. Session::set('redirect_url', $url);
  114. $this->view->assign('title', __('Register'));
  115. return $this->view->fetch();
  116. }
  117. /**
  118. * 会员登录
  119. */
  120. public function login()
  121. {
  122. $url = $this->request->request('url', url('user/index'));
  123. if ($this->auth->id)
  124. $this->success(__('You\'ve logged in, do not login again'), $url);
  125. if ($this->request->isPost()) {
  126. $account = $this->request->post('account');
  127. $password = $this->request->post('password');
  128. $keeplogin = (int)$this->request->post('keeplogin');
  129. $token = $this->request->post('__token__');
  130. $rule = [
  131. 'account' => 'require|length:3,50',
  132. 'password' => 'require|length:6,30',
  133. '__token__' => 'token',
  134. ];
  135. $msg = [
  136. 'account.require' => 'Account can not be empty',
  137. 'account.length' => 'Account must be 3 to 50 characters',
  138. 'password.require' => 'Password can not be empty',
  139. 'password.length' => 'Password must be 6 to 30 characters',
  140. ];
  141. $data = [
  142. 'account' => $account,
  143. 'password' => $password,
  144. '__token__' => $token,
  145. ];
  146. $validate = new Validate($rule, $msg);
  147. $result = $validate->check($data);
  148. if (!$result) {
  149. $this->error(__($validate->getError()));
  150. return FALSE;
  151. }
  152. if ($this->auth->login($account, $password)) {
  153. $synchtml = '';
  154. ////////////////同步到Ucenter////////////////
  155. if (defined('UC_STATUS') && UC_STATUS) {
  156. $uc = new \addons\ucenter\library\client\Client();
  157. $synchtml = $uc->uc_user_synlogin($this->auth->id);
  158. }
  159. $this->success(__('Logged in successful') . $synchtml, $url);
  160. } else {
  161. $this->error($this->auth->getError());
  162. }
  163. }
  164. $this->view->assign('title', __('Login'));
  165. return $this->view->fetch();
  166. }
  167. /**
  168. * 注销登录
  169. */
  170. function logout()
  171. {
  172. //注销本站
  173. $this->auth->logout();
  174. $synchtml = '';
  175. ////////////////同步到Ucenter////////////////
  176. if (defined('UC_STATUS') && UC_STATUS) {
  177. $uc = new \addons\ucenter\library\client\Client();
  178. $synchtml = $uc->uc_user_synlogout();
  179. }
  180. $this->success(__('Logout successful') . $synchtml, url('user/index'));
  181. }
  182. /**
  183. * 个人信息
  184. */
  185. public function profile()
  186. {
  187. $this->view->assign('title', __('Profile'));
  188. return $this->view->fetch();
  189. }
  190. /**
  191. * 修改密码
  192. */
  193. public function changepwd()
  194. {
  195. if ($this->request->isPost()) {
  196. $oldpassword = $this->request->post("oldpassword");
  197. $newpassword = $this->request->post("newpassword");
  198. $renewpassword = $this->request->post("renewpassword");
  199. $token = $this->request->post('__token__');
  200. $rule = [
  201. 'oldpassword' => 'require|length:6,30',
  202. 'newpassword' => 'require|length:6,30',
  203. 'renewpassword' => 'require|length:6,30|confirm:newpassword',
  204. '__token__' => 'token',
  205. ];
  206. $msg = [
  207. ];
  208. $data = [
  209. 'oldpassword' => $oldpassword,
  210. 'newpassword' => $newpassword,
  211. 'renewpassword' => $renewpassword,
  212. '__token__' => $token,
  213. ];
  214. $field = [
  215. 'oldpassword' => __('Old password'),
  216. 'newpassword' => __('New password'),
  217. 'renewpassword' => __('Renew password')
  218. ];
  219. $validate = new Validate($rule, $msg, $field);
  220. $result = $validate->check($data);
  221. if (!$result) {
  222. $this->error(__($validate->getError()));
  223. return FALSE;
  224. }
  225. $ret = $this->auth->changepwd($newpassword, $oldpassword);
  226. if ($ret) {
  227. $synchtml = '';
  228. ////////////////同步到Ucenter////////////////
  229. if (defined('UC_STATUS') && UC_STATUS) {
  230. $uc = new \addons\ucenter\library\client\Client();
  231. $synchtml = $uc->uc_user_synlogout();
  232. }
  233. $this->success(__('Reset password successful') . $synchtml, url('user/login'));
  234. } else {
  235. $this->error($this->auth->getError());
  236. }
  237. }
  238. $this->view->assign('title', __('Change password'));
  239. return $this->view->fetch();
  240. }
  241. }