yii框架 微信开发多域名中转服务

/**
 * openid中转服务,为了使公众帐号支持多个多个二级域名
 * 中转服务只负责和微信服务器通信,获取用户基本信息和openid,并将openid保存到session中
 * 调用过程:
 * 1.A.com 跳转到 openid.B.com
 * 2.openid.B.com将referer存储到session,跳转到微信服务器获取授权码
 * 3.微信服务器带授权码返回到openid.B.com 
 * 4.openid.B.com拿到openid, access_token写入到session,并从session中获取referrer,跳转回 A.com
 */
class IndexController extends Controller{

    public function actionIndex(){
        $fromUrl = Yii::$app->request->get('from_url');
        $wx = WxapiModel::getWx();
        $res = $wx->getOpenIdUserInfo();
        $res = json_decode($res, true);
        Yii::$app->session->set('openid', $res['openid']);
        Yii::$app->session->set('access_token_userinfo', $res['access_token']);
        if(!$fromUrl){
            return 'no referer';
        }
        return $this->redirect($fromUrl);
    }
    public function actionGetWxInfo(){
        $fromUrl = Yii::$app->request->get('from_url');
        $wx = WxapiModel::getWx();
        $res = $wx->getOpenIdUserInfo();
        $res = json_decode($res, true);
        Yii::$app->session->set('openid', $res['openid']);
        Yii::$app->session->set('access_token_userinfo', $res['access_token']);
        return json_encode([
            'oid' => $res['openid'],
            'access_token' => $res['access_token']
        ]);
        if(!$fromUrl){
            return 'no referer';
        }
        return $this->redirect($fromUrl);
    }
}

你可能感兴趣的:(微信)