feat: 初始化AI应用项目结构并添加基础功能

添加项目基础文件结构包括静态资源、配置文件、页面组件和工具函数
实现web-view通信功能,支持配置传递和消息处理
添加安全区域高度计算工具和全局样式配置
This commit is contained in:
超级老白兔
2025-08-29 15:48:59 +08:00
parent db4bc8651d
commit a5dcb64a10
14 changed files with 846 additions and 0 deletions

1
aiApp/.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
unpackage

7
aiApp/App.vue Normal file
View File

@@ -0,0 +1,7 @@
<script>
export default {}
</script>
<style>
/*每个页面公共css */
</style>

20
aiApp/index.html Normal file
View File

@@ -0,0 +1,20 @@
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8" />
<script>
var coverSupport = 'CSS' in window && typeof CSS.supports === 'function' && (CSS.supports('top: env(a)') ||
CSS.supports('top: constant(a)'))
document.write(
'<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0' +
(coverSupport ? ', viewport-fit=cover' : '') + '" />')
</script>
<title></title>
<!--preload-links-->
<!--app-context-->
</head>
<body>
<div id="app"><!--app-html--></div>
<script type="module" src="/main.js"></script>
</body>
</html>

22
aiApp/main.js Normal file
View File

@@ -0,0 +1,22 @@
import App from './App'
// #ifndef VUE3
import Vue from 'vue'
import './uni.promisify.adaptor'
Vue.config.productionTip = false
App.mpType = 'app'
const app = new Vue({
...App
})
app.$mount()
// #endif
// #ifdef VUE3
import { createSSRApp } from 'vue'
export function createApp() {
const app = createSSRApp(App)
return {
app
}
}
// #endif

106
aiApp/manifest.json Normal file
View File

@@ -0,0 +1,106 @@
{
"name" : "存客宝",
"appid" : "__UNI__2B34F1A",
"description" : "",
"versionName" : "1.0.0",
"versionCode" : "100",
"transformPx" : false,
/* 5+App */
"app-plus" : {
"usingComponents" : true,
"nvueStyleCompiler" : "uni-app",
"compilerVersion" : 3,
"splashscreen" : {
"alwaysShowBeforeRender" : true,
"waiting" : true,
"autoclose" : true,
"delay" : 0
},
/* */
"modules" : {},
/* */
"distribute" : {
/* android */
"android" : {
"permissions" : [
"<uses-permission android:name=\"android.permission.CHANGE_NETWORK_STATE\"/>",
"<uses-permission android:name=\"android.permission.MOUNT_UNMOUNT_FILESYSTEMS\"/>",
"<uses-permission android:name=\"android.permission.VIBRATE\"/>",
"<uses-permission android:name=\"android.permission.READ_LOGS\"/>",
"<uses-permission android:name=\"android.permission.ACCESS_WIFI_STATE\"/>",
"<uses-feature android:name=\"android.hardware.camera.autofocus\"/>",
"<uses-permission android:name=\"android.permission.ACCESS_NETWORK_STATE\"/>",
"<uses-permission android:name=\"android.permission.CAMERA\"/>",
"<uses-permission android:name=\"android.permission.GET_ACCOUNTS\"/>",
"<uses-permission android:name=\"android.permission.READ_PHONE_STATE\"/>",
"<uses-permission android:name=\"android.permission.CHANGE_WIFI_STATE\"/>",
"<uses-permission android:name=\"android.permission.WAKE_LOCK\"/>",
"<uses-permission android:name=\"android.permission.FLASHLIGHT\"/>",
"<uses-feature android:name=\"android.hardware.camera\"/>",
"<uses-permission android:name=\"android.permission.WRITE_SETTINGS\"/>"
]
},
/* ios */
"ios" : {
"dSYMs" : false
},
/* SDK */
"sdkConfigs" : {},
"icons" : {
"android" : {
"hdpi" : "unpackage/res/icons/72x72.png",
"xhdpi" : "unpackage/res/icons/96x96.png",
"xxhdpi" : "unpackage/res/icons/144x144.png",
"xxxhdpi" : "unpackage/res/icons/192x192.png"
},
"ios" : {
"appstore" : "unpackage/res/icons/1024x1024.png",
"ipad" : {
"app" : "unpackage/res/icons/76x76.png",
"app@2x" : "unpackage/res/icons/152x152.png",
"notification" : "unpackage/res/icons/20x20.png",
"notification@2x" : "unpackage/res/icons/40x40.png",
"proapp@2x" : "unpackage/res/icons/167x167.png",
"settings" : "unpackage/res/icons/29x29.png",
"settings@2x" : "unpackage/res/icons/58x58.png",
"spotlight" : "unpackage/res/icons/40x40.png",
"spotlight@2x" : "unpackage/res/icons/80x80.png"
},
"iphone" : {
"app@2x" : "unpackage/res/icons/120x120.png",
"app@3x" : "unpackage/res/icons/180x180.png",
"notification@2x" : "unpackage/res/icons/40x40.png",
"notification@3x" : "unpackage/res/icons/60x60.png",
"settings@2x" : "unpackage/res/icons/58x58.png",
"settings@3x" : "unpackage/res/icons/87x87.png",
"spotlight@2x" : "unpackage/res/icons/80x80.png",
"spotlight@3x" : "unpackage/res/icons/120x120.png"
}
}
}
}
},
/* */
"quickapp" : {},
/* */
"mp-weixin" : {
"appid" : "",
"setting" : {
"urlCheck" : false
},
"usingComponents" : true
},
"mp-alipay" : {
"usingComponents" : true
},
"mp-baidu" : {
"usingComponents" : true
},
"mp-toutiao" : {
"usingComponents" : true
},
"uniStatistics" : {
"enable" : false
},
"vueVersion" : "3"
}

