User.php 23 KB

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