diff --git a/components/my/My.vue b/components/my/My.vue index a6cbaa4..e196065 100644 --- a/components/my/My.vue +++ b/components/my/My.vue @@ -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 // 设置更新标志 + + // 检查下载地址是否存在(兼容 packageUrl、downloadUrl、download_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 } + // 兼容不同的字段名(packageUrl、downloadUrl 或 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 () => { diff --git a/utils/update.js b/utils/update.js index 4481db2..b49af82 100644 --- a/utils/update.js +++ b/utils/update.js @@ -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) {