User.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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. $data = Hook::listen("user_request_empty", $name);
  55. foreach ($data as $index => $datum) {
  56. $this->view->assign($datum);
  57. }
  58. return $this->view->fetch('user/' . $name);
  59. }
  60. /**
  61. * 会员中心
  62. */
  63. public function index()
  64. {
  65. $this->view->assign('title', __('User center'));
  66. return $this->view->fetch();
  67. }
  68. /**
  69. * 注册会员
  70. */
  71. public function register()
  72. {
  73. $url = $this->request->request('url');
  74. if ($this->auth->id)
  75. $this->success(__('You\'ve logged in, do not login again'), $url);
  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. 'captcha' => 'require|captcha',
  89. '__token__' => 'token',
  90. ];
  91. $msg = [
  92. 'username.require' => 'Username can not be empty',
  93. 'username.length' => 'Username must be 3 to 30 characters',
  94. 'password.require' => 'Password can not be empty',
  95. 'password.length' => 'Password must be 6 to 30 characters',
  96. 'captcha.require' => 'Captcha can not be empty',
  97. 'captcha.captcha' => 'Captcha is incorrect',
  98. 'email' => 'Email is incorrect',
  99. 'mobile' => 'Mobile is incorrect',
  100. ];
  101. $data = [
  102. 'username' => $username,
  103. 'password' => $password,
  104. 'email' => $email,
  105. 'mobile' => $mobile,
  106. 'captcha' => $captcha,
  107. '__token__' => $token,
  108. ];
  109. $validate = new Validate($rule, $msg);
  110. $result = $validate->check($data);
  111. if (!$result) {
  112. $this->error(__($validate->getError()), null, ['token' => $this->request->token()]);
  113. }
  114. if ($this->auth->register($username, $password, $email, $mobile)) {
  115. $synchtml = '';
  116. ////////////////同步到Ucenter////////////////
  117. if (defined('UC_STATUS') && UC_STATUS) {
  118. $uc = new \addons\ucenter\library\client\Client();
  119. $synchtml = $uc->uc_user_synregister($this->auth->id, $password);
  120. }
  121. $this->success(__('Sign up successful') . $synchtml, $url ? $url : url('user/index'));
  122. } else {
  123. $this->error($this->auth->getError(), null, ['token' => $this->request->token()]);
  124. }
  125. }
  126. //判断来源
  127. $referer = $this->request->server('HTTP_REFERER');
  128. if (!$url && (strtolower(parse_url($referer, PHP_URL_HOST)) == strtolower($this->request->host()))
  129. && !preg_match("/(user\/login|user\/register)/i", $referer)) {
  130. $url = $referer;
  131. }
  132. $this->view->assign('url', $url);
  133. $this->view->assign('title', __('Register'));
  134. return $this->view->fetch();
  135. }
  136. /**
  137. * 会员登录
  138. */
  139. public function login()
  140. {
  141. $url = $this->request->request('url');
  142. if ($this->auth->id)
  143. $this->success(__('You\'ve logged in, do not login again'), $url);
  144. if ($this->request->isPost()) {
  145. $account = $this->request->post('account');
  146. $password = $this->request->post('password');
  147. $keeplogin = (int)$this->request->post('keeplogin');
  148. $token = $this->request->post('__token__');
  149. $rule = [
  150. 'account' => 'require|length:3,50',
  151. 'password' => 'require|length:6,30',
  152. '__token__' => 'token',
  153. ];
  154. $msg = [
  155. 'account.require' => 'Account can not be empty',
  156. 'account.length' => 'Account must be 3 to 50 characters',
  157. 'password.require' => 'Password can not be empty',
  158. 'password.length' => 'Password must be 6 to 30 characters',
  159. ];
  160. $data = [
  161. 'account' => $account,
  162. 'password' => $password,
  163. '__token__' => $token,
  164. ];
  165. $validate = new Validate($rule, $msg);
  166. $result = $validate->check($data);
  167. if (!$result) {
  168. $this->error(__($validate->getError()), null, ['token' => $this->request->token()]);
  169. return FALSE;
  170. }
  171. if ($this->auth->login($account, $password)) {
  172. $synchtml = '';
  173. ////////////////同步到Ucenter////////////////
  174. if (defined('UC_STATUS') && UC_STATUS) {
  175. $uc = new \addons\ucenter\library\client\Client();
  176. $synchtml = $uc->uc_user_synlogin($this->auth->id);
  177. }
  178. $this->success(__('Logged in successful') . $synchtml, $url ? $url : url('user/index'));
  179. } else {
  180. $this->error($this->auth->getError(), null, ['token' => $this->request->token()]);
  181. }
  182. }
  183. //判断来源
  184. $referer = $this->request->server('HTTP_REFERER');
  185. if (!$url && (strtolower(parse_url($referer, PHP_URL_HOST)) == strtolower($this->request->host()))
  186. && !preg_match("/(user\/login|user\/register)/i", $referer)) {
  187. $url = $referer;
  188. }
  189. $this->view->assign('url', $url);
  190. $this->view->assign('title', __('Login'));
  191. return $this->view->fetch();
  192. }
  193. /**
  194. * 注销登录
  195. */
  196. function logout()
  197. {
  198. //注销本站
  199. $this->auth->logout();
  200. $synchtml = '';
  201. ////////////////同步到Ucenter////////////////
  202. if (defined('UC_STATUS') && UC_STATUS) {
  203. $uc = new \addons\ucenter\library\client\Client();
  204. $synchtml = $uc->uc_user_synlogout();
  205. }
  206. $this->success(__('Logout successful') . $synchtml, url('user/index'));
  207. }
  208. /**
  209. * 个人信息
  210. */
  211. public function profile()
  212. {
  213. $this->view->assign('title', __('Profile'));
  214. return $this->view->fetch();
  215. }
  216. /**
  217. * 修改密码
  218. */
  219. public function changepwd()
  220. {
  221. if ($this->request->isPost()) {
  222. $oldpassword = $this->request->post("oldpassword");
  223. $newpassword = $this->request->post("newpassword");
  224. $renewpassword = $this->request->post("renewpassword");
  225. $token = $this->request->post('__token__');
  226. $rule = [
  227. 'oldpassword' => 'require|length:6,30',
  228. 'newpassword' => 'require|length:6,30',
  229. 'renewpassword' => 'require|length:6,30|confirm:newpassword',
  230. '__token__' => 'token',
  231. ];
  232. $msg = [
  233. ];
  234. $data = [
  235. 'oldpassword' => $oldpassword,
  236. 'newpassword' => $newpassword,
  237. 'renewpassword' => $renewpassword,
  238. '__token__' => $token,
  239. ];
  240. $field = [
  241. 'oldpassword' => __('Old password'),
  242. 'newpassword' => __('New password'),
  243. 'renewpassword' => __('Renew password')
  244. ];
  245. $validate = new Validate($rule, $msg, $field);
  246. $result = $validate->check($data);
  247. if (!$result) {
  248. $this->error(__($validate->getError()), null, ['token' => $this->request->token()]);
  249. return FALSE;
  250. }
  251. $ret = $this->auth->changepwd($newpassword, $oldpassword);
  252. if ($ret) {
  253. $synchtml = '';
  254. ////////////////同步到Ucenter////////////////
  255. if (defined('UC_STATUS') && UC_STATUS) {
  256. $uc = new \addons\ucenter\library\client\Client();
  257. $synchtml = $uc->uc_user_synlogout();
  258. }
  259. $this->success(__('Reset password successful') . $synchtml, url('user/login'));
  260. } else {
  261. $this->error($this->auth->getError(), null, ['token' => $this->request->token()]);
  262. }
  263. }
  264. $this->view->assign('title', __('Change password'));
  265. return $this->view->fetch();
  266. }
  267. }