AuthController.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. namespace mobile\controllers;
  3. use biz\user\services\UserService;
  4. use Yii;
  5. use common\components\util;
  6. use common\services\xhMerchantService;
  7. use common\services\xhUserService;
  8. use common\components\weixinUtil;
  9. /**
  10. * 微信 oauth 授权相关
  11. *
  12. */
  13. class AuthController extends PublicController
  14. {
  15. public function actionGetUserInfo()
  16. {
  17. $get = Yii::$app->request->get();
  18. $code = isset($get['code']) ? $get['code'] : '';
  19. if (empty($code)) {
  20. echo '没有获取用户code';
  21. util::end();
  22. }
  23. if (isset($get['state']) && $get['state'] == 'authdeny') {
  24. util::stop('auth fail');
  25. }
  26. $account = '';
  27. $params = $get['params'];
  28. $suffixUrl = base64_decode($params);
  29. $parse = parse_url($suffixUrl);
  30. if (isset($parse['query'])) {
  31. parse_str($parse['query'], $queryArray);
  32. if (isset($queryArray['account']) && !empty($queryArray['account'])) {
  33. $account = $queryArray['account'];
  34. }
  35. }
  36. //获取m.huahb.com/account/12358这类链接的商家id
  37. if (empty($account) && preg_match('/\/account\/(\d+)/', $suffixUrl, $match)) {
  38. $account = $match[1];
  39. }
  40. if (empty($account)) {
  41. util::stop('商店ID不存在!');
  42. }
  43. $merchant = xhMerchantService::getByAccount($account);
  44. if (empty($merchant)) {
  45. util::stop('商店不存在哦!');
  46. }
  47. $merchantId = $merchant['id'];
  48. $authScope = isset($get['authScope']) ? $get['authScope'] : '';
  49. if (empty($authScope)) {
  50. util::stop('没有授权类型');
  51. }
  52. $data = weixinUtil::getOpenId($code, $merchant);
  53. //获取code失败,重新获取
  54. if ($data === false) {
  55. $params = $_GET['params'];
  56. $url = 'http://' . $_SERVER['HTTP_HOST'] . '/auth/get-user-info?params=' . $params . '&authScope=' . $authScope;
  57. weixinUtil::getToken($url, $merchant, $authScope);
  58. util::end();
  59. }
  60. $openId = $data['openid'];
  61. $access_token = $data['access_token'];
  62. $user = UserService::getByOpenId($openId, $merchantId);
  63. //用户来源
  64. $userSource = Yii::$app->dict->getValue('userSourceGetId','official');
  65. $source = $userSource['name'];
  66. if (empty($user)) {
  67. //$authScope snsapi_base 公众号菜单打开网页(已经关注公众号,有完整信息)、打开付款页(有些客户只付款,没有后续可以不用登记完整信息),只需要获取openid
  68. $originalInfo = [];
  69. $originalInfo['openId'] = $openId;
  70. $originalInfo['merchantId'] = $merchantId;
  71. //没有关注公众号,需要获取用户完整信息的情况,则通过弹框授权
  72. if ($authScope == 'snsapi_userinfo') {
  73. $baseInfo = weixinUtil::authGetUserInfo($openId, $access_token);
  74. $originalInfo = array_merge($baseInfo,$originalInfo);
  75. $originalInfo['isFull'] = 1;
  76. $originalInfo['unionId'] = $baseInfo['unionid'];
  77. $originalInfo['headImgUrl'] = $baseInfo['headimgurl'];
  78. $originalInfo['nickName'] = $baseInfo['nickname'];
  79. }
  80. $user = UserService::replaceUser($originalInfo, $source, $merchantId);
  81. } else {
  82. if ($user['isFull'] == 0) {
  83. if ($authScope == 'snsapi_userinfo') {
  84. $baseInfo = weixinUtil::authGetUserInfo($openId, $access_token);
  85. $originalInfo = $baseInfo;
  86. $originalInfo['isFull'] = 1;
  87. $originalInfo['unionId'] = $baseInfo['unionid'];
  88. $originalInfo['headImgUrl'] = $baseInfo['headimgurl'];
  89. $originalInfo['openId'] = $baseInfo['openid'];
  90. $originalInfo['nickName'] = $baseInfo['nickname'];
  91. $originalInfo['merchantId'] = $merchantId;
  92. $user = UserService::replaceUser($originalInfo, $source, $merchantId);
  93. }
  94. }
  95. }
  96. //这个登陆没有经过父类方法配置全局变量,这里重新设置一次
  97. Yii::$app->params['userId'] = $user['id'];
  98. Yii::$app->params['user'] = $user;
  99. Yii::$app->params['merchantId'] = $merchantId;
  100. Yii::$app->params['merchant'] = $merchant;
  101. xhUserService::loginByThird($user);
  102. $session = Yii::$app->session;
  103. $session['visitingMerchantAccount'] = $merchant['id'];//以此判断是否访问其它商家,如果是用户需要重新登陆
  104. $url = 'http://' . $_SERVER['HTTP_HOST'] . $suffixUrl;
  105. $this->redirect($url);
  106. }
  107. }