网页授权
参考文档
参考代码
<?php
namespace app\official\controller;
use think\Controller;
use EasyWeChat\Factory;
use addons\wechat\library\Config;
class Auth extends Controller
{
protected $weChat = null;
public function __construct()
{
parent::__construct();
$this->weChat = Factory::officialAccount(Config::load());
}
public function index()
{
if(empty(session('wechat_user')))
{
$result = $this->weChat->oauth->scopes(['snsapi_userinfo'])->redirect();
return $result->send();
}else{
echo '欢迎回来';
}
}
public function login()
{
$openid = $this->weChat->oauth->user()->getId();
if(empty($openid))
{
$this->error('获取openid失败');
}
$user = $this->weChat->user->get($openid);
session('wechat_user',$user);
$this->success('授权成功',url('/official/auth/index'));
}
}