User.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  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::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\Users())
  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. $token = $this->request->post('token');
  195. if(!captcha_check($captcha,$token)){
  196. $this->error(__('验证码错误'));
  197. };
  198. $user = UserModel::getByCodeAndMobile($code,$mobile);
  199. if(empty($user)){
  200. $this->error(__('手机号码不存在'));
  201. }
  202. if($user['is_lock']){
  203. $this->error(__('账号锁定'));
  204. }
  205. (new \app\api\controller\Sms())->send_international_sms($user['code'], $user['mobile'], 'find_pwd');
  206. }
  207. public function verify_code()
  208. {
  209. $country_code = $this->request->post('country_code');
  210. $mobile = $this->request->post('mobile');
  211. $code = $this->request->post('code');
  212. $verify = (new \app\api\controller\Sms())->check($country_code, $mobile, $code, 'find_pwd');
  213. if($verify){
  214. $token = uniqid();
  215. Cache::set($token, ['code' => $country_code, 'mobile' => $mobile], 60 * 10);//临时token缓存10分钟
  216. $this->success('', $token);
  217. }
  218. $this->error(__('手机验证码错误'));
  219. }
  220. public function reset_pwd()
  221. {
  222. $token = $this->request->post('token');
  223. $new_pwd = $this->request->post("new_pwd", '', null);
  224. $confirm_pwd = $this->request->post("confirm_pwd", '', null);
  225. $rule = [
  226. 'new_pwd' => 'require|regex:\S{6,30}',
  227. 'confirm_pwd' => 'require|regex:\S{6,30}|confirm:new_pwd',
  228. ];
  229. $msg = [
  230. 'new_pwd.confirm' => __('Password and confirm password don\'t match')
  231. ];
  232. $data = [
  233. 'new_pwd' => $new_pwd,
  234. 'confirm_pwd' => $confirm_pwd,
  235. ];
  236. $field = [
  237. 'new_pwd' => __('新资金密码'),
  238. 'confirm_pwd' => __('新密码')
  239. ];
  240. $validate = new Validate($rule, $msg, $field);
  241. $result = $validate->check($data);
  242. if (!$result) {
  243. $this->error(__($validate->getError()));
  244. }
  245. $info = Cache::get($token);
  246. if(empty($info)){
  247. $this->error(__('操作超时'));
  248. }
  249. Db::startTrans();
  250. try {
  251. $salt = Random::alnum();
  252. $new_password = $this->auth->getEncryptPassword($new_pwd, $salt);
  253. UserModel::where($info)
  254. ->update([
  255. 'login_pwd' => $new_password,
  256. 'salt' => $salt
  257. ]);
  258. Db::commit();
  259. } catch (Exception $e) {
  260. Db::rollback();
  261. $this->error($e->getMessage());
  262. }
  263. Cache::rm($token);//删除缓存
  264. $this->success('');
  265. }
  266. /**
  267. * 手机验证码登录
  268. *
  269. * @ApiMethod (POST)
  270. * @param string $mobile 手机号
  271. * @param string $captcha 验证码
  272. */
  273. public function mobilelogin()
  274. {
  275. $mobile = $this->request->post('mobile');
  276. $captcha = $this->request->post('captcha');
  277. if (!$mobile || !$captcha) {
  278. $this->error(__('Invalid parameters'));
  279. }
  280. if (!Validate::regex($mobile, "^1\d{10}$")) {
  281. $this->error(__('Mobile is incorrect'));
  282. }
  283. if (!Sms::check($mobile, $captcha, 'mobilelogin')) {
  284. $this->error(__('Captcha is incorrect'));
  285. }
  286. $user = \app\common\model\Users::getByMobile($mobile);
  287. if ($user) {
  288. if ($user->status != 'normal') {
  289. $this->error(__('Account is locked'));
  290. }
  291. //如果已经有账号则直接登录
  292. $ret = $this->auth->direct($user->id);
  293. } else {
  294. $ret = $this->auth->register($mobile, Random::alnum(), '', $mobile, []);
  295. }
  296. if ($ret) {
  297. Sms::flush($mobile, 'mobilelogin');
  298. $data = ['userinfo' => $this->auth->getUserinfo()];
  299. $this->success(__('Logged in successful'), $data);
  300. } else {
  301. $this->error($this->auth->getError());
  302. }
  303. }
  304. /**
  305. * 注册会员
  306. *
  307. * @ApiMethod (POST)
  308. * @param string $username 用户名
  309. * @param string $password 密码
  310. * @param string $email 邮箱
  311. * @param string $mobile 手机号
  312. * @param string $code 验证码
  313. */
  314. public function register()
  315. {
  316. //$username = $this->request->post('username');
  317. //$email = $this->request->post('email');
  318. $code = $this->request->post('code');
  319. $mobile = $this->request->post('mobile');
  320. $pwd = $this->request->post('pwd');
  321. $confirm_pwd = $this->request->post('confirm_pwd');
  322. $invitation_code = $this->request->post('invitation_code');
  323. if (!$code || !$mobile || !$pwd || !$confirm_pwd || !$invitation_code) {
  324. $this->error(__('Invalid parameters'));
  325. }
  326. if ($pwd != $confirm_pwd) {
  327. $this->error(__('确认密码不一致'));
  328. }
  329. // if ($email && !Validate::is($email, "email")) {
  330. // $this->error(__('Email is incorrect'));
  331. // }
  332. if ($mobile && !Validate::regex($mobile, "^\d{8,11}$")) {
  333. $this->error(__('Mobile is incorrect'));
  334. }
  335. // $ret = Sms::check($mobile, $code, 'register');
  336. // if (!$ret) {
  337. // $this->error(__('Captcha is incorrect'));
  338. // }
  339. $ret = $this->auth->register($code, $mobile, $pwd, $invitation_code);
  340. if ($ret) {
  341. $data = ['userinfo' => $this->auth->getUserinfo()];
  342. $this->success(__('Sign up successful'), $data);
  343. } else {
  344. $this->error($this->auth->getError());
  345. }
  346. }
  347. /**
  348. * 退出登录
  349. * @ApiMethod (POST)
  350. */
  351. public function logout()
  352. {
  353. if (!$this->request->isPost()) {
  354. $this->error(__('Invalid parameters'));
  355. }
  356. $this->auth->logout();
  357. $this->success(__('Logout successful'));
  358. }
  359. /**
  360. * 修改会员个人信息
  361. *
  362. * @ApiMethod (POST)
  363. * @param string $avatar 头像地址
  364. * @param string $username 用户名
  365. * @param string $nickname 昵称
  366. * @param string $bio 个人简介
  367. */
  368. public function profile()
  369. {
  370. $user = $this->auth->getUser();
  371. $username = $this->request->post('username');
  372. $nickname = $this->request->post('nickname');
  373. $bio = $this->request->post('bio');
  374. $avatar = $this->request->post('avatar', '', 'trim,strip_tags,htmlspecialchars');
  375. if ($username) {
  376. $exists = \app\common\model\Users::where('username', $username)->where('id', '<>', $this->auth->id)->find();
  377. if ($exists) {
  378. $this->error(__('Username already exists'));
  379. }
  380. $user->username = $username;
  381. }
  382. if ($nickname) {
  383. $exists = \app\common\model\Users::where('nickname', $nickname)->where('id', '<>', $this->auth->id)->find();
  384. if ($exists) {
  385. $this->error(__('Nickname already exists'));
  386. }
  387. $user->nickname = $nickname;
  388. }
  389. $user->bio = $bio;
  390. $user->avatar = $avatar;
  391. $user->save();
  392. $this->success();
  393. }
  394. /**
  395. * 修改邮箱
  396. *
  397. * @ApiMethod (POST)
  398. * @param string $email 邮箱
  399. * @param string $captcha 验证码
  400. */
  401. public function changeemail()
  402. {
  403. $user = $this->auth->getUser();
  404. $email = $this->request->post('email');
  405. $captcha = $this->request->post('captcha');
  406. if (!$email || !$captcha) {
  407. $this->error(__('Invalid parameters'));
  408. }
  409. if (!Validate::is($email, "email")) {
  410. $this->error(__('Email is incorrect'));
  411. }
  412. if (\app\common\model\Users::where('email', $email)->where('id', '<>', $user->id)->find()) {
  413. $this->error(__('Email already exists'));
  414. }
  415. $result = Ems::check($email, $captcha, 'changeemail');
  416. if (!$result) {
  417. $this->error(__('Captcha is incorrect'));
  418. }
  419. $verification = $user->verification;
  420. $verification->email = 1;
  421. $user->verification = $verification;
  422. $user->email = $email;
  423. $user->save();
  424. Ems::flush($email, 'changeemail');
  425. $this->success();
  426. }
  427. /**
  428. * 修改手机号
  429. *
  430. * @ApiMethod (POST)
  431. * @param string $mobile 手机号
  432. * @param string $captcha 验证码
  433. */
  434. public function changemobile()
  435. {
  436. $user = $this->auth->getUser();
  437. $mobile = $this->request->post('mobile');
  438. $captcha = $this->request->post('captcha');
  439. if (!$mobile || !$captcha) {
  440. $this->error(__('Invalid parameters'));
  441. }
  442. if (!Validate::regex($mobile, "^1\d{10}$")) {
  443. $this->error(__('Mobile is incorrect'));
  444. }
  445. if (\app\common\model\Users::where('mobile', $mobile)->where('id', '<>', $user->id)->find()) {
  446. $this->error(__('Mobile already exists'));
  447. }
  448. $result = Sms::check($mobile, $captcha, 'changemobile');
  449. if (!$result) {
  450. $this->error(__('Captcha is incorrect'));
  451. }
  452. $verification = $user->verification;
  453. $verification->mobile = 1;
  454. $user->verification = $verification;
  455. $user->mobile = $mobile;
  456. $user->save();
  457. Sms::flush($mobile, 'changemobile');
  458. $this->success();
  459. }
  460. /**
  461. * 第三方登录
  462. *
  463. * @ApiMethod (POST)
  464. * @param string $platform 平台名称
  465. * @param string $code Code码
  466. */
  467. public function third()
  468. {
  469. $url = url('user/index');
  470. $platform = $this->request->post("platform");
  471. $code = $this->request->post("code");
  472. $config = get_addon_config('third');
  473. if (!$config || !isset($config[$platform])) {
  474. $this->error(__('Invalid parameters'));
  475. }
  476. $app = new \addons\third\library\Application($config);
  477. //通过code换access_token和绑定会员
  478. $result = $app->{$platform}->getUserInfo(['code' => $code]);
  479. if ($result) {
  480. $loginret = \addons\third\library\Service::connect($platform, $result);
  481. if ($loginret) {
  482. $data = [
  483. 'userinfo' => $this->auth->getUserinfo(),
  484. 'thirdinfo' => $result
  485. ];
  486. $this->success(__('Logged in successful'), $data);
  487. }
  488. }
  489. $this->error(__('Operation failed'), $url);
  490. }
  491. /**
  492. * 重置密码
  493. *
  494. * @ApiMethod (POST)
  495. * @param string $mobile 手机号
  496. * @param string $newpassword 新密码
  497. * @param string $captcha 验证码
  498. */
  499. public function resetpwd()
  500. {
  501. $type = $this->request->post("type", "mobile");
  502. $mobile = $this->request->post("mobile");
  503. $email = $this->request->post("email");
  504. $newpassword = $this->request->post("newpassword");
  505. $captcha = $this->request->post("captcha");
  506. if (!$newpassword || !$captcha) {
  507. $this->error(__('Invalid parameters'));
  508. }
  509. //验证Token
  510. if (!Validate::make()->check(['newpassword' => $newpassword], ['newpassword' => 'require|regex:\S{6,30}'])) {
  511. $this->error(__('Password must be 6 to 30 characters'));
  512. }
  513. if ($type == 'mobile') {
  514. if (!Validate::regex($mobile, "^1\d{10}$")) {
  515. $this->error(__('Mobile is incorrect'));
  516. }
  517. $user = \app\common\model\Users::getByMobile($mobile);
  518. if (!$user) {
  519. $this->error(__('User not found'));
  520. }
  521. $ret = Sms::check($mobile, $captcha, 'resetpwd');
  522. if (!$ret) {
  523. $this->error(__('Captcha is incorrect'));
  524. }
  525. Sms::flush($mobile, 'resetpwd');
  526. } else {
  527. if (!Validate::is($email, "email")) {
  528. $this->error(__('Email is incorrect'));
  529. }
  530. $user = \app\common\model\Users::getByEmail($email);
  531. if (!$user) {
  532. $this->error(__('User not found'));
  533. }
  534. $ret = Ems::check($email, $captcha, 'resetpwd');
  535. if (!$ret) {
  536. $this->error(__('Captcha is incorrect'));
  537. }
  538. Ems::flush($email, 'resetpwd');
  539. }
  540. //模拟一次登录
  541. $this->auth->direct($user->id);
  542. $ret = $this->auth->changepwd($newpassword, '', true);
  543. if ($ret) {
  544. $this->success(__('Reset password successful'));
  545. } else {
  546. $this->error($this->auth->getError());
  547. }
  548. }
  549. }