<script setup>
	/**
	 * name：
	 * user：sa0ChunLuyu
	 * date：2024年8月7日 20:05:05
	 */
	import {
		$api,
		$response,
	} from '@/api'
	import {
		onShow,
		onLoad,
		onError
	} from '@dcloudio/uni-app'

	import {
		useStore
	} from '@/store'
	const $store = useStore()

	// 初始化错误上报
	try {
		if (typeof uni.$lu !== 'undefined' && uni.$lu.errorReport) {
			// 初始化全局错误捕获
			uni.$lu.errorReport.initErrorHandler()
			console.log('[错误上报] 已初始化')

			// 处理早期错误缓冲区（index.html 中捕获的错误）
			if (typeof window.flushErrorBuffer === 'function') {
				try {
					var earlyErrors = window.flushErrorBuffer()
					if (earlyErrors && earlyErrors.length > 0) {
						console.log('[错误上报] 早期错误数量:', earlyErrors.length)
						// 重新上报早期错误
						earlyErrors.forEach(function(err) {
							try {
								uni.$lu.errorReport.reportError(err, 'early-error')
							} catch (e) {
								console.warn('[错误上报] 重新上报早期错误失败', e)
							}
						})
					}
				} catch (e) {
					console.warn('[错误上报] 处理早期错误缓冲区失败', e)
				}
			}
		}
	} catch (e) {
		console.warn('[错误上报] 初始化失败', e)
	}

	const setConfigStore = () => {
		let config = {}
		try {
		  const config_str = uni.getStorageSync('CONFIG_CONFIG')
		  if (config_str) config = JSON.parse(config_str)
		} catch (e) {
			console.warn('CONFIG_CONFIG 解析失败', e)
		}
		$store.config = config
		try {
			// 安全检查：确保 document.body 存在
			if (document && document.body && !config.color) {
				document.body.classList.toggle('grayscale');
			}
		} catch (e) {
			console.warn('设置样式失败', e)
		}
		let openid_str = ''
		try {
			openid_str = uni.getStorageSync('OPENID')
		} catch (e) {
			console.warn('获取 OPENID 失败', e)
		}
		let url = ''
		try {
			url = window.location.href
		} catch (e) {
			console.warn('获取 URL 失败', e)
		}
		if (!openid_str) {
			if (url && url.indexOf('/pages/main/login/login') === -1) {
				uni.redirectTo({
					url: '/pages/main/login/login'
				})
			}
		}
		try {
			const save_info_str = uni.getStorageSync('SAVE_INFO')
			if (!!save_info_str) {
				$store.save_info = JSON.parse(save_info_str)
			} else {
				$store.resetSaveInfo()
			}
		} catch (e) {
			console.warn('解析 SAVE_INFO 失败', e)
			$store.resetSaveInfo()
		}
	}

	const getConfigConfig = async () => {
		try {
			const response = await $api('ConfigConfig')
			$response(response, () => {
				try {
					uni.setStorageSync('CONFIG_CONFIG', JSON.stringify(response.data.config))
					setConfigStore()
				} catch (e) {
					console.error('保存配置失败', e)
				}
			})
		} catch (e) {
			console.error('获取配置接口失败', e)
		}
	}

	const getConfigVersion = async () => {
		try {
			const response = await $api('ConfigVersion')
			$response(response, () => {
				try {
					const config_version = uni.getStorageSync('CONFIG_VERSION')
					let get_config = false
					if (!config_version) {
						get_config = true
					} else {
						if (config_version !== response.data.version) {
							get_config = true
						}
					}
					uni.setStorageSync('CONFIG_VERSION', response.data.version)
					//if (!!get_config) {
					if (true) {
						getConfigConfig()
					} else {
						setConfigStore()
					}
				} catch (e) {
					console.error('处理版本配置失败', e)
				}
			})
		} catch (e) {
			console.error('获取版本接口失败', e)
		}
	}
	const handleFontSize = () => {
		try {
			// 设置网页字体为默认大小
			WeixinJSBridge.invoke('setFontSizeCallback', {
				'fontSize': 0
			});
			// 重写设置网页字体大小的事件
			WeixinJSBridge.on('menu:setfont', function() {
				WeixinJSBridge.invoke('setFontSizeCallback', {
					'fontSize': 0
				});
			});
		} catch (e) {
			console.warn('设置字体大小失败', e)
		}
	}
	onShow(() => {
		try {
			if (typeof WeixinJSBridge == "object" && typeof WeixinJSBridge.invoke == "function") {
				handleFontSize();
			} else {
				try {
					if (document && document.addEventListener) {
						document.addEventListener("WeixinJSBridgeReady", handleFontSize, false);
					} else if (document && document.attachEvent) {
						document.attachEvent("WeixinJSBridgeReady", handleFontSize);
						document.attachEvent("onWeixinJSBridgeReady", handleFontSize);
					}
				} catch (e) {
					console.warn('监听 WeixinJSBridge 失败', e)
				}
			}
		} catch (e) {
			console.warn('onShow 初始化失败', e)
		}
		console.log(`\n %c 鹿和 %c https://sa0.online/ \n\n`, 'color: #ffffff; background: #fd6b60; padding:5px 0;',
			'color: #fd6b60;background: #ffffff; padding:5px 0;')
		try {
			getConfigVersion()
		} catch (e) {
			console.error('获取配置版本失败', e)
		}
	})
	onError((err) => {
		console.error('[全局错误]', err)

		// iOS WebView 特殊处理：强制清理可能阻塞的loading
		const isIOS = /iPhone|iPad|iPod/.test(navigator.userAgent)
		if (isIOS) {
			try {
				uni.hideLoading()
				console.log('[iOS清理] 强制关闭loading')
			} catch (e) {
				console.warn('[iOS清理loading失败]', e)
			}
		}

		// 上报错误
		try {
			if (typeof uni.$lu !== 'undefined' && uni.$lu.errorReport) {
				uni.$lu.errorReport.reportError(err, 'uni-app-error')
			}
		} catch (e) {
			console.warn('[错误上报] 上报失败', e)
		}
	})

	onLoad(()=>{


	})
	 
</script>

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

	/* #endif */
	.grayscale {
		filter: grayscale(100%);
	}

	// 设置整个项目的背景色
	page {
		background-color: #f5f5f5;
	}

	.bottom_blank_wrapper {
		height: calc(20rpx + var(--safe-area-inset-bottom));
	}

	.botm_blank_wrapper {
		width: 100%;
		height: 20rpx;
	}

	.top_blank_wrapper::after {
		content: ".";
	}

	.top_blank_wrapper {
		height: 0;
		width: 100%;
		opacity: 0;
	}

	.navbar_blank_wrapper {
		height: calc(100rpx + var(--safe-area-inset-top));
	}

	.input_line_wrapper {
		display: flex;
		align-items: center;
		flex-wrap: wrap;
		justify-content: space-between;
	}

	.input_line_tag_wrapper {
		width: 200rpx;
		height: 60rpx;
		line-height: 60rpx;
		text-align: center;
		background: #f9f9f9;
		font-size: 26rpx;
		font-weight: bold;
		border-radius: 6rpx;
	}

	.input_line_input_wrapper {
		width: 500rpx;
	}
</style>
<style>
@import "@/static/iconfont.css";
/* #ifdef H5 */
body { /* IOS禁止微信调整字体大小 */
            -webkit-text-size-adjust: 100% !important;
            text-size-adjust: 100% !important;
            -moz-text-size-adjust: 100% !important;
}
/* #endif */
</style>
 