24
aiApp/pages.json Normal file
View File

@@ -0,0 +1,24 @@
{
"pages": [
{
"path": "pages/index/index",
"style": {
"navigationBarTitleText": "",
"navigationStyle": "custom"
}
},
{
"path": "pages/index/test",
"style": {
"navigationBarTitleText": "",
"navigationStyle": "custom"
}
}
],
"globalStyle": {
"navigationBarTextStyle": "black",
"navigationBarBackgroundColor": "#F8F8F8",
"backgroundColor": "#F8F8F8"
},
"uniIdRouter": {}
}

View File

@@ -0,0 +1,99 @@
<template>
<web-view
ref="webviewRef"
:src="iframeUrl"
@message="handleMessage"
:fullscreen="true"
></web-view>
</template>
<script>
import { getTopSafeAreaHeightAsync } from '@utils/common';
const TYPE_EMUE = {
CONNECT: 0,
DATA: 1,
FUNCTION: 2,
CONFIG: 3,
}
export default {
data() {
return {
baseUrl: 'https://kr-op.quwanzhi.com/init',
iframeUrl: '', // 动态构建的 URL
receivedMessages: [],
messageId: 0,
urlParams: {}
}
},
onLoad() {
this.sendBaseConfig()
},
methods: {
// 构建 iframe URL包含参数
buildIframeUrl() {
const params = [];
Object.keys(this.urlParams).forEach(key => {
const value = this.urlParams[key];
if (value !== null && value !== undefined) {
const encodedKey = encodeURIComponent(key);
const encodedValue = encodeURIComponent(String(value));
params.push(`${encodedKey}=${encodedValue}`);
}
});
const queryString = params.join('&');
this.iframeUrl = queryString ? `${this.baseUrl}?${queryString}` : this.baseUrl;
},
// 发送消息到 iframe通过URL传参
async sendBaseConfig() {
const message = {
type: TYPE_EMUE.CONFIG,
data: {
paddingTop: await getTopSafeAreaHeightAsync(),
appId: '1234567890',
appName: '存客宝',
appVersion: '1.0.0',
isAppMode:true
}
};
// 将消息添加到URL参数中
this.urlParams.message = encodeURIComponent(JSON.stringify(message));
this.buildIframeUrl();
console.log('[App]SendMessage=>\n' + JSON.stringify(message));
},
// 接收 web-view 发送的消息
handleMessage(event) {
console.log("event", event);
const [ResDetail] = event.detail.data;
this.receivedMessages.push(`[${new Date().toLocaleTimeString()}] ${JSON.stringify(ResDetail)}`);
switch (ResDetail.type) {
case TYPE_EMUE.DATA:
console.log('[App]ReceiveMessage=>\n' + JSON.stringify(ResDetail.data));
break;
case TYPE_EMUE.FUNCTION:
console.log('[App]ReceiveMessage=>\n' + JSON.stringify(ResDetail.data));
if (ResDetail.data.action === 'clearCache') {
this.clearCache();
}
break;
}
},
clearCache() {
// 清除 webview 缓存
if (this.$refs.webviewRef) {
// 重新加载 webview
this.$refs.webviewRef.reload();
}
// 清除 webview 缓存数据
uni.clearStorage({
success: () => {
console.log('Webview 缓存已清除');
}
});
}
}
}
</script>

