发现新版本1.1
This commit is contained in:
parent
84d4792aca
commit
7387f6912b
|
|
@ -73,17 +73,14 @@ import { ref, computed, onMounted } from 'vue'
|
||||||
import { storeToRefs } from 'pinia'
|
import { storeToRefs } from 'pinia'
|
||||||
import { useUserStore } from '@/store/user'
|
import { useUserStore } from '@/store/user'
|
||||||
import { logout } from '@/api/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 loading = ref(false)
|
||||||
const checking = ref(false)
|
const checking = ref(false)
|
||||||
const currentVersion = ref('')
|
const currentVersion = ref('')
|
||||||
const latestVersion = ref('')
|
const latestVersion = ref('')
|
||||||
const updateInfo = ref(null)
|
const updateInfo = ref(null)
|
||||||
const hasUpdate = computed(() => {
|
const hasUpdate = ref(false) // 直接使用 API 返回的 hasUpdate 值
|
||||||
if (!latestVersion.value || !currentVersion.value) return false
|
|
||||||
return compareVersion(currentVersion.value, latestVersion.value) < 0
|
|
||||||
})
|
|
||||||
|
|
||||||
const userStore = useUserStore()
|
const userStore = useUserStore()
|
||||||
const { privateView, userInfo } = storeToRefs(userStore)
|
const { privateView, userInfo } = storeToRefs(userStore)
|
||||||
|
|
@ -109,17 +106,34 @@ const checkUpdate = async () => {
|
||||||
try {
|
try {
|
||||||
const result = await checkForUpdate(true) // 静默检查,不显示提示
|
const result = await checkForUpdate(true) // 静默检查,不显示提示
|
||||||
|
|
||||||
|
console.log('检查更新结果:', result)
|
||||||
|
|
||||||
if (result && result.hasUpdate) {
|
if (result && result.hasUpdate) {
|
||||||
latestVersion.value = result.versionName || ''
|
// 确保版本号有值,即使 API 返回为空也显示一个默认值
|
||||||
|
latestVersion.value = result.versionName || result.versionNumber || result.version || '未知版本'
|
||||||
updateInfo.value = result
|
updateInfo.value = result
|
||||||
uni.showToast({
|
hasUpdate.value = true // 设置更新标志
|
||||||
title: '发现新版本',
|
|
||||||
icon: 'success',
|
// 检查下载地址是否存在(兼容 packageUrl、downloadUrl、download_url)
|
||||||
duration: 2000
|
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 {
|
} else {
|
||||||
|
// 没有新版本时,也显示当前版本号
|
||||||
latestVersion.value = currentVersion.value
|
latestVersion.value = currentVersion.value
|
||||||
updateInfo.value = null
|
updateInfo.value = null
|
||||||
|
hasUpdate.value = false // 清除更新标志
|
||||||
uni.showToast({
|
uni.showToast({
|
||||||
title: '已是最新版本',
|
title: '已是最新版本',
|
||||||
icon: 'success',
|
icon: 'success',
|
||||||
|
|
@ -140,7 +154,7 @@ const checkUpdate = async () => {
|
||||||
|
|
||||||
// 处理更新
|
// 处理更新
|
||||||
const handleUpdate = () => {
|
const handleUpdate = () => {
|
||||||
if (!updateInfo.value || !updateInfo.value.downloadUrl) {
|
if (!updateInfo.value) {
|
||||||
uni.showToast({
|
uni.showToast({
|
||||||
title: '更新信息无效',
|
title: '更新信息无效',
|
||||||
icon: 'none'
|
icon: 'none'
|
||||||
|
|
@ -148,8 +162,25 @@ const handleUpdate = () => {
|
||||||
return
|
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 () => {
|
const onLogout = async () => {
|
||||||
|
|
|
||||||
|
|
@ -4,25 +4,31 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { checkAppVersion } from '@/api/update'
|
import { checkAppVersion } from '@/api/update'
|
||||||
|
import manifest from '@/manifest.json'
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取当前应用版本信息
|
* 获取当前应用版本信息
|
||||||
* @returns {Object} {versionName}
|
* @returns {Object} {versionName}
|
||||||
*/
|
*/
|
||||||
export const getCurrentVersion = () => {
|
export const getCurrentVersion = () => {
|
||||||
|
// 统一从 manifest.json 读取版本号
|
||||||
|
let versionName = manifest?.versionName || ''
|
||||||
|
|
||||||
// #ifdef APP-PLUS
|
// #ifdef APP-PLUS
|
||||||
const versionName = plus.runtime.version || ''
|
// APP 环境下,如果 manifest 读取失败,使用运行时版本作为后备
|
||||||
return {
|
if (!versionName) {
|
||||||
versionName: versionName
|
versionName = plus.runtime.version || ''
|
||||||
}
|
}
|
||||||
// #endif
|
// #endif
|
||||||
|
|
||||||
// #ifndef APP-PLUS
|
// 如果都读取不到,使用默认值
|
||||||
// 非 APP 环境,从 manifest.json 读取(开发环境)
|
if (!versionName) {
|
||||||
return {
|
versionName = '1.0.1'
|
||||||
versionName: '1.0.1'
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
versionName: versionName
|
||||||
}
|
}
|
||||||
// #endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -77,7 +83,12 @@ export const checkForUpdate = async (silent = false) => {
|
||||||
platform: 'android'
|
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 (!result || !result.hasUpdate) {
|
||||||
if (!silent) {
|
if (!silent) {
|
||||||
|
|
@ -90,7 +101,22 @@ export const checkForUpdate = async (silent = false) => {
|
||||||
return null
|
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) {
|
} catch (error) {
|
||||||
console.error('检查版本更新失败:', error)
|
console.error('检查版本更新失败:', error)
|
||||||
if (!silent) {
|
if (!silent) {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user