解决微信公众号只能设置两个网页授权域名的究极优化版本 (转载)

  • 2022-02-26 22:36
  • by 刘管家
  • 3539
  • 0
  • 0

  

由于微信公众号关联了多个项目,而授权域名仅有两个可以设置,查边网络找到中转授权的方法,但是发现每个项目的回调地址都需要配置,然而这个回调地址我们每次在访问之前就已经知道了,并且特殊项目中这个回调地址还有参数需要添加,所以本人将此回调地址作为参数传到了中转站点中,这样可以避免每次有线项目接入时都需要去中转站点配置回调地址,做到了一次部署多次零成本使用目的

在调用中转站点时携带的回调地址参数需要用urlencode方法格式化网址,各后端语言应该都有该方法,如果是js访问,本人从网络中找到已封装好的js引入即可,js在文末提供

index.php
<?php
if(isset($_GET['param']) && !empty($_GET['param'])){
    $param = urldecode($_GET['param']);
    if(!strpos($param, '?')){
         $action = "huidiao.php?param=" .$param.'?1=1';
    }else{
         $action = "huidiao.php?param=" .$param;
    }
    //发起授权
    $appid = "你的APPID";  
    $redirect_url = "http://zhongzhuan.kmwl.com/".$action;
    $code_url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=".$appid."&redirect_uri=".urlencode($redirect_url)."&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect";
    header("location: ".$code_url);    
    die;
}else{
    echo "参数错误请携带参数名为param的参数";
}
huidiao.php
<?php
if(isset($_GET['code']) && !empty($_GET['code'])){
    $code = $_GET['code'];
    $param = $_GET['param'];
    header("location:".$param."&code=".$code);
}else{
    echo '参数错误为携带code';
}
服务端调用
 $appurl = "http://zhongzhuan.kmwl.com/index.php?param=".urlencode(你的回调地址);
header('location:' . $appurl);
js端调用
var url = "http://zhongzhuan.kmwl.com?param="+littleUrl.encode('你的回调地址') 
window.location.href = url
js访问地址转码代码
var littleUrl ={
		// public method for url encoding
		encode : function (string){
			return escape(this._utf8_encode(string));
		}
		,
		// public method for url decoding
		decode : function (string){
			return this._utf8_decode(unescape(string));
		}
		,
		// private method for UTF-8 encoding
		_utf8_encode : function (string){
			string = string.replace(/\r\n/g,"\n");
			var utftext = "";
			for (var n = 0; n < string.length; n++){
				var c = string.charCodeAt(n);
				if (c < 128){
					utftext += String.fromCharCode(c);
				}
				else
				if((c > 127) && (c < 2048)){
					utftext += String.fromCharCode((c >> 6) | 192);
					utftext += String.fromCharCode((c & 63) | 128);
				}
				else{
					utftext += String.fromCharCode((c >> 12) | 224);
					utftext += String.fromCharCode(((c >> 6) & 63) | 128);
					utftext += String.fromCharCode((c & 63) | 128);
				}
			}
			return utftext;
		}
		,
		// private method for UTF-8 decoding
		_utf8_decode : function (utftext){
			var string = "";
			var i = 0;
			var c = c1 = c2 = 0;
			while ( i < utftext.length ){
				c = utftext.charCodeAt(i);
				if (c < 128){
					string += String.fromCharCode(c);
					i++;
				}
				else
				if((c > 191) && (c < 224)){
					c2 = utftext.charCodeAt(i+1);
					string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
					i += 2;
				}
				else{
					c2 = utftext.charCodeAt(i+1);
					c3 = utftext.charCodeAt(i+2);
					string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
					i += 3;
				}
			}
			return string;
		}
	}
//export default littleUrl; //小程序,uniapp等框架需要暴露接口变量时使用


注:本文来源:转载,转载目的在于传递更多信息,并不代表本网赞同其观点和对其真实性负责。如有侵权行为,请联系我们,我们会及时删除。


评论