privacyGps.js 8.3 KB
// middlePage/privacyGps/privacyGps.js
const app = getApp();

Page({
	data: {
		showPrivacy: false,
		showGps: false,
		contentPrivacyImage: decodeURIComponent(
			'https%3A%2F%2Fucode-cdn.aax6.cn%2Ffront-end%2Fproject%2Fjunyao%2Fminiprogram%2Ftip_privacy.png'
			),
		btnPrivacyAgree: decodeURIComponent(
			'https%3A%2F%2Fucode-cdn.aax6.cn%2Ffront-end%2Fproject%2Fjunyao%2Fminiprogram%2Fbtn_agree.png'),
		btnPrivacyReject: decodeURIComponent(
				'https%3A%2F%2Fucode-cdn.aax6.cn%2Ffront-end%2Fproject%2Fjunyao%2Fminiprogram%2Fbtn_reject.png'
				),
		contentGpsImage: decodeURIComponent(
			'https%3A%2F%2Fucode-cdn.aax6.cn%2Ffront-end%2Fproject%2Fjunyao%2Fminiprogram%2Ftip_gps.png'),
	},

	onLoad(options) {
		// options.q='HTTP://JY.AAX6.CN/Y/FXYRLD*C3MVD2-0JS7'

		// latitude=34.80829&longitude=118.05272&openId=ozn1a5I4Ie_Bv3gITzMPgcowEFOU&q=HTTP://AAX6.CN/JY/DT5I+67$R$:I$70-SJ2J&bgImage=https%3A%2F%2Fucode-cdn.aax6.cn%2Ffront-end%2Fproject%2Fjunyao%2Fminiprogram%2Fbg.png&contentPrivacyImage=https%3A%2F%2Fucode-cdn.aax6.cn%2Ffront-end%2Fproject%2Fjunyao%2Fminiprogram%2Ftip_privacy.png&btnPrivacyAgree=https%3A%2F%2Fucode-cdn.aax6.cn%2Ffront-end%2Fproject%2Fjunyao%2Fminiprogram%2Fbtn_agree.png&btnPrivacyReject=https%3A%2F%2Fucode-cdn.aax6.cn%2Ffront-end%2Fproject%2Fjunyao%2Fminiprogram%2Fbtn_reject.png&contentGpsImage=https%3A%2F%2Fucode-cdn.aax6.cn%2Ffront-end%2Fproject%2Fjunyao%2Fminiprogram%2Ftip_gps.png&btnGpsSetting=https%3A%2F%2Fucode-cdn.aax6.cn%2Ffront-end%2Fproject%2Fjunyao%2Fminiprogram%2Ftip_gps.png&btnGpsReject=https%3A%2F%2Fucode-cdn.aax6.cn%2Ffront-end%2Fproject%2Fjunyao%2Fminiprogram%2Ftip_gps.png&path=https%3A%2F%2Fucode-test-cdn.aax6.cn%2Ffront-end%2Fproject%2Fjunyao%2FjunyaoH5%2Findex.html%23%2Fhome&title=%E5%91%B3%E5%8A%A8%E5%8A%9B

		// let opt = {
		//   latitude: '34.80829',
		//   longitude: '118.05272',
		//   openId: 'ozn1a5I4Ie_Bv3gITzMPgcowEFOU',
		//   q: 'HTTP://AAX6.CN/JY/DT5I+67$R$:I$70-SJ2J',
		//   bgImage: 'https%3A%2F%2Fucode-cdn.aax6.cn%2Ffront-end%2Fproject%2Fjunyao%2Fminiprogram%2Fbg.png',
		//   contentPrivacyImage: 'https%3A%2F%2Fucode-cdn.aax6.cn%2Ffront-end%2Fproject%2Fjunyao%2Fminiprogram%2Ftip_privacy.png',
		//   btnPrivacyAgree: 'https%3A%2F%2Fucode-cdn.aax6.cn%2Ffront-end%2Fproject%2Fjunyao%2Fminiprogram%2Fbtn_agree.png',
		//   btnPrivacyReject: 'https%3A%2F%2Fucode-cdn.aax6.cn%2Ffront-end%2Fproject%2Fjunyao%2Fminiprogram%2Fbtn_reject.png',
		//   contentGpsImage: 'https%3A%2F%2Fucode-cdn.aax6.cn%2Ffront-end%2Fproject%2Fjunyao%2Fminiprogram%2Ftip_gps.png',
		//   btnGpsSetting: 'https%3A%2F%2Fucode-cdn.aax6.cn%2Ffront-end%2Fproject%2Fjunyao%2Fminiprogram%2Ftip_gps.png',
		//   btnGpsReject: 'https%3A%2F%2Fucode-cdn.aax6.cn%2Ffront-end%2Fproject%2Fjunyao%2Fminiprogram%2Ftip_gps.png',
		//   path: 'https%3A%2F%2Fucode-test-cdn.aax6.cn%2Ffront-end%2Fproject%2Fjunyao%2FjunyaoH5%2Findex.html%23%2Fhome',
		//   title: '%E5%91%B3%E5%8A%A8%E5%8A%9B',
		// };
		// options = opt

		let transitionStrArr = []; // 登录后跳转的参数
		Object.keys(JSON.parse(JSON.stringify(options))).map((keys) => {
			transitionStrArr.push(`${keys}=${JSON.parse(JSON.stringify(options))?.[keys]}`);
		});
		this.transitionStr = transitionStrArr.join('&');

		let decodeOptions = {}; // 当前页面参数
		Object.keys(JSON.parse(JSON.stringify(options))).map((item) => {
			decodeOptions[item] = decodeURIComponent(JSON.parse(JSON.stringify(options))?.[item]);
		});
		const {
			isLogin,
			bgImage,
			contentPrivacyImage,
			btnPrivacyAgree,
			btnPrivacyReject,
			contentGpsImage,
			btnGpsSetting,
			btnGpsReject,
			path,
			title,
			...others
		} = decodeOptions;
		this.options = others; // 逻辑参数
		if (isLogin) this.isLogin = true;
		this.setData({
			bgImage,
			contentPrivacyImage,
			btnPrivacyAgree,
			btnPrivacyReject,
			contentGpsImage,
			btnGpsSetting,
			btnGpsReject,
			path,
			title
		});

		// 设置页面title
		wx.setNavigationBarTitle({
			title,
			success: (result) => {},
			fail: () => {},
			complete: () => {},
		});
	},

	onShow() {
		// 查询是否需要展示隐私条款弹窗

		console.error('onshow --onShow');

		wx.getPrivacySetting({
			success: (res) => {
				if (res.needAuthorization) {
					// 用户未同意隐私政策,提示用户同意
					this.setData({
						showPrivacy: true
					});
					// wx.onNeedPrivacyAuthorization((resolve) => {
					//   this.resolvePrivacyAuthorization = resolve;
					// });
				} else {
					// 用户已同意隐私政策,调用后台数据获取接口
					this.getLocation();
				}
			},
			fail() {
			},
		});
	},

	// 打开隐私条款页面
	openPrivacyContract() {
		wx.openPrivacyContract({
			success: () => {}, // 打开成功
			fail: () => {}, // 打开失败
			complete: () => {},
		});
	},

	// 隐私条款 拒绝
	privacyReject() {
		// this.resolvePrivacyAuthorization({ event: 'disagree' });
		wx.exitMiniProgram({});
	},
	// 隐私条款 同意
	privacyAgree() {
		// this.resolvePrivacyAuthorization({ buttonId: 'agree-btn', event: 'agree' });
		this.setData({
			showPrivacy: false
		});
		this.getLocation();
	},

	async getLoginCode() {
		return new Promise((resolve, reject) => {
			wx.login({
				success: (result) => {
					resolve(result.code);
				},
				fail: () => {
					reject(false);
				},
				complete: () => {},
			});
		});
	},

	// 获取gps
	getLocation() {
		wx.getSetting({
			success: (res) => {
				if (!res.authSetting['scope.userFuzzyLocation']) {
					// 用户未授权,请求授权
					wx.authorize({
						scope: 'scope.userFuzzyLocation',
						success: () => {
							console.log(1111);
							// 用户已授权,调用 getFuzzyLocation
							this.getFuzzyLocation();
						},
						fail: (err) => {
							console.error('授权失败:', err);
							wx.showModal({
								title: '提示',
								content: '需要获取您的地理位置权限,请前往设置开启',
								success: (modalRes) => {
									if (modalRes.confirm) {
										wx.openSetting(); // 打开设置页面
									}
								},
							});
						},
					});
				} else {
					// 用户已授权,直接调用 getFuzzyLocation
					this.getFuzzyLocation();
				}
			},
			fail: (err) => {
				console.error('获取设置失败:', err);
			},
		})
	},
	getFuzzyLocation() {
		let userInfo = getApp().globalData.userInfo
		if(userInfo?.token){
			//用户已登录
			let qrCode = "HTTP://JY.AAX6.CN/Y/NZU-NWL.2BDJ8F$:IA,0000003101"
			let id = "33426877997676"
			let pageData = encodeURIComponent(JSON.stringify(Object.assign({ qrCode,id }, {})))
			wx.navigateTo({
				url: `/scanGroup/dahaoda/index/index?pageData=${pageData}`
			})
		}
		else{
			//未登录
			wx.getFuzzyLocation({
				type: 'wgs84',
				success: async (res) => {
					const latitude = res.latitude;
					const longitude = res.longitude;
					this.jumpFun({
						latitude,
						longitude
					});
				},
				fail: (err) => {
					console.log(err,"err");
					const latitude = 22.793763;
					const longitude = 108.346942;
					this.jumpFun({
						latitude,
						longitude
					});
					return
					if (this.isLogin) {
						this.gpsReject();
					} else {
						this.setData({
							showGps: true
						});
					}
				},
			});
		}
	},
	// 跳转逻辑
	async jumpFun(latLong) {
		const {
			latitude,
			longitude
		} = latLong || {};

		const subData = wx.getStorageSync('subData');

		if (!subData || !Object.keys(subData)?.length) {
			wx.reLaunch({
				url: `/pages/login/login?router=${encodeURIComponent(`/middlePage/pages/privacyGps/privacyGps?isLogin=${true}&${this.transitionStr}`)}`,
			});
		} else {
			const code = await this.getLoginCode();
			let query = latLong ?
				`t=${+new Date()}&code=${code}&latitude=${latitude}&longitude=${longitude}` :
				`code=${code}`;
			Object.keys(subData).map((v) => {
				if (!query.includes(`${v}=`)) query += `&${v}=${subData[v]}`;
			});
			if (this.options) {
				Object.keys(this.options).map((v) => {
					if (!query.includes(`${v}=`)) query += `&${v}=${this.options[v]}`;
				});
			}
			const webUrl = `${this.data.path}?${query}`;

			this.setData({
				webUrl,
				showGps: false,
				showPrivacy: false
			});
		}
	},

	// gps 拒绝
	gpsReject() {
		this.getLocation();
		// this.setData({ showGps: false, showPrivacy: false });
		// this.jumpFun();
	},
	// gps 同意
	gpsSetting() {
		wx.openSetting({
			success: (result) => {
				console.log(result, "result");
			},
			fail: () => {
			},
			complete: () => {
				this.getLocation();
			},
		});
	},

	onShareAppMessage() {},
});