User.php 21 KB

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