<script>
	import request from '/common/request.js';

	export default {
		onLaunch: function() {
			// #ifdef APP-PLUS || APP-PLUS-NVUE
			const ua = uni.getSystemInfoSync().platform;
			if (/ios/i.test(ua)) {
				// 苹果

			} else {
				this.checkVersion();
			}
			// #endif
		},
		onShow: function() {
			console.log('App Show');
			

		},
		onHide: function() {
			console.log('App Hide');
		},
		data() {
			return { fastOrderTimer: {} };
		},
		mounted() {
			uni.$on('startPlayVoiceBroadcast', (res) => {
				clearInterval(this.fastOrderTimer);
				if (res) {
					this.startPlayVoiceBroadcast();
				}
			});
		},
		methods: {
			startPlayVoiceBroadcast() {
				const _this = this;
				this.fastOrderTimer = setInterval(function() {
					_this.getFastOrderNum();
				}, 3000);
			},
			getFastOrderNum() {
				request({
					url: '/api/jsOrder/getSeizeOrder',
					data: {},
					success: (res) => {
						console.log('抢单订单数量', res);
						if (res.data.data?.fast_number) {
							uni.setTabBarBadge({ index: 3, text: res.data.data?.fast_number + '' });
						}
						if (!res.data.data?.count) {
							// 没有认证
							console.log('结束');
							uni.removeTabBarBadge({ index: 3 });
							clearInterval(this.fastOrderTimer);
						}
					},
					fail: () => {},
					complete: () => {}
				});
			},


			// #ifdef APP-PLUS || APP-PLUS-NVUE
			// app更新检测
			checkVersion() {
				// 获取应用版本号
				let version = plus.runtime.version;
				//检测当前平台，如果是安卓则启动安卓更新
				uni.getSystemInfo({
					success: res => {
						this.updateHandler(res.platform, version);
					}
				})
			},
			// 更新操作
			updateHandler(platform, version) {
				let data = {
					platform: platform,
					version: version
				}
				let _this = this;
				request({
					url: '/api/getdow',
					data: { data },
					success: res => {
						console.log('版本检测', res)
						if (res.data.data.version_num) {
							const info = res.data.data.version_num;
							var downUrl = res.data.data.url
							let version_num_d = info.split('.').join('');
							let version_d = version.split('.').join('');
							if (info !== '' && version_num_d > version_d) {
								uni.showModal({
									//提醒用户更新
									title: '更新提示',
									content: `发现新版本,是否更新`,
									success: (res) => {
										if (res.confirm) {
											if (platform == 'android') {
												plus.runtime.openURL(downUrl)
											} else if (platform == 'ios') {
												const appleId = 1628804508; //app对应的appleId
												plus.runtime.launchApplication({
														action: `itms-apps://itunes.apple.com/cn/app/id${appleId}?mt=8`
													},
													function(e) {
														console.log(
															'Open system default browser failed:' +
															e.message);
													}
												);
											}
										} else {
											//	plus.runtime.quit();
										}
									}
								});
							}
						}
					},
					fail: () => {},
					complete: () => {
						//	uni.hideLoading();
					}
				});

			}
			// #endif
		}
	};
</script>

<style>
	/*每个页面公共css */
</style>