User.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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. //监听注册登录注销的事件
  21. Hook::add('user_login_successed', function($user) use($auth) {
  22. Cookie::set('uid', $user->id);
  23. Cookie::set('token', $auth->getToken());
  24. });
  25. Hook::add('user_register_successed', function($user) use($auth) {
  26. Cookie::set('uid', $user->id);
  27. Cookie::set('token', $auth->getToken());
  28. });
  29. Hook::add('user_delete_successed', function($user) use($auth) {
  30. Cookie::delete('uid');
  31. Cookie::delete('token');
  32. });
  33. Hook::add('user_logout_successed', function($user) use($auth) {
  34. Cookie::delete('uid');
  35. Cookie::delete('token');
  36. });
  37. }
  38. /**
  39. * 会员中心
  40. */
  41. public function index()
  42. {
  43. $this->view->assign('title', __('User center'));
  44. return $this->view->fetch();
  45. }
  46. /**
  47. * 注册会员
  48. */
  49. public function register()
  50. {
  51. $url = $this->request->request('url', url('user/index'));
  52. if ($this->auth->id)
  53. $this->success(__('You\'ve logged in, do not login again'), $url);
  54. if ($this->request->isPost())
  55. {
  56. $username = $this->request->post('username');
  57. $password = $this->request->post('password');
  58. $email = $this->request->post('email');
  59. $mobile = $this->request->post('mobile', '');
  60. $captcha = $this->request->post('captcha');
  61. $token = $this->request->post('__token__');
  62. $rule = [
  63. 'username' => 'require|length:3,30',
  64. 'password' => 'require|length:6,30',
  65. 'email' => 'require|email',
  66. 'mobile' => 'regex:/^1\d{10}$/',
  67. 'captcha' => 'require|captcha',
  68. '__token__' => 'token',
  69. ];
  70. $msg = [
  71. 'username.require' => 'Username can not be empty',
  72. 'username.length' => 'Username must be 3 to 30 characters',
  73. 'password.require' => 'Password can not be empty',
  74. 'password.length' => 'Password must be 6 to 30 characters',
  75. 'captcha.require' => 'Captcha can not be empty',
  76. 'captcha.captcha' => 'Captcha is incorrect',
  77. 'email' => 'Email is incorrect',
  78. 'mobile' => 'Mobile is incorrect',
  79. ];
  80. $data = [
  81. 'username' => $username,
  82. 'password' => $password,
  83. 'email' => $email,
  84. 'mobile' => $mobile,
  85. 'captcha' => $captcha,
  86. '__token__' => $token,
  87. ];
  88. $validate = new Validate($rule, $msg);
  89. $result = $validate->check($data);
  90. if (!$result)
  91. {
  92. $this->error(__($validate->getError()));
  93. }
  94. if ($this->auth->register($username, $password, $email, $mobile))
  95. {
  96. $synchtml = '';
  97. ////////////////同步到Ucenter////////////////
  98. if (defined('UC_STATUS') && UC_STATUS)
  99. {
  100. $uc = new \addons\ucenter\library\client\Client();
  101. $synchtml = $uc->uc_user_synregister($this->auth->id, $password);
  102. }
  103. $referer = Cookie::get('referer_url');
  104. $this->success(__('Sign up successful') . $synchtml, $referer);
  105. }
  106. else
  107. {
  108. $this->error($this->auth->getError());
  109. }
  110. }
  111. Session::set('redirect_url', $url);
  112. $this->view->assign('title', __('Register'));
  113. return $this->view->fetch();
  114. }
  115. /**
  116. * 会员登录
  117. */
  118. public function login()
  119. {
  120. $url = $this->request->request('url', url('user/index'));
  121. if ($this->auth->id)
  122. $this->success(__('You\'ve logged in, do not login again'), $url);
  123. if ($this->request->isPost())
  124. {
  125. $account = $this->request->post('account');
  126. $password = $this->request->post('password');
  127. $keeptime = (int) $this->request->post('keeptime');
  128. $token = $this->request->post('__token__');
  129. $rule = [
  130. 'account' => 'require|length:3,50',
  131. 'password' => 'require|length:6,30',
  132. '__token__' => 'token',
  133. ];
  134. $msg = [
  135. 'account.require' => 'Account can not be empty',
  136. 'account.length' => 'Account must be 3 to 50 characters',
  137. 'password.require' => 'Password can not be empty',
  138. 'password.length' => 'Password must be 6 to 30 characters',
  139. ];
  140. $data = [
  141. 'account' => $account,
  142. 'password' => $password,
  143. '__token__' => $token,
  144. ];
  145. $validate = new Validate($rule, $msg);
  146. $result = $validate->check($data);
  147. if (!$result)
  148. {
  149. $this->error(__($validate->getError()));
  150. return FALSE;
  151. }
  152. if ($this->auth->login($account, $password, $keeptime))
  153. {
  154. $synchtml = '';
  155. ////////////////同步到Ucenter////////////////
  156. if (defined('UC_STATUS') && UC_STATUS)
  157. {
  158. $uc = new \addons\ucenter\library\client\Client();
  159. $synchtml = $uc->uc_user_synlogin($this->auth->id);
  160. }
  161. $this->success(__('Logged in successful') . $synchtml, $url);
  162. }
  163. else
  164. {
  165. $this->error($this->auth->getError());
  166. }
  167. }
  168. $this->view->assign('title', __('Login'));
  169. return $this->view->fetch();
  170. }
  171. /**
  172. * 注销登录
  173. */
  174. function logout()
  175. {
  176. //注销本站
  177. $this->auth->logout();
  178. $synchtml = '';
  179. ////////////////同步到Ucenter////////////////
  180. if (defined('UC_STATUS') && UC_STATUS)
  181. {
  182. $uc = new \addons\ucenter\library\client\Client();
  183. $synchtml = $uc->uc_user_synlogout();
  184. }
  185. $this->success(__('Logout successful') . $synchtml, url('user/index'));
  186. }
  187. /**
  188. * 第三方登录跳转和回调处理
  189. */
  190. public function third()
  191. {
  192. $url = url('user/index');
  193. $action = $this->request->param('action');
  194. $platform = $this->request->param('platform');
  195. $config = get_addon_config('third');
  196. if (!$config || !isset($config[$platform]))
  197. {
  198. $this->error(__('Invalid parameters'));
  199. }
  200. foreach ($config as $k => &$v)
  201. {
  202. $v['callback'] = url('user/third', ['action' => 'callback', 'platform' => $k], false, true);
  203. }
  204. unset($v);
  205. $app = new \addons\third\library\Application($config);
  206. if ($action == 'redirect')
  207. {
  208. // 跳转到登录授权页面
  209. $this->redirect($app->{$platform}->getAuthorizeUrl());
  210. }
  211. else if ($action == 'callback')
  212. {
  213. // 授权成功后的回调
  214. $result = $app->{$platform}->getUserInfo();
  215. if ($result)
  216. {
  217. $loginret = \addons\third\library\Service::connect($platform, $result);
  218. if ($loginret)
  219. {
  220. $synchtml = '';
  221. ////////////////同步到Ucenter////////////////
  222. if (defined('UC_STATUS') && UC_STATUS)
  223. {
  224. $uc = new \addons\ucenter\library\client\Client();
  225. $synchtml = $uc->uc_user_synlogin($this->auth->id);
  226. }
  227. $this->success(__('Logged in successful') . $synchtml, $url);
  228. }
  229. }
  230. $this->error(__('Operation failed'), $url);
  231. }
  232. else
  233. {
  234. $this->error(__('Invalid parameters'));
  235. }
  236. }
  237. /**
  238. * 个人信息
  239. */
  240. public function profile()
  241. {
  242. $this->view->assign('title', __('Profile'));
  243. return $this->view->fetch();
  244. }
  245. /**
  246. * 激活邮箱
  247. */
  248. public function activeemail()
  249. {
  250. $code = $this->request->request('code');
  251. $code = base64_decode($code);
  252. parse_str($code, $params);
  253. if (!isset($params['id']) || !isset($params['time']) || !isset($params['key']))
  254. {
  255. $this->error(__('Invalid parameters'));
  256. }
  257. $user = \app\common\model\User::get($params['id']);
  258. if (!$user)
  259. {
  260. $this->error(__('User not found'));
  261. }
  262. if ($user->verification->email)
  263. {
  264. $this->error(__('Email already activation'));
  265. }
  266. if ($key !== md5(md5($user->id . $user->email . $time) . $user->salt) || time() - $params['time'] > 1800)
  267. {
  268. $this->error(__('Secrity code already invalid'));
  269. }
  270. $verification = $user->verification;
  271. $verification->email = 1;
  272. $user->verification = $verification;
  273. $user->save();
  274. $this->success(__('Active email successful'), url('user/index'));
  275. return;
  276. }
  277. /**
  278. * 修改密码
  279. */
  280. public function changepwd()
  281. {
  282. if ($this->request->isPost())
  283. {
  284. $oldpassword = $this->request->post("oldpassword");
  285. $newpassword = $this->request->post("newpassword");
  286. $renewpassword = $this->request->post("renewpassword");
  287. $token = $this->request->post('__token__');
  288. $rule = [
  289. 'oldpassword' => 'require|length:6,30',
  290. 'newpassword' => 'require|length:6,30',
  291. 'renewpassword' => 'require|length:6,30|confirm:newpassword',
  292. '__token__' => 'token',
  293. ];
  294. $msg = [
  295. ];
  296. $data = [
  297. 'oldpassword' => $oldpassword,
  298. 'newpassword' => $newpassword,
  299. 'renewpassword' => $renewpassword,
  300. '__token__' => $token,
  301. ];
  302. $field = [
  303. 'oldpassword' => __('Old password'),
  304. 'newpassword' => __('New password'),
  305. 'renewpassword' => __('Renew password')
  306. ];
  307. $validate = new Validate($rule, $msg, $field);
  308. $result = $validate->check($data);
  309. if (!$result)
  310. {
  311. $this->error(__($validate->getError()));
  312. return FALSE;
  313. }
  314. $ret = $this->auth->changepwd($newpassword, $oldpassword);
  315. if ($ret)
  316. {
  317. $synchtml = '';
  318. ////////////////同步到Ucenter////////////////
  319. if (defined('UC_STATUS') && UC_STATUS)
  320. {
  321. $uc = new \addons\ucenter\library\client\Client();
  322. $synchtml = $uc->uc_user_synlogout();
  323. }
  324. $this->success(__('Reset password successful') . $synchtml, url('user/login'));
  325. }
  326. else
  327. {
  328. $this->error($this->auth->getError());
  329. }
  330. }
  331. $this->view->assign('title', __('Change password'));
  332. return $this->view->fetch();
  333. }
  334. }