304
aiApp/pages/index/test.vue Normal file
View File

@@ -0,0 +1,304 @@
<template>
<view class="content">
<!-- iframe 容器 -->
<view class="iframe-container">
<web-view
ref="webviewRef"
:src="iframeUrl"
@message="handleMessage"
:fullscreen="false"
:webview-styles="{
width:'100%',
height:'406px'
}"
></web-view>
</view>
<!-- 消息控制区域 -->
<view class="message-controls">
<text class="control-title">消息控制</text>
<view class="control-buttons">
<button @click="sendMessageToIframe" class="btn-send">发送消息到iframe</button>
</view>
</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>
<script>
import { getTopSafeAreaHeightAsync } from '@utils/common';
const TYPE_EMUE = {
CONNECT: 0,
DATA: 1,
FUNCTION: 2,
}
export default {
data() {
return {
baseUrl: 'https://kr-op.quwanzhi.com/init',
iframeUrl: '', // 动态构建的 URL
receivedMessages: [],
messageId: 0,
urlParams: {}
}
},
onLoad() {
this.buildIframeUrl();
setTimeout(() => {
this.sendMessageToIframeConnect()
}, 1000);
},
methods: {
// 构建 iframe URL包含参数
buildIframeUrl() {
const params = [];
Object.keys(this.urlParams).forEach(key => {
const value = this.urlParams[key];
if (value !== null && value !== undefined) {
const encodedKey = encodeURIComponent(key);
const encodedValue = encodeURIComponent(String(value));
params.push(`${encodedKey}=${encodedValue}`);
}
});
const queryString = params.join('&');
this.iframeUrl = queryString ? `${this.baseUrl}?${queryString}` : this.baseUrl;
},
// 发送消息到 iframe通过URL传参
async sendMessageToIframe() {
const paddingTop = await getTopSafeAreaHeightAsync();
this.messageId++;
const message = {
type: TYPE_EMUE.DATA, // 数据类型0数据交互 1App功能调用
data: {
id: this.messageId,
content: `Hello我是 App 发送的消息 ${this.messageId}`,
timestamp: Date.now(),
paddingTop: paddingTop
}
};
// 将消息添加到URL参数中
this.urlParams.message = encodeURIComponent(JSON.stringify(message));
this.buildIframeUrl();
console.log('[App]SendMessage=>\n' + JSON.stringify(message));
},
// 发送消息到 iframe通过URL传参
async sendMessageToIframeConnect() {
const message = {
type: TYPE_EMUE.CONNECT,
data: {
action: 'ping',
content: '联通测试通过',
timestamp: Date.now()
}
};
// 将消息添加到URL参数中
this.urlParams.message = encodeURIComponent(JSON.stringify(message));
this.buildIframeUrl();
console.log('[App]SendMessage=>\n' + JSON.stringify(message));
},
// 接收 web-view 发送的消息
handleMessage(event) {
console.log("event", event);
const [ResDetail] = event.detail.data;
this.receivedMessages.push(`[${new Date().toLocaleTimeString()}] ${JSON.stringify(ResDetail)}`);
switch (ResDetail.type) {
case TYPE_EMUE.DATA:
console.log('[App]ReceiveMessage=>\n' + JSON.stringify(ResDetail.data));
break;
case TYPE_EMUE.FUNCTION:
console.log('[App]ReceiveMessage=>\n' + JSON.stringify(ResDetail.data));
break;
}
}
}
}
</script>
<style lang="scss" scoped>
.content {
display: flex;
flex-direction: column;
height: 100vh;
background-color: #f8f9fa;
}
.iframe-container {
overflow: hidden;
background: white;
position: relative;
margin: 20rpx;
border-radius: 20rpx;
box-shadow: 0 4rpx 20rpx rgba(0, 0, 0, 0.1);
height: 800rpx;
:deep(web-view) {
width: 100% !important;
height: 100% !important;
border: none !important;
outline: none !important;
box-shadow: none !important;
}
:deep(iframe) {
border: none !important;
outline: none !important;
box-shadow: none !important;
}
}
.message-controls {
padding: 20rpx;
background: #f0f0f0;
border-bottom: 2rpx solid #e0e0e0;
display: flex;
flex-direction: column;
gap: 16rpx;
}
.control-title {
font-size: 28rpx;
color: #333;
font-weight: 600;
margin-bottom: 16rpx;
padding-bottom: 16rpx;
border-bottom: 2rpx solid #d0d0d0;
}
.control-buttons {
display: flex;
gap: 16rpx;
}
.btn-send {
flex: 1;
background: linear-gradient(135deg, #188eee 0%, #096dd9 100%);
color: white;
border: none;
border-radius: 20rpx;
padding: 20rpx 16rpx;
font-size: 26rpx;
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;
}
.message-area {
padding: 16rpx;
}
.message-item {
padding: 16rpx;
margin-bottom: 12rpx;
}
}
</style>

