关于uniapp授权以及参数截取

117 阅读1分钟

关于uniapp授权以及参数截取

//进入页面进行授权
const _this = this
const {
	code
	} = _this.getCode(window.location.href)
	this.showCode = code
	if (code) {
	// 获取userOpenId
	uni.post('/juhuimp/wxPublicAuth', {
	code: code
	}).then((res) => {
	if (res.status == '200') {
	if (res.data.token) {
	const _customerOpenId = res.data.userOpenId
	app.globalData.userOpenId = _customerOpenId
	app.globalData.token = res.data.token
	uni.getStorage({
	key: 'activityId',
	success: function(resGetStorage) {
	const _activityId = resGetStorage.data.split('?')[1].split(
	'=')[1].split('#')[0]
	// const {activited}=_this.getCode(resGetStorage.data)
	// 请求活动商品列表
        _this.placeActiveList(_customerOpenId, _activityId)
        }
});
}
}
}).catch(err => {
console.log(err, 'ooooo')
uni.showToast({
title: '获取OpenId失败',
icon: 'none'
})
})
} else {
uni.setStorage({
key: 'activityId',
data:window.location.href
});
let userInfoCodeUrl =
`https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appId}&redirect_uri=${host}&response_type=code&scope=snsapi_base&state=STATE#connect_redirect=1#wechat_redirect`
window.location.href = userInfoCodeUrl
}
//截取参数的方法
getCode(appletUrl) {
		const code = appletUrl.split('?')[1]
		if (!code) return {}
		const obj = {}
		code.split('&').forEach(item => {
		const arr = item.split('=')
		obj[arr[0]] = arr[1]
		})
		 return obj
	},