<script>
import { useMainStore } from "@/store/index.js";
import { shunfenLogin,getQueryParam } from "@/utils/common";
import { useLoginApi } from "@/Apis/login.js";
const getApi = useLoginApi();

const updateManagerFn = () => {
	const updateManager = uni.getUpdateManager();

	updateManager.onCheckForUpdate(function (res) {
		// 请求完新版本信息的回调
		console.log(res.hasUpdate, "请求完新版本信息的回调");
	});

	updateManager.onUpdateReady(function (res) {
		uni.showModal({
			title: "更新提示",
			content: "新版本已经准备好，是否重启应用？",
			showCancel: false,
			success(res) {
				if (res.confirm) {
					// 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
					updateManager.applyUpdate();
				}
			},
		});
	});

	updateManager.onUpdateFailed(function (res) {
		// 新的版本下载失败
		uni.showModal({
			title: "提示",
			content: "新版小程序下载失败\n请自行退出程序，手动卸载本程序，再运行",
			confirmText: "知道了",
		});
	});
};
const listenNetworkChange = () => {
	uni.onNetworkStatusChange((res) => {
		if (res.isConnected) {
			// uni.showToast({
			// 	title: "网络已连接",
			// 	icon: "none",
			// 	duration: 3000,
			// });
		} else {
			uni.showToast({
				title: "当前无网络连接",
				icon: "none",
				duration: 3000,
			});
		}
	});
};
export default {
	globalData: {
		statusBarHeight: 0,
		navbarHeight: 0,
	},
	onLaunch: async function () {
		 uni.getNetworkType({
			success: (res) => {
			if (res.networkType === 'none') {
				uni.showToast({
				title: '当前无网络连接',
				icon: 'none',
				duration: 3000
				});
			}
			}
		});
		uni.removeStorageSync('getCouponCodeTime'); //清掉弹窗有优惠劵的时间记录 避免重进的时候不会弹窗
		// // 开始监听网络状态变化
		listenNetworkChange();
		uni.hideTabBar()
		const { setTheme, getUserInfo, storeState,logOut } = useMainStore();
		setTheme();
		// #ifdef MP-WEIXIN || MP-XHS
		// 状态栏高度
		const statusBarHeight = uni.getSystemInfoSync().statusBarHeight;
		const wxMenuBtn = uni.getMenuButtonBoundingClientRect();
		// 导航栏高度(标题栏高度) = 胶囊高度 + (顶部距离 - 状态栏高度) * 2
		const barHeight = wxMenuBtn.height + (wxMenuBtn.top - statusBarHeight) * 2;
		// 总体高度 = 状态栏高度 + 导航栏高度
		this.globalData.navbarHeight = (barHeight || 40) + statusBarHeight;
		this.globalData.statusBarHeight = statusBarHeight || 20;
		// #endif
		// authCode url 存在 就直接登陆
			const authCode = getQueryParam('authCode');
			const sfId = getQueryParam('sfId');
			const lockerId = getQueryParam('lockerId');
			if(lockerId) {
				uni.setStorageSync("lockerId", lockerId);
			}
			if(sfId) {
				uni.setStorageSync("sfId", sfId);
			}
			if(authCode){
				let source = window.sf.isSfApp() ? 2 : 1;
				getApi.ShunFengLogin({
			        		code:authCode,
							source,
							sfId:uni.getStorageSync("sfId")
			        	})
			        	.then(async(res) => {
			        		uni.hideLoading();
							storeState.hasTrytoLogin = true;
			        		if (res.code == 200) {
								uni.setStorageSync("token", res.data.token);
								// 如果用户没有授权手机号 不登陆
							 		await getUserInfo();
									console.log('用户信息',storeState.userInfo)
									if(storeState.userInfo.phone){
										uni.$emit('loginSuccess',{msg:'页面更新'})
										storeState.token = res.data.token;
										uni.setStorageSync("token", res.data.token);
										uni.setStorageSync("openId", res.data.openId);
									}else{
										uni.removeStorageSync("token");
										console.log('用户未授权手机号,启动shunfenLogin')
										shunfenLogin();
									}
			        			// 更新用户信息
			        			
			        		}else{
			        			logOut();
			        		}
			        	}).catch((err) => {
			        	    logOut();
			        	})
			}else{
				if(window.sf.isSfApp()){
					shunfenLogin();
				}
			}
			
		// 清掉请求有优惠价的时间记录
		uni.removeStorageSync('getCouponCodeTime');
	},
	onShow: function () {
		// #ifdef MP-WEIXIN
		// 检查更新
		updateManagerFn();
		// #endif
	},
	onHide: function () { },
	onPageNotFound() {
		uni.switchTab({
			url: "/pages/index/index",
		});
	},
};
</script>

<style lang="scss">
/*每个页面公共css */
@import "@/uni_modules/uni-scss/index.scss";
@import "@/static/iconfont/iconfont.css";
// @import "@/static/style/theme.scss";
/* #ifndef APP-NVUE */
@import "@/static/customicons.css";

// 设置整个项目的背景色
[hidden] {
	display: none !important;
}
html{
height: unset;
min-height: 100vh;
}
body {
    background:$backgroundColor;
	min-height: 100vh;
}
page {
	// uni-input,
	// uni-button,
	// uni-textarea {
	// 	line-height: 2.55555;
	// }
}

:deep(.uv-radio__icon-wrap){
	border-width: 5rpx!important;
}
:deep(.uv-checkbox__icon-wrap){
	border-width: 5rpx!important;
}

.uni-tabbar{
	display: none;
}

view,
text {
	box-sizing: border-box;
}

.myCustomTabbar {
	position: fixed;
	bottom: -1px;
	left: 0;
	height: 95rpx;
	padding-bottom: 40rpx;
	background-color: #fff;
	height: 160upx;
	.uni-tabbar__icon {
		display: flex;
		align-items: center;
		justify-content: center;
		width: 40rpx;
		height: 40rpx;
	}

	.uni-tabbar {
		height: 200upx;
	}

	.uni-tabbar__label {
		color: #000;
		font-size: 24rpx !important;
		margin-top: 4rpx;
	}

	.uni-tabbar__item.call-phone-item {
		position: relative;

		.uni-tabbar__bd {
			top: -80rpx;
			width: 160rpx;
			position: absolute;
			height: 160rpx;

			.uni-tabbar__icon {
				width: 40rpx;
				height: 40rpx;

				svg {
					max-width: 100%;
					height: 40rpx;
					width: 40rpx;
				}
			}

			border-radius: 1000rpx;
			background: linear-gradient(180deg,
				var(--left-linear),
				var(--right-linear) 100%);
			box-shadow: 0px 0px 5px 0px rgba(0, 0, 0, 0.2);
		}
	}
}

.uni-app--showleftwindow+.uni-tabbar-bottom {
	display: none;
}

/* #endif */
.example-info {
	font-size: 14px;
	color: #333;
	padding: 10px;
}
</style>