BIN
aiApp/static/logo.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.9 KiB

109
aiApp/test.html Normal file
View File

@@ -0,0 +1,109 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, viewport-fit=cover">
<!-- iOS 图标和应用配置 -->
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
<meta name="apple-mobile-web-app-title" content="My PWA">
<!-- iOS 专用图标 -->
<link rel="apple-touch-icon" sizes="76x76" href="icons/76x76.png">
<link rel="apple-touch-icon" sizes="120x120" href="icons/120x120.png">
<link rel="apple-touch-icon" sizes="152x152" href="icons/152x152.png">
<link rel="apple-touch-icon" sizes="167x167" href="icons/167x167.png">
<link rel="apple-touch-icon" sizes="180x180" href="icons/180x180.png">
<!-- 通用图标配置 -->
<link rel="icon" type="image/png" sizes="40x40" href="icons/40x40.png">
<link rel="icon" type="image/png" sizes="20x20" href="icons/20x20.png">
<link rel="manifest" href="manifest.json">
<link rel="mask-icon" href="icons/safari-pinned-tab.svg" color="#2196f3">
<meta name="msapplication-TileColor" content="#2196f3">
<meta name="theme-color" content="#2196f3">
<title>My PWA with Custom Icons</title>
<style>
/* 保持之前的样式不变 */
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
html, body {
height: 100%;
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
}
body {
padding-top: constant(safe-area-inset-top);
padding-top: env(safe-area-inset-top);
padding-bottom: constant(safe-area-inset-bottom);
padding-bottom: env(safe-area-inset-bottom);
background: linear-gradient(135deg, #2196f3 0%, #0d47a1 100%);
color: white;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
text-align: center;
}
.container {
max-width: 600px;
padding: 2rem;
}
h1 {
font-size: 2.5rem;
margin-bottom: 1rem;
}
.icon-preview {
width: 120px;
height: 120px;
margin: 2rem auto;
border-radius: 24px; /* 模拟iOS图标圆角 */
overflow: hidden;
box-shadow: 0 4px 12px rgba(0,0,0,0.15);
}
.icon-preview img {
width: 100%;
height: 100%;
object-fit: cover;
}
</style>
</head>
<body>
<div class="container">
<h1>Custom PWA Icons</h1>
<p>Your app will now have proper icons when added to home screen</p>
<div class="icon-preview">
<img src="icons/icon-180x180.png" alt="App Icon Preview">
</div>
<p>Make sure to use all required icon sizes for best results across devices</p>
</div>
<script>
if ('serviceWorker' in navigator) {
window.addEventListener('load', () => {
navigator.serviceWorker.register('sw.js')
.then(registration => {
console.log('ServiceWorker registered successfully');
})
.catch(err => {
console.log('ServiceWorker registration failed:', err);
});
});
}
</script>
</body>
</html>

View File

@@ -0,0 +1,13 @@
uni.addInterceptor({
returnValue (res) {
if (!(!!res && (typeof res === "object" || typeof res === "function") && typeof res.then === "function")) {
return res;
}
return new Promise((resolve, reject) => {
res.then((res) => {
if (!res) return resolve(res)
return res[0] ? reject(res[0]) : resolve(res[1])
});
});
},
});

76
aiApp/uni.scss Normal file
View File

@@ -0,0 +1,76 @@
/**
* 这里是uni-app内置的常用样式变量
*
* uni-app 官方扩展插件及插件市场https://ext.dcloud.net.cn上很多三方插件均使用了这些样式变量
* 如果你是插件开发者建议你使用scss预处理并在插件代码中直接使用这些变量无需 import 这个文件方便用户通过搭积木的方式开发整体风格一致的App
*
*/
/**
* 如果你是App开发者插件使用者你可以通过修改这些变量来定制自己的插件主题实现自定义主题功能
*
* 如果你的项目同样使用了scss预处理你也可以直接在你的 scss 代码中使用如下变量,同时无需 import 这个文件
*/
/* 颜色变量 */
/* 行为相关颜色 */
$uni-color-primary: #007aff;
$uni-color-success: #4cd964;
$uni-color-warning: #f0ad4e;
$uni-color-error: #dd524d;
/* 文字基本颜色 */
$uni-text-color:#333;//基本色
$uni-text-color-inverse:#fff;//反色
$uni-text-color-grey:#999;//辅助灰色,如加载更多的提示信息
$uni-text-color-placeholder: #808080;
$uni-text-color-disable:#c0c0c0;
/* 背景颜色 */
$uni-bg-color:#ffffff;
$uni-bg-color-grey:#f8f8f8;
$uni-bg-color-hover:#f1f1f1;//点击状态颜色
$uni-bg-color-mask:rgba(0, 0, 0, 0.4);//遮罩颜色
/* 边框颜色 */
$uni-border-color:#c8c7cc;
/* 尺寸变量 */
/* 文字尺寸 */
$uni-font-size-sm:12px;
$uni-font-size-base:14px;
$uni-font-size-lg:16px;
/* 图片尺寸 */
$uni-img-size-sm:20px;
$uni-img-size-base:26px;
$uni-img-size-lg:40px;
/* Border Radius */
$uni-border-radius-sm: 2px;
$uni-border-radius-base: 3px;
$uni-border-radius-lg: 6px;
$uni-border-radius-circle: 50%;
/* 水平间距 */
$uni-spacing-row-sm: 5px;
$uni-spacing-row-base: 10px;
$uni-spacing-row-lg: 15px;
/* 垂直间距 */
$uni-spacing-col-sm: 4px;
$uni-spacing-col-base: 8px;
$uni-spacing-col-lg: 12px;
/* 透明度 */
$uni-opacity-disabled: 0.3; // 组件禁用态的透明度
/* 文章场景相关 */
$uni-color-title: #2C405A; // 文章标题颜色
$uni-font-size-title:20px;
$uni-color-subtitle: #555555; // 二级标题颜色
$uni-font-size-subtitle:26px;
$uni-color-paragraph: #3F536E; // 文章段落颜色
$uni-font-size-paragraph:15px;

36
aiApp/utils/common.js Normal file
View File

@@ -0,0 +1,36 @@
/**
* 异步获取设备顶部安全区域高度
* @returns {Promise<number>} 顶部安全区域高度
*/
export function getTopSafeAreaHeightAsync() {
return new Promise((resolve, reject) => {
uni.getSystemInfo({
success: (res) => {
try {
const safeAreaInsets = res.safeAreaInsets;
if (safeAreaInsets && safeAreaInsets.top !== undefined) {
resolve(safeAreaInsets.top);
return;
}
if (res.safeArea) {
const safeArea = res.safeArea;
const statusBarHeight = res.statusBarHeight || 0;
const topSafeHeight = safeArea.top - statusBarHeight;
resolve(Math.max(0, topSafeHeight));
return;
}
resolve(`${res.statusBarHeight*2 || 0}px`);
} catch (error) {
reject(error);
}
},
fail: (error) => {
reject(error);
}
});
});
}

29
aiApp/vite.config.js Normal file
View File

@@ -0,0 +1,29 @@
import { defineConfig } from 'vite'
import uni from '@dcloudio/vite-plugin-uni'
import { resolve } from 'path'
// https://vitejs.dev/config/
export default defineConfig({
plugins: [uni()],
resolve: {
alias: {
// 根目录别名
'@': resolve(__dirname, '/'),
'@root': resolve(__dirname, '/'),
// 页面和组件别名
'@pages': resolve(__dirname, 'pages'),
'@components': resolve(__dirname, 'components'),
// 工具和配置别名
'@utils': resolve(__dirname, 'utils'),
}
},
css: {
preprocessorOptions: {
scss: {
additionalData: `@import "@/uni.scss";`
}
}
}
})