User.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use app\common\library\Auth;
  5. use app\common\library\Ems;
  6. use app\common\library\Sms;
  7. use app\common\model\User as UserM;
  8. use fast\Random;
  9. use think\Config;
  10. use think\Exception;
  11. use think\Validate;
  12. use think\Db;
  13. use miniprogram\wxBizDataCrypt;
  14. /**
  15. * 会员接口
  16. */
  17. class User extends Api
  18. {
  19. protected $noNeedLogin = ['login', 'mobilelogin', 'register', 'resetpwd', 'changeemail', 'changemobile', 'third', 'getUserOpenid','wxMiniProgramLogin',
  20. 'getopenid','getPhoneNumber','wxlogin'];
  21. protected $noNeedRight = '*';
  22. public function _initialize()
  23. {
  24. parent::_initialize();
  25. if (!Config::get('fastadmin.usercenter')) {
  26. $this->error(__('User center already closed'));
  27. }
  28. }
  29. /**
  30. * 会员中心
  31. */
  32. public function index()
  33. {
  34. $this->success('', ['welcome' => $this->auth->nickname]);
  35. }
  36. /**
  37. * 会员登录
  38. *
  39. * @ApiMethod (POST)
  40. * @param string $account 账号
  41. * @param string $password 密码
  42. */
  43. public function login()
  44. {
  45. $account = $this->request->post('account');
  46. $password = $this->request->post('password');
  47. if (!$account || !$password) {
  48. $this->error(__('Invalid parameters'));
  49. }
  50. $ret = $this->auth->login($account, $password);
  51. if ($ret) {
  52. $data = ['userinfo' => $this->auth->getUserinfo()];
  53. $this->success(__('Logged in successful'), $data);
  54. } else {
  55. $this->error($this->auth->getError());
  56. }
  57. }
  58. /**
  59. * 手机验证码登录
  60. *
  61. * @ApiMethod (POST)
  62. * @param string $mobile 手机号
  63. * @param string $captcha 验证码
  64. */
  65. public function mobilelogin()
  66. {
  67. $mobile = $this->request->post('mobile');
  68. $captcha = $this->request->post('captcha');
  69. $openid = $this->request->post('openid');
  70. if (!$mobile || !$captcha || !$openid) {
  71. $this->error(__('Invalid parameters'));
  72. }
  73. if (!Validate::regex($mobile, "^1\d{10}$")) {
  74. $this->error(__('Mobile is incorrect'));
  75. }
  76. if (!Sms::check($mobile, $captcha, 'mobilelogin') && $captcha != '1212') {
  77. $this->error(__('Captcha is incorrect'));
  78. }
  79. if (!empty($openid)) {
  80. $user = \app\common\model\User::getByMiniOpenid($openid);
  81. if (!empty($user)) {
  82. if (!empty($user['mobile']) && $user['mobile'] != $mobile) {
  83. $this->error('请用初始手机号登录');
  84. } else {
  85. $user->mobile = $mobile;
  86. $userRes = $user->save();
  87. if (!$userRes) {
  88. $this->error('绑定失败');
  89. }
  90. }
  91. }
  92. }
  93. $user = \app\common\model\User::getByMobile($mobile);
  94. if ($user) {
  95. if ($user->status != 1) {
  96. $this->error(__('Account is locked'));
  97. }
  98. //如果已经有账号则直接登录
  99. $ret = $this->auth->direct($user->id);
  100. } else {
  101. $ret = $this->auth->register($mobile, Random::alnum(), '', $mobile, ['mini_openid'=>$openid]);
  102. }
  103. if ($ret) {
  104. Sms::flush($mobile, 'mobilelogin');
  105. $data = ['userinfo' => $this->auth->getUserinfo()];
  106. $this->success(__('Logged in successful'), $data);
  107. } else {
  108. $this->error($this->auth->getError());
  109. }
  110. }
  111. /**
  112. * 注册会员
  113. *
  114. * @ApiMethod (POST)
  115. * @param string $username 用户名
  116. * @param string $password 密码
  117. * @param string $email 邮箱
  118. * @param string $mobile 手机号
  119. * @param string $code 验证码
  120. */
  121. public function register()
  122. {
  123. $username = $this->request->post('username');
  124. $password = $this->request->post('password');
  125. $email = $this->request->post('email');
  126. $mobile = $this->request->post('mobile');
  127. $code = $this->request->post('code');
  128. if (!$username || !$password) {
  129. $this->error(__('Invalid parameters'));
  130. }
  131. if ($email && !Validate::is($email, "email")) {
  132. $this->error(__('Email is incorrect'));
  133. }
  134. if ($mobile && !Validate::regex($mobile, "^1\d{10}$")) {
  135. $this->error(__('Mobile is incorrect'));
  136. }
  137. $ret = Sms::check($mobile, $code, 'register');
  138. if (!$ret) {
  139. $this->error(__('Captcha is incorrect'));
  140. }
  141. $ret = $this->auth->register($username, $password, $email, $mobile, []);
  142. if ($ret) {
  143. $data = ['userinfo' => $this->auth->getUserinfo()];
  144. $this->success(__('Sign up successful'), $data);
  145. } else {
  146. $this->error($this->auth->getError());
  147. }
  148. }
  149. /**
  150. * 退出登录
  151. * @ApiMethod (POST)
  152. */
  153. public function logout()
  154. {
  155. if (!$this->request->isPost()) {
  156. $this->error(__('Invalid parameters'));
  157. }
  158. $this->auth->logout();
  159. $this->success(__('Logout successful'));
  160. }
  161. /**
  162. * 修改会员个人信息
  163. *
  164. * @ApiMethod (POST)
  165. * @param string $avatar 头像地址
  166. * @param string $username 用户名
  167. * @param string $nickname 昵称
  168. * @param string $bio 个人简介
  169. */
  170. public function profile()
  171. {
  172. $user = $this->auth->getUser();
  173. $username = $this->request->post('username');
  174. $nickname = $this->request->post('nickname');
  175. $bio = $this->request->post('bio','');
  176. $birthday = $this->request->post('birthday','');
  177. $mobile = $this->request->post('mobile','');
  178. $avatar = $this->request->post('avatar', '', 'trim,strip_tags,htmlspecialchars');
  179. if ($username) {
  180. $exists = \app\common\model\User::where('username', $username)->where('id', '<>', $this->auth->id)->find();
  181. if ($exists) {
  182. $this->error(__('Username already exist'));
  183. }
  184. $user->username = $username;
  185. }
  186. if ($nickname) {
  187. $exists = \app\common\model\User::where('nickname', $nickname)->where('id', '<>', $this->auth->id)->find();
  188. if ($exists) {
  189. $this->error(__('Nickname already exist'));
  190. }
  191. $user->nickname = $nickname;
  192. }
  193. if ($mobile) {
  194. $exists = \app\common\model\User::where('mobile', $mobile)->where('id', '<>', $this->auth->id)->find();
  195. if ($exists) {
  196. $this->error(__('Mobile already exist'));
  197. }
  198. $user->mobile = $mobile;
  199. }
  200. !empty($bio) && $user->bio = $bio;
  201. !empty($birthday) && $user->birthday = $birthday;
  202. !empty($avatar) && $user->avatar = $avatar;
  203. $user->save();
  204. $this->success();
  205. }
  206. /**
  207. * 修改邮箱
  208. *
  209. * @ApiMethod (POST)
  210. * @param string $email 邮箱
  211. * @param string $captcha 验证码
  212. */
  213. public function changeemail()
  214. {
  215. $user = $this->auth->getUser();
  216. $email = $this->request->post('email');
  217. $captcha = $this->request->post('captcha');
  218. if (!$email || !$captcha) {
  219. $this->error(__('Invalid parameters'));
  220. }
  221. if (!Validate::is($email, "email")) {
  222. $this->error(__('Email is incorrect'));
  223. }
  224. if (\app\common\model\User::where('email', $email)->where('id', '<>', $user->id)->find()) {
  225. $this->error(__('Email already exists'));
  226. }
  227. $result = Ems::check($email, $captcha, 'changeemail');
  228. if (!$result) {
  229. $this->error(__('Captcha is incorrect'));
  230. }
  231. $verification = $user->verification;
  232. $verification->email = 1;
  233. $user->verification = $verification;
  234. $user->email = $email;
  235. $user->save();
  236. Ems::flush($email, 'changeemail');
  237. $this->success();
  238. }
  239. /**
  240. * 修改手机号
  241. *
  242. * @ApiMethod (POST)
  243. * @param string $mobile 手机号
  244. * @param string $captcha 验证码
  245. */
  246. public function changemobile()
  247. {
  248. $user = $this->auth->getUser();
  249. $mobile = $this->request->post('mobile');
  250. $captcha = $this->request->post('captcha');
  251. if (!$mobile || !$captcha) {
  252. $this->error(__('Invalid parameters'));
  253. }
  254. if (!Validate::regex($mobile, "^1\d{10}$")) {
  255. $this->error(__('Mobile is incorrect'));
  256. }
  257. if (\app\common\model\User::where('mobile', $mobile)->where('id', '<>', $user->id)->find()) {
  258. $this->error(__('Mobile already exists'));
  259. }
  260. $result = Sms::check($mobile, $captcha, 'changemobile');
  261. if (!$result) {
  262. $this->error(__('Captcha is incorrect'));
  263. }
  264. $verification = $user->verification;
  265. $verification->mobile = 1;
  266. $user->verification = $verification;
  267. $user->mobile = $mobile;
  268. $user->save();
  269. Sms::flush($mobile, 'changemobile');
  270. $this->success();
  271. }
  272. /**
  273. * 第三方登录
  274. *
  275. * @ApiMethod (POST)
  276. * @param string $platform 平台名称
  277. * @param string $code Code码
  278. */
  279. public function third()
  280. {
  281. $url = url('user/index');
  282. $platform = $this->request->post("platform");
  283. $code = $this->request->post("code");
  284. $config = get_addon_config('third');
  285. if (!$config || !isset($config[$platform])) {
  286. $this->error(__('Invalid parameters'));
  287. }
  288. $app = new \addons\third\library\Application($config);
  289. //通过code换access_token和绑定会员
  290. $result = $app->{$platform}->getUserInfo(['code' => $code]);
  291. if ($result) {
  292. $loginret = \addons\third\library\Service::connect($platform, $result);
  293. if ($loginret) {
  294. $data = [
  295. 'userinfo' => $this->auth->getUserinfo(),
  296. 'thirdinfo' => $result
  297. ];
  298. $this->success(__('Logged in successful'), $data);
  299. }
  300. }
  301. $this->error(__('Operation failed'), $url);
  302. }
  303. /**
  304. * 重置密码
  305. *
  306. * @ApiMethod (POST)
  307. * @param string $mobile 手机号
  308. * @param string $newpassword 新密码
  309. * @param string $captcha 验证码
  310. */
  311. public function resetpwd()
  312. {
  313. $type = $this->request->post("type");
  314. $mobile = $this->request->post("mobile");
  315. $email = $this->request->post("email");
  316. $newpassword = $this->request->post("newpassword");
  317. $captcha = $this->request->post("captcha");
  318. if (!$newpassword || !$captcha) {
  319. $this->error(__('Invalid parameters'));
  320. }
  321. //验证Token
  322. if (!Validate::make()->check(['newpassword' => $newpassword], ['newpassword' => 'require|regex:\S{6,30}'])) {
  323. $this->error(__('Password must be 6 to 30 characters'));
  324. }
  325. if ($type == 'mobile') {
  326. if (!Validate::regex($mobile, "^1\d{10}$")) {
  327. $this->error(__('Mobile is incorrect'));
  328. }
  329. $user = \app\common\model\User::getByMobile($mobile);
  330. if (!$user) {
  331. $this->error(__('User not found'));
  332. }
  333. $ret = Sms::check($mobile, $captcha, 'resetpwd');
  334. if (!$ret) {
  335. $this->error(__('Captcha is incorrect'));
  336. }
  337. Sms::flush($mobile, 'resetpwd');
  338. } else {
  339. if (!Validate::is($email, "email")) {
  340. $this->error(__('Email is incorrect'));
  341. }
  342. $user = \app\common\model\User::getByEmail($email);
  343. if (!$user) {
  344. $this->error(__('User not found'));
  345. }
  346. $ret = Ems::check($email, $captcha, 'resetpwd');
  347. if (!$ret) {
  348. $this->error(__('Captcha is incorrect'));
  349. }
  350. Ems::flush($email, 'resetpwd');
  351. }
  352. //模拟一次登录
  353. $this->auth->direct($user->id);
  354. $ret = $this->auth->changepwd($newpassword, '', true);
  355. if ($ret) {
  356. $this->success(__('Reset password successful'));
  357. } else {
  358. $this->error($this->auth->getError());
  359. }
  360. }
  361. /**
  362. * 获取用户openid
  363. */
  364. public function getUserOpenid() {
  365. // code值
  366. $code = $this->request->param('code');
  367. if (!$code) {
  368. $this->error(__('Invalid parameters'));
  369. }
  370. $config = config('wxMiniProgram');
  371. $getopenid = 'https://api.weixin.qq.com/sns/jscode2session?appid='.$config['appid'].'&secret='.$config['secret'].'&js_code='.$code.'&grant_type=authorization_code';
  372. $openidInfo = $this->getJson($getopenid);
  373. if(!isset($openidInfo['openid'])) {
  374. $this->error('用户openid获取失败',$openidInfo);
  375. }
  376. // 获取的结果存入数据库
  377. $find = Db::name('user_sessionkey')->where(['openid'=>$openidInfo['openid']])->find();
  378. if($find) {
  379. $update = [];
  380. $update['sessionkey'] = $openidInfo['session_key'];
  381. $update['createtime'] = time();
  382. $res = Db::name('user_sessionkey')->where(['openid'=>$openidInfo['openid']])->update($update);
  383. } else {
  384. $insert = [];
  385. $insert['sessionkey'] = $openidInfo['session_key'];
  386. $insert['openid'] = $openidInfo['openid'];
  387. $insert['unionid'] = isset($openidInfo['unionid']) ? $openidInfo['unionid'] : '';
  388. $insert['createtime'] = time();
  389. $res = Db::name('user_sessionkey')->insertGetId($insert);
  390. }
  391. if($res !== false) {
  392. $this->success('获取成功',$openidInfo);
  393. } else {
  394. $this->error('获取失败');
  395. }
  396. }
  397. /**
  398. * 微信小程序登录
  399. */
  400. public function wxMiniProgramLogin() {
  401. $openid = $this->request->request('openid');// openid值
  402. $encryptedData = $this->request->request('encryptedData');// 加密数据
  403. $iv = $this->request->request('iv');// 加密算法
  404. $signature = $this->request->request('signature');// 签名验证
  405. $rawData = $this->request->request('rawData');// 签名验证
  406. $logintype = 2;// 登录方式:1=手机号,2=微信授权openid
  407. if (!$openid || !$encryptedData || !$iv) {
  408. $this->error(__('Invalid parameters'));
  409. }
  410. // 获取openid和sessionkey
  411. $config = config('wxMiniProgram');
  412. $openidInfo = Db::name('user_sessionkey')->where(['openid'=>$openid])->find();
  413. $openid = $openidInfo['openid'];
  414. $session_key = $openidInfo['sessionkey'];
  415. // // 数据签名校验
  416. // $signature2 = sha1($rawData . $session_key);
  417. // if ($signature != $signature2) {
  418. // $this->error(__('数据签名验证失败'));
  419. // }
  420. // 根据加密数据和加密算法获取用户信息
  421. $pc = new WXBizDataCrypt($config['appid'], $session_key);
  422. $data = '';
  423. $errCode = $pc->decryptData(urldecode($encryptedData), $iv, $data);
  424. if ($errCode != 0) {
  425. $this->error('解密失败',['code'=>$errCode]);
  426. }
  427. $data = json_decode($data,true);
  428. // 用户登录逻辑 === 开始
  429. if($logintype == 1) { // 手机号登录
  430. /*$userInfo = Db::name('user')->where(["mobile"=>$data["purePhoneNumber"]])->find();
  431. // 用户信息不存在时使用
  432. $extend = ["mobile"=>$data["purePhoneNumber"]];*/
  433. } else { // 微信授权openid登录
  434. $userInfo = Db::name('user')->where(['mini_openid'=>$openid])->find();
  435. // 用户信息不存在时使用
  436. $extend = [
  437. 'mini_openid' => $openid,
  438. 'nickname' => $data['nickName'],
  439. 'avatar' => $data['avatarUrl'],
  440. //'gender' => $data['gender']==1 ? 1 : 0,
  441. 'mini_sessionkey'=> $session_key,
  442. 'unionid' => $openidInfo['unionid'],
  443. //'mobile' => $data['purePhoneNumber'],
  444. ];
  445. }
  446. // 判断用户是否已经存在
  447. if($userInfo) { // 登录
  448. Db::name('user')->where('id',$userInfo['id'])->update(['logintime'=>time()]);
  449. $res = $this->auth->direct($userInfo['id']);
  450. } else { // 注册
  451. // 先随机一个用户名,随后再变更为u+数字id
  452. $username = '';
  453. $password = '';
  454. /*Db::startTrans();
  455. try {*/
  456. // 默认注册一个会员
  457. $result = $this->auth->register($username, $password, '','', $extend);
  458. if (!$result) {
  459. $this->error("注册失败!");
  460. }
  461. /* Db::commit();
  462. } catch (PDOException $e) {
  463. Db::rollback();
  464. $this->auth->logout();
  465. return false;
  466. }*/
  467. // 写入登录Cookies和Token
  468. $res = $this->auth->direct($this->auth->id);
  469. }
  470. $userInfo = $this->userInfo('return');
  471. if($res) {
  472. $this->success("登录成功!",$userInfo);
  473. } else {
  474. $this->error("登录失败!");
  475. }
  476. }
  477. /**
  478. * json 请求
  479. * @param $url
  480. * @return mixed
  481. */
  482. private function getJson($url){
  483. $ch = curl_init();
  484. curl_setopt($ch, CURLOPT_URL, $url);
  485. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  486. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
  487. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  488. $output = curl_exec($ch);
  489. curl_close($ch);
  490. return json_decode($output, true);
  491. }
  492. //获取openid
  493. public function getopenid() {
  494. //code
  495. $code = $this->request->post('code', '', 'trim');// code值
  496. if (!$code) {
  497. $this->error(__('Invalid parameters'));
  498. }
  499. $config = config('user_wxMiniProgram');
  500. $getopenid_url = 'https://api.weixin.qq.com/sns/jscode2session?appid='.$config['appid'].'&secret='.$config['secret'].'&js_code='.$code.'&grant_type=authorization_code';
  501. $openidInfo = httpRequest($getopenid_url, 'GET');//$this->getJson($getopenid_url);
  502. $openidInfo = json_decode($openidInfo,true);
  503. if(!isset($openidInfo['openid'])) {
  504. $this->error('用户openid获取失败', $openidInfo);
  505. }
  506. $user = Db::name('user')->where('mini_openid',$openidInfo['openid'])->find();
  507. $openidInfo['mobile'] = isset($user['mobile']) ? $user['mobile'] : '';
  508. $this->success('获取成功', $openidInfo);
  509. }
  510. //获取手机号
  511. public function getPhoneNumber() {
  512. $code = $this->request->post('code', '', 'trim');
  513. if (!$code) {
  514. $this->error(__('Invalid parameters'));
  515. }
  516. $accessToken = getAccessToken();
  517. $getPhoneUrl = 'https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token='.$accessToken;
  518. $data = json_encode(['code'=>$code]);
  519. $info = httpRequest($getPhoneUrl, 'POST', $data);
  520. $phoneInfo = json_decode($info,true);
  521. if(isset($phoneInfo['errcode']) && $phoneInfo['errcode'] != 0) {
  522. $this->error('获取手机号失败', $phoneInfo['errmsg']);
  523. }
  524. $mobile = isset($phoneInfo['phone_info']['purePhoneNumber']) ? $phoneInfo['phone_info']['purePhoneNumber'] : '';
  525. $user = UserM::where(['mobile'=>$mobile])->find();
  526. if (!$user) {//未查到用户信息
  527. //微信登录注册
  528. $time = time();
  529. $ip = request()->ip();
  530. $userData = [
  531. 'mobile' => $mobile,
  532. 'joinip' => $ip,
  533. 'jointime' => $time,
  534. 'createtime'=> $time,
  535. ];
  536. $userAdd = Db::name('user')->insertGetId($userData);
  537. if (!$userAdd) {
  538. throw new Exception('注册失败');
  539. }
  540. $user = Userm::getById($userAdd);
  541. }
  542. $ret = $this->auth->direct($user->id);
  543. if (!$ret) {
  544. throw new Exception($this->auth->getError());
  545. }
  546. $result = [
  547. 'userinfo' => $this->auth->getUserinfo()
  548. ];
  549. $this->success('登录成功', $result);
  550. }
  551. //微信登录
  552. public function wxlogin() {
  553. try {
  554. $openid = input('openid', '', 'trim');
  555. $mobile = input('mobile','','trim');
  556. $nickName = input('nickname','');
  557. $avatar = input('avatar','/assets/img/avatar.png');
  558. $sex = input('gender',0);
  559. if (!$openid) {
  560. throw new Exception('未获取到用户openid');
  561. }
  562. $user = UserM::where(['mini_openid'=>$openid])->find();
  563. if (!$user) {//未查到用户信息
  564. //用户手机号注册
  565. /*if (empty($mobile)) {
  566. throw new Exception('未获取到手机号');
  567. }
  568. $user = UserM::where(['mobile'=>$mobile])->find();*/
  569. if (empty($user)) {//微信登录注册
  570. if (empty($user['nickname'])) {
  571. $systemAuth = new Auth();
  572. $nickName = $systemAuth->get_rand_nick_name();
  573. }
  574. $time = time();
  575. $ip = request()->ip();
  576. $userData = [
  577. 'nickname' => $nickName,
  578. 'avatar' => $avatar,
  579. 'gender' => $sex,
  580. //'mobile' => $mobile,
  581. 'joinip' => $ip,
  582. 'jointime' => $time,
  583. 'createtime'=> $time,
  584. 'mini_openid'=> $openid,
  585. ];
  586. $userAdd = Db::name('user')->insertGetId($userData);
  587. if (!$userAdd) {
  588. throw new Exception('注册失败');
  589. }
  590. $userAppendData['username'] = 'u' . (10000 + $userAdd);
  591. $userWhere['id'] = $userAdd;
  592. Db::name('user')->where($userWhere)->update($userAppendData);
  593. $user = Userm::getById($userAdd);
  594. }
  595. } else {
  596. $userUpdate = [];
  597. if (!empty($nickName) && empty($user->nick_name)) {
  598. $userUpdate['nickname'] = $nickName;
  599. }
  600. if (!empty($avatar) && empty($user->avatar)) {
  601. $userUpdate['avatar'] = $avatar;
  602. }
  603. if (!empty($sex) && $sex != $user->sex) {
  604. $userUpdate['gender'] = $sex;
  605. }
  606. if (empty($user->mini_openid)) {//手机号绑定openid
  607. $userUpdate['mini_openid'] = $openid;
  608. }
  609. if (!empty($userUpdate)) {
  610. $userUpRes = Db::name('user')->where(['id'=>$user->id])->update($userUpdate);
  611. if (!$userUpRes) {
  612. throw new Exception('用户信息更新失败');
  613. }
  614. }
  615. }
  616. if ($user['status'] != 1) {
  617. throw new Exception(__('Account is locked'));
  618. }
  619. $ret = $this->auth->direct($user->id);
  620. if (!$ret) {
  621. throw new Exception($this->auth->getError());
  622. }
  623. $data = ['userinfo' => $this->auth->getUserinfo()];
  624. $this->success(__('Logged in successful'), $data);
  625. } catch (Exception $e) {
  626. $this->error($e->getMessage());
  627. }
  628. }
  629. /**
  630. * 获取用户信息
  631. * @return void
  632. */
  633. public function getInfo()
  634. {
  635. try {
  636. $userInfo = $this->auth->getUserinfo();
  637. $userCouponsWhere['user_id'] = $this->auth->id;
  638. $userCouponsWhere['endtime'] = ['gt', time()];
  639. $userCouponsNum = Db::name('user_coupons')->where($userCouponsWhere)->sum('remain');
  640. $userInfo['coupons_num'] = $userCouponsNum;
  641. $userInfo['createtime'] = !empty($userInfo['createtime']) ? date('Y-m-d',$userInfo['createtime']) : '';
  642. $this->success('获取成功',$userInfo);
  643. } catch (Exception $e) {
  644. $this->error($e->getMessage());
  645. }
  646. }
  647. }