<template>
	<view>
		<web-view :src="h5url" @message="handleWebMessage"></web-view>
	</view>
</template>

<script setup lang="ts">
	import { onShow, onLoad } from "@dcloudio/uni-app";
	import { ref } from "vue";
	import { Service } from "@/Service/Service";




	const pages = getCurrentPages()
	const vw = ref(null)

	let h5url = ref()
	let url = ref('http://192.168.0.192:5174/#')
	let address = ref('/pages/userFunc/orderDetail')
	onLoad((data : any) => {
		h5url.value = url.value + address.value
		
	});

	onShow(() => {

	});

	// 接收H5 postMessage消息
	const handleWebMessage = (e : any) => {
		const msg = e.detail.data[0]
		console.log('收到H5消息', msg)
		const { code, data } = msg

		if (code == 'Back') {
			Service.GoPageBack()
		}
		if (code === 'Ready') {
			SendMsg()
		}

	};

	// 向子元素传递的值
	function SendMsg(data : any) {
		vw.value = pages[pages.length - 1].$getAppWebview().children()[0]
		const jsonStr = JSON.stringify({ code: 'init', data: data })
		vw.value.evalJS(`GetTwEvent('${jsonStr}')`)
	}
</script>

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