User.php 24 KB

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