AuthController.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. <?php
  2. namespace backend\controllers;
  3. use common\services\xhAdminService;
  4. use common\components\util;
  5. use Yii;
  6. use yii\web\Controller;
  7. use common\services\xhMerchantService;
  8. use common\services\xhUserService;
  9. use common\components\weixinUtil;
  10. use common\components\configDict;
  11. use common\components\stringUtil;
  12. use common\services\xhWxOpenService;
  13. /**
  14. * 微信 oauth 授权相关
  15. */
  16. class AuthController extends PublicController{
  17. public function actionGetUserInfo()
  18. {
  19. $get = Yii::$app->request->get();
  20. $code = isset($get['code']) ? $get['code'] : '';
  21. if(isset($get['state']) && $get['state'] == 'authdeny'){
  22. util::stop('auth fail');
  23. }
  24. $account = '';
  25. $params = $get['params'];
  26. $suffixUrl = base64_decode($params);
  27. $openMerchant = xhWxOpenService::getMerchant();
  28. $user = [];
  29. $baseInfo = [];
  30. $data = weixinUtil::getOpenId($code,$openMerchant);//没关注公众号,用户授权获取openId
  31. $openId = $data['openid'];
  32. $access_token = $data['access_token'];
  33. $admin = xhAdminService::getByOpenId($openId);
  34. if(empty($admin)){
  35. if($get['authScope'] == 'snsapi_userinfo'){
  36. //经测试,已经关注公众号的粉丝直接点击菜单上的链接时,不走下面的静默自动方式,走这条,也会以静默自动的方式处理获取openId;
  37. $baseInfo = weixinUtil::authGetUserInfo($openId,$access_token);//用户授权方式获取用户信息
  38. }else{
  39. $baseInfo = weixinUtil::autoGetUserInfo($openId,$openMerchant);//静默自动方式获取用户信息
  40. }
  41. $admin = xhAdminService::generateAdmin($baseInfo);
  42. }
  43. xhAdminService::loginByThird($admin);
  44. $url = 'http://'.$_SERVER['HTTP_HOST'].$suffixUrl;
  45. $this->redirect($url);
  46. }
  47. public function actionLogout()
  48. {
  49. xhAdminService::logout();
  50. echo 'ok';
  51. }
  52. }