User.php 12 KB

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