User.php 24 KB

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