User.php 23 KB

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