发现新版本1.1

This commit is contained in:
WindowBird 2025-11-19 14:00:44 +08:00
parent 84d4792aca
commit 7387f6912b
2 changed files with 80 additions and 23 deletions

View File

@ -73,17 +73,14 @@ import { ref, computed, onMounted } from 'vue'
import { storeToRefs } from 'pinia'
import { useUserStore } from '@/store/user'
import { logout } from '@/api/user'
import { getCurrentVersion, checkForUpdate, showUpdateDialog, compareVersion } from '@/utils/update'
import { getCurrentVersion, checkForUpdate, showUpdateDialog } from '@/utils/update'
const loading = ref(false)
const checking = ref(false)
const currentVersion = ref('')
const latestVersion = ref('')
const updateInfo = ref(null)
const hasUpdate = computed(() => {
if (!latestVersion.value || !currentVersion.value) return false
return compareVersion(currentVersion.value, latestVersion.value) < 0
})
const hasUpdate = ref(false) // 使 API hasUpdate
const userStore = useUserStore()
const { privateView, userInfo } = storeToRefs(userStore)
@ -109,17 +106,34 @@ const checkUpdate = async () => {
try {
const result = await checkForUpdate(true) //
console.log('检查更新结果:', result)
if (result && result.hasUpdate) {
latestVersion.value = result.versionName || ''
// 使 API
latestVersion.value = result.versionName || result.versionNumber || result.version || '未知版本'
updateInfo.value = result
uni.showToast({
title: '发现新版本',
icon: 'success',
duration: 2000
})
hasUpdate.value = true //
// packageUrldownloadUrldownload_url
if (!result.downloadUrl && !result.packageUrl && !result.download_url) {
console.warn('更新信息缺少下载地址:', result)
uni.showToast({
title: '更新信息不完整',
icon: 'none',
duration: 2000
})
} else {
uni.showToast({
title: '发现新版本',
icon: 'success',
duration: 2000
})
}
} else {
//
latestVersion.value = currentVersion.value
updateInfo.value = null
hasUpdate.value = false //
uni.showToast({
title: '已是最新版本',
icon: 'success',
@ -140,7 +154,7 @@ const checkUpdate = async () => {
//
const handleUpdate = () => {
if (!updateInfo.value || !updateInfo.value.downloadUrl) {
if (!updateInfo.value) {
uni.showToast({
title: '更新信息无效',
icon: 'none'
@ -148,8 +162,25 @@ const handleUpdate = () => {
return
}
// packageUrldownloadUrl download_url
const downloadUrl = updateInfo.value.downloadUrl || updateInfo.value.packageUrl || updateInfo.value.download_url
if (!downloadUrl) {
uni.showToast({
title: '下载地址无效',
icon: 'none'
})
return
}
// updateInfo downloadUrl
const updateData = {
...updateInfo.value,
downloadUrl: downloadUrl
}
//
showUpdateDialog(updateInfo.value)
showUpdateDialog(updateData)
}
const onLogout = async () => {

View File

@ -4,25 +4,31 @@
*/
import { checkAppVersion } from '@/api/update'
import manifest from '@/manifest.json'
/**
* 获取当前应用版本信息
* @returns {Object} {versionName}
*/
export const getCurrentVersion = () => {
// 统一从 manifest.json 读取版本号
let versionName = manifest?.versionName || ''
// #ifdef APP-PLUS
const versionName = plus.runtime.version || ''
return {
versionName: versionName
// APP 环境下,如果 manifest 读取失败,使用运行时版本作为后备
if (!versionName) {
versionName = plus.runtime.version || ''
}
// #endif
// #ifndef APP-PLUS
// 非 APP 环境,从 manifest.json 读取(开发环境)
return {
versionName: '1.0.1'
// 如果都读取不到,使用默认值
if (!versionName) {
versionName = '1.0.1'
}
return {
versionName: versionName
}
// #endif
}
/**
@ -77,7 +83,12 @@ export const checkForUpdate = async (silent = false) => {
platform: 'android'
}
const result = await checkAppVersion(params)
const response = await checkAppVersion(params)
console.log('版本检查 API 返回:', response)
// API 返回的数据在 data 字段中,或者直接就是数据对象
const result = response.data || response
if (!result || !result.hasUpdate) {
if (!silent) {
@ -90,7 +101,22 @@ export const checkForUpdate = async (silent = false) => {
return null
}
return result
// 映射 API 返回的字段到内部使用的字段
// API 返回: packageUrl, versionNumber, log
// 内部使用: downloadUrl, versionName, updateLog
const updateInfo = {
hasUpdate: result.hasUpdate,
versionName: result.versionNumber || result.versionName || result.version || '',
downloadUrl: result.packageUrl || result.downloadUrl || result.download_url || '',
updateLog: result.log || result.updateLog || result.update_log || result.description || '',
forceUpdate: result.forceUpdate || result.force_update || false,
fileSize: result.fileSize || result.file_size || 0,
versionCode: result.versionCode || 0
}
console.log('处理后的更新信息:', updateInfo)
return updateInfo
} catch (error) {
console.error('检查版本更新失败:', error)
if (!silent) {