User.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use app\common\library\Email;
  5. use app\common\library\Sms;
  6. use fast\Random;
  7. use think\Validate;
  8. /**
  9. * 会员接口
  10. */
  11. class User extends Api
  12. {
  13. protected $noNeedLogin = ['login', 'mobilelogin', 'register', 'resetpwd', 'changeemail', 'changemobile', 'third'];
  14. protected $noNeedRight = '*';
  15. public function _initialize()
  16. {
  17. parent::_initialize();
  18. }
  19. /**
  20. * 会员中心
  21. */
  22. public function index()
  23. {
  24. $this->success('', ['welcome' => $this->auth->nickname]);
  25. }
  26. /**
  27. * 会员登录
  28. *
  29. * @param string $account 账号
  30. * @param string $password 密码
  31. */
  32. public function login()
  33. {
  34. $account = $this->request->request('account');
  35. $password = $this->request->request('password');
  36. if (!$account || !$password)
  37. {
  38. $this->error(__('Invalid parameters'));
  39. }
  40. $ret = $this->auth->login($account, $password);
  41. if ($ret)
  42. {
  43. $data = ['userinfo' => $this->auth->getUserinfo()];
  44. $this->success(__('Logged in successful'), $data);
  45. }
  46. else
  47. {
  48. $this->error($this->auth->getError());
  49. }
  50. }
  51. /**
  52. * 手机验证码登录
  53. *
  54. * @param string $mobile 手机号
  55. * @param string $captcha 验证码
  56. */
  57. public function mobilelogin()
  58. {
  59. $mobile = $this->request->request('mobile');
  60. $captcha = $this->request->request('captcha');
  61. if (!$mobile || !$captcha)
  62. {
  63. $this->error(__('Invalid parameters'));
  64. }
  65. if (!Validate::regex($mobile, "^1\d{10}$"))
  66. {
  67. $this->error(__('Mobile is incorrect'));
  68. }
  69. if (!Sms::check($mobile, $captcha, 'mobilelogin'))
  70. {
  71. $this->error(__('Captcha is incorrect'));
  72. }
  73. $user = \app\common\model\User::getByMobile($mobile);
  74. if ($user)
  75. {
  76. //如果已经有账号则直接登录
  77. $ret = $this->auth->direct($user->id);
  78. }
  79. else
  80. {
  81. $ret = $this->auth->register($mobile, Random::alnum(), '', $mobile, []);
  82. }
  83. if ($ret)
  84. {
  85. Sms::flush($mobile, 'mobilelogin');
  86. $data = ['userinfo' => $this->auth->getUserinfo()];
  87. $this->success(__('Logged in successful'), $data);
  88. }
  89. else
  90. {
  91. $this->error($this->auth->getError());
  92. }
  93. }
  94. /**
  95. * 注册会员
  96. *
  97. * @param string $username 用户名
  98. * @param string $password 密码
  99. * @param string $email 邮箱
  100. * @param string $mobile 手机号
  101. */
  102. public function register()
  103. {
  104. $username = $this->request->request('username');
  105. $password = $this->request->request('password');
  106. $email = $this->request->request('email');
  107. $mobile = $this->request->request('mobile');
  108. if (!$username || !$password)
  109. {
  110. $this->error(__('Invalid parameters'));
  111. }
  112. if ($email && !Validate::is($email, "email"))
  113. {
  114. $this->error(__('Email is incorrect'));
  115. }
  116. if ($mobile && !Validate::regex($mobile, "^1\d{10}$"))
  117. {
  118. $this->error(__('Mobile is incorrect'));
  119. }
  120. $ret = $this->auth->register($username, $password, $email, $mobile, []);
  121. if ($ret)
  122. {
  123. $data = ['userinfo' => $this->auth->getUserinfo()];
  124. $this->success(__('Sign up successful'), $data);
  125. }
  126. else
  127. {
  128. $this->error($this->auth->getError());
  129. }
  130. }
  131. /**
  132. * 注销登录
  133. */
  134. public function logout()
  135. {
  136. $this->auth->logout();
  137. $this->success(__('Logout successful'));
  138. }
  139. /**
  140. * 修改会员个人信息
  141. *
  142. * @param string $avatar 头像地址
  143. * @param string $username 用户名
  144. * @param string $nickname 昵称
  145. * @param string $bio 个人简介
  146. */
  147. public function profile()
  148. {
  149. $user = $this->auth->getUser();
  150. $username = $this->request->request('username');
  151. $nickname = $this->request->request('nickname');
  152. $bio = $this->request->request('bio');
  153. $avatar = $this->request->request('avatar');
  154. $exists = \app\common\model\User::where('username', $username)->where('id', '<>', $this->auth->id)->find();
  155. if ($exists)
  156. {
  157. $this->error(__('Username already exists'));
  158. }
  159. $user->username = $username;
  160. $user->nickname = $nickname;
  161. $user->bio = $bio;
  162. $user->avatar = $avatar;
  163. $user->save();
  164. $this->success();
  165. }
  166. /**
  167. * 修改邮箱
  168. *
  169. * @param string $email 邮箱
  170. */
  171. public function changeemail()
  172. {
  173. $user = $this->auth->getUser();
  174. $email = $this->request->post('email');
  175. if (!$email)
  176. {
  177. $this->error(__('Invalid parameters'));
  178. }
  179. if (!Validate::is($email, "email"))
  180. {
  181. $this->error(__('Email is incorrect'));
  182. }
  183. if (\app\common\model\User::where('email', $email)->where('id', '<>', $user->id)->find())
  184. {
  185. $this->error(__('Email already exists'));
  186. }
  187. $verification = $user->verification;
  188. $verification->email = 0;
  189. $user->verification = $verification;
  190. $user->email = $email;
  191. $user->save();
  192. $time = time();
  193. $code = ['id' => $user->id, 'time' => $time, 'key' => md5(md5($user->id . $user->email . $time) . $user->salt)];
  194. $code = base64_encode(http_build_query($code));
  195. $url = url("index/user/activeemail", ['code' => $code], true, true);
  196. $message = __('Verify email') . ":<a href='{$url}'>{$url}</a>";
  197. Email::instance()->to($email)->subject(__('Verify email'))->message($message)->send();
  198. $this->success();
  199. }
  200. /**
  201. * 修改手机号
  202. *
  203. * @param string $email 手机号
  204. * @param string $captcha 验证码
  205. */
  206. public function changemobile()
  207. {
  208. $user = $this->auth->getUser();
  209. $mobile = $this->request->request('mobile');
  210. $captcha = $this->request->request('captcha');
  211. if (!$mobile || !$captcha)
  212. {
  213. $this->error(__('Invalid parameters'));
  214. }
  215. if (!Validate::regex($mobile, "^1\d{10}$"))
  216. {
  217. $this->error(__('Mobile is incorrect'));
  218. }
  219. if (\app\common\model\User::where('mobile', $mobile)->where('id', '<>', $user->id)->find())
  220. {
  221. $this->error(__('Mobile already exists'));
  222. }
  223. $result = Sms::check($mobile, $captcha, 'changemobile');
  224. if (!$result)
  225. {
  226. $this->error(__('Captcha is incorrect'));
  227. }
  228. $verification = $user->verification;
  229. $verification->mobile = 1;
  230. $user->verification = $verification;
  231. $user->mobile = $mobile;
  232. $user->save();
  233. Sms::flush($mobile, 'changemobile');
  234. $this->success();
  235. }
  236. /**
  237. * 第三方登录
  238. *
  239. * @param string $platform 平台名称
  240. * @param string $code Code码
  241. */
  242. public function third()
  243. {
  244. $url = url('user/index');
  245. $platform = $this->request->request("platform");
  246. $code = $this->request->request("code");
  247. $config = get_addon_config('third');
  248. if (!$config || !isset($config[$platform]))
  249. {
  250. $this->error(__('Invalid parameters'));
  251. }
  252. $app = new \addons\third\library\Application($config);
  253. //通过code换access_token和绑定会员
  254. $result = $app->{$platform}->getUserInfo(['code' => $code]);
  255. if ($result)
  256. {
  257. $loginret = \addons\third\library\Service::connect($platform, $result);
  258. if ($loginret)
  259. {
  260. $data = [
  261. 'userinfo' => $this->auth->getUserinfo(),
  262. 'thirdinfo' => $result
  263. ];
  264. $this->success(__('Logged in successful'), $data);
  265. }
  266. }
  267. $this->error(__('Operation failed'), $url);
  268. }
  269. /**
  270. * 重置密码
  271. *
  272. * @param string $mobile 手机号
  273. * @param string $newpassword 新密码
  274. * @param string $captcha 验证码
  275. */
  276. public function resetpwd()
  277. {
  278. $mobile = $this->request->request("mobile");
  279. $newpassword = $this->request->request("newpassword");
  280. $captcha = $this->request->request("captcha");
  281. if (!$mobile || !$newpassword || !$captcha)
  282. {
  283. $this->error(__('Invalid parameters'));
  284. }
  285. if ($mobile && !Validate::regex($mobile, "^1\d{10}$"))
  286. {
  287. $this->error(__('Mobile is incorrect'));
  288. }
  289. $user = \app\common\model\User::getByMobile($mobile);
  290. if (!$user)
  291. {
  292. $this->error(__('User not found'));
  293. }
  294. $ret = Sms::check($mobile, $captcha, 'resetpwd');
  295. if (!$ret)
  296. {
  297. $this->error(__('Captcha is incorrect'));
  298. }
  299. Sms::flush($mobile, 'resetpwd');
  300. //模拟一次登录
  301. $this->auth->direct($user->id);
  302. $ret = $this->auth->changepwd($newpassword, '', true);
  303. if ($ret)
  304. {
  305. $this->success(__('Reset password successful'));
  306. }
  307. else
  308. {
  309. $this->error($this->auth->getError());
  310. }
  311. }
  312. }