AuthController.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. namespace mall\controllers;
  3. use biz\shop\classes\ShopClass;
  4. use bizHd\wx\classes\WxOpenClass;
  5. use bizMall\merchant\services\MerchantService;
  6. use bizMall\user\classes\UserClass;
  7. use bizMall\user\services\UserService;
  8. use common\components\httpUtil;
  9. use common\components\jwt;
  10. use common\components\stringUtil;
  11. use Yii;
  12. use common\components\util;
  13. use common\services\xhMerchantService;
  14. use common\components\wxUtil;
  15. use linslin\yii2\curl;
  16. use yii\helpers\Json;
  17. class AuthController extends PublicController
  18. {
  19. //准备授权 ssh 2019.11.19
  20. public function actionPrepare()
  21. {
  22. $get = Yii::$app->request->get();
  23. $url = isset($get['url']) && !empty($get['url']) ? $get['url'] : '';
  24. if (empty($url)) {
  25. util::fail('没有网址');
  26. }
  27. $account = httpUtil::getAccount($url);
  28. if (empty($account)) {
  29. util::fail('没有商家帐号');
  30. }
  31. $sj = MerchantService::getById($account);
  32. if (empty($sj)) {
  33. util::fail('商家无效');
  34. }
  35. /**
  36. * authScope 参数的说明
  37. * 1.用户通过公众号菜单打开网页,因为已经关注过了公众号,已经生成过用户信息,所以只要使用snsapi_base静默方式拿到openid就可以
  38. * 2.为了保证付款页的访问速度,暂时只要通过snsapi_base静默方式拿到openid先生成可以用的用户信息即可
  39. * 3.其它情况需要获取用户完整信息的,使用snsapi_userinfo让用户授权获取信息即可
  40. * 4.snsapi_base静默方式使用的场景更多,所以参数authScope默认使用snsapi_base
  41. */
  42. $authScope = isset($get['authScope']) ? $get['authScope'] : 'snsapi_base';
  43. $urlEncode = stringUtil::urlSafeB64Encode($url);
  44. //微信授权获取code ssh 2019.11.24
  45. wxUtil::getCode($urlEncode, $sj, $authScope);
  46. util::end();
  47. }
  48. //微信授权获取用户信息 ssh 2019.11.20
  49. public function actionGetUserInfo()
  50. {
  51. $get = Yii::$app->request->get();
  52. $code = isset($get['code']) ? $get['code'] : '';
  53. if (empty($code)) {
  54. util::fail('没有获取用户code');
  55. }
  56. if (isset($get['state']) && $get['state'] == 'authdeny') {
  57. util::fail('auth fail');
  58. }
  59. $urlEncode = $get['url'];
  60. $url = stringUtil::urlSafeBase64Decode($urlEncode);
  61. $account = httpUtil::getAccount($url);
  62. if (empty($account)) {
  63. util::stop('商店ID无效。');
  64. }
  65. $sj = xhMerchantService::getByAccount($account);
  66. if (empty($sj)) {
  67. util::stop('商店无效');
  68. }
  69. $sjId = $sj['id'];
  70. $authScope = isset($get['authScope']) ? $get['authScope'] : '';
  71. if (empty($authScope)) {
  72. util::stop('没有授权类型');
  73. }
  74. $data = wxUtil::getOpenId($code, $sj);
  75. if ($data === false) {
  76. //微信授权获取code ssh 2019.11.24
  77. wxUtil::getCode($urlEncode, $sj, $authScope);
  78. util::end();
  79. }
  80. $openId = $data['openid'];
  81. $access_token = $data['access_token'];
  82. $user = UserService::getByOpenId($openId);
  83. //用户来源
  84. $userSource = Yii::$app->dict->getValue('userSourceGetId', 'official');
  85. $source = $userSource['name'];
  86. if (empty($user)) {
  87. //$authScope 默认是 snsapi_base 公众号菜单打开网页(已经关注公众号,有完整信息)、打开付款页(有些客户只付款,没有后续可以不用登记完整信息),只需要获取openid
  88. $originalInfo = [];
  89. $originalInfo['openId'] = $openId;
  90. $originalInfo['sjId'] = $sjId;
  91. //没有关注公众号,需要获取用户完整信息的情况,则通过弹框授权
  92. if ($authScope == 'snsapi_userinfo') {
  93. $baseInfo = wxUtil::authGetUserInfo($openId, $access_token);
  94. $originalInfo = array_merge($baseInfo, $originalInfo);
  95. $originalInfo['isFull'] = 1;
  96. $originalInfo['unionId'] = $baseInfo['unionid'];
  97. $originalInfo['headImgUrl'] = $baseInfo['headimgurl'];
  98. $originalInfo['nickName'] = $baseInfo['nickName'];
  99. }
  100. $user = UserClass::replaceUser($originalInfo, $source, $sjId);
  101. } else {
  102. if ($user['isFull'] == 0) {
  103. if ($authScope == 'snsapi_userinfo') {
  104. $baseInfo = wxUtil::authGetUserInfo($openId, $access_token);
  105. $originalInfo = $baseInfo;
  106. $originalInfo['isFull'] = 1;
  107. $originalInfo['unionId'] = $baseInfo['unionid'];
  108. $originalInfo['headImgUrl'] = $baseInfo['headimgurl'];
  109. $originalInfo['openId'] = $baseInfo['openid'];
  110. $originalInfo['nickName'] = $baseInfo['nickName'];
  111. $originalInfo['sjId'] = $sjId;
  112. $user = UserClass::replaceUser($originalInfo, $source, $sjId);
  113. }
  114. }
  115. }
  116. //这个的基类没有设置统一的全局变量,这里进行设置一次
  117. $userId = $user['id'];
  118. //获取token
  119. $token = jwt::getNewToken($userId);
  120. $url = strpos($url, '?') === false ? $url . '?token=' . $token : $url . '&token=' . $token;
  121. $this->redirect($url);
  122. }
  123. //静默获取小程序用户信息
  124. public function actionMiniInfo()
  125. {
  126. $code = Yii::$app->request->get('code', '');
  127. if (empty($code)) {
  128. util::fail('没有CODE信息');
  129. }
  130. $sjWx = $this->sjWx;
  131. $appId = $sjWx['miniAppId'];
  132. $appSecret = $sjWx['miniAppSecret'];
  133. if (empty($appSecret)) {
  134. util::fail('没有找到小程序的密钥');
  135. }
  136. $url = "https://api.weixin.qq.com/sns/jscode2session?appid={$appId}&secret={$appSecret}&js_code={$code}&grant_type=authorization_code";
  137. $curl = new curl\Curl();
  138. $curl->setOption(CURLOPT_SSL_VERIFYPEER, false);
  139. $result = $curl->get($url);
  140. $arr = Json::decode($result);
  141. $sessionKey = isset($arr['session_key']) ? $arr['session_key'] : '';
  142. $miniOpenId = isset($arr['openid']) ? $arr['openid'] : '';
  143. if (empty($miniOpenId)) {
  144. util::fail('没有获取到用户的miniOpenId');
  145. }
  146. $user = UserClass::getByCondition(['miniOpenId' => $miniOpenId]);
  147. $cacheKey = 'MALL_MINI_SESSION_KEY_' . $miniOpenId;
  148. Yii::$app->redis->executeCommand('SET', [$cacheKey, $sessionKey]);
  149. $userId = $user['id'] ?? 0;
  150. $token = !empty($userId) ? jwt::getNewToken($userId) : '';
  151. util::success([
  152. 'token' => $token,
  153. 'user' => $user,
  154. //有小程序新版本提示更新
  155. 'update' => 1,
  156. 'miniOpenId' => $miniOpenId
  157. ]);
  158. }
  159. }