// h5授权
export function authorizeH5(mallid, groupId, urls, callback) {
	const value = uni.getStorageSync('member');
	console.log('授权后的会员号', value)
	if (value) {
		callback()
	} else {
		var datas = {
			mallid: mallid,
			groupId: groupId
		}
		this.$md(datas)
		uni.request({
			url: this.$baseURL + 'api/1.0/login/getAppIdByMallid',
			data: datas,
			method: 'POST',
			header: JSON.parse(uni.getStorageSync('handleUser')),
			success: (res) => {
				if (res.data.code === 0) {
					const data = res.data.data || ''
					this.$getCode(mallid, groupId, urls, callback, data.appid, data.componentAppid)
				} else {
					uni.showToast({
						title: res.data.msg,
						duration: 2000,
						icon: 'none'
					})
				}
			},
			fail: (err) => {
				console.log('------err', err)
			}
		});
	}
}
// 获取code
export function getCode(mallid, groupId, urls, callback, appid, componentAppid) {
	let origin = ''
	origin = this.$baseURLH5 + 'index.html#' + urls
	//网页授权的回调域名，这里设置的是本地端口

	let urlNow = encodeURIComponent(origin); //处理域名
	console.log('urlNow', urlNow)
	let scope = "snsapi_userinfo"; //弹框显示授权
	// 公众号的 appid
	// componentAppid 服务方的 appid，在申请创建公众号服务成功后，可在公众号服务详情页找到
	const code = getUrlCode('code') //获取code
	// 截取code
	if (code == null || code === '') { //未授权去授权
		let url =
			`https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appid}&redirect_uri=${urlNow}&response_type=code&scope=${scope}&state=STATE&component_appid=${componentAppid}#wechat_redirect`;
		window.location.href = url;
	} else {
		console.log('-------------code授权成功')
		// 发送code
		this.$postCode(mallid, groupId, urls, callback, code)
	}
}
// 截取url中的code方法
function getUrlCode(name) {
	let reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)')
	let r = window.location.search.substr(1).match(reg)
	if (r != null) {
		return unescape(r[2])
	}
	return null
}
// 发送code 获取会员信息
export function postCode(mallid, groupId, urls, callback, code) {
	// // const app = getApp()
	const app = {}
	var datas = {
		mallid: mallid,
		groupId: groupId,
		scope: 'snsapi_userinfo',
		code,
		source: '2'
	}
	this.$md(datas)
	uni.request({
		url: this.$baseURL + "api/1.0/login/getOpenIdAndHeadimg",
		method: 'POST',
		data: datas,
		header: JSON.parse(uni.getStorageSync('handleUser')),
		success: (res) => {
			if (res.data.code == 0) {
				console.log('-----------code0获取会员信息', res.data.data)
				//res里面包含用户信息  openid等
				app.globalData.member = res.data.data.member
				app.globalData.wxMember = res.data.data.wxMember
				app.globalData.userInfo = res.data.data.userinfo || {}
				uni.setStorage({
					key: 'wxMember',
					data: JSON.stringify(res.data.data.wxMember),
					success: function () {
						console.log('userinfo-success');
					}
				});
				// 存储会员信息
				uni.setStorage({
					key: 'member',
					data: JSON.stringify(res.data.data.member),
					success: function () {
						console.log('userinfo-success');
					}
				});
				try {
					const member = uni.getStorageSync('member');
					if (member) {
						var token = JSON.parse(member).mobile
						console.log('token', token)
					}
				} catch (e) {
					// error
				}
				// 存储userInfo
				uni.setStorage({
					key: 'userinfo',
					data: JSON.stringify(res.data.data.userinfo),
					success: function () {
						console.log('userinfo-success');
					}
				});
				try {
					const value = uni.getStorageSync('userinfo');
					if (value) {
						var openId = JSON.parse(value).openid
						app.globalData.openId = openId
						uni.setStorageSync('openId', openId)
						console.log('openId', openId)
					}
				} catch (e) {
					// error
				}
				console.log('授权后的会员号0')
				callback()
			} else if (res.data.code == 1005) {
				console.log('-----------code1005获取会员信息', res.data.data)
				app.globalData.userInfo = res.data.data.userinfo
				uni.setStorage({
					key: 'userinfo',
					data: JSON.stringify(res.data.data.userinfo),
					success: function () {
						console.log('userinfo-success');
					}
				});
				try {
					const value = uni.getStorageSync('userinfo');
					if (value) {
						var openId = JSON.parse(value).openid //openid
						app.globalData.openId = openId
						uni.setStorageSync('openId', openId)
					}
				} catch (e) {
					// error
				}
				console.log('---------注册登录')
				// 仅限于集字页面的判断
				console.log('仅限于集字页面的查看分享人手机号判断执行的回调...', this.$getLocationValue(urls, 'sharePhone'))
				if (this.$getLocationValue(urls, 'sharePhone')) {
					console.log('-----------有分享sharePhone', urls)
					callback()
				} else {
					// 注册登录
					window.location.href = this.$baseURLH5 + 'index.html#/pages/loginPubilc/loginH5?url=' + encodeURIComponent(JSON.stringify(urls))
				}
				// window.location.href = this.$baseURLH5 + 'index.html#/pages/loginPubilc/loginH5?url='+encodeURIComponent(JSON.stringify(urls))
			} else if (res.data.code == 1006) {
				console.log('-----------code1006获取会员信息', res.data.data)
				// 有会员查活动
				try {
					const member = uni.getStorageSync('member');
					if (member) {
						callback()
					} else {
						authorizeH5(mallid, groupId, urls, callback)
					}
				} catch (e) {
					// error
				}
			} else {
				uni.showToast({
					title: res.data.msg,
					duration: 2000,
					icon: 'none'
				})
			}
		}
	});
}
