<script>
	let platform = ""
	let SystemVersion = ""
	export default {
		data() {
			return {}
		},
		onLaunch: function() {
			let that = this
			// 返回的是apk包信息
			// #ifdef APP-PLUS
			uni.getSystemInfo({
				success(e) {
					platform = e.platform
					that.$store.commit('changePhoneInfo', {
						platform: e.platform
					})
				}
			})
			if (platform === 'ios') { // ios首次安装没有网络
				uni.onNetworkStatusChange(function(res) {
					if (res.isConnected == true) {
						that.handleoginversion()
					}
				});
			}
			plus.runtime.getProperty(plus.runtime.appid, function(info) {
				uni.setStorageSync('VERSION', info.version)
				SystemVersion = info.version
				that.$store.commit('changePhoneInfo', {
					info: info
				})
			})
			// #endif
			that.handleoginversion()
			console.log('App Launch')
		},
		onShow: function() {
			console.log('App Show')
		},
		onHide: function() {
			console.log('App Hide')
			// #ifdef APP-PLUS
			uni.offNetworkStatusChange(function(res) {
				console.log("取消网络监听")
			})
			// #endif
		},
		methods: {
			// 公共信息
			handleoginversion() {
				let that = this
				that.$model.getLoginVersion({}).then(res => {
					that.$store.dispatch("getHomeConfig") //配置接口信息
					if (res.code == 0) {
						that.handleUserList()
					} else {
						uni.setStorageSync('token', null)
						uni.setStorageSync('aan_id', null)
						setTimeout(() => {
							uni.reLaunch({
								url: '/body/login/login',
							})
						}, 500);
					}
					// #ifdef APP-PLUS||APP
					that.$store.commit('changePhoneInfo', {
						versionUrl: res.data
					})
					// 比对版本号
					let version = that.$tools.compareVersions(SystemVersion, res.data.version)
					console.log("是否登录及版本号", res, res.data.version, SystemVersion, version)
					if (version == -1) { // 0版本号相通，1，：第一个版本号大于第二个版本号，-1：第一个版本号小于第二个版本号
						uni.showModal({
							title: '发现新版本',
							content: '检查到新版本' + res.data.version + '，是否更新？',
							success: (modalRes) => {
								if (modalRes.confirm) { //确定更新
									if (platform === 'android') { //安卓更新
										uni.setStorageSync('VERSION', res.data.version)
										uni.navigateTo({
											url: "/body/me/about"
										})
									} else { //ios跳转
										plus.runtime.launchApplication({
											action: `itms-apps://itunes.apple.com/cn/app/id6654906497?mt=8`
										})
									}
								} else {
									that.$tools.msg("稍后可在'关于我们'内更新程序！")
								}
							}
						});
					}
					// #endif

				})
			},
			handleUserList() {
				let that = this
				that.$model.getUserList({
					type: 2
				}).then(res => {
					that.isShow = true
					if (res.code != 0) {
						that.$tools.msg(res.msg)
						return
					}
					that.$store.commit('changeFamilay', res.data.user_list)
					if (res.data.user_list.length) {
						let userid = ""
						if (uni.getStorageSync('userid')) {
							let found = res.data.user_list.find(e => e.id == uni.getStorageSync('userid'));
							if (found !== undefined) {
								userid = found.id
							} else {
								userid = res.data.user_list[0].id
								uni.setStorageSync('userid', res.data.user_list[0].id)
							}
						} else {
							userid = res.data.user_list[0].id
							uni.setStorageSync('userid', res.data.user_list[0].id)
						}
						// 用户信息接口
						that.$store.dispatch('getUserInfo', {
							aud_id: userid
						})
						// 厨房秤接口
						that.$store.dispatch("getCountFoodInfo", {
							aud_id: userid,
							time: that.$tools.getDate("start")
						})
						// 全部卡片
						that.$store.dispatch("getCardAllList", {
							aud_id: userid
						})
						// 
						that.$store.dispatch("getResult", {
							aud_id: userid
						})
						that.$store.dispatch("getPublicRecord", {
							aud_id: userid
						})
						that.$store.commit("changehomeCard", 0);
					}
				}).catch(err => {})
			},
			//  微信小程序更新
			updataWeiXin() {
				let that = this
				const updateManager = uni.getUpdateManager()
				// 请求完新版本信息的回调
				updateManager.onCheckForUpdate(function(res) {
					// console.log("是否有新版本", res.hasUpdate)
				})
				updateManager.onUpdateReady(function() {
					uni.showModal({
						title: '更新提示',
						content: '新版本已经准备好，是否重启应用？',
						success: function(res) {
							if (res.confirm) {
								// 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
								updateManager.applyUpdate()
							}
						}
					})
				})
				updateManager.onUpdateFailed(function() {
					uni.showModal({
						title: '新版本更新失败',
						content: '请退出并移除小程序，重新打开...',
					})
				})
			}
		}
	}
</script>

<style lang="scss">
	/*每个页面公共css */
	/* #ifndef APP-NVUE */
	@import "/uni.scss";
	@import "/scss/food.scss";
	@import "/scss/common.scss";
	@import "/scss/iconfont.css";
	@import "/scss/iconfont-weapp-icon.css";

	/* #endif*/

	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		background-color: #f7f7f7;
		min-height: 100vh;
	}
</style>