AuthController.php 5.5 KB

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