微信公众号授权
1.公众号后台配置授权域名比如(www.xiaozhu.com)
直接获取用户的openid
前端
created() { this.getCode(); }, methods: { getCode() {
//APPID 是公众号的appid
//https://www.xiaozhu.com/index/test/get_openid 授权获取openid
let local = 'https://www.xiaozhu.com/index/test/get_openid'; window.location.href = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + AppID + "&redirect_uri=" + encodeURIComponent(local) + "&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect"; } }
后端
public function get_openid(){
$code = input('param.code');
$url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$this->appid."&secret=".$this->secret."&code=".$code. "&grant_type=authorization_code";
$res = file_get_contents($url);
$data = json_decode($res, true);
var_dump($data);die;
}