User.php 19 KB

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