优惠券小程序

WechatService.php 1.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. <?php
  2. namespace App\Service;
  3. use App\Log;
  4. use App\Support\EmailQueue;
  5. class WechatService
  6. {
  7. /**
  8. * 登录凭证校验
  9. * */
  10. public static function getSessionKey($code, &$errno)
  11. {
  12. $appId = config('wechat.app_id');
  13. $appSecret = config('wechat.app_secret');
  14. # code2session请求地址
  15. $authUrl = "https://api.weixin.qq.com/sns/jscode2session?appid=". $appId ."&secret=" . $appSecret
  16. . "&js_code=". $code ."&grant_type=authorization_code";
  17. Log::info('登录凭证校验请求地址:', ['request_uri' => $authUrl], 'GetSessionKeyTrace');
  18. $response = HttpService::httpGet($authUrl);
  19. Log::info('登录凭证响应结果:', ['response' => $response], 'GetSessionKeyTrace');
  20. $responseData = json_decode($response, true);
  21. if(!$response || (isset($responseData['errcode']) && $responseData['errcode'])) { // 返回结果异常
  22. EmailQueue::rPush('小程序登录凭证校验失败', '错误码:'.$responseData['errcode'] ,
  23. ['xiaohua.hou@kuxuan-inc.com'], '小程序登录凭证校验失败');
  24. Log::error('小程序登录凭证校验失败', [
  25. 'response' => $responseData
  26. ], 'GetSessionKeyFail');
  27. $errno = 1001;
  28. return [];
  29. }
  30. $errno = 0;
  31. return [
  32. 'session_key' => $responseData['session_key'],
  33. 'union_id' => $responseData['unionid'] ?? '',
  34. 'openid' => $responseData['openid']
  35. ];
  36. }
  37. }