User.php 24 KB

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