AuthController.php 6.0 KB

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