User.php 10 KB

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