<script>
	import {
		appId
	} from './common/config'
	import menuList from './common/menuList'
	import {
		updateAppMessageShareData
	} from "@/utils/js_sdk_utils.js";
	export default {
		onLaunch: function() {
			this.createUser();
			//判断是h5 然后是微信浏览器
			// #ifdef H5
			if (this.is_wx()) {
				if (!uni.getStorageSync("openId")) {
					if (this.getCode() == "") {
						// const currenturl = location.href.split("#")[0];
						uni.setStorageSync("currentUrl", window.location.href.split("#")[0])
						const currenturl = encodeURIComponent(window.location.href.split("#")[0]);
						//访问这个链接 redirect_uri 传入当前路径---执行后重新跳转到当前页面--但是路径上会带上code参数
						window.location.href =
							`https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appId}&redirect_uri=${currenturl}&response_type=code&scope=snsapi_base#wechat_redirect`;
					} else {
						let code = this.getCode();
						//拿着code 传输给后台

						this.$request
							.get({
								url: `mp/api/code2accesstoken/${code}`,
								//   loadingTip: "加载中...",
								data: {},
							})
							.then((res) => {
								//将openId参数放进缓存
								uni.setStorageSync('openId', res.data.openid)
							});
					}
				}
			}

			let url = window.location.href;
			let temp = url.split('newScale/')[1];
			let tName = temp.split('/index')[0];
			let params = {};
			menuList.forEach(item => {
				if (tName == item.tName) {
					params = item;
				}
			})
			updateAppMessageShareData({
				title: params.name, // 分享标题
				desc: params.desc, // 分享描述
				link: `https://hnhong-duo.com/webo${params.pageUrl}`, // 分享路径
				imgUrl: params.imageUrl // 分享图标 
			});
			// #endif
		},
		onShow: function() {
			console.log("App Show");
		},
		onHide: function() {
			console.log("App Hide");
		},
		methods: {
			//获取code
			getCode() {
				let code = "";
				let url = window.location.search;
				if (url.indexOf("?") !== -1) {
					var strings = url.substring(1).split("&");
					for (var i in strings) {
						if (strings[i].indexOf("code") === 0) {
							code = strings[i].split("=")[1];
						}
					}
				}
				return code;
			},

			//判断是否是微信浏览器i
			is_wx() {
				let en = window.navigator.userAgent.toLowerCase();
				// 匹配en中是否含有MicroMessenger字符串
				if (en.match(/MicroMessenger/i) == "micromessenger") {
					return true;
				} else {
					return false;
				}
			},
			//微信浏览器h5 调用登录

			//查询用户信息
			createUser() {
				let _this = this;
				// #ifdef H5
				this.$request
					.get({
						url: "system/temporaryUser",
						loadingTip: "加载中...",
					})
					.then((res) => {
						//当是微信浏览器的时候

						uni.setStorageSync("user", res.data.user);
						uni.setStorageSync("token", res.data.token);
					});
				// #endif

				// #ifdef MP-TOUTIAO
				tt.login({
					success(res) {
						let para = {
							code: res.code,
							petName: "",
							modelPhone: "",
						};
						_this.$request
							.post({
								url: "api/douyin/code2Session",
								data: para,
							})
							.then(
								(res) => {
									console.log(res);
									uni.setStorageSync("user", res.data.user);
									uni.setStorageSync("token", res.data.token);
									uni.setStorageSync("openId", res.data.openId);
								},
								(err) => {
									console.log("登录失败", err);
								}
							);
					},
				});
				// #endif
			},
		},
	};
</script>

<style>
	/*每个页面公共css */
	@font-face {
		font-family: "Alibaba PuHuiTi 2.0";
		src: url("https://test.jue-ming.com:8849/api/show?filePath=./webo/font/AlibabaPuHuiTi-2-55-Regular/AlibabaPuHuiTi-2-55-Regular.woff2") format("woff2"),
			url("https://test.jue-ming.com:8849/api/show?filePath=./webo/font/AlibabaPuHuiTi-2-55-Regular/AlibabaPuHuiTi-2-55-Regular.woff") format("woff"),
			url("https://test.jue-ming.com:8849/api/show?filePath=./webo/font/AlibabaPuHuiTi-2-55-Regular/AlibabaPuHuiTi-2-55-Regular.ttf") format("truetype");
		font-weight: normal;
		font-style: normal;
		font-display: swap;
	}

	@font-face {
		font-family: "ZhankuKuaiLeTi";
		src: url("https://test.jue-ming.com:8849/api/show?filePath=./webo/font/ZhankuKuaiLeTi.ttf") format("truetype");
		font-weight: normal;
		font-style: normal;
		font-display: swap;
	}

	@font-face {
		font-family: "YanShiXieHeiTi";
		src: url("https://test.jue-ming.com:8849/api/show?filePath=./webo/font/演示斜黑体.otf") format("truetype");
		font-weight: normal;
		font-style: normal;
		font-display: swap;
	}
</style>