User.php 16 KB

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