easywechat公众号授权登录
super
2021-01-07 20:19
3253
环境: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 条讨论