AuthController.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. namespace mobile\controllers;
  3. use biz\merchant\services\MerchantService;
  4. use biz\platform\services\WxOpenService;
  5. use biz\user\services\UserService;
  6. use common\components\jwt;
  7. use common\components\stringUtil;
  8. use Yii;
  9. use common\components\util;
  10. use common\services\xhMerchantService;
  11. use common\components\weixinUtil;
  12. use Lcobucci\JWT\Builder;
  13. use Lcobucci\JWT\Signer\Hmac\Sha256;
  14. use Lcobucci\JWT\Parser;
  15. use Lcobucci\JWT\ValidationData;
  16. /**
  17. * 微信 oauth 授权相关
  18. *
  19. */
  20. class AuthController extends PublicController
  21. {
  22. public $enableCsrfValidation = false;
  23. //准备授权 shish 2019.11.19
  24. public function actionPrepare()
  25. {
  26. $get = Yii::$app->request->get();
  27. $suffixUrl = isset($get['suffixUrl']) && !empty($get['suffixUrl']) ? $get['suffixUrl'] : '';
  28. if (empty($suffixUrl)) {
  29. util::fail('没有网址');
  30. }
  31. $account = isset($get['account']) ? $get['account'] : 0;
  32. $merchant = MerchantService::getById($account);
  33. if (empty($merchant)) {
  34. util::fail('商家无效');
  35. }
  36. /**
  37. * authScope 参数的说明
  38. * 1.用户通过公众号菜单打开网页,因为已经关注过了公众号,已经生成过用户信息,所以只要使用snsapi_base静默方式拿到openid就可以
  39. * 2.为了保证付款页的访问速度,暂时只要通过snsapi_base静默方式拿到openid先生成可以用的用户信息即可
  40. * 3.其它情况需要获取用户完整信息的,使用snsapi_userinfo让用户授权获取信息即可
  41. * 4.snsapi_base静默方式使用的场景更多,所以参数authScope默认使用snsapi_base
  42. */
  43. $authScope = isset($get['authScope']) ? $get['authScope'] : 'snsapi_base';
  44. $host = Yii::$app->request->getHostInfo();
  45. $suffixUrlEncode = stringUtil::urlSafeB64Encode($suffixUrl);
  46. $url = $host . '/auth/get-user-info?suffixUrl=' . $suffixUrlEncode . '&authScope=' . $authScope;
  47. Yii::info($url);
  48. weixinUtil::getToken($url, $merchant, $authScope);
  49. util::success();
  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. //这里要换成根据前端的数据来转 todo
  63. $account = 0;
  64. $suffixUrlEncode = $get['suffixUrl'];
  65. $suffixUrl = stringUtil::urlSafeBase64Decode($suffixUrlEncode);
  66. $newSuffixUrl = str_replace("#", "", $suffixUrl);
  67. $parse = parse_url($newSuffixUrl);
  68. if (isset($parse['query'])) {
  69. parse_str($parse['query'], $queryArray);
  70. if (isset($queryArray['account']) && !empty($queryArray['account'])) {
  71. $account = $queryArray['account'];
  72. }
  73. }
  74. if (empty($account)) {
  75. util::stop('商店ID无效!');
  76. }
  77. $merchant = xhMerchantService::getByAccount($account);
  78. if (empty($merchant)) {
  79. util::stop('商店无效');
  80. }
  81. $merchantId = $merchant['id'];
  82. $authScope = isset($get['authScope']) ? $get['authScope'] : '';
  83. if (empty($authScope)) {
  84. util::stop('没有授权类型');
  85. }
  86. $data = weixinUtil::getOpenId($code, $merchant);
  87. //获取code失败,重新获取
  88. if ($data === false) {
  89. $url = 'http://' . $_SERVER['HTTP_HOST'] . '/auth/get-user-info?suffixUrl=' . $suffixUrlEncode . '&authScope=' . $authScope;
  90. weixinUtil::getToken($url, $merchant, $authScope);
  91. util::end();
  92. }
  93. $openId = $data['openid'];
  94. $access_token = $data['access_token'];
  95. $user = UserService::getByOpenId($openId, $merchantId);
  96. //用户来源
  97. $userSource = Yii::$app->dict->getValue('userSourceGetId', 'official');
  98. $source = $userSource['name'];
  99. if (empty($user)) {
  100. //$authScope 默认是 snsapi_base 公众号菜单打开网页(已经关注公众号,有完整信息)、打开付款页(有些客户只付款,没有后续可以不用登记完整信息),只需要获取openid
  101. $originalInfo = [];
  102. $originalInfo['openId'] = $openId;
  103. $originalInfo['merchantId'] = $merchantId;
  104. //没有关注公众号,需要获取用户完整信息的情况,则通过弹框授权
  105. if ($authScope == 'snsapi_userinfo') {
  106. $baseInfo = weixinUtil::authGetUserInfo($openId, $access_token);
  107. $originalInfo = array_merge($baseInfo, $originalInfo);
  108. $originalInfo['isFull'] = 1;
  109. $originalInfo['unionId'] = $baseInfo['unionid'];
  110. $originalInfo['headImgUrl'] = $baseInfo['headimgurl'];
  111. $originalInfo['nickName'] = $baseInfo['nickname'];
  112. }
  113. $user = UserService::replaceUser($originalInfo, $source, $merchantId);
  114. } else {
  115. if ($user['isFull'] == 0) {
  116. if ($authScope == 'snsapi_userinfo') {
  117. $baseInfo = weixinUtil::authGetUserInfo($openId, $access_token);
  118. $originalInfo = $baseInfo;
  119. $originalInfo['isFull'] = 1;
  120. $originalInfo['unionId'] = $baseInfo['unionid'];
  121. $originalInfo['headImgUrl'] = $baseInfo['headimgurl'];
  122. $originalInfo['openId'] = $baseInfo['openid'];
  123. $originalInfo['nickName'] = $baseInfo['nickname'];
  124. $originalInfo['merchantId'] = $merchantId;
  125. $user = UserService::replaceUser($originalInfo, $source, $merchantId);
  126. }
  127. }
  128. }
  129. //这个的基类没有设置统一的全局变量,这里进行设置一次
  130. Yii::$app->params['userId'] = $user['id'];
  131. Yii::$app->params['user'] = $user;
  132. Yii::$app->params['merchantId'] = $merchantId;
  133. Yii::$app->params['merchant'] = $merchant;
  134. $host = Yii::$app->request->getHostInfo();
  135. //来源
  136. $sourceId = WxOpenService::getSourceId();
  137. $userId = $user['id'];
  138. //获取token
  139. $token = jwt::get($sourceId, $userId);
  140. $suffixUrl = strpos($suffixUrl, '?') === false ? $suffixUrl . '?token=' . $token : $suffixUrl . '&token=' . $token;
  141. $url = $host . '/' . $suffixUrl;
  142. $this->redirect($url);
  143. }
  144. }