User.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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. * @param $name
  50. * @return mixed
  51. */
  52. public function _empty($name)
  53. {
  54. Hook::listen("user_request_empty", $name);
  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');
  71. if ($this->auth->id)
  72. $this->success(__('You\'ve logged in, do not login again'), $url);
  73. if ($this->request->isPost()) {
  74. $username = $this->request->post('username');
  75. $password = $this->request->post('password');
  76. $email = $this->request->post('email');
  77. $mobile = $this->request->post('mobile', '');
  78. $captcha = $this->request->post('captcha');
  79. $token = $this->request->post('__token__');
  80. $rule = [
  81. 'username' => 'require|length:3,30',
  82. 'password' => 'require|length:6,30',
  83. 'email' => 'require|email',
  84. 'mobile' => 'regex:/^1\d{10}$/',
  85. 'captcha' => 'require|captcha',
  86. '__token__' => 'token',
  87. ];
  88. $msg = [
  89. 'username.require' => 'Username can not be empty',
  90. 'username.length' => 'Username must be 3 to 30 characters',
  91. 'password.require' => 'Password can not be empty',
  92. 'password.length' => 'Password must be 6 to 30 characters',
  93. 'captcha.require' => 'Captcha can not be empty',
  94. 'captcha.captcha' => 'Captcha is incorrect',
  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. 'captcha' => $captcha,
  104. '__token__' => $token,
  105. ];
  106. $validate = new Validate($rule, $msg);
  107. $result = $validate->check($data);
  108. if (!$result) {
  109. $this->error(__($validate->getError()), null, ['token' => $this->request->token()]);
  110. }
  111. if ($this->auth->register($username, $password, $email, $mobile)) {
  112. $synchtml = '';
  113. ////////////////同步到Ucenter////////////////
  114. if (defined('UC_STATUS') && UC_STATUS) {
  115. $uc = new \addons\ucenter\library\client\Client();
  116. $synchtml = $uc->uc_user_synregister($this->auth->id, $password);
  117. }
  118. $this->success(__('Sign up successful') . $synchtml, $url ? $url : url('user/index'));
  119. } else {
  120. $this->error($this->auth->getError(), null, ['token' => $this->request->token()]);
  121. }
  122. }
  123. //判断来源
  124. $referer = $this->request->server('HTTP_REFERER');
  125. if (!$url && (strtolower(parse_url($referer, PHP_URL_HOST)) == strtolower($this->request->host()))
  126. && !preg_match("/(user\/login|user\/register)/i", $referer)) {
  127. $url = $referer;
  128. }
  129. $this->view->assign('url', $url);
  130. $this->view->assign('title', __('Register'));
  131. return $this->view->fetch();
  132. }
  133. /**
  134. * 会员登录
  135. */
  136. public function login()
  137. {
  138. $url = $this->request->request('url');
  139. if ($this->auth->id)
  140. $this->success(__('You\'ve logged in, do not login again'), $url);
  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__' => '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. $synchtml = '';
  170. ////////////////同步到Ucenter////////////////
  171. if (defined('UC_STATUS') && UC_STATUS) {
  172. $uc = new \addons\ucenter\library\client\Client();
  173. $synchtml = $uc->uc_user_synlogin($this->auth->id);
  174. }
  175. $this->success(__('Logged in successful') . $synchtml, $url ? $url : url('user/index'));
  176. } else {
  177. $this->error($this->auth->getError(), null, ['token' => $this->request->token()]);
  178. }
  179. }
  180. //判断来源
  181. $referer = $this->request->server('HTTP_REFERER');
  182. if (!$url && (strtolower(parse_url($referer, PHP_URL_HOST)) == strtolower($this->request->host()))
  183. && !preg_match("/(user\/login|user\/register)/i", $referer)) {
  184. $url = $referer;
  185. }
  186. $this->view->assign('url', $url);
  187. $this->view->assign('title', __('Login'));
  188. return $this->view->fetch();
  189. }
  190. /**
  191. * 注销登录
  192. */
  193. function logout()
  194. {
  195. //注销本站
  196. $this->auth->logout();
  197. $synchtml = '';
  198. ////////////////同步到Ucenter////////////////
  199. if (defined('UC_STATUS') && UC_STATUS) {
  200. $uc = new \addons\ucenter\library\client\Client();
  201. $synchtml = $uc->uc_user_synlogout();
  202. }
  203. $this->success(__('Logout successful') . $synchtml, 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. $synchtml = '';
  251. ////////////////同步到Ucenter////////////////
  252. if (defined('UC_STATUS') && UC_STATUS) {
  253. $uc = new \addons\ucenter\library\client\Client();
  254. $synchtml = $uc->uc_user_synlogout();
  255. }
  256. $this->success(__('Reset password successful') . $synchtml, url('user/login'));
  257. } else {
  258. $this->error($this->auth->getError(), null, ['token' => $this->request->token()]);
  259. }
  260. }
  261. $this->view->assign('title', __('Change password'));
  262. return $this->view->fetch();
  263. }
  264. }