feat: 版本更新
This commit is contained in:
@@ -15,107 +15,12 @@
|
||||
import {
|
||||
ShoproInit
|
||||
} from './sheep';
|
||||
import {
|
||||
gethotupdatelist
|
||||
} from '@/sheep/api/index/index';
|
||||
let globalData = ref({
|
||||
statusBarHeight: 0, // 状态导航栏高度
|
||||
navHeight: 0, // 总体高度
|
||||
navigationBarHeight: 0, // 导航栏高度(标题栏高度)
|
||||
windowHeight: 0,
|
||||
});
|
||||
let filePath = ref();
|
||||
const wgtUp = async () => {
|
||||
// console.log(plus.runtime);
|
||||
|
||||
let res = await gethotupdatelist();
|
||||
if (res.code !== 0) return;
|
||||
filePath.value = res.data.filePath;
|
||||
|
||||
let localVersion = uni.getSystemInfoSync().appWgtVersion;
|
||||
if (res.data.versionNumber != localVersion) {
|
||||
if (res.data.type == 'min') {
|
||||
if (res.data.isForceUpdate) {
|
||||
// 强制更新
|
||||
downloadFile();
|
||||
} else {
|
||||
uni.showModal({
|
||||
title: '提示',
|
||||
content: '发现新的版本,是否更新?',
|
||||
confirmColor: '#FA7E31',
|
||||
success: (res) => {
|
||||
if (res.confirm) {
|
||||
downloadFile();
|
||||
} else if (res.cancel) {
|
||||
if (res.data.isForceUpdate) {
|
||||
plus.runtime.quit(); //退出应用
|
||||
}
|
||||
}
|
||||
},
|
||||
});
|
||||
}
|
||||
} else {
|
||||
uni.showModal({
|
||||
title: '提示',
|
||||
content: '发现新的版本,请到立即更新到最新版本',
|
||||
confirmText: '立即更新',
|
||||
cancelText: res.data.isForceUpdate ? '关闭' : '取消',
|
||||
confirmColor: '#FA7E31',
|
||||
|
||||
success: (res) => {
|
||||
if (res.confirm) {
|
||||
updateApp(filePath.value);
|
||||
} else if (res.cancel) {
|
||||
if (res.data.isForceUpdate) {
|
||||
plus.runtime.quit(); //退出应用
|
||||
}
|
||||
}
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
};
|
||||
const updateApp = (url) => {
|
||||
uni.showLoading({
|
||||
title: '下载中',
|
||||
});
|
||||
uni.downloadFile({
|
||||
url: url, //下载地址
|
||||
success: (res) => {
|
||||
console.log(res, '下载成功');
|
||||
if (res.statusCode === 200) {
|
||||
console.log('下载成功');
|
||||
uni.hideLoading();
|
||||
uni.showToast({
|
||||
title: '下载成功',
|
||||
icon: 'success',
|
||||
});
|
||||
uni.saveFile({
|
||||
tempFilePath: res.tempFilePath,
|
||||
success: function(res) {
|
||||
uni.openDocument({
|
||||
filePath: res.savedFilePath,
|
||||
success: function(res) {
|
||||
console.log(res, '打开安装包');
|
||||
},
|
||||
});
|
||||
},
|
||||
fail: (err) => {
|
||||
console.log(err, '打开安装包-失败');
|
||||
},
|
||||
});
|
||||
}
|
||||
},
|
||||
fail(e) {
|
||||
uni.hideLoading();
|
||||
uni.showToast({
|
||||
title: '下载失败,请检查网络',
|
||||
icon: 'none',
|
||||
});
|
||||
console.log(e);
|
||||
},
|
||||
});
|
||||
};
|
||||
const socket = () => {
|
||||
let url = 'ws://192.168.101.229:48080/aishop/ws';
|
||||
let socketTask = uni.connectSocket({
|
||||
@@ -138,6 +43,122 @@
|
||||
console.log('WebSocket 错误!', res);
|
||||
});
|
||||
};
|
||||
|
||||
const parseTitleToProgress = (title) => {
|
||||
const text = String(title || '');
|
||||
const percentMatch = text.match(/(\d{1,3})%/);
|
||||
const percent = percentMatch ? Number(percentMatch[1]) : null;
|
||||
const cleanText = text.replace(/\s*\d{1,3}%\s*$/, '').trim() || text;
|
||||
const normalizedPercent =
|
||||
percent !== null && Number.isFinite(percent) ?
|
||||
Math.max(0, Math.min(100, percent)) :
|
||||
text.includes('安装') && !text.includes('下载') ? 100 : null;
|
||||
return {
|
||||
percent: normalizedPercent,
|
||||
text: cleanText
|
||||
};
|
||||
};
|
||||
|
||||
const shouldUseUpdateOverlay = (title) => {
|
||||
const text = String(title || '');
|
||||
if (!text) return false;
|
||||
if (/下载|安装|更新/.test(text) && (/%/.test(text) || /下载中|安装中/.test(text))) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
};
|
||||
|
||||
const ensureLoadingPatched = () => {
|
||||
if (!uni || uni.__updateLoadingPatched) return;
|
||||
const originalShowLoading = uni.showLoading;
|
||||
const originalHideLoading = uni.hideLoading;
|
||||
if (typeof originalShowLoading !== 'function' || typeof originalHideLoading !== 'function') {
|
||||
uni.__updateLoadingPatched = true;
|
||||
return;
|
||||
}
|
||||
|
||||
const HOT_UPDATE_PROGRESS_EVENT = '__hot_update_progress__';
|
||||
const canEmit = () => typeof uni !== 'undefined' && typeof uni.$emit === 'function';
|
||||
const emitProgress = (payload) => {
|
||||
if (!canEmit()) return false;
|
||||
uni.__hotUpdatePendingProgress = payload;
|
||||
uni.$emit(HOT_UPDATE_PROGRESS_EVENT, payload);
|
||||
return true;
|
||||
};
|
||||
|
||||
uni.showLoading = (options = {}) => {
|
||||
const title = options?.title ?? '';
|
||||
if (shouldUseUpdateOverlay(title) && canEmit()) {
|
||||
const parsed = parseTitleToProgress(title);
|
||||
if (!uni.__updateProgressShown) {
|
||||
uni.__updateProgressShown = true;
|
||||
emitProgress({
|
||||
type: 'show',
|
||||
text: parsed.text || '下载中...'
|
||||
});
|
||||
}
|
||||
if (parsed.percent !== null) {
|
||||
emitProgress({
|
||||
type: 'set',
|
||||
percent: parsed.percent,
|
||||
text: parsed.text || ''
|
||||
});
|
||||
}
|
||||
return;
|
||||
}
|
||||
return originalShowLoading.call(uni, options);
|
||||
};
|
||||
|
||||
uni.hideLoading = () => {
|
||||
if (uni.__updateProgressShown && canEmit()) {
|
||||
uni.__updateProgressShown = false;
|
||||
emitProgress({
|
||||
type: 'hide'
|
||||
});
|
||||
return;
|
||||
}
|
||||
return originalHideLoading.call(uni);
|
||||
};
|
||||
|
||||
uni.__updateLoadingPatched = true;
|
||||
};
|
||||
|
||||
const waitForPlusRuntime = ({
|
||||
timeoutMs = 5000,
|
||||
intervalMs = 50
|
||||
} = {}) =>
|
||||
new Promise((resolve) => {
|
||||
const ready = () => typeof plus !== 'undefined' && !!plus?.runtime;
|
||||
if (ready()) {
|
||||
resolve(true);
|
||||
return;
|
||||
}
|
||||
const startedAt = Date.now();
|
||||
const timer = setInterval(() => {
|
||||
if (ready()) {
|
||||
clearInterval(timer);
|
||||
resolve(true);
|
||||
return;
|
||||
}
|
||||
if (Date.now() - startedAt >= timeoutMs) {
|
||||
clearInterval(timer);
|
||||
resolve(false);
|
||||
}
|
||||
}, intervalMs);
|
||||
});
|
||||
|
||||
const ensureGlobalHotUpdateChecked = async () => {
|
||||
if (uni.__globalHotUpdateCheckedInApp) return;
|
||||
uni.__globalHotUpdateCheckedInApp = true;
|
||||
const ok = await waitForPlusRuntime();
|
||||
if (!ok) return;
|
||||
if (typeof uni.__globalHotUpdateCheck === 'function') {
|
||||
await uni.__globalHotUpdateCheck({
|
||||
force: true,
|
||||
source: 'app'
|
||||
});
|
||||
}
|
||||
};
|
||||
onMounted(() => {
|
||||
// #ifdef APP-PLUS
|
||||
// plus.nativeUI.setUIStyle('dark');
|
||||
@@ -156,54 +177,10 @@
|
||||
globalData.value.statusBarHeight = uni.getSystemInfoSync()?.statusBarHeight;
|
||||
globalData.value.windowHeight = uni.getSystemInfoSync()?.windowHeight;
|
||||
|
||||
// 热更新
|
||||
// #ifdef APP-PLUS
|
||||
wgtUp();
|
||||
// #endif
|
||||
ensureLoadingPatched();
|
||||
ensureGlobalHotUpdateChecked();
|
||||
// socket()
|
||||
});
|
||||
const downloadFile = () => {
|
||||
let url = filePath.value;
|
||||
uni.downloadFile({
|
||||
url: url,
|
||||
success: (downloadResult) => {
|
||||
uni.showToast({
|
||||
title: '更新中....',
|
||||
icon: 'none',
|
||||
});
|
||||
if (downloadResult) {
|
||||
if (downloadResult.statusCode === 200) {
|
||||
plus.runtime.install(
|
||||
downloadResult.tempFilePath, {
|
||||
force: true,
|
||||
},
|
||||
function() {
|
||||
uni.showModal({
|
||||
title: '成功提示',
|
||||
content: '热更新成功,请重启应用',
|
||||
confirmColor: '#FA7E31',
|
||||
showCancel: false,
|
||||
success: function(res) {
|
||||
if (res.confirm) {
|
||||
plus.runtime.restart(); //自动重启软件
|
||||
}
|
||||
},
|
||||
});
|
||||
},
|
||||
function(e) {
|
||||
uni.showModal({
|
||||
title: '失败提示',
|
||||
content: e,
|
||||
showCancel: false,
|
||||
confirmColor: '#FA7E31',
|
||||
});
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
},
|
||||
});
|
||||
};
|
||||
onLaunch(() => {
|
||||
// 隐藏原生导航栏 使用自定义底部导航
|
||||
// uni.hideTabBar({
|
||||
@@ -264,4 +241,4 @@
|
||||
.uni-app--showtopwindow uni-page-head {
|
||||
display: none;
|
||||
}
|
||||
</style>
|
||||
</style>
|
||||
|
||||
Reference in New Issue
Block a user