FEAT => 本次更新项目为:

基础通信搞定了
This commit is contained in:
超级老白兔
2025-08-01 16:29:34 +08:00
parent 325d53cad6
commit fddda27ca8
11 changed files with 1179 additions and 41 deletions

1
ckApp/.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
unpackage

View File

@@ -3,13 +3,13 @@
{
"path": "pages/index/index",
"style": {
"navigationBarTitleText": "uni-app"
"navigationBarTitleText": "",
"navigationStyle": "custom"
}
}
],
"globalStyle": {
"navigationBarTextStyle": "black",
"navigationBarTitleText": "uni-app",
"navigationBarBackgroundColor": "#F8F8F8",
"backgroundColor": "#F8F8F8"
},

View File

@@ -1,8 +1,33 @@
<template>
<view class="content">
<image class="logo" src="/static/logo.png"></image>
<view class="text-area">
<text class="title">{{title}}</text>
<view class="header">
123
</view>
<!-- iframe 容器 -->
<view class="iframe-container" :class="'iframe-' + iframeSize">
<web-view
ref="webviewRef"
:src="iframeUrl"
@message="handleMessage"
:fullscreen="false"
></web-view>
</view>
<!-- 控制按钮 -->
<view class="controls">
<button @click="sendMessageToIframe" class="btn">发送消息到iframe</button>
<button @click="toggleIframeSize" class="btn btn-secondary">切换iframe大小</button>
</view>
<!-- 消息显示区域 -->
<view class="message-area">
<text class="message-title">接收到的消息</text>
<view class="message-list">
<view v-for="(msg, index) in receivedMessages" :key="index" class="message-item">
<text class="message-text">{{msg}}</text>
</view>
</view>
</view>
</view>
</template>
@@ -11,42 +36,287 @@
export default {
data() {
return {
title: 'Hello'
title: 'iframe 通信示例',
// iframeUrl: 'http://localhost:3000/iframe',
iframeUrl: 'https://kr-op.quwanzhi.com/iframe',
receivedMessages: [],
messageId: 0,
iframeSize: 'medium' // 控制 iframe 大小small, medium, large
}
},
onLoad() {
// 页面加载时的初始化
console.log('页面加载完成');
},
methods: {
}
// 接收 web-view 发送的消息
handleMessage(event) {
// 消息在 event.detail.data 中(是一个数组,取最后一条)
const webData = event.detail.data[0];
console.log('App 收到 web-view 消息:', webData);
// 将消息添加到显示列表
this.receivedMessages.push(`[${new Date().toLocaleTimeString()}] ${JSON.stringify(webData)}`);
// 根据消息类型处理逻辑
if (webData.type === 'ready') {
console.log('web-view 已准备就绪');
uni.showToast({
title: 'web-view 连接成功',
icon: 'success'
});
} else if (webData.type === 'toApp') {
uni.showToast({
title: `收到:${webData.content}`,
icon: 'none'
});
} else if (webData.type === 'data') {
console.log('收到数据:', webData.data);
}
},
// App 发送消息到 web-view
sendMessageToIframe() {
this.messageId++;
const message = {
id: this.messageId,
type: 'fromApp',
content: `Hello我是 App 发送的消息 ${this.messageId}`,
timestamp: Date.now()
};
// 通过 evalJS 发送消息到 web-view
this.sendMessageToWebView(message);
},
// App 向 web-view 发送消息
sendMessageToWebView(message) {
// 通过 ref 获取 web-view 实例,调用 evalJS 执行 web-view 中的函数
// 注意:需将数据转为字符串(避免语法错误)
if (this.$refs.webviewRef && this.$refs.webviewRef.evalJS) {
try {
this.$refs.webviewRef.evalJS(`receiveFromApp(${JSON.stringify(message)})`);
console.log('App 发送消息到 web-view', message);
} catch (e) {
console.error('evalJS 执行失败:', e);
}
} else {
console.error('web-view ref 不存在或 evalJS 方法不可用');
}
},
// 移除 localStorage 相关代码,使用 postMessage 通信
// 切换 iframe 大小
toggleIframeSize() {
const sizes = ['small', 'medium', 'large'];
const currentIndex = sizes.indexOf(this.iframeSize);
const nextIndex = (currentIndex + 1) % sizes.length;
this.iframeSize = sizes[nextIndex];
console.log('iframe 大小切换为:', this.iframeSize);
}
},
// 移除 mounted 生命周期,因为 uni-app 中可能不支持
// 移除 onUnload因为不再需要清理定时器
}
</script>
<style>
<style lang="scss" scoped>
// 主容器
.content {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
height: 100vh;
background-color: #f8f9fa;
}
.logo {
height: 200rpx;
width: 200rpx;
margin-top: 200rpx;
margin-left: auto;
margin-right: auto;
margin-bottom: 50rpx;
// 头部区域
.header {
height: 46rpx;
background: #fff;
}
.text-area {
// iframe 容器
.iframe-container {
overflow: hidden;
background: white;
position: relative;
margin: 20rpx;
border-radius: 20rpx;
box-shadow: 0 4rpx 20rpx rgba(0, 0, 0, 0.1);
// 针对 web-view 的特殊样式
:deep(web-view) {
width: 100% !important;
height: 100% !important;
border: none !important;
outline: none !important;
box-shadow: none !important;
}
// 兼容不同平台的 web-view 样式
:deep(iframe) {
border: none !important;
outline: none !important;
box-shadow: none !important;
}
// 不同大小的样式
&.iframe-small {
height: 300rpx;
min-height: 300rpx;
}
&.iframe-medium {
height: 500rpx;
min-height: 500rpx;
}
&.iframe-large {
height: 700rpx;
min-height: 700rpx;
}
}
// 控制按钮区域
.controls {
display: flex;
justify-content: center;
gap: 20rpx;
padding: 20rpx;
background: white;
border-bottom: 2rpx solid #f0f0f0;
flex-wrap: wrap;
}
.title {
font-size: 36rpx;
color: #8f8f94;
.btn {
flex: 1;
background: linear-gradient(135deg, #188eee 0%, #096dd9 100%);
color: white;
border: none;
border-radius: 20rpx;
padding: 24rpx 20rpx;
font-size: 28rpx;
font-weight: 600;
cursor: pointer;
transition: all 0.3s ease;
box-shadow: 0 4rpx 12rpx rgba(24, 142, 238, 0.3);
&:active {
transform: translateY(2rpx);
box-shadow: 0 2rpx 8rpx rgba(24, 142, 238, 0.4);
}
&:hover {
background: linear-gradient(135deg, #096dd9 0%, #0050b3 100%);
}
}
// 消息显示区域
.message-area {
flex: 1;
padding: 20rpx;
background: white;
overflow: hidden;
display: flex;
flex-direction: column;
}
.message-title {
font-size: 28rpx;
color: #333;
margin-bottom: 20rpx;
display: block;
font-weight: 600;
border-bottom: 2rpx solid #f0f0f0;
padding-bottom: 16rpx;
}
.message-list {
flex: 1;
overflow-y: auto;
border: 2rpx solid #f0f0f0;
border-radius: 16rpx;
padding: 16rpx;
background: #fafafa;
// 自定义滚动条
&::-webkit-scrollbar {
width: 8rpx;
}
&::-webkit-scrollbar-track {
background: #f1f1f1;
border-radius: 4rpx;
}
&::-webkit-scrollbar-thumb {
background: #c1c1c1;
border-radius: 4rpx;
&:hover {
background: #a8a8a8;
}
}
}
.message-item {
background: white;
padding: 20rpx;
margin-bottom: 16rpx;
border-radius: 12rpx;
border-left: 6rpx solid #188eee;
box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.08);
transition: all 0.3s ease;
&:last-child {
margin-bottom: 0;
}
&:hover {
transform: translateX(4rpx);
box-shadow: 0 4rpx 16rpx rgba(0, 0, 0, 0.12);
}
}
.message-text {
font-size: 24rpx;
color: #333;
word-break: break-all;
line-height: 1.5;
font-family: "Courier New", monospace;
}
// 响应式设计
@media (max-width: 768rpx) {
.content {
padding: 0;
}
.iframe-container {
margin: 16rpx;
border-radius: 16rpx;
}
.controls {
padding: 16rpx;
gap: 16rpx;
}
.btn {
padding: 20rpx 16rpx;
font-size: 26rpx;
}
.message-area {
padding: 16rpx;
}
.message-item {
padding: 16rpx;
margin-bottom: 12rpx;
}
}
</style>

0
ckApp/utils/config.js Normal file
View File