微信公众平台授权登录篇之静默授权

index.html

<button onclick="jump">点击静默授权</button>

<script>
	function jump(){
		//微信公众平台配置参数
		let appid  = "xxxxxxxxxxxx",
			secret = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
			
		//微信登录回调地址(接受回调code的值)
		let redirect_uri = encodeURIComponent("http://www.xxxh5.com/lj/test/callback.php"); //该域名必须和公众平台"网页授权域名"配置保持一致
		
		**//静默授权 scope = snsapi_base**
		window.location.href = "https://open.weixin.qq.com/connect/oauth2/authorize?appid="+appid+"&redirect_uri="+redirect_uri+"&response_type=code&scope=snsapi_base&state=123#wechat_redirect";
	}
	
	
	//授权获得参数后由callback.php返回给前端
	if( getUrlParams('openid') ){
		console.log( 'openid = ' + getUrlParams('openid')  )
		console.log( 'access_token = ' + getUrlParams('access_token')  )
	}
	//获取url参数方法
	function getUrlParams(variable)
	{
       var query = window.location.search.substring(1);
       var vars = query.split("&");
       for (var i=0;i<vars.length;i++) {
               var pair = vars[i].split("=");
               if(pair[0] == variable){return pair[1];}
       }
       return
   }

</script>

callback.php

<?php

// 微信公众平台配置参数
$appid  = "xxxxxxxxxxxx";
$secret = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx";

//获取微信回调code
$code = $_REQUEST['code'];

//用code置换openid,access_token
$get_openid_url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$appid."&secret=".$secret."&code=".$code."&grant_type=authorization_code";
$result = DO_POST($get_openid_url);
$result_arr=array();
$result_arr = json_decode($result,true);

$openid=$result_arr['openid'];
$access_token=$result_arr['access_token'];

//没获取到openid,重新再来一次,回到index.html文件
if(empty($openid)){
	echo "<script> window.location.href = http://192.168.101.7/demo/wxlogin/test/index.html</script>";
	exit;
}

$bakpath = "http://192.168.101.7/demo/wxlogin/test/index.html?openid=".$openid."&access_token=".$access_token;

//方法
function DO_POST( $url,$post='',$cookie='',$returnCookie=0 ){
	$curl = curl_init();
	curl_setopt($curl, CURLOPT_URL, $url);
	curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/6.0)');
	curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);    //自动301,302跳转;
	curl_setopt($curl, CURLOPT_AUTOREFERER, 1);
	if($post) {
	    curl_setopt($curl, CURLOPT_POST, 1);
	    curl_setopt($curl, CURLOPT_POSTFIELDS, $post);
	}
	if($cookie) {
	    curl_setopt($curl, CURLOPT_COOKIE, $cookie);
	}
	curl_setopt($curl, CURLOPT_HEADER, $returnCookie);
	curl_setopt($curl, CURLOPT_TIMEOUT, 30);
	curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
	$data = curl_exec($curl);
	if (curl_errno($curl)) {
	    return curl_error($curl);
	}
	curl_close($curl);
	if($returnCookie){
	    list($header, $body) = explode("\r\n\r\n", $data, 2);
	    preg_match_all("/Set\-Cookie:([^;]*);/", $header, $matches);
	    $info['cookie']  = substr($matches[1][0], 1);
	    $info['content'] = $body;
	    return $info;
	}
	else{
	    return $data;
	}
}

?>
<script>
	//跳转到index.html文件
	window.location.href="<?php echo $bakpath;?>";
</script>

按理来说可以用纯前端实现获取用户openid、access_token,
但是回调获取code后需要请求微信官方接口(跨域)置换openid、access_token,跨域问题不好解决。所以就用了前后端分离的方式

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值