Wechat.php 923 字节
<?php


namespace app\common\controller;

use app\common\controller\Frontend;
use EasyWeChat\Foundation\Application;
use think\Cache;
use think\Request;
use think\Session;

/**
 * 微信公众号控制器基类
 */
class Wechat extends Frontend
{

    protected $app = null;
    protected $config = null;

    public function _initialize()
    {
        parent::_initialize(); // TODO: Change the autogenerated stub
        $this->config = \config('wechat');
        $this->app = new Application($this->config);
        $this->oauth();
    }

    public function oauth() {
        $user = Session::get('user');
        if(!$user) {
            $request = Request::instance();
            Session::set('target_url',$request->url(true));
            $response = $this->app->oauth->scopes(['snsapi_userinfo'])
                ->redirect();
            $response->send();
        }
    }
}