Merge branch 'yongpxu-dev' into yongpxu-dev4
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name" : "存客宝",
|
||||
"appid" : "__UNI__2B34F1A",
|
||||
"name" : "Ai数智员工",
|
||||
"appid" : "__UNI__8F915F5",
|
||||
"description" : "",
|
||||
"versionName" : "1.0.0",
|
||||
"versionCode" : "100",
|
||||
|
||||
@@ -6,13 +6,6 @@
|
||||
"navigationBarTitleText": "",
|
||||
"navigationStyle": "custom"
|
||||
}
|
||||
},
|
||||
{
|
||||
"path": "pages/index/test",
|
||||
"style": {
|
||||
"navigationBarTitleText": "",
|
||||
"navigationStyle": "custom"
|
||||
}
|
||||
}
|
||||
],
|
||||
"globalStyle": {
|
||||
|
||||
164
aiApp/pages/index/index copy.vue
Normal file
164
aiApp/pages/index/index copy.vue
Normal file
@@ -0,0 +1,164 @@
|
||||
<template>
|
||||
<web-view
|
||||
ref="webviewRef"
|
||||
:src="iframeUrl"
|
||||
@message="handleMessage"
|
||||
:fullscreen="true"
|
||||
></web-view>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
import { getTopSafeAreaHeightAsync } from '@utils/common';
|
||||
import { checkUpdate, downloadAndInstallUpdate, showUpdateDialog } from '@utils/updateService';
|
||||
const TYPE_EMUE = {
|
||||
CONNECT: 0,
|
||||
DATA: 1,
|
||||
FUNCTION: 2,
|
||||
CONFIG: 3,
|
||||
}
|
||||
export default {
|
||||
data() {
|
||||
return {
|
||||
baseUrl: 'kr-phone.quwanzhi.com',
|
||||
iframeUrl: '', // 动态构建的 URL
|
||||
receivedMessages: [],
|
||||
messageId: 0,
|
||||
urlParams: {},
|
||||
appVersion: '1.0.0',
|
||||
updateApiUrl: 'https://ckbapi.quwanzhi.com/app/update?type=ai_store' // 更新检查API地址
|
||||
}
|
||||
},
|
||||
onLoad() {
|
||||
this.sendBaseConfig()
|
||||
// 检查更新
|
||||
// this.checkAppUpdate()
|
||||
},
|
||||
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: this.appVersion,
|
||||
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 缓存已清除');
|
||||
}
|
||||
});
|
||||
},
|
||||
|
||||
/**
|
||||
* 检查应用更新
|
||||
*/
|
||||
checkAppUpdate() {
|
||||
console.log('检查应用更新...');
|
||||
checkUpdate({
|
||||
currentVersion: this.appVersion,
|
||||
apiUrl: this.updateApiUrl,
|
||||
onSuccess: (result) => {
|
||||
if (result.hasUpdate) {
|
||||
console.log(`发现新版本: ${result.version}`);
|
||||
// 显示更新对话框
|
||||
showUpdateDialog(result, () => {
|
||||
this.downloadUpdate(result.downloadUrl);
|
||||
});
|
||||
} else {
|
||||
console.log('当前已是最新版本');
|
||||
}
|
||||
},
|
||||
onError: (errorMsg, error) => {
|
||||
console.error(`检查更新失败: ${errorMsg}`, error);
|
||||
}
|
||||
});
|
||||
},
|
||||
|
||||
/**
|
||||
* 下载并安装更新
|
||||
* @param {String} downloadUrl 下载地址
|
||||
*/
|
||||
downloadUpdate(downloadUrl) {
|
||||
// 显示下载进度
|
||||
uni.showLoading({
|
||||
title: '正在下载更新...',
|
||||
mask: true
|
||||
});
|
||||
|
||||
downloadAndInstallUpdate({
|
||||
downloadUrl,
|
||||
onProgress: (progress) => {
|
||||
uni.showLoading({
|
||||
title: `正在下载更新...${progress}%`,
|
||||
mask: true
|
||||
});
|
||||
},
|
||||
onSuccess: () => {
|
||||
uni.hideLoading();
|
||||
// 安装成功或跳转到应用商店成功
|
||||
console.log('更新下载成功,准备安装');
|
||||
},
|
||||
onError: (errorMsg, error) => {
|
||||
uni.hideLoading();
|
||||
console.error(`更新失败: ${errorMsg}`, error);
|
||||
uni.showToast({
|
||||
title: `更新失败: ${errorMsg}`,
|
||||
icon: 'none',
|
||||
duration: 2000
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
</script>
|
||||
@@ -1,7 +1,7 @@
|
||||
<template>
|
||||
<web-view
|
||||
ref="webviewRef"
|
||||
:src="iframeUrl"
|
||||
:src="baseUrl"
|
||||
@message="handleMessage"
|
||||
:fullscreen="true"
|
||||
></web-view>
|
||||
@@ -9,6 +9,7 @@
|
||||
|
||||
<script>
|
||||
import { getTopSafeAreaHeightAsync } from '@utils/common';
|
||||
import { checkUpdate, downloadAndInstallUpdate, showUpdateDialog } from '@utils/updateService';
|
||||
const TYPE_EMUE = {
|
||||
CONNECT: 0,
|
||||
DATA: 1,
|
||||
@@ -18,15 +19,19 @@
|
||||
export default {
|
||||
data() {
|
||||
return {
|
||||
baseUrl: 'https://kr-op.quwanzhi.com/init',
|
||||
baseUrl: 'https://kr-phone.quwanzhi.com',
|
||||
iframeUrl: '', // 动态构建的 URL
|
||||
receivedMessages: [],
|
||||
messageId: 0,
|
||||
urlParams: {}
|
||||
urlParams: {},
|
||||
appVersion: '1.0.0',
|
||||
updateApiUrl: 'https://ckbapi.quwanzhi.com/app/update?type=ai_store' // 更新检查API地址
|
||||
}
|
||||
},
|
||||
onLoad() {
|
||||
this.sendBaseConfig()
|
||||
// this.sendBaseConfig()
|
||||
// 检查更新
|
||||
// this.checkAppUpdate()
|
||||
},
|
||||
methods: {
|
||||
// 构建 iframe URL,包含参数
|
||||
@@ -51,7 +56,7 @@
|
||||
paddingTop: await getTopSafeAreaHeightAsync(),
|
||||
appId: '1234567890',
|
||||
appName: '存客宝',
|
||||
appVersion: '1.0.0',
|
||||
appVersion: this.appVersion,
|
||||
isAppMode:true
|
||||
}
|
||||
};
|
||||
@@ -92,7 +97,67 @@
|
||||
console.log('Webview 缓存已清除');
|
||||
}
|
||||
});
|
||||
},
|
||||
|
||||
/**
|
||||
* 检查应用更新
|
||||
*/
|
||||
checkAppUpdate() {
|
||||
console.log('检查应用更新...');
|
||||
checkUpdate({
|
||||
currentVersion: this.appVersion,
|
||||
apiUrl: this.updateApiUrl,
|
||||
onSuccess: (result) => {
|
||||
if (result.hasUpdate) {
|
||||
console.log(`发现新版本: ${result.version}`);
|
||||
// 显示更新对话框
|
||||
showUpdateDialog(result, () => {
|
||||
this.downloadUpdate(result.downloadUrl);
|
||||
});
|
||||
} else {
|
||||
console.log('当前已是最新版本');
|
||||
}
|
||||
},
|
||||
onError: (errorMsg, error) => {
|
||||
console.error(`检查更新失败: ${errorMsg}`, error);
|
||||
}
|
||||
});
|
||||
},
|
||||
|
||||
/**
|
||||
* 下载并安装更新
|
||||
* @param {String} downloadUrl 下载地址
|
||||
*/
|
||||
downloadUpdate(downloadUrl) {
|
||||
// 显示下载进度
|
||||
uni.showLoading({
|
||||
title: '正在下载更新...',
|
||||
mask: true
|
||||
});
|
||||
|
||||
downloadAndInstallUpdate({
|
||||
downloadUrl,
|
||||
onProgress: (progress) => {
|
||||
uni.showLoading({
|
||||
title: `正在下载更新...${progress}%`,
|
||||
mask: true
|
||||
});
|
||||
},
|
||||
onSuccess: () => {
|
||||
uni.hideLoading();
|
||||
// 安装成功或跳转到应用商店成功
|
||||
console.log('更新下载成功,准备安装');
|
||||
},
|
||||
onError: (errorMsg, error) => {
|
||||
uni.hideLoading();
|
||||
console.error(`更新失败: ${errorMsg}`, error);
|
||||
uni.showToast({
|
||||
title: `更新失败: ${errorMsg}`,
|
||||
icon: 'none',
|
||||
duration: 2000
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,304 +0,0 @@
|
||||
<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>
|
||||
207
aiApp/utils/updateService.js
Normal file
207
aiApp/utils/updateService.js
Normal file
@@ -0,0 +1,207 @@
|
||||
/**
|
||||
* App自动更新服务
|
||||
* 用于检查新版本并触发下载更新
|
||||
*/
|
||||
|
||||
/**
|
||||
* 检查应用更新
|
||||
* @param {Object} options 配置选项
|
||||
* @param {String} options.currentVersion 当前应用版本号
|
||||
* @param {String} options.apiUrl 检查更新的API地址
|
||||
* @param {Function} options.onSuccess 检查成功回调
|
||||
* @param {Function} options.onError 检查失败回调
|
||||
* @returns {Promise} 更新检查的Promise
|
||||
*/
|
||||
export function checkUpdate(options) {
|
||||
const { currentVersion, apiUrl, onSuccess, onError } = options;
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
uni.request({
|
||||
url: apiUrl,
|
||||
method: 'GET',
|
||||
success: (res) => {
|
||||
|
||||
|
||||
if (res.statusCode === 200 && res.data) {
|
||||
|
||||
|
||||
try {
|
||||
const result = res.data;
|
||||
|
||||
const updateInfo = result.data;
|
||||
|
||||
// 检查是否有新版本
|
||||
if (compareVersion(updateInfo.version, currentVersion) > 0) {
|
||||
// 有新版本
|
||||
const result = {
|
||||
hasUpdate: true,
|
||||
version: updateInfo.version,
|
||||
downloadUrl: updateInfo.downloadUrl,
|
||||
updateContent: updateInfo.updateContent || '发现新版本,请更新!'
|
||||
};
|
||||
|
||||
if (onSuccess) {
|
||||
onSuccess(result);
|
||||
}
|
||||
|
||||
resolve(result);
|
||||
} else {
|
||||
// 没有新版本
|
||||
const result = {
|
||||
hasUpdate: false,
|
||||
currentVersion
|
||||
};
|
||||
|
||||
if (onSuccess) {
|
||||
onSuccess(result);
|
||||
}
|
||||
|
||||
resolve(result);
|
||||
}
|
||||
} catch (error) {
|
||||
const errorMsg = '解析更新信息失败';
|
||||
if (onError) {
|
||||
onError(errorMsg, error);
|
||||
}
|
||||
reject(error);
|
||||
}
|
||||
} else {
|
||||
const errorMsg = '获取更新信息失败';
|
||||
if (onError) {
|
||||
onError(errorMsg);
|
||||
}
|
||||
reject(new Error(errorMsg));
|
||||
}
|
||||
},
|
||||
fail: (error) => {
|
||||
const errorMsg = '检查更新请求失败';
|
||||
if (onError) {
|
||||
onError(errorMsg, error);
|
||||
}
|
||||
reject(error);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 下载并安装更新
|
||||
* @param {Object} options 配置选项
|
||||
* @param {String} options.downloadUrl 下载地址
|
||||
* @param {Function} options.onProgress 下载进度回调
|
||||
* @param {Function} options.onSuccess 下载成功回调
|
||||
* @param {Function} options.onError 下载失败回调
|
||||
*/
|
||||
export function downloadAndInstallUpdate(options) {
|
||||
const { downloadUrl, onProgress, onSuccess, onError } = options;
|
||||
|
||||
// 判断平台
|
||||
const platform = uni.getSystemInfoSync().platform;
|
||||
|
||||
if (platform === 'android') {
|
||||
// Android平台下载APK并安装
|
||||
const downloadTask = uni.downloadFile({
|
||||
url: downloadUrl,
|
||||
success: (res) => {
|
||||
if (res.statusCode === 200) {
|
||||
// 下载成功,安装APK
|
||||
plus.runtime.install(
|
||||
res.tempFilePath,
|
||||
{
|
||||
force: false
|
||||
},
|
||||
() => {
|
||||
if (onSuccess) {
|
||||
onSuccess();
|
||||
}
|
||||
},
|
||||
(error) => {
|
||||
if (onError) {
|
||||
onError('安装更新失败', error);
|
||||
}
|
||||
}
|
||||
);
|
||||
} else {
|
||||
if (onError) {
|
||||
onError('下载更新文件失败');
|
||||
}
|
||||
}
|
||||
},
|
||||
fail: (error) => {
|
||||
if (onError) {
|
||||
onError('下载更新文件失败', error);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// 监听下载进度
|
||||
if (onProgress) {
|
||||
downloadTask.onProgressUpdate((res) => {
|
||||
onProgress(res.progress);
|
||||
});
|
||||
}
|
||||
} else if (platform === 'ios') {
|
||||
// iOS平台打开App Store
|
||||
plus.runtime.openURL(downloadUrl);
|
||||
if (onSuccess) {
|
||||
onSuccess();
|
||||
}
|
||||
} else {
|
||||
if (onError) {
|
||||
onError('不支持的平台');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 比较版本号
|
||||
* @param {String} v1 版本号1
|
||||
* @param {String} v2 版本号2
|
||||
* @returns {Number} 1: v1>v2, -1: v1<v2, 0: v1=v2
|
||||
*/
|
||||
export function compareVersion(v1, v2) {
|
||||
const v1Parts = v1.split('.');
|
||||
const v2Parts = v2.split('.');
|
||||
|
||||
const maxLength = Math.max(v1Parts.length, v2Parts.length);
|
||||
|
||||
for (let i = 0; i < maxLength; i++) {
|
||||
const v1Part = parseInt(v1Parts[i] || 0, 10);
|
||||
const v2Part = parseInt(v2Parts[i] || 0, 10);
|
||||
|
||||
if (v1Part > v2Part) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (v1Part < v2Part) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* 显示更新对话框
|
||||
* @param {Object} updateInfo 更新信息
|
||||
* @param {Function} onConfirm 确认更新回调
|
||||
*/
|
||||
export function showUpdateDialog(updateInfo, onConfirm) {
|
||||
// 处理更新内容中的换行符
|
||||
let content = updateInfo.updateContent || `发现新版本 ${updateInfo.version},是否更新?`;
|
||||
|
||||
// 确保字符串中的\n被转换为实际的换行符
|
||||
content = content.replace(/\\n/g, '\n');
|
||||
|
||||
uni.showModal({
|
||||
title: '发现新版本',
|
||||
content: content,
|
||||
confirmText: '立即更新',
|
||||
cancelText: '稍后再说',
|
||||
success: (res) => {
|
||||
if (res.confirm && onConfirm) {
|
||||
onConfirm();
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -9,6 +9,7 @@
|
||||
|
||||
<script>
|
||||
import { getTopSafeAreaHeightAsync } from '@utils/common';
|
||||
import { checkUpdate, downloadAndInstallUpdate, showUpdateDialog } from '@utils/updateService';
|
||||
const TYPE_EMUE = {
|
||||
CONNECT: 0,
|
||||
DATA: 1,
|
||||
@@ -22,11 +23,15 @@
|
||||
iframeUrl: '', // 动态构建的 URL
|
||||
receivedMessages: [],
|
||||
messageId: 0,
|
||||
urlParams: {}
|
||||
urlParams: {},
|
||||
appVersion: '1.0.0',
|
||||
updateApiUrl: 'https://ckbapi.quwanzhi.com/app/update?type=ckb' // 更新检查API地址
|
||||
}
|
||||
},
|
||||
onLoad() {
|
||||
this.sendBaseConfig()
|
||||
// 检查更新
|
||||
this.checkAppUpdate()
|
||||
},
|
||||
methods: {
|
||||
// 构建 iframe URL,包含参数
|
||||
@@ -51,7 +56,7 @@
|
||||
paddingTop: await getTopSafeAreaHeightAsync(),
|
||||
appId: '1234567890',
|
||||
appName: '存客宝',
|
||||
appVersion: '1.0.0',
|
||||
appVersion: this.appVersion,
|
||||
isAppMode:true
|
||||
}
|
||||
};
|
||||
@@ -92,7 +97,67 @@
|
||||
console.log('Webview 缓存已清除');
|
||||
}
|
||||
});
|
||||
},
|
||||
|
||||
/**
|
||||
* 检查应用更新
|
||||
*/
|
||||
checkAppUpdate() {
|
||||
console.log('检查应用更新...');
|
||||
checkUpdate({
|
||||
currentVersion: this.appVersion,
|
||||
apiUrl: this.updateApiUrl,
|
||||
onSuccess: (result) => {
|
||||
if (result.hasUpdate) {
|
||||
console.log(`发现新版本: ${result.version}`);
|
||||
// 显示更新对话框
|
||||
showUpdateDialog(result, () => {
|
||||
this.downloadUpdate(result.downloadUrl);
|
||||
});
|
||||
} else {
|
||||
console.log('当前已是最新版本');
|
||||
}
|
||||
},
|
||||
onError: (errorMsg, error) => {
|
||||
console.error(`检查更新失败: ${errorMsg}`, error);
|
||||
}
|
||||
});
|
||||
},
|
||||
|
||||
/**
|
||||
* 下载并安装更新
|
||||
* @param {String} downloadUrl 下载地址
|
||||
*/
|
||||
downloadUpdate(downloadUrl) {
|
||||
// 显示下载进度
|
||||
uni.showLoading({
|
||||
title: '正在下载更新...',
|
||||
mask: true
|
||||
});
|
||||
|
||||
downloadAndInstallUpdate({
|
||||
downloadUrl,
|
||||
onProgress: (progress) => {
|
||||
uni.showLoading({
|
||||
title: `正在下载更新...${progress}%`,
|
||||
mask: true
|
||||
});
|
||||
},
|
||||
onSuccess: () => {
|
||||
uni.hideLoading();
|
||||
// 安装成功或跳转到应用商店成功
|
||||
console.log('更新下载成功,准备安装');
|
||||
},
|
||||
onError: (errorMsg, error) => {
|
||||
uni.hideLoading();
|
||||
console.error(`更新失败: ${errorMsg}`, error);
|
||||
uni.showToast({
|
||||
title: `更新失败: ${errorMsg}`,
|
||||
icon: 'none',
|
||||
duration: 2000
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
207
ckApp/utils/updateService.js
Normal file
207
ckApp/utils/updateService.js
Normal file
@@ -0,0 +1,207 @@
|
||||
/**
|
||||
* App自动更新服务
|
||||
* 用于检查新版本并触发下载更新
|
||||
*/
|
||||
|
||||
/**
|
||||
* 检查应用更新
|
||||
* @param {Object} options 配置选项
|
||||
* @param {String} options.currentVersion 当前应用版本号
|
||||
* @param {String} options.apiUrl 检查更新的API地址
|
||||
* @param {Function} options.onSuccess 检查成功回调
|
||||
* @param {Function} options.onError 检查失败回调
|
||||
* @returns {Promise} 更新检查的Promise
|
||||
*/
|
||||
export function checkUpdate(options) {
|
||||
const { currentVersion, apiUrl, onSuccess, onError } = options;
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
uni.request({
|
||||
url: apiUrl,
|
||||
method: 'GET',
|
||||
success: (res) => {
|
||||
|
||||
|
||||
if (res.statusCode === 200 && res.data) {
|
||||
|
||||
|
||||
try {
|
||||
const result = res.data;
|
||||
|
||||
const updateInfo = result.data;
|
||||
|
||||
// 检查是否有新版本
|
||||
if (compareVersion(updateInfo.version, currentVersion) > 0) {
|
||||
// 有新版本
|
||||
const result = {
|
||||
hasUpdate: true,
|
||||
version: updateInfo.version,
|
||||
downloadUrl: updateInfo.downloadUrl,
|
||||
updateContent: updateInfo.updateContent || '发现新版本,请更新!'
|
||||
};
|
||||
|
||||
if (onSuccess) {
|
||||
onSuccess(result);
|
||||
}
|
||||
|
||||
resolve(result);
|
||||
} else {
|
||||
// 没有新版本
|
||||
const result = {
|
||||
hasUpdate: false,
|
||||
currentVersion
|
||||
};
|
||||
|
||||
if (onSuccess) {
|
||||
onSuccess(result);
|
||||
}
|
||||
|
||||
resolve(result);
|
||||
}
|
||||
} catch (error) {
|
||||
const errorMsg = '解析更新信息失败';
|
||||
if (onError) {
|
||||
onError(errorMsg, error);
|
||||
}
|
||||
reject(error);
|
||||
}
|
||||
} else {
|
||||
const errorMsg = '获取更新信息失败';
|
||||
if (onError) {
|
||||
onError(errorMsg);
|
||||
}
|
||||
reject(new Error(errorMsg));
|
||||
}
|
||||
},
|
||||
fail: (error) => {
|
||||
const errorMsg = '检查更新请求失败';
|
||||
if (onError) {
|
||||
onError(errorMsg, error);
|
||||
}
|
||||
reject(error);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 下载并安装更新
|
||||
* @param {Object} options 配置选项
|
||||
* @param {String} options.downloadUrl 下载地址
|
||||
* @param {Function} options.onProgress 下载进度回调
|
||||
* @param {Function} options.onSuccess 下载成功回调
|
||||
* @param {Function} options.onError 下载失败回调
|
||||
*/
|
||||
export function downloadAndInstallUpdate(options) {
|
||||
const { downloadUrl, onProgress, onSuccess, onError } = options;
|
||||
|
||||
// 判断平台
|
||||
const platform = uni.getSystemInfoSync().platform;
|
||||
|
||||
if (platform === 'android') {
|
||||
// Android平台下载APK并安装
|
||||
const downloadTask = uni.downloadFile({
|
||||
url: downloadUrl,
|
||||
success: (res) => {
|
||||
if (res.statusCode === 200) {
|
||||
// 下载成功,安装APK
|
||||
plus.runtime.install(
|
||||
res.tempFilePath,
|
||||
{
|
||||
force: false
|
||||
},
|
||||
() => {
|
||||
if (onSuccess) {
|
||||
onSuccess();
|
||||
}
|
||||
},
|
||||
(error) => {
|
||||
if (onError) {
|
||||
onError('安装更新失败', error);
|
||||
}
|
||||
}
|
||||
);
|
||||
} else {
|
||||
if (onError) {
|
||||
onError('下载更新文件失败');
|
||||
}
|
||||
}
|
||||
},
|
||||
fail: (error) => {
|
||||
if (onError) {
|
||||
onError('下载更新文件失败', error);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// 监听下载进度
|
||||
if (onProgress) {
|
||||
downloadTask.onProgressUpdate((res) => {
|
||||
onProgress(res.progress);
|
||||
});
|
||||
}
|
||||
} else if (platform === 'ios') {
|
||||
// iOS平台打开App Store
|
||||
plus.runtime.openURL(downloadUrl);
|
||||
if (onSuccess) {
|
||||
onSuccess();
|
||||
}
|
||||
} else {
|
||||
if (onError) {
|
||||
onError('不支持的平台');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 比较版本号
|
||||
* @param {String} v1 版本号1
|
||||
* @param {String} v2 版本号2
|
||||
* @returns {Number} 1: v1>v2, -1: v1<v2, 0: v1=v2
|
||||
*/
|
||||
export function compareVersion(v1, v2) {
|
||||
const v1Parts = v1.split('.');
|
||||
const v2Parts = v2.split('.');
|
||||
|
||||
const maxLength = Math.max(v1Parts.length, v2Parts.length);
|
||||
|
||||
for (let i = 0; i < maxLength; i++) {
|
||||
const v1Part = parseInt(v1Parts[i] || 0, 10);
|
||||
const v2Part = parseInt(v2Parts[i] || 0, 10);
|
||||
|
||||
if (v1Part > v2Part) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (v1Part < v2Part) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* 显示更新对话框
|
||||
* @param {Object} updateInfo 更新信息
|
||||
* @param {Function} onConfirm 确认更新回调
|
||||
*/
|
||||
export function showUpdateDialog(updateInfo, onConfirm) {
|
||||
// 处理更新内容中的换行符
|
||||
let content = updateInfo.updateContent || `发现新版本 ${updateInfo.version},是否更新?`;
|
||||
|
||||
// 确保字符串中的\n被转换为实际的换行符
|
||||
content = content.replace(/\\n/g, '\n');
|
||||
|
||||
uni.showModal({
|
||||
title: '发现新版本',
|
||||
content: content,
|
||||
confirmText: '立即更新',
|
||||
cancelText: '稍后再说',
|
||||
success: (res) => {
|
||||
if (res.confirm && onConfirm) {
|
||||
onConfirm();
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user