<script>
	const jwx = require('jweixin-module')

	export default {
		onLaunch: async function() {
			try {
				await new Promise(resolve => {
					this.$nextTick(() => {
						resolve();
					});
				});
				let link = window.location.href;
				if (/code=/.test(link) || link.indexOf("code") > -1) {
					let temp = decodeURIComponent((new RegExp('[?|&]' + 'code' + '=' + '([^&;]+?)(&|#|;|$)').exec(
						link) || [, ''])[1].replace(/\+/g, '%20')) || null;
					let xcyjdt_lifeData = uni.getStorageSync('xcyjdt_lifeData');
					if (!xcyjdt_lifeData.vuex_token) {
						const loginRes = await this.$u.api.login({
							code: temp
						});
						this.$u.vuex('vuex_token', loginRes.token);

						const userRes = await this.$u.api.user();
						this.$u.vuex('vuex_user', userRes);

						let url = location.href.split('#')[0];
						let shareLink = "https://vr.langye.net/h5xcyjdt/";
						const shareRes = await this.$u.api.share({
							url
						});

						jwx.config({
							debug: false,
							appId: shareRes.appId,
							jsApiList: shareRes.jsApiList,
							nonceStr: shareRes.nonceStr,
							signature: shareRes.signature,
							timestamp: shareRes.timestamp,
						});

						await new Promise((resolve) => {
							jwx.ready(() => {
								jwx.updateAppMessageShareData({
									title: '人人讲安全、个个会应急',
									link: shareLink,
									desc: '——排查身边灾害隐患',
									imgUrl: 'https://vr.langye.net/h5xcyjdt/static/share.jpg',
									success: () => {
										console.log('updateAppMessageShareData-success');
									},
								});
								jwx.updateTimelineShareData({
									title: '人人讲安全、个个会应急',
									desc: '——排查身边灾害隐患',
									link: shareLink,
									imgUrl: 'https://vr.langye.net/h5xcyjdt/static/share.jpg',
									success: () => {
										console.log('updateTimelineShareData-success');
									}
								});
								resolve();
							});
						});
					} else {
						const userRes = await this.$u.api.user();
						this.$u.vuex('vuex_user', userRes);

						let url = location.href.split('#')[0];
						let shareLink = "https://vr.langye.net/h5xcyjdt/";
						const shareRes = await this.$u.api.share({
							url
						});

						jwx.config({
							debug: false,
							appId: shareRes.appId,
							jsApiList: shareRes.jsApiList,
							nonceStr: shareRes.nonceStr,
							signature: shareRes.signature,
							timestamp: shareRes.timestamp,
						});

						await new Promise((resolve) => {
							jwx.ready(() => {
								jwx.updateAppMessageShareData({
									title: '人人讲安全、个个会应急',
									link: shareLink,
									desc: '——排查身边灾害隐患',
									imgUrl: 'https://vr.langye.net/h5xcyjdt/static/share.jpg',
									success: () => {
										console.log('updateAppMessageShareData-success');
									},
								});
								jwx.updateTimelineShareData({
									title: '人人讲安全、个个会应急',
									desc: '——排查身边灾害隐患',
									link: shareLink,
									imgUrl: 'https://vr.langye.net/h5xcyjdt/static/share.jpg',
									success: () => {
										console.log('updateTimelineShareData-success');
									}
								});
								resolve();
							});
						});
					}
				} else {
					const appIdRes = await this.$u.api.getAppId();
					let redirect = encodeURIComponent(link.replace(/#\//, ""));
					console.log(redirect);
					window.location.href =
						`https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appIdRes.appid}&redirect_uri=${redirect}&response_type=code&scope=snsapi_base#wechat_redirect`;
				
				}
			} catch (error) {
				console.error('onLaunch 中出现错误:', error);
			}
		},
		onShow: function() {

		},
		onHide: function() {
			console.log('App Hide')
		}
		
	}
</script>

<style lang="scss">
	/*每个页面公共css */
	@import "uview-ui/index.scss";
</style>