uniapp在微信环境内静默授权

	let ua = navigator.userAgent.toLowerCase()
				let that = this
				if (ua.match(/MicroMessenger/i) == "micromessenger") {
					let code = this.GetQueryString('code');
					if (code == null) {
						window.location.href =
							'https://open.weixin.qq.com/connect/oauth2/authorize?appid=w**********e&redirect_uri=http%3A%2F%2Fymh.frp.dzkandian.com%2F&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect'
						code = this.GetQueryString('code');
					} else {
						let obj = {
							code: code
						}
						Logins(obj).then(res => {
							uni.setStorageSync('token', res.data.token);
							that.$isResolve()
						})
					}
				} else {
					uni.showModal({
						title: '提示',
						content: '非微信浏览器',
						success: function(res) {
							if (res.confirm) {
								console.log('用户点击确定');
							} else if (res.cancel) {
								console.log('用户点击取消');
							}
						}
					});
				}

1.首先判断是否在微信环境

2.如果是在微信环境通过'https://open.weixin.qq.com/connect/oauth2/authorize?appid=w**********e&redirect_uri=http%3A%2F%2Fymh.frp.dzkandian.com%2F&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect拿到code进行登录其中appid为公众号的APPID 参数redirect_uri是通过 urlEncode 处理后的授权后重定向的回调链接地址

3.然后通过

	GetQueryString(name) {
				var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
				var r = window.location.search.substr(1).match(reg);
				if (r != null) return unescape(r[2]);
				return null;
			},

即可拿到code

上一篇:Android O新特性和行为变更总结zz


下一篇:TinyFrame升级之一:框架概览