AuthController.php 7.2 KB

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