123456789101112131415161718192021222324252627282930313233343536373839404142434445464748 |
- <?php
- namespace App\Service;
- use App\Log;
- use App\Support\EmailQueue;
- class WechatService
- {
- /**
- * 登录凭证校验
- * */
- public static function getSessionKey($code, &$errno)
- {
- $appId = config('wechat.app_id');
- $appSecret = config('wechat.app_secret');
- # code2session请求地址
- $authUrl = "https://api.weixin.qq.com/sns/jscode2session?appid=". $appId ."&secret=" . $appSecret
- . "&js_code=". $code ."&grant_type=authorization_code";
- Log::info('登录凭证校验请求地址:', ['request_uri' => $authUrl], 'GetSessionKeyTrace');
- $response = HttpService::httpGet($authUrl);
- Log::info('登录凭证响应结果:', ['response' => $response], 'GetSessionKeyTrace');
- $responseData = json_decode($response, true);
- if(!$response || (isset($responseData['errcode']) && $responseData['errcode'])) { // 返回结果异常
- EmailQueue::rPush('小程序登录凭证校验失败', '错误码:'.$responseData['errcode'] ,
- ['xiaohua.hou@kuxuan-inc.com'], '小程序登录凭证校验失败');
- Log::error('小程序登录凭证校验失败', [
- 'response' => $responseData
- ], 'GetSessionKeyFail');
- $errno = 1001;
- return [];
- }
- $errno = 0;
- return [
- 'session_key' => $responseData['session_key'],
- 'union_id' => $responseData['unionid'] ?? '',
- 'openid' => $responseData['openid']
- ];
- }
- }
|