<script>
import request from '/common/request.js';
export default {
	onLaunch: function () {
		console.log('App Launch');
		// #ifdef APP-PLUS || APP-PLUS-NVUE
		this.checkVersion();
		// #endif
	},
	onShow: function () {
		console.log('App Show');
	},
	onHide: function () {
		console.log('App Hide');
	},
	data() {
		return { orderTimer: {}, fastOrderTimer: {}, receiveOrderTimer: {} };
	},
	mounted() {
		// this.playVoiceBroadcast();
		// this.getFastOrderNum();
		this.startPlayVoiceBroadcast();
		uni.$on('startPlayVoiceBroadcast', (res) => {
			clearInterval(this.orderTimer);

			if (res) {
				this.startPlayVoiceBroadcast();
			}
		});
		uni.$on('receiveOrdernum', (res) => {
			clearInterval(this.receiveOrderTimer);
			res && this.receiveOrdernum();
		});
		uni.$on('fastOrderNum', (res) => {
			clearInterval(this.fastOrderTimer);
			res && this.fastOrderNum();
		});
	},
	methods: {
		// #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) => {
					if (res.data.data.version_num1) {
						const info = res.data.data.version_num1;
						var urlxz = 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('https://dd.niusenyun.com/dow/嘀咚点到师傅端.apk');
										} 
										else if (platform == 'ios') {
											const appleId = 1629163610; //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
		// 可接单数量
		receiveOrdernum() {
			const _this = this;
			this.receiveOrderTimer = setInterval(function () {
				_this.getOrderNum();
			}, 3000);
		},
		startPlayVoiceBroadcast() {
			const _this = this;
			this.orderTimer = setInterval(function () {
				_this.playVoiceBroadcast();
			}, 3000);
		},
		fastOrderNum() {
			const _this = this;
			this.fastOrderTimer = setInterval(function () {
				_this.getFastOrderNum();
			}, 3000);
		},
		playVoiceBroadcast() {
			request({
				url: '/api/userjs/play',
				data: {},
				success: (res) => {
					console.log('订单播报', res);
					if (res.data.code == 1) {
						const innerAudioContext = uni.createInnerAudioContext();
						innerAudioContext.autoplay = true;
						innerAudioContext.src = '/static/voice/order_new.mp3';
						innerAudioContext.onPlay(() => {});

						console.log('订单播报', res);
					} else if (res.data.code == 10001) {
						clearInterval(this.orderTimer);
					}
				},
				fail: () => {},
				complete: () => {}
			});
			request({
				url: '/api/userjs/cancelplay',
				data: {},
				success: (res) => {
					if (res.data.code == 1) {
						console.log('取消订单播报', res);
						const innerAudioContext = uni.createInnerAudioContext();
						innerAudioContext.autoplay = true;
						innerAudioContext.src = '/static/voice/order_cancel.mp3';
						innerAudioContext.onPlay(() => {});
					}
				},
				fail: () => {},
				complete: () => {}
			});
			// that.jiaobiao();
			//that.tishi()

			//   uni.$on("sxqd", function (data) {
			//     that.jiaobiao();
			//   });
		},
		getFastOrderNum() {
			request({
				url: '/api/userjs/getJsOrdernum',
				data: {},
				success: (res) => {
					console.log('抢单订单数量', res);
					if (res.data.code == 0) {
						clearInterval(this.fastOrderTimer);
					}
					if (res.data.data > 0) {
						uni.setTabBarBadge({ index: 2, text: res.data.data + '' });
					} else {
						// 没有认证
						uni.removeTabBarBadge({ index: 2 });
					}
				},
				fail: () => {},
				complete: () => {}
			});
		},
		getOrderNum() {
			request({
				url: '/api/userjs/getOrdernum',
				data: {},
				success: (res) => {
					console.log('可接单数量', res);
					if (res.data.code == 0) {
						clearInterval(this.receiveOrderTimer);
					}
					if (res.data.data > 0) {
						uni.setTabBarBadge({ index: 3, text: res.data.data + '' });
					} else {
						// 没有认证
						uni.removeTabBarBadge({ index: 3 });
					}
				},
				fail: () => {},
				complete: () => {}
			});
		}
	}
};
</script>

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