congming_huose-apk/common/config/appVersionConfig.js

50 lines
1.7 KiB
JavaScript
Raw Normal View History

/**
* 全局仅维护一个版本号字符串 1.0.2
*
* - 本地存储 APP_GLOBAL_VERSION_INFO只存版本名字符串每次进入应用 onShow 会重新写入
* - 编译注入 vue.config __APP_MANIFEST_VERSION__ 来自 manifest versionName否则用 FALLBACK
*/
import { getCurrentAppVersionInfo } from '@/common/utils/appVersion.js'
const FALLBACK_VERSION = '1.0.1'
/** 与 manifest 同步的展示用版本(未注入构建时等于 FALLBACK */
export const APP_GLOBAL_VERSION =
typeof __APP_MANIFEST_VERSION__ !== 'undefined'
? __APP_MANIFEST_VERSION__
: FALLBACK_VERSION
export const APP_GLOBAL_VERSION_STORAGE_KEY = 'APP_GLOBAL_VERSION_INFO'
/**
* 重新读取当前运行版本名并写入本地仅存字符串 versionCode / 无二次写入
*/
export function syncGlobalVersionToLocalStorage() {
try {
const info = getCurrentAppVersionInfo()
const v = String(info.versionName || '').trim()
uni.setStorageSync(APP_GLOBAL_VERSION_STORAGE_KEY, v || APP_GLOBAL_VERSION)
} catch (e) {
console.warn('[appVersionConfig] syncGlobalVersionToLocalStorage', e)
}
}
/**
* 读取本地已保存的版本名字符串无则 null
* 兼容旧版曾写入对象 { versionName, versionCode } 的情况
*/
export function readGlobalVersionFromStorage() {
try {
const raw = uni.getStorageSync(APP_GLOBAL_VERSION_STORAGE_KEY)
if (raw == null || raw === '') return null
if (typeof raw === 'string') return raw.trim() || null
if (typeof raw === 'object' && raw !== null) {
const n = raw.versionName != null ? String(raw.versionName).trim() : ''
if (n) return n
const v = raw.version != null ? String(raw.version).trim() : ''
if (v) return v
}
} catch (e) {}
return null
}