<script>
	import wx from 'weixin-js-sdk';
	import {
		SY
	} from '@/api/index.js'
	import {
		HTTP
	} from '@/api/http.js'
	import {
		setUserInfo, //存储用户信息
		setToken //储存用户token
	} from "@/utils/auth.js";
	import {
		jieqiFn,
		BASE_URL,
		CDX,
		appId
	} from '@/config'
	export default {
		onLaunch: function() {
			console.log('App Launch')
		},
		onShow: function() {
			console.log('App Show')
		},
		onHide: function() {
			console.log('App Hide')
		},
		data() {
			return {
				page: "",
				code: "",
				data: undefined,
				url: undefined
			};
		},
		methods: {
			// 获取链接上带的参数
			getQueryString(name) {
				var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
				var r = window.location.search.substr(1).match(reg);
				console.log(window.location.search, '我是链接里携带的参数');
				if (r != null) {
					return unescape(r[2]);
				}
				return null;
			},
			infff(data) {
				console.log('url', this.url, data)

				/* eslint-disable */
				let lat = this;
				wx.ready(() => { //需在用户可能点击分享按钮前就先调用
					wx.updateTimelineShareData({
						title: '《阳光少年·二十四节气养生文化夏令营》活动火热进行中', // 分享标题
						link: lat.url, // 分享链接，该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
						imgUrl: data.img_url, // 分享图标
						success: () => {
							// 设置成功
							console.log('1 设置成功')


							wx.updateAppMessageShareData({
								title: '《阳光少年·二十四节气养生文化夏令营》活动火热进行中', // 分享标题
								// 您的好友“张三”和阳光少年“王五”送您一个花神红包，请点击领取
								desc: '您的好友“' + data.user_name +
									'”和阳光少年“' + data.receive_user_name +
									'”送您一个花神红包，请点击领取', // 分享描述
								link: lat.url, // 分享链接，该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
								imgUrl: data.img_url, // 分享图标
								success: function() {

									uni.setStorageSync("fenXiang", "true")
									// 设置成功
									console.log('2 设置成功')
									let state = lat.getQueryString("state") || "";
									console.log('state', state)
									let strings = state.split("-");
									console.log('strings', strings)
									if (strings.length > 2) {
										let string = strings[0];
										let string1 = strings[1];
										let share_user_Id = undefined;
										let bFxren = undefined;
										if (string && string.indexOf('sid_') > -1) {
											share_user_Id = string.split('sid_')[1];
										}
										if (string1 && string1.indexOf('bFxen_') > -1) {
											bFxren = string1.split('_')[1];
										}
										// this.$router.push({path:'/redict',share_user_Id:share_user_Id,beifenxiang_id:bFxren})
										uni.navigateTo({
											url: '/pages/luoDi/luoDi?share_user_Id=' +
												share_user_Id +
												'&beifenxiang_id=' + bFxren
										})
									}

								},
								fail: (e) => {
									console.log('2', e)

								}
							})
						},
						fail: (e) => {
							console.log('1', e)
						}
					})
				})
				wx.error(function(res) {
					// config信息验证失败会执行error函数，如签名过期导致验证失败，具体错误信息可以打开config的debug模式查看，也可以在返回的res参数中查看，对于SPA可以在这里更新签名。
					console.log(res)
				})
			},
			// 解析参数
			getQueryParams(url) {
				const params = new URLSearchParams(new URL(url).search);
				const obj = {};

				for (const [key, value] of params.entries()) {
					obj[key] = value;
				}

				return obj;
			}

		},

		created() {
			if(CDX == ""){//没有重定向就直接去首页
				uni.reLaunch({
					url:"/pages/index/index"
				})
			}
			// console.log("记录了吗啊")
			if (CDX != "") { //有重定向
				uni.removeStorageSync("fenXiang")
				this.code = this.getQueryString("code") || ""; //授权登录信息
				let lat = this;
				if (window.location.href.indexOf('share_user_id') > -1) {
					let wx_url =
						`https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appId}&redirect_uri=${CDX}?page=null&response_type=code&scope=snsapi_userinfo&state=sid_`+
						this.getQueryString("share_user_id") + "-bFxen_" + this.getQueryString("bfxren") +
						"-#wechat_redirect";
					window.location.href = wx_url; // 跳转路径 移动端
				}


				if (this.code) {

					let state = lat.getQueryString("state") || "";
					console.log('state', state)
					let strings = state.split("-");
					console.log('strings', strings)
					let share_user_Id = undefined;
					let bFxren = undefined;
					if (strings.length > 2) {
						let string = strings[0];
						let string1 = strings[1];
						// let share_user_Id = undefined;
						// let bFxren = undefined;
						if (string && string.indexOf('sid_') > -1) {
							share_user_Id = string.split('sid_')[1];
						}
						if (string1 && string1.indexOf('bFxen_') > -1) {
							bFxren = string1.split('_')[1];
						}
						// this.$router.push({path:'/redict',share_user_Id:share_user_Id,beifenxiang_id:bFxren})
						uni.navigateTo({
							url: '/pages/luoDi/luoDi?share_user_Id=' +
								share_user_Id +
								'&beifenxiang_id=' + bFxren
						})
					}

					// 登录逻辑
					SY.login({
						code: lat.code,
						share_user_id: share_user_Id,
						relation_id: bFxren
					}).then((res) => {
						console.log('登录信息', res, lat.code);

						if (res.code == 200) {
							setToken(res.data.app_token)
							uni.setStorageSync('open_id', res.data.open_id);
							console.log('用户id', res.data.user_id)

							lat.url = window.location.href.split("#")[0] + '?page=shareUrl&&share_user_id=' + res
								.data
								.user_id + "&&bfxren=";

							let chae = undefined;
							SY.shareData().then(res => {
								chae = res.data;
								console.log('SY.shareData()_1', res)
								lat.url = lat.url + res.data.user_flora_id;


								// console.log('SY.shareData()', lat.data)
								console.log('lat.url', lat.url)
								HTTP.SY.getWxConfig({
										url: window.location.href.split("#")[0]
									})
									.then((res) => {
										var result = res.data
										// eslint-disable-next-line
										if (!wx) {
											console.log('请升级当前微信版本')
										}
										wx.checkJsApi({
											jsApiList: ['updateTimelineShareData',
												'updateAppMessageShareData',
												'onMenuShareTimeline',
												'onMenuShareAppMessage'
											], // 需要检测的JS接口列表，所有JS接口列表见附录2,
											success: function(res) {
												console.log('res', res)
											}
										});
										wx.config({
											debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来，若要查看传入的参数，可以在pc端打开，参数信息会通过log打出，仅在pc端时才会打印。
											appId: result.appId, // 必填，公众号的唯一标识
											timestamp: result.timestamp, // 必填，生成签名的时间戳
											nonceStr: result.nonceStr, // 必填，生成签名的随机串
											signature: result.signature, // 必填，签名
											jsApiList: ['updateTimelineShareData',
												'updateAppMessageShareData',
												'onMenuShareTimeline',
												'onMenuShareAppMessage'

											] // 必填，需要使用的JS接口列表
										})

										setTimeout((res) => {
											console.log("需要使用的JS接口列表：ok", res)
											lat.infff(chae)

										}, 500)
									})

							})

						}

					})
				} else {
					let wx_url =
						`https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appId}&redirect_uri=${CDX}?page=null&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect`;
					window.location.href = wx_url; // 跳转路径 移动端
				}
			}
		}
	}
</script>

<style lang='scss'>
	page {
		background-color: #fff;
	}


	/*每个页面公共css */
</style>