| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556 |
- <?php
- namespace backend\controllers;
- use common\services\xhAdminService;
- use common\components\util;
- use Yii;
- use yii\web\Controller;
- use common\services\xhMerchantService;
- use common\services\xhUserService;
- use common\components\weixinUtil;
- use common\components\configDict;
- use common\components\stringUtil;
- use common\services\xhWxOpenService;
- /**
- * 微信 oauth 授权相关
- */
- class AuthController extends PublicController{
- public function actionGetUserInfo()
- {
- $get = Yii::$app->request->get();
- $code = isset($get['code']) ? $get['code'] : '';
- if(isset($get['state']) && $get['state'] == 'authdeny'){
- util::stop('auth fail');
- }
- $account = '';
- $params = $get['params'];
- $suffixUrl = base64_decode($params);
- $openMerchant = xhWxOpenService::getMerchant();
- $user = [];
- $baseInfo = [];
- $data = weixinUtil::getOpenId($code,$openMerchant);//没关注公众号,用户授权获取openId
- $openId = $data['openid'];
- $access_token = $data['access_token'];
- $admin = xhAdminService::getByOpenId($openId);
- if(empty($admin)){
- if($get['authScope'] == 'snsapi_userinfo'){
- //经测试,已经关注公众号的粉丝直接点击菜单上的链接时,不走下面的静默自动方式,走这条,也会以静默自动的方式处理获取openId;
- $baseInfo = weixinUtil::authGetUserInfo($openId,$access_token);//用户授权方式获取用户信息
- }else{
- $baseInfo = weixinUtil::autoGetUserInfo($openId,$openMerchant);//静默自动方式获取用户信息
- }
- $admin = xhAdminService::generateAdmin($baseInfo);
- }
- xhAdminService::loginByThird($admin);
- $url = 'http://'.$_SERVER['HTTP_HOST'].$suffixUrl;
- $this->redirect($url);
- }
- public function actionLogout()
- {
- xhAdminService::logout();
- echo 'ok';
- }
- }
|