<script>
// #ifndef APP-PLUS
import Fingerprint from 'fingerprintjs2';
// #endif

import { app } from '@/utils/api';
const unorm = require('./unorm.js');
import APPUpdate from '@/plugins/APPUpdate';
// #ifdef APP-PLUS
const openinstall = uni.requireNativePlugin('openinstall-plugin');
import { NativeHandler } from '@/utils/nativeHandler.ts';
// #endif
export default {
	methods: {
		async init() {
			const channel = uni.getStorageSync('channel');
			const inviteCode = uni.getStorageSync('inviteCode');
			await app
				.init(inviteCode, channel)
				.then((res) => {
					if (res.code === 1) {
						const resData = res.data;
						// #ifdef APP-PLUS
						//APPUpdate();
						// #endif
						const token = uni.getStorageSync('user_token');
						if (resData.is_login == 0) {
							if (token) {
								uni.removeStorage({
									key: 'user_token'
								});
								getApp().globalData.userToken = '';
							}
						} else {
							getApp().globalData.userToken = token;
						}
						console.log(resData.launch);
						getApp().globalData.advList = resData.advlist;
						getApp().globalData.share = resData.share;
						getApp().globalData.launch = resData.launch;
					}
				})
				.catch((err) => {});
		},
		getuuid() {
			return new Promise((resolve, reject) => {
				// #ifdef APP-PLUS
				plus.device.getInfo({
					success(e) {
						resolve(e.uuid);
					},
					fail(e) {
						let data = uni.getDeviceInfo();
						resolve(data.deviceId);
					}
				});
				// #endif

				// #ifdef WEB
				Fingerprint.get(async function (components) {
					console.log(components);
					const ipdata = await uni
						.request({
							method: 'GET',
							url: '/getip' // 编译打包H5的时候 换成/getip不带域名
							// url: 'http://h5.ht324.com:10001/getip', // 编译打包H5的时候 换成/getip不带域名
						})
						.then((response) => {
							const [error, res] = response;
							if (res.statusCode === 200) {
								return res.data;
							}
						})
						.catch((error) => {});
					if (ipdata) {
						components.push({
							key: 'ip',
							value: ipdata.ip
						});
					}
					const values = components.map((component) => component.value);
					const uuid = Fingerprint.x64hash128(values.join(''), 31);
					resolve(uuid);
				});
				// #endif
			});
		},
		removeLaunch() {
			NativeHandler(this.funcMap['removeLaunch']);
		},
		getFuncMap() {
			return new Promise((resolve) => {
				NativeHandler('getFuncDic', null, (funcMap, b) => {
					console.log('get map resolve getFunc');
					resolve(funcMap);
				});
			});
		}
	},
	onLaunch: async function () {
		if (this.isFullApp()) {
			// #ifdef APP-PLUS
			if (uni.getSystemInfoSync().platform == 'ios') {
				//this.getFuncMap()
				this.funcMap = await this.getFuncMap();
			}

				if (process.env.NODE_ENV != 'development') {
					openinstall.init();
					setTimeout(() => {
						openinstall.getInstall(8, (result) => {
							let data = JSON.parse(decodeURIComponent(result.bindData));
							uni.setStorageSync('inviteCode', data.inviteCode || '');
							uni.setStorageSync('channel', result.channelCode || '');
						});
					}, 50);
				}
			// #endif
			// #ifdef H5
			let platform = uni.getStorageSync('platform') || {};
			platform.version = 130;
			platform.versionCode = '1.3.0';
			platform.type = 1102;
			uni.setStorageSync('platform', platform);
			// #endif
			// #ifdef WEB
			if (window.parames) {
				if (window.parames.code) {
					uni.setStorageSync('inviteCode', window.parames.code || '');
				}
				if (window.parames.channel) {
					uni.setStorageSync('channel', window.parames.channel || '');
				}
			}
			// #endif

			let uuid = uni.getStorageSync('user_uuid');
			if (uuid == '') {
				uuid = await this.getuuid().then((uuid) => uuid);
				if (uuid) {
					uni.setStorageSync('user_uuid', uuid);
				} else {
					uni.setStorageSync('user_uuid', '');
				}
			}
			const apiDomain = uni.getStorageSync('API_DOMAIN');
			if (apiDomain == '' || apiDomain.length == 0) {
				this.$isResolve();
				// #ifdef APP-PLUS
				if (uni.getSystemInfoSync().platform == 'ios') {
					this.removeLaunch();
				}
				// #endif
				return;
			} else {
				await this.init();
				// #ifdef APP-PLUS
				if (uni.getSystemInfoSync().platform == 'ios') {
					this.removeLaunch();
				}
				// #endif
				this.$isResolve();
			}
		} else {
			uni.reLaunch({
				url: '/pages/splash/splash'
			});
		}
	},
	onShow: function () {
		//console.log('App Show')
	},
	onHide: function () {
		//console.log('App Hide')
	},
	globalData: {
		advlist: [],
		share: {},
		userToken: '',
		switchChannel: {
			id: 0,
			type: 0,
			type_pid: 0
		}
	}
};
</script>
<style>
@import '@/static/iconfont/iconfont.css';
</style>
<style lang="scss">
@import 'uview-ui/index.scss';
page {
	background-color: #0c0c16;
}
/deep/.uni-scroll-view-refresh__spinner > circle {
	color: red !important;
}
/deep/.uni-scroll-view-refresh-inner > svg {
	fill: red !important;
}
</style>
