User.php 6.9 KB

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