AuthController.php 5.5 KB

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