AuthController.php 6.2 KB

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