<template>
  <view>
    <web-view :src="webviewUrl" @message="handleH5Message"></web-view>
  </view>
</template>

<script setup>
import { ref, onMounted } from "vue";

const webviewUrl = ref("");

onMounted(() => {
  // 获取页面参数
  const pages = getCurrentPages();
  const currentPage = pages[pages.length - 1];
  const options = currentPage.options;

  // 从页面参数中获取url
  if (options.url) {
    // 对URL进行解码，因为传递时可能被编码了
    webviewUrl.value = decodeURIComponent(options.url);
  }
});

const handleH5Message = (event) => {
  const messageData = event.detail.data[0];
  console.log("Received message from H5:", messageData);
  // 根据需要处理H5传递过来的消息
  const action = messageData.action;

  switch (action) {
    case "navigateBack":
      uni.navigateBack();
      break;
    case "navigateTo":

      break
    default:
      console.log("Unknown action:", action);
  }

};

</script>

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