User.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use app\common\library\Ems;
  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. * @param string $captcha 验证码
  171. */
  172. public function changeemail()
  173. {
  174. $user = $this->auth->getUser();
  175. $email = $this->request->post('email');
  176. $captcha = $this->request->request('captcha');
  177. if (!$email || !$captcha)
  178. {
  179. $this->error(__('Invalid parameters'));
  180. }
  181. if (!Validate::is($email, "email"))
  182. {
  183. $this->error(__('Email is incorrect'));
  184. }
  185. if (\app\common\model\User::where('email', $email)->where('id', '<>', $user->id)->find())
  186. {
  187. $this->error(__('Email already exists'));
  188. }
  189. $result = Ems::check($email, $captcha, 'changeemail');
  190. if (!$result)
  191. {
  192. $this->error(__('Captcha is incorrect'));
  193. }
  194. $verification = $user->verification;
  195. $verification->email = 1;
  196. $user->verification = $verification;
  197. $user->email = $email;
  198. $user->save();
  199. Ems::flush($email, 'changeemail');
  200. $this->success();
  201. }
  202. /**
  203. * 修改手机号
  204. *
  205. * @param string $email 手机号
  206. * @param string $captcha 验证码
  207. */
  208. public function changemobile()
  209. {
  210. $user = $this->auth->getUser();
  211. $mobile = $this->request->request('mobile');
  212. $captcha = $this->request->request('captcha');
  213. if (!$mobile || !$captcha)
  214. {
  215. $this->error(__('Invalid parameters'));
  216. }
  217. if (!Validate::regex($mobile, "^1\d{10}$"))
  218. {
  219. $this->error(__('Mobile is incorrect'));
  220. }
  221. if (\app\common\model\User::where('mobile', $mobile)->where('id', '<>', $user->id)->find())
  222. {
  223. $this->error(__('Mobile already exists'));
  224. }
  225. $result = Sms::check($mobile, $captcha, 'changemobile');
  226. if (!$result)
  227. {
  228. $this->error(__('Captcha is incorrect'));
  229. }
  230. $verification = $user->verification;
  231. $verification->mobile = 1;
  232. $user->verification = $verification;
  233. $user->mobile = $mobile;
  234. $user->save();
  235. Sms::flush($mobile, 'changemobile');
  236. $this->success();
  237. }
  238. /**
  239. * 第三方登录
  240. *
  241. * @param string $platform 平台名称
  242. * @param string $code Code码
  243. */
  244. public function third()
  245. {
  246. $url = url('user/index');
  247. $platform = $this->request->request("platform");
  248. $code = $this->request->request("code");
  249. $config = get_addon_config('third');
  250. if (!$config || !isset($config[$platform]))
  251. {
  252. $this->error(__('Invalid parameters'));
  253. }
  254. $app = new \addons\third\library\Application($config);
  255. //通过code换access_token和绑定会员
  256. $result = $app->{$platform}->getUserInfo(['code' => $code]);
  257. if ($result)
  258. {
  259. $loginret = \addons\third\library\Service::connect($platform, $result);
  260. if ($loginret)
  261. {
  262. $data = [
  263. 'userinfo' => $this->auth->getUserinfo(),
  264. 'thirdinfo' => $result
  265. ];
  266. $this->success(__('Logged in successful'), $data);
  267. }
  268. }
  269. $this->error(__('Operation failed'), $url);
  270. }
  271. /**
  272. * 重置密码
  273. *
  274. * @param string $mobile 手机号
  275. * @param string $newpassword 新密码
  276. * @param string $captcha 验证码
  277. */
  278. public function resetpwd()
  279. {
  280. $type = $this->request->request("type");
  281. $mobile = $this->request->request("mobile");
  282. $email = $this->request->request("email");
  283. $newpassword = $this->request->request("newpassword");
  284. $captcha = $this->request->request("captcha");
  285. if (!$newpassword || !$captcha)
  286. {
  287. $this->error(__('Invalid parameters'));
  288. }
  289. if ($type == 'mobile')
  290. {
  291. if (!Validate::regex($mobile, "^1\d{10}$"))
  292. {
  293. $this->error(__('Mobile is incorrect'));
  294. }
  295. $user = \app\common\model\User::getByMobile($mobile);
  296. if (!$user)
  297. {
  298. $this->error(__('User not found'));
  299. }
  300. $ret = Sms::check($mobile, $captcha, 'resetpwd');
  301. if (!$ret)
  302. {
  303. $this->error(__('Captcha is incorrect'));
  304. }
  305. Sms::flush($mobile, 'resetpwd');
  306. }
  307. else
  308. {
  309. if (!Validate::is($email, "email"))
  310. {
  311. $this->error(__('Email is incorrect'));
  312. }
  313. $user = \app\common\model\User::getByEmail($email);
  314. if (!$user)
  315. {
  316. $this->error(__('User not found'));
  317. }
  318. $ret = Ems::check($email, $captcha, 'resetpwd');
  319. if (!$ret)
  320. {
  321. $this->error(__('Captcha is incorrect'));
  322. }
  323. Ems::flush($email, 'resetpwd');
  324. }
  325. //模拟一次登录
  326. $this->auth->direct($user->id);
  327. $ret = $this->auth->changepwd($newpassword, '', true);
  328. if ($ret)
  329. {
  330. $this->success(__('Reset password successful'));
  331. }
  332. else
  333. {
  334. $this->error($this->auth->getError());
  335. }
  336. }
  337. }