User.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. <?php
  2. namespace app\index\controller;
  3. use app\common\controller\Frontend;
  4. use fast\Random;
  5. use fast\third\Application;
  6. use fast\ucenter\client\Client;
  7. use think\Config;
  8. use think\Cookie;
  9. use think\Loader;
  10. /**
  11. * 会员中心
  12. */
  13. class User extends Frontend
  14. {
  15. // 使用布局
  16. protected $layout = 'bootstrap';
  17. protected $noNeedLogin = ['*'];
  18. public function _initialize()
  19. {
  20. parent::_initialize();
  21. //导入UC常量
  22. Loader::import('uc', APP_PATH);
  23. }
  24. public function index()
  25. {
  26. return $this->view->fetch();
  27. }
  28. /**
  29. * 注册会员
  30. */
  31. public function register()
  32. {
  33. $url = $this->request->get('url', '/');
  34. if ($this->user->check())
  35. $this->error(__('You\'ve logged in, do not login again'), $url);
  36. if ($this->request->isPost())
  37. {
  38. $username = $this->request->post('username');
  39. $password = $this->request->post('password');
  40. $repassword = $password;
  41. $email = $this->request->post('email');
  42. $captcha = $this->request->post('captcha');
  43. if (!captcha_check($captcha))
  44. {
  45. $this->error(__('Captcha is incorrect'));
  46. }
  47. if ($this->user->register($username, $password, $email))
  48. {
  49. $synchtml = '';
  50. ////////////////同步到Ucenter////////////////
  51. if (defined('UC_STATUS') && UC_STATUS)
  52. {
  53. $uc = new Client();
  54. $synchtml = $uc->uc_user_synregister($this->user->id, $password);
  55. }
  56. $referer = Cookie::get('referer_url');
  57. $this->success(__('Sign up successful') . $synchtml, $url);
  58. }
  59. else
  60. {
  61. $this->error($this->user->getError());
  62. }
  63. }
  64. return $this->view->fetch();
  65. }
  66. /**
  67. * 会员登录
  68. */
  69. public function login()
  70. {
  71. $url = $this->request->get('url', '/');
  72. if ($this->user->check())
  73. $this->error(__('You\'ve logged in, do not login again'), $url);
  74. if ($this->request->isPost())
  75. {
  76. $account = $this->request->post('account');
  77. $password = $this->request->post('password');
  78. // $captcha = $this->request->post('captcha');
  79. // if (!captcha_check($captcha))
  80. // {
  81. // $this->error(__('Captcha is incorrect'));
  82. // }
  83. if ($this->user->login($account, $password))
  84. {
  85. $synchtml = '';
  86. ////////////////同步到Ucenter////////////////
  87. if (defined('UC_STATUS') && UC_STATUS)
  88. {
  89. $uc = new Client();
  90. $synchtml = $uc->uc_user_synlogin($this->user->id);
  91. }
  92. $this->success(__('Logged in successful') . $synchtml, $url);
  93. }
  94. else
  95. {
  96. $this->error($this->user->getError());
  97. }
  98. }
  99. return $this->view->fetch();
  100. }
  101. /**
  102. * 注销登录
  103. */
  104. function logout()
  105. {
  106. //注销本站
  107. $this->user->logout();
  108. $synchtml = '';
  109. ////////////////同步到Ucenter////////////////
  110. if (defined('UC_STATUS') && UC_STATUS)
  111. {
  112. $uc = new Client();
  113. $synchtml = $uc->uc_user_synlogout();
  114. }
  115. $this->success(__('Logout successful') . $synchtml, '/');
  116. }
  117. /**
  118. * 第三方登录跳转和回调处理
  119. */
  120. public function third()
  121. {
  122. $action = $this->request->param('action');
  123. $platform = $this->request->param('platform');
  124. $config = Config::get('third');
  125. if (!isset($config[$platform]))
  126. {
  127. $this->error(__('Invalid parameters'));
  128. return;
  129. }
  130. $thirdapp = new Application();
  131. if ($action == 'redirect')
  132. {
  133. // 跳转到登录授权页面
  134. $this->redirect($thirdapp->{$platform}->getAuthorizeUrl());
  135. }
  136. else if ($action == 'callback')
  137. {
  138. // 授权成功后的回调
  139. $result = $thirdapp->{$platform}->getUserInfo();
  140. if ($result)
  141. {
  142. $loginret = $this->user->connect($platform, $result);
  143. if ($loginret)
  144. {
  145. $synchtml = '';
  146. ////////////////同步到Ucenter////////////////
  147. if (defined('UC_STATUS') && UC_STATUS)
  148. {
  149. $uc = new Client();
  150. $synchtml = $uc->uc_user_synlogin($this->user->id);
  151. }
  152. $this->success(__('Logged in successful') . $synchtml, '/');
  153. return;
  154. }
  155. }
  156. $this->error(__('Operation failed'), 'user/login');
  157. }
  158. else
  159. {
  160. $this->error(__('Invalid parameters'));
  161. }
  162. return;
  163. }
  164. /**
  165. * 修改密码
  166. */
  167. public function changepwd()
  168. {
  169. if ($this->request->isPost())
  170. {
  171. $oldpassword = $this->request->post("oldpassword");
  172. $newpassword = $this->request->post("newpassword");
  173. //判断旧密码是否正确
  174. if ($this->user->password == $this->user->getEncryptPassword($oldpassword, $this->user->salt))
  175. {
  176. ////////////////同步到Ucenter////////////////
  177. if (defined('UC_STATUS') && UC_STATUS)
  178. {
  179. $uc = new Client();
  180. $ret = $uc->uc_user_edit($this->user->id, $this->user->username, $newpassword, $this->user->email, $this->user->mobile);
  181. // 如果小于0则说明发生错误
  182. if ($ret < 0)
  183. {
  184. $this->error(__('Change password failure'));
  185. }
  186. }
  187. $salt = Random::alnum();
  188. $newpassword = $this->user->getEncryptPassword($newpassword, $salt);
  189. $this->user->save(['password' => $newpassword, 'salt' => $salt]);
  190. $this->user->logout();
  191. $synchtml = '';
  192. ////////////////同步到Ucenter////////////////
  193. if (defined('UC_STATUS') && UC_STATUS)
  194. {
  195. $uc = new Client();
  196. $synchtml = $uc->uc_user_synlogout();
  197. }
  198. $this->success(__('Change password successful') . $synchtml, "user/login");
  199. }
  200. else
  201. {
  202. //旧密码不正确
  203. $this->error(__('Password is incorrect'));
  204. }
  205. }
  206. return $this->view->fetch();
  207. }
  208. }