Base.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. namespace App\libs\wechat\auth\Gateways;
  3. use Illuminate\Support\Facades\Config;
  4. use swdz\helpers\Helper;
  5. use Illuminate\Support\Facades\DB;
  6. use \Illuminate\Support\Facades\Cookie;
  7. class Base
  8. {
  9. protected $config = [];
  10. public function __construct()
  11. {
  12. $this->config = Config::get('swdz.wechat');
  13. }
  14. /**
  15. * @desc 快速授权
  16. * @param string $scope
  17. * @param string|null $redirect
  18. * @return mixed
  19. */
  20. public function auth(string $scope = 'snsapi_userinfo', string $redirect = null)
  21. {
  22. $code = request()->get('code', null);
  23. if (!$code) {
  24. header('Location: ' . $this->authUrl($scope, $redirect));
  25. exit();
  26. }
  27. $openInfo = $this->code2Openid($code);
  28. if (isset($openInfo['errcode']) && in_array($openInfo['errcode'], [40163, 40029])) {
  29. header('Location: ' . $this->authUrl($scope, $redirect));
  30. exit();
  31. }
  32. if (isset($openInfo['errcode'])) {
  33. exit($openInfo['errmsg']);
  34. }
  35. if ($scope == 'snsapi_userinfo') {
  36. $userInfo = $this->userSimpleInfo($openInfo['access_token'], $openInfo['openid']);
  37. } else {
  38. $userInfo['openid'] = $openInfo['openid'];
  39. }
  40. $baseTable = $this->config['appid'] . '_user';
  41. $userId = Db::table($baseTable)->where('openid', $userInfo['openid'])->value('user_id');
  42. if (empty($userId)) {
  43. $userId = Db::table($baseTable)
  44. ->insertGetId([
  45. 'openid' => $userInfo['openid'],
  46. 'unionid' => $userInfo['unionid'] ?? '',
  47. 'nickname' => $userInfo['nickname'] ?? '',
  48. 'headimgurl' => $userInfo['headimgurl'] ?? '',
  49. 'sex' => $userInfo['sex'] ?? '',
  50. 'country' => $userInfo['country'] ?? '',
  51. 'province' => $userInfo['province'] ?? '',
  52. 'city' => $userInfo['city'] ?? '',
  53. 'language' => $userInfo['language'] ?? '',
  54. 'created_at' => time()
  55. ]);
  56. }
  57. $userInfo['user_id'] = $userId;
  58. return $userInfo;
  59. }
  60. /**
  61. * @desc 获取jssdk
  62. * @param string $url 需要授权的地址
  63. * @param array $api 开放接口列表
  64. * @param array $tag 开发能力列表
  65. * @return array
  66. */
  67. public function jssdk(string $url, $api = [], $tag = [])
  68. {
  69. $jsApiList = array_merge($api, [
  70. 'checkJsApi',
  71. 'onMenuShareTimeline',
  72. 'onMenuShareAppMessage',
  73. 'onMenuShareQQ',
  74. 'onMenuShareWeibo',
  75. 'hideMenuItems',
  76. 'showMenuItems',
  77. 'hideAllNonBaseMenuItem',
  78. 'getLocalImgData',
  79. 'chooseImage'
  80. ]);
  81. $openTagList = array_merge($tag, ['wx-open-launch-weapp', 'wx-open-launch-app']);
  82. $jsApiTicket = $this->jsApiTicket();
  83. $timestamp = time();
  84. $nonceStr = uniqid();
  85. return [
  86. 'debug' => false,
  87. 'appId' => $this->config['appid'],
  88. 'timestamp' => $timestamp,
  89. 'nonceStr' => $nonceStr,
  90. 'signature' => sha1('jsapi_ticket=' . $jsApiTicket . '&noncestr=' . $nonceStr . '&timestamp=' . $timestamp . '&url=' . $url),
  91. 'jsApiList' => $jsApiList,
  92. 'openTagList' => $openTagList
  93. ];
  94. }
  95. /**
  96. * @desc 获取授权地址
  97. * @param string $scope 授权方式 snsapi_base|snsapi_userinfo
  98. * @param string|null $redirect
  99. * @return string
  100. */
  101. public function authUrl(string $scope = 'snsapi_userinfo', string $redirect = null): string {}
  102. /**
  103. * @desc 使用授权code换取openid
  104. * @param string $code
  105. * @return array
  106. */
  107. public function code2Openid(string $code): array {}
  108. /**
  109. * @desc 获取用户基础信息
  110. * @param string $accessToken
  111. * @param string $openid
  112. * @return array
  113. */
  114. public function userSimpleInfo(string $accessToken, string $openid): array {}
  115. /**
  116. * @desc 获取用户详细信息
  117. * @param string $openid
  118. * @return array
  119. */
  120. public function userDetailInfo(string $openid): array {}
  121. /**
  122. * @desc 获取 JSAPI TICKET
  123. * @return string
  124. */
  125. public function jsApiTicket() {}
  126. }