- 当前方法应只适用跳转回原小程序
- 小程序需要配置当前网页部署的域名到业务域名,不配置的话webview中打不开网页
- 公众号需要进行关联小程序,需要配置当前网页部署的域名到JS安全域名
参数 | 说明 (参数传递需要使用 encodeURIComponent 方法进行编码) |
---|
code | 微信授权返回后携带 |
GzhappId | 微信公众号的appid |
type | 1 跳转小程序 / 2 跳转网页 |
fullPath | 跳转路径 |
hj | 环境 (在区分环境的条件下传递配合其它扩展参数,例如在获取到code之后的获取用户openid的操作来区分生产/测试环境的接口前缀) |
- 上述参数需要以?拼接的形式传递
- 部署路径?GzhappId=微信公众号的appid&type=1&fullPath=跳转路径&hj=dev
- 参数如有特殊字符与url冲突需使用 encodeURIComponent 方法进行编码
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>授权</title>
<script charset="utf-8" src="https://res.wx.qq.com/open/js/jweixin-1.3.2.js"></script>
</head>
<body>
</body>
<script>
window.onload = async function() {
var obj = getUrlparam();
console.log(obj)
if (obj.get('code')) {
if (obj.get('type') == "1") {
try {
wx.miniProgram.reLaunch({
url: decodeURIComponent(obj.get('fullPath'))
})
} catch (ex) {
alert("错误信息:" + JSON.stringify(ex))
}
return;
}
if (obj.get('type') == "2") {
try {
window.location.replace(decodeURIComponent(obj.get('fullPath')))
} catch (ex) {
alert("错误信息:" + JSON.stringify(ex))
}
return;
}
} else {
var authorize = "https://open.weixin.qq.com/connect/oauth2/authorize";
authorize = authorize + '?appid=' + obj.get('GzhappId');
authorize = authorize + '&redirect_uri=' + encodeURIComponent(window.location.href);
window.location.href = authorize + '&response_type=code&scope=snsapi_userinfo#wechat_redirect';
}
}
function getUrlparam() {
let url = window.location.href;
let p = url.split('?')[1];
if (!p) {
var curl = decodeURIComponent(url);
p = curl.split('?')[1];
}
let params = new URLSearchParams(p);
return params;
}
</script>
</html>