Files
YGChatCS/src/pages/webview/index.vue
2026-04-06 13:58:06 +08:00

79 lines
1.9 KiB
Vue
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<template>
<view>
<web-view id="webview" :src="webviewUrl" @message="handleMessage"></web-view>
</view>
</template>
<script setup>
import { ref, onMounted, onUnmounted } from "vue";
import { navigateToNewPage, saveImageToAlbum } from "./bridge.js";
const webviewUrl = ref("");
const originalWebviewUrl = ref("");
onUnmounted(() => {
uni.$off('UPLOAD_RESULT')
})
onMounted(() => {
// 获取页面参数
const pages = getCurrentPages();
const currentPage = pages[pages.length - 1];
const options = currentPage.options;
// 从页面参数中获取url
if (options.url) {
// 对URL进行解码因为传递时可能被编码了
const decoded = decodeURIComponent(options.url);
webviewUrl.value = decoded;
originalWebviewUrl.value = decoded;
console.log("WebView URL:", decoded);
}
chooseAndUpload();
});
const chooseAndUpload = () => {
uni.$on('UPLOAD_RESULT', (imageUrl) => {
console.log('收到图片地址:', imageUrl)
const resultUrl = originalWebviewUrl.value + '&imageUrl=' + encodeURIComponent(imageUrl);
console.log('新的URL:', resultUrl)
webviewUrl.value = resultUrl;
})
}
const handleMessage = (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":
navigateToNewPage(messageData.url);
break;
case "showToast":
uni.showToast({ title: messageData.title, icon: messageData.icon || "none" });
break;
case "saveImage":
saveImageToAlbum(messageData.imageUrl);
break;
case "chooseAndUpload": {
uni.navigateTo({
url: '/pages-bridge/UploadImage',
})
}
break;
default:
console.log("Unknown action:", action);
}
};
</script>
<style lang="scss" scoped></style>