Base.php 4.1 KB

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