<template>
	<web-view :src="src">
	</web-view>
</template>

<script>
import { sendPos } from "@/api/core/api";
import {
	server_url
} from '@/utils/config.js'
var wv;
export default {
	data() {
		return {
			timer: '',
			src: '',
			query: {},
			webviewServer: server_url,
			uuid: '',
			bgAudioManager: '',
			lastPlayTime: 0
		};
	},
	onLoad(query) {
		this.query = query

		this.initData(query)


	},

	onReady() {
		// #ifdef APP-PLUS
		var self = this;
		var currentWebview = this.$scope
			.$getAppWebview(); //此对象相当于html5plus里的plus.webview.currentWebview()。在uni-app里vue页面直接使用plus.webview.currentWebview()无效，非v3编译模式使用this.$mp.page.$getAppWebview()
		setTimeout(function () {
			wv = currentWebview.children()[0];
			wv.addEventListener(
				"progressChanged",
				function (e) {
					wv.canBack(function (e) {
						self.canBack = e.canBack;
					});
				},
				false
			);
		}, 500); //如果是页面初始化调用时，需要延时一下
		// #endif
	},
	// 设备上点击返回按钮时的处理
	onBackPress(e) {
		if (wv && this.canBack) {
			wv.back();
		} else {
			// 没有可返回的页面了, 可以做些其他的处理, 比如回首页等等
		}
		return true;
	},
	/* onNavigationBarButtonTap(e) {
		
	  // 返回
	  if (e.index === 0) {
		// #ifdef H5
		uni.navigateBack();
		// #endif
	 
		// #ifdef APP-PLUS
		if (this.canBack) {
			wv.back();
		} else {
			// 没有可返回的页面了, 可以做些其他的处理, 比如回首页等等
		}
		// #endif
	  }
	  // 首页
	  if (e.index === 1) {
		// 显示tabbar
		//uni.showTabBar({
		//  animation: false
		//});
		uni.switchTab({
		  url: '/pages/index/index'
		});
	  }
	}, */
	watch: {
		authtoken: {
			immediate: true,
			deep: true,
			handler(newVal) {
				// console.log(newVal,'newValnewValnewVal')
				// this.initData(this.query)
			}
		}
	},
	computed: {
		token() {
			return this.$store.state.user.token
		}
	},
	methods: {
		PlayVoice(url) {
			//测试背景音频播放
			try {
				// 检查是否在8秒防重复时间内
				const now = Date.now();
				if (this.lastPlayTime && (now - this.lastPlayTime) < 7500) {
					console.log('8秒内重复调用，跳过本次播放');
					return;
				}

				let bgAudioManager;
				if (this.bgAudioManager) {
					bgAudioManager = this.bgAudioManager;
				} else {
					this.bgAudioManager = uni.getBackgroundAudioManager();
					bgAudioManager = this.bgAudioManager;
				}

				// 更新上次播放时间
				this.lastPlayTime = now;

				bgAudioManager.title = '报警播报';
				bgAudioManager.singer = '暂无';
				bgAudioManager.coverImgUrl = 'https://qiniu-web-assets.dcloud.net.cn/unidoc/zh/music-a.png';
				bgAudioManager.src = url;

			} catch (error) {
				//TODO handle the exception
				console.log('语音播报失败', error)
			}
		},


		getUuid() {
			var uuid = "";
			// #ifdef APP-PLUS
			if (plus.os.name == "Android") {
				try {
					var Build = plus.android.importClass("android.os.Build");
					var serial = Build.SERIAL;
					var mainActivity = plus.android.runtimeMainActivity();
					var Settings = plus.android.importClass("android.provider.Settings");
					var ANDROID_ID = Settings.Secure.getString(mainActivity.getContentResolver(), Settings.Secure
						.ANDROID_ID)
					uuid = `${ANDROID_ID}-${serial}`
				} catch (e) {
					console.log("获取设备唯一标识失败:", e);
				}
			}
			// #endif
			return uuid;
		},


		initData(query) {


			let urlParams = ''
			for (let key in query) {
				if (key != 'token') {
					urlParams += `&${key}=${query[key]}`
				}
			}
			urlParams += `&uuid=${this.getUuid()}`
			urlParams = urlParams.replace('&', '')
			this.src = `${this.webviewServer}/mobile/pages/attachedVessel/index/?${urlParams}`
			console.log('webview地址', this.src)
			this.uuid = this.getUuid()

			try {
				let this_ = this

				// 高精度定位
				this.timer = setInterval(() => {
					uni.getLocation({
						type: 'gcj02',
						// isHighAccuracy: true,
						success: function (res) {
							console.log('持续位置信息', res)
							// console.log('当前位置的经度：' + res.longitude);
							// console.log('当前位置的纬度：' + res.latitude);
							let datas = {
								deviceId: this_.uuid,
								gpsTime: this_.dayjs().format('YYYY-MM-DD HH:mm:ss'),
								lat02: res.latitude,
								lng02: res.longitude,
								accuracy: res.accuracy,
								speed: res.speed
							}
							console.log('上传定位信息', datas)
							sendPos(datas).then(res => {
								console.log('位置信息上传成功', res)
								if (res.code == 0) {
									this_.PlayVoice(`https://yhdd.dasongyuhe.com${res.data}`);
								}
							}).catch(err => {
								console.log('位置信息上传失败', err)
							})
						}
					});
				}, 8 * 1000)



				// 持续定位
				// uni.startLocationUpdate({
				// 	type: 'gcj02',
				// 	// success: res => console.log('开启小程序接收位置消息成功'),
				// 	fail: err => console.error('开启接收位置消息失败：', err),
				// 	complete: msg => console.log('调用开启接收位置消息 API 完成')
				// });

				// uni.onLocationChange((res) => {
				// 	console.log('持续位置信息', res)
				// 	let datas = {
				// 		deviceId: this_.uuid,
				// 		gpsTime: this_.dayjs().format('YYYY-MM-DD HH:mm:ss'),
				// 		lat02: res.latitude,
				// 		lng02: res.longitude,
				// 		accuracy: res.accuracy,
				// 		speed: res.speed
				// 	}
				// 	console.log('上传定位信息', datas)
				// 	sendPos(datas).then(res => {
				// 		console.log('位置信息上传成功', res)
				// 		if (res.code == 0) {
				// 			this_.PlayVoice(`https://yhdd.dasongyuhe.com${res.data}`);
				// 		}
				// 	}).catch(err => {
				// 		console.log('位置信息上传失败', err)
				// 	})
				// });
				// 	uni.startLocationUpdateBackground({
				// 		type:'gcj02',
				// 		// success: res => console.log('开启小程序接收位置消息成功'),
				// 		fail: err => console.error('开启接收位置消息失败：', err),
				// 		complete: msg => console.log('调用开启接收位置消息 API 完成')
				// 	});
			} catch (error) {
				console.log('获取定位失败', error)
			}
		},
		getLocation() {

		},
		scanCode() {

		}
	}
}
</script>

<style lang="scss"></style>