User.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595
  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 app\common\model\Order as OrderModel;
  7. use app\common\model\User as UserModel;
  8. use fast\Random;
  9. use think\Cache;
  10. use think\Config;
  11. use think\Db;
  12. use think\Env;
  13. use think\Exception;
  14. use think\Hook;
  15. use think\Validate;
  16. /**
  17. * 会员接口
  18. */
  19. class User extends Api
  20. {
  21. protected $noNeedLogin = ['login', 'register', 'find_pwd_send_sms', 'verify_code', 'reset_pwd'];
  22. protected $noNeedRight = '*';
  23. public function _initialize()
  24. {
  25. parent::_initialize();
  26. if (!Config::get('fastadmin.usercenter')) {
  27. $this->error(__('User center already closed'));
  28. }
  29. }
  30. /**
  31. * 会员中心
  32. */
  33. public function index()
  34. {
  35. $user = $this->auth->getUser();
  36. $data['code'] = $user['code'];//今日已做任务
  37. $data['mobile'] = $user['mobile'];
  38. $data['balance'] = $user['balance'];
  39. $data['avatar'] = $user['avatar'];
  40. $data['bonus_sum'] = $user['bonus_sum'];
  41. $data['order_num'] = (new OrderModel())
  42. ->where('user_id', $user['id'])
  43. ->where('status', OrderModel::STATUSFINISH)
  44. ->count();
  45. $data['invite_link'] = Env::get('app.invite_domain') . '/?i=' . $user['invitation_code'];
  46. $data['invitation_code'] = $user['invitation_code'];
  47. $this->success('', $data);
  48. }
  49. /**
  50. * 会员登录
  51. *
  52. * @ApiMethod (POST)
  53. * @param string $account 账号
  54. * @param string $password 密码
  55. */
  56. public function login()
  57. {
  58. $code = $this->request->post('code');
  59. $mobile = $this->request->post('mobile');
  60. $pwd = $this->request->post('pwd');
  61. if (!$code || !$mobile || !$pwd) {
  62. $this->error(__('Invalid parameters'));
  63. }
  64. $ret = $this->auth->login($code, $mobile, $pwd);
  65. if ($ret) {
  66. $data = ['userinfo' => $this->auth->getUserinfo()];
  67. $this->success(__('Logged in successful'), $data);
  68. } else {
  69. $this->error($this->auth->getError());
  70. }
  71. }
  72. /**
  73. * 重置密码
  74. *
  75. * @ApiMethod (POST)
  76. * @param string $mobile 手机号
  77. * @param string $newpassword 新密码
  78. * @param string $captcha 验证码
  79. */
  80. public function change_login_pwd()
  81. {
  82. $old_pwd = $this->request->post("old_pwd", '', null);
  83. $new_pwd = $this->request->post("new_pwd", '', null);
  84. $confirm_pwd = $this->request->post("confirm_pwd", '', null);
  85. $rule = [
  86. 'old_pwd' => 'require|regex:\S{6,30}',
  87. 'new_pwd' => 'require|regex:\S{6,30}',
  88. 'confirm_pwd' => 'require|regex:\S{6,30}|confirm:new_pwd',
  89. ];
  90. $msg = [
  91. 'new_pwd.confirm' => __('Password and confirm password don\'t match')
  92. ];
  93. $data = [
  94. 'old_pwd' => $old_pwd,
  95. 'new_pwd' => $new_pwd,
  96. 'confirm_pwd' => $confirm_pwd,
  97. ];
  98. $field = [
  99. 'old_pwd' => __('旧密码'),
  100. 'new_pwd' => __('新密码'),
  101. 'confirm_pwd' => __('新密码')
  102. ];
  103. $validate = new Validate($rule, $msg, $field);
  104. $result = $validate->check($data);
  105. if (!$result) {
  106. $this->error(__($validate->getError()));
  107. }
  108. $ret = $this->auth->changepwd($new_pwd, $old_pwd);
  109. if ($ret) {
  110. $this->success(__('Reset password successful'));
  111. } else {
  112. $this->error($this->auth->getError());
  113. }
  114. }
  115. /**
  116. * 发送重置密码短信验证码
  117. * @return void
  118. */
  119. public function reset_fund_pwd_sms()
  120. {
  121. $login_pwd = $this->request->post("login_pwd", '', null);
  122. $user = $this->auth->getUser();
  123. if ($user->login_pwd != $this->auth->getEncryptPassword($login_pwd, $user->salt)) {
  124. $this->error(__('Password is incorrect'));
  125. }
  126. (new \app\api\controller\Sms())->send_international_sms($user['code'], $user['mobile'], 'rest_fund_pwd');
  127. }
  128. /**
  129. * 重置资金密码
  130. *
  131. * @ApiMethod (POST)
  132. * @param string $mobile 手机号
  133. * @param string $newpassword 新密码
  134. * @param string $captcha 验证码
  135. */
  136. public function reset_fund_pwd()
  137. {
  138. $login_pwd = $this->request->post("login_pwd", '', null);
  139. $new_pwd = $this->request->post("new_pwd", '', null);
  140. $confirm_pwd = $this->request->post("confirm_pwd", '', null);
  141. $captcha = $this->request->post("captcha");
  142. $rule = [
  143. 'login_pwd' => 'require|regex:\S{6,30}',
  144. 'new_pwd' => 'require|regex:\S{6,30}',
  145. 'confirm_pwd' => 'require|regex:\S{6,30}|confirm:new_pwd',
  146. ];
  147. $msg = [
  148. 'new_pwd.confirm' => __('Password and confirm password don\'t match')
  149. ];
  150. $data = [
  151. 'login_pwd' => $login_pwd,
  152. 'new_pwd' => $new_pwd,
  153. 'confirm_pwd' => $confirm_pwd,
  154. ];
  155. $field = [
  156. 'login_pwd' => __('登录密码'),
  157. 'new_pwd' => __('新资金密码'),
  158. 'confirm_pwd' => __('新密码')
  159. ];
  160. $validate = new Validate($rule, $msg, $field);
  161. $result = $validate->check($data);
  162. if (!$result) {
  163. $this->error(__($validate->getError()));
  164. }
  165. if (empty($captcha)) {
  166. $this->error(__('Invalid parameters'));
  167. }
  168. $user = $this->auth->getUser();
  169. //验证手机验证码
  170. $ret = (new \app\api\controller\Sms())->check($user['code'],$user['mobile'], $captcha, 'rest_fund_pwd');
  171. if (!$ret) {
  172. $this->error(__('Captcha is incorrect'));
  173. }
  174. $res = (new \app\common\model\User())
  175. ->where('id', $user['id'])
  176. ->update([
  177. 'fund_pwd' => md5($new_pwd)
  178. ]);
  179. if($res){
  180. $this->success(__('资金密码重置成功'));
  181. }
  182. $this->error(__('资金密码重置失败'));
  183. }
  184. /**
  185. * 找回密码发送验证码
  186. * @return void
  187. * @throws \think\Exception
  188. */
  189. public function find_pwd_send_sms()
  190. {
  191. $code = $this->request->post('country_code');
  192. $mobile = $this->request->post('mobile');
  193. $captcha = $this->request->post('captcha');
  194. if(!captcha_check($captcha)){
  195. $this->error(__('验证码错误'));
  196. };
  197. $user = UserModel::getByCodeAndMobile($code,$mobile);
  198. if(empty($user)){
  199. $this->error(__('手机号码不存在'));
  200. }
  201. if($user['is_lock']){
  202. $this->error(__('账号锁定'));
  203. }
  204. (new \app\api\controller\Sms())->send_international_sms($user['code'], $user['mobile'], 'find_pwd');
  205. }
  206. public function verify_code()
  207. {
  208. $country_code = $this->request->post('country_code');
  209. $mobile = $this->request->post('mobile');
  210. $code = $this->request->post('code');
  211. $verify = (new \app\api\controller\Sms())->check($country_code, $mobile, $code, 'find_pwd');
  212. if($verify){
  213. $token = uniqid();
  214. Cache::set($token, ['code' => $country_code, 'mobile' => $mobile], 60 * 10);//临时token缓存10分钟
  215. $this->success('', $token);
  216. }
  217. $this->error(__('手机验证码错误'));
  218. }
  219. public function reset_pwd()
  220. {
  221. $token = $this->request->post('token');
  222. $new_pwd = $this->request->post("new_pwd", '', null);
  223. $confirm_pwd = $this->request->post("confirm_pwd", '', null);
  224. $rule = [
  225. 'new_pwd' => 'require|regex:\S{6,30}',
  226. 'confirm_pwd' => 'require|regex:\S{6,30}|confirm:new_pwd',
  227. ];
  228. $msg = [
  229. 'new_pwd.confirm' => __('Password and confirm password don\'t match')
  230. ];
  231. $data = [
  232. 'new_pwd' => $new_pwd,
  233. 'confirm_pwd' => $confirm_pwd,
  234. ];
  235. $field = [
  236. 'new_pwd' => __('新资金密码'),
  237. 'confirm_pwd' => __('新密码')
  238. ];
  239. $validate = new Validate($rule, $msg, $field);
  240. $result = $validate->check($data);
  241. if (!$result) {
  242. $this->error(__($validate->getError()));
  243. }
  244. $info = Cache::get($token);
  245. if(empty($info)){
  246. $this->error(__('操作超时'));
  247. }
  248. Db::startTrans();
  249. try {
  250. $salt = Random::alnum();
  251. $new_password = $this->auth->getEncryptPassword($new_pwd, $salt);
  252. UserModel::where($info)
  253. ->update([
  254. 'login_pwd' => $new_password,
  255. 'salt' => $salt
  256. ]);
  257. Db::commit();
  258. } catch (Exception $e) {
  259. Db::rollback();
  260. $this->error($e->getMessage());
  261. }
  262. Cache::rm($token);//删除缓存
  263. $this->success('');
  264. }
  265. /**
  266. * 手机验证码登录
  267. *
  268. * @ApiMethod (POST)
  269. * @param string $mobile 手机号
  270. * @param string $captcha 验证码
  271. */
  272. public function mobilelogin()
  273. {
  274. $mobile = $this->request->post('mobile');
  275. $captcha = $this->request->post('captcha');
  276. if (!$mobile || !$captcha) {
  277. $this->error(__('Invalid parameters'));
  278. }
  279. if (!Validate::regex($mobile, "^1\d{10}$")) {
  280. $this->error(__('Mobile is incorrect'));
  281. }
  282. if (!Sms::check($mobile, $captcha, 'mobilelogin')) {
  283. $this->error(__('Captcha is incorrect'));
  284. }
  285. $user = \app\common\model\User::getByMobile($mobile);
  286. if ($user) {
  287. if ($user->status != 'normal') {
  288. $this->error(__('Account is locked'));
  289. }
  290. //如果已经有账号则直接登录
  291. $ret = $this->auth->direct($user->id);
  292. } else {
  293. $ret = $this->auth->register($mobile, Random::alnum(), '', $mobile, []);
  294. }
  295. if ($ret) {
  296. Sms::flush($mobile, 'mobilelogin');
  297. $data = ['userinfo' => $this->auth->getUserinfo()];
  298. $this->success(__('Logged in successful'), $data);
  299. } else {
  300. $this->error($this->auth->getError());
  301. }
  302. }
  303. /**
  304. * 注册会员
  305. *
  306. * @ApiMethod (POST)
  307. * @param string $username 用户名
  308. * @param string $password 密码
  309. * @param string $email 邮箱
  310. * @param string $mobile 手机号
  311. * @param string $code 验证码
  312. */
  313. public function register()
  314. {
  315. //$username = $this->request->post('username');
  316. //$email = $this->request->post('email');
  317. $code = $this->request->post('code');
  318. $mobile = $this->request->post('mobile');
  319. $pwd = $this->request->post('pwd');
  320. $confirm_pwd = $this->request->post('confirm_pwd');
  321. $invitation_code = $this->request->post('invitation_code');
  322. if (!$code || !$mobile || !$pwd || !$confirm_pwd || !$invitation_code) {
  323. $this->error(__('Invalid parameters'));
  324. }
  325. if ($pwd != $confirm_pwd) {
  326. $this->error(__('确认密码不一致'));
  327. }
  328. // if ($email && !Validate::is($email, "email")) {
  329. // $this->error(__('Email is incorrect'));
  330. // }
  331. if ($mobile && !Validate::regex($mobile, "^\d{8,11}$")) {
  332. $this->error(__('Mobile is incorrect'));
  333. }
  334. // $ret = Sms::check($mobile, $code, 'register');
  335. // if (!$ret) {
  336. // $this->error(__('Captcha is incorrect'));
  337. // }
  338. $ret = $this->auth->register($code, $mobile, $pwd, $invitation_code);
  339. if ($ret) {
  340. $data = ['userinfo' => $this->auth->getUserinfo()];
  341. $this->success(__('Sign up successful'), $data);
  342. } else {
  343. $this->error($this->auth->getError());
  344. }
  345. }
  346. /**
  347. * 退出登录
  348. * @ApiMethod (POST)
  349. */
  350. public function logout()
  351. {
  352. if (!$this->request->isPost()) {
  353. $this->error(__('Invalid parameters'));
  354. }
  355. $this->auth->logout();
  356. $this->success(__('Logout successful'));
  357. }
  358. /**
  359. * 修改会员个人信息
  360. *
  361. * @ApiMethod (POST)
  362. * @param string $avatar 头像地址
  363. * @param string $username 用户名
  364. * @param string $nickname 昵称
  365. * @param string $bio 个人简介
  366. */
  367. public function profile()
  368. {
  369. $user = $this->auth->getUser();
  370. $username = $this->request->post('username');
  371. $nickname = $this->request->post('nickname');
  372. $bio = $this->request->post('bio');
  373. $avatar = $this->request->post('avatar', '', 'trim,strip_tags,htmlspecialchars');
  374. if ($username) {
  375. $exists = \app\common\model\User::where('username', $username)->where('id', '<>', $this->auth->id)->find();
  376. if ($exists) {
  377. $this->error(__('Username already exists'));
  378. }
  379. $user->username = $username;
  380. }
  381. if ($nickname) {
  382. $exists = \app\common\model\User::where('nickname', $nickname)->where('id', '<>', $this->auth->id)->find();
  383. if ($exists) {
  384. $this->error(__('Nickname already exists'));
  385. }
  386. $user->nickname = $nickname;
  387. }
  388. $user->bio = $bio;
  389. $user->avatar = $avatar;
  390. $user->save();
  391. $this->success();
  392. }
  393. /**
  394. * 修改邮箱
  395. *
  396. * @ApiMethod (POST)
  397. * @param string $email 邮箱
  398. * @param string $captcha 验证码
  399. */
  400. public function changeemail()
  401. {
  402. $user = $this->auth->getUser();
  403. $email = $this->request->post('email');
  404. $captcha = $this->request->post('captcha');
  405. if (!$email || !$captcha) {
  406. $this->error(__('Invalid parameters'));
  407. }
  408. if (!Validate::is($email, "email")) {
  409. $this->error(__('Email is incorrect'));
  410. }
  411. if (\app\common\model\User::where('email', $email)->where('id', '<>', $user->id)->find()) {
  412. $this->error(__('Email already exists'));
  413. }
  414. $result = Ems::check($email, $captcha, 'changeemail');
  415. if (!$result) {
  416. $this->error(__('Captcha is incorrect'));
  417. }
  418. $verification = $user->verification;
  419. $verification->email = 1;
  420. $user->verification = $verification;
  421. $user->email = $email;
  422. $user->save();
  423. Ems::flush($email, 'changeemail');
  424. $this->success();
  425. }
  426. /**
  427. * 修改手机号
  428. *
  429. * @ApiMethod (POST)
  430. * @param string $mobile 手机号
  431. * @param string $captcha 验证码
  432. */
  433. public function changemobile()
  434. {
  435. $user = $this->auth->getUser();
  436. $mobile = $this->request->post('mobile');
  437. $captcha = $this->request->post('captcha');
  438. if (!$mobile || !$captcha) {
  439. $this->error(__('Invalid parameters'));
  440. }
  441. if (!Validate::regex($mobile, "^1\d{10}$")) {
  442. $this->error(__('Mobile is incorrect'));
  443. }
  444. if (\app\common\model\User::where('mobile', $mobile)->where('id', '<>', $user->id)->find()) {
  445. $this->error(__('Mobile already exists'));
  446. }
  447. $result = Sms::check($mobile, $captcha, 'changemobile');
  448. if (!$result) {
  449. $this->error(__('Captcha is incorrect'));
  450. }
  451. $verification = $user->verification;
  452. $verification->mobile = 1;
  453. $user->verification = $verification;
  454. $user->mobile = $mobile;
  455. $user->save();
  456. Sms::flush($mobile, 'changemobile');
  457. $this->success();
  458. }
  459. /**
  460. * 第三方登录
  461. *
  462. * @ApiMethod (POST)
  463. * @param string $platform 平台名称
  464. * @param string $code Code码
  465. */
  466. public function third()
  467. {
  468. $url = url('user/index');
  469. $platform = $this->request->post("platform");
  470. $code = $this->request->post("code");
  471. $config = get_addon_config('third');
  472. if (!$config || !isset($config[$platform])) {
  473. $this->error(__('Invalid parameters'));
  474. }
  475. $app = new \addons\third\library\Application($config);
  476. //通过code换access_token和绑定会员
  477. $result = $app->{$platform}->getUserInfo(['code' => $code]);
  478. if ($result) {
  479. $loginret = \addons\third\library\Service::connect($platform, $result);
  480. if ($loginret) {
  481. $data = [
  482. 'userinfo' => $this->auth->getUserinfo(),
  483. 'thirdinfo' => $result
  484. ];
  485. $this->success(__('Logged in successful'), $data);
  486. }
  487. }
  488. $this->error(__('Operation failed'), $url);
  489. }
  490. /**
  491. * 重置密码
  492. *
  493. * @ApiMethod (POST)
  494. * @param string $mobile 手机号
  495. * @param string $newpassword 新密码
  496. * @param string $captcha 验证码
  497. */
  498. public function resetpwd()
  499. {
  500. $type = $this->request->post("type", "mobile");
  501. $mobile = $this->request->post("mobile");
  502. $email = $this->request->post("email");
  503. $newpassword = $this->request->post("newpassword");
  504. $captcha = $this->request->post("captcha");
  505. if (!$newpassword || !$captcha) {
  506. $this->error(__('Invalid parameters'));
  507. }
  508. //验证Token
  509. if (!Validate::make()->check(['newpassword' => $newpassword], ['newpassword' => 'require|regex:\S{6,30}'])) {
  510. $this->error(__('Password must be 6 to 30 characters'));
  511. }
  512. if ($type == 'mobile') {
  513. if (!Validate::regex($mobile, "^1\d{10}$")) {
  514. $this->error(__('Mobile is incorrect'));
  515. }
  516. $user = \app\common\model\User::getByMobile($mobile);
  517. if (!$user) {
  518. $this->error(__('User not found'));
  519. }
  520. $ret = Sms::check($mobile, $captcha, 'resetpwd');
  521. if (!$ret) {
  522. $this->error(__('Captcha is incorrect'));
  523. }
  524. Sms::flush($mobile, 'resetpwd');
  525. } else {
  526. if (!Validate::is($email, "email")) {
  527. $this->error(__('Email is incorrect'));
  528. }
  529. $user = \app\common\model\User::getByEmail($email);
  530. if (!$user) {
  531. $this->error(__('User not found'));
  532. }
  533. $ret = Ems::check($email, $captcha, 'resetpwd');
  534. if (!$ret) {
  535. $this->error(__('Captcha is incorrect'));
  536. }
  537. Ems::flush($email, 'resetpwd');
  538. }
  539. //模拟一次登录
  540. $this->auth->direct($user->id);
  541. $ret = $this->auth->changepwd($newpassword, '', true);
  542. if ($ret) {
  543. $this->success(__('Reset password successful'));
  544. } else {
  545. $this->error($this->auth->getError());
  546. }
  547. }
  548. }