早睡早起,方能养生
Sleep early rise early, way to keep healthy

easywechat公众号授权登录

super
2021-01-07 20:19
views 3290

环境:easywechat4.x     ThinkPHP5.1

public function initialize()
{
    parent::initialize(); // TODO: Change the autogenerated stub

    $config = [
        'app_id' => 'xx',
        'secret' => 'xx',
        'response_type' => 'array',
    ];
    $app = Factory::officialAccount($config);

    if (!session('?user_id')) {
        $code = input('code', '');
        if (!$code) {
            // 跳转授权页面
            $app->oauth->scopes(['snsapi_userinfo'])
                ->redirect('http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'])->send();
        }
        $user = $app->oauth->user();
        if ($user['id']) {
            $user_info = Db::name('user')->where('open_id', $user['id'])->find();
            if (!$user_info) exit('用户信息不存在');
            session('user_id', $user_info['user_id']);
        }
    }
}


分享
0 条讨论
top