微信公众号网页授权

  • 当前方法应只适用跳转回原小程序
  • 小程序需要配置当前网页部署的域名到业务域名,不配置的话webview中打不开网页
  • 公众号需要进行关联小程序,需要配置当前网页部署的域名到JS安全域名
参数说明 (参数传递需要使用 encodeURIComponent 方法进行编码)
code微信授权返回后携带
GzhappId微信公众号的appid
type1 跳转小程序 / 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>
		<!-- 微信sdk -->
		<script charset="utf-8" src="https://res.wx.qq.com/open/js/jweixin-1.3.2.js"></script>
		<!-- 获取请求时引入 -->
		<!-- <script src="https://unpkg.com/axios/dist/axios.min.js"></script> -->
	</head>
	<body>
	</body>
	<script>
		window.onload = async function() {
			// url携带的参数说明
			// code  微信授权返回的code
			// 以下参数传参时需要 encodeURIComponent()
			// GzhappId  微信公众号的appid   
			// type    1 跳转小程序       2 跳转网页
			// fullPath  完整路径例子 /pages/index/index     http://123.com/ddd
			// hj        环境      dev 测试     pred 预发布     prod 发布
			// 根据需要来扩展其它字段

			var obj = getUrlparam();
			console.log(obj)
			if (obj.get('code')) {
				if (obj.get('type') == "1") {
					try {

						// var jump = decodeURIComponent(obj.get('fullPath'))
						// if (jump == '/pages/index/index') { // 判断是底部导航
						// 	uni.miniProgram.switchTab({
						// 		url: jump
						// 	})
						// } else {
						// 	wx.miniProgram.navigateTo({
						// 		 url:jump
						// 	})
						// }
						// 小程序跳转路径不能有错,例如首页/pages/index/index 使用encodeURIComponent 方法进行编码后通过fullPath参数传递
						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')))
						// window.location.href = 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';
			}
		}

		// window.location.href 获取地址
		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>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值