|
|
|
@ -2,13 +2,14 @@ |
|
|
|
import { |
|
|
|
WxToken |
|
|
|
} from './common/wx_token.js' |
|
|
|
// import user from '@/api/user.js' |
|
|
|
|
|
|
|
import Api_url from '@/common/config.js' |
|
|
|
var wxtoken = new WxToken(); |
|
|
|
export default { |
|
|
|
async onLaunch() { |
|
|
|
// 检查小程序更新 |
|
|
|
// 初始化版本信息 |
|
|
|
this.initializeVersionInfo(); |
|
|
|
|
|
|
|
// 检查更新 |
|
|
|
this.checkForUpdate(); |
|
|
|
|
|
|
|
// #ifdef MP-WEIXIN |
|
|
|
@ -46,6 +47,22 @@ |
|
|
|
onHide: function() {}, |
|
|
|
|
|
|
|
methods: { |
|
|
|
/** |
|
|
|
* 初始化版本信息 |
|
|
|
*/ |
|
|
|
initializeVersionInfo() { |
|
|
|
// 设置应用版本号(可配置化) |
|
|
|
const appVersion = '1.0.0'; // 可从配置文件读取 |
|
|
|
const buildTime = Date.now(); |
|
|
|
|
|
|
|
// 如果没有存储过版本信息,则设置初始值 |
|
|
|
if (!uni.getStorageSync('app_version')) { |
|
|
|
uni.setStorageSync('app_version', appVersion); |
|
|
|
uni.setStorageSync('build_time', buildTime); |
|
|
|
console.log(`初始化版本信息: ${appVersion}`); |
|
|
|
} |
|
|
|
}, |
|
|
|
|
|
|
|
/** |
|
|
|
* 检查小程序更新 |
|
|
|
*/ |
|
|
|
@ -185,6 +202,24 @@ |
|
|
|
}); |
|
|
|
}, |
|
|
|
|
|
|
|
/** |
|
|
|
* 统一的版本检查方法(支持多平台) |
|
|
|
*/ |
|
|
|
checkVersionUpdate() { |
|
|
|
// 检查是否需要提醒更新(避免频繁提示) |
|
|
|
const reminderTime = uni.getStorageSync('update_reminder_time'); |
|
|
|
const now = Date.now(); |
|
|
|
|
|
|
|
// 如果用户选择稍后更新,24小时内不再提示 |
|
|
|
if (reminderTime && (now - reminderTime < 24 * 60 * 60 * 1000)) { |
|
|
|
console.log('用户选择稍后更新,暂不提示'); |
|
|
|
return; |
|
|
|
} |
|
|
|
|
|
|
|
// 执行服务器版本检查 |
|
|
|
this.checkVersionFromServer(); |
|
|
|
}, |
|
|
|
|
|
|
|
/** |
|
|
|
* H5环境下的更新检查(可选功能) |
|
|
|
*/ |
|
|
|
@ -196,60 +231,193 @@ |
|
|
|
const buildTime = uni.getStorageSync('build_time') || Date.now(); |
|
|
|
const now = Date.now(); |
|
|
|
|
|
|
|
// 每小时检查一次更新 |
|
|
|
if (now - buildTime > 60 * 60 * 1000) { |
|
|
|
// 每30分钟检查一次更新 |
|
|
|
if (now - buildTime > 30 * 60 * 1000) { |
|
|
|
console.log('H5环境检查更新'); |
|
|
|
// 这里可以调用API检查是否有新版本 |
|
|
|
// 如果有新版本,可以提示用户刷新页面 |
|
|
|
this.checkVersionFromServer(); |
|
|
|
this.checkVersionUpdate(); |
|
|
|
} |
|
|
|
} catch (error) { |
|
|
|
console.error('H5更新检查失败:', error); |
|
|
|
} |
|
|
|
// #endif |
|
|
|
|
|
|
|
// #ifdef MP-WEIXIN |
|
|
|
// 微信小程序也进行服务器版本检查 |
|
|
|
this.checkVersionUpdate(); |
|
|
|
// #endif |
|
|
|
}, |
|
|
|
|
|
|
|
/** |
|
|
|
* 从服务器检查版本信息(H5环境) |
|
|
|
* 从服务器检查版本信息(通用) |
|
|
|
*/ |
|
|
|
async checkVersionFromServer() { |
|
|
|
try { |
|
|
|
// 这里应该调用你的API来获取最新版本信息 |
|
|
|
// const response = await uni.request({ |
|
|
|
// url: Api_url.domain + '/api/app/version', |
|
|
|
// method: 'GET' |
|
|
|
// }); |
|
|
|
// |
|
|
|
// if (response.data.code === 1) { |
|
|
|
// const serverVersion = response.data.data.version; |
|
|
|
// const currentVersion = uni.getStorageSync('app_version') || '1.0.0'; |
|
|
|
// |
|
|
|
// if (this.compareVersion(serverVersion, currentVersion) > 0) { |
|
|
|
// this.showH5UpdateDialog(); |
|
|
|
// } |
|
|
|
// } |
|
|
|
|
|
|
|
console.log('从服务器检查H5版本更新'); |
|
|
|
const currentVersion = uni.getStorageSync('app_version') || '1.0.0'; |
|
|
|
const platform = this.getPlatform(); |
|
|
|
|
|
|
|
console.log(`检查版本更新 - 当前版本: ${currentVersion}, 平台: ${platform}`); |
|
|
|
|
|
|
|
// 调用API获取最新版本信息 |
|
|
|
const response = await uni.request({ |
|
|
|
url: Api_url.domain + '/api/app/version', |
|
|
|
method: 'GET', |
|
|
|
data: { |
|
|
|
platform: platform, |
|
|
|
version: currentVersion |
|
|
|
} |
|
|
|
}); |
|
|
|
|
|
|
|
if (response.data && response.data.code === 1) { |
|
|
|
const versionData = response.data.data; |
|
|
|
const serverVersion = versionData.version; |
|
|
|
const forceUpdate = versionData.force_update || false; |
|
|
|
const updateLog = versionData.update_log || ''; |
|
|
|
|
|
|
|
console.log(`服务器版本: ${serverVersion}, 强制更新: ${forceUpdate}`); |
|
|
|
|
|
|
|
if (this.compareVersion(serverVersion, currentVersion) > 0) { |
|
|
|
// 保存更新信息 |
|
|
|
uni.setStorageSync('latest_version', serverVersion); |
|
|
|
uni.setStorageSync('update_log', updateLog); |
|
|
|
uni.setStorageSync('force_update', forceUpdate); |
|
|
|
|
|
|
|
// #ifdef MP-WEIXIN |
|
|
|
// 微信小程序的更新由微信管理,这里只提示用户 |
|
|
|
this.showVersionUpdateDialog(serverVersion, updateLog, forceUpdate); |
|
|
|
// #endif |
|
|
|
|
|
|
|
// #ifdef H5 |
|
|
|
// H5环境直接提示刷新 |
|
|
|
this.showH5UpdateDialog(serverVersion, updateLog, forceUpdate); |
|
|
|
// #endif |
|
|
|
} else { |
|
|
|
console.log('当前版本已是最新'); |
|
|
|
// 清除过期的更新信息 |
|
|
|
uni.removeStorageSync('latest_version'); |
|
|
|
uni.removeStorageSync('update_log'); |
|
|
|
uni.removeStorageSync('force_update'); |
|
|
|
} |
|
|
|
} else { |
|
|
|
console.warn('获取版本信息失败:', response.data?.msg || '未知错误'); |
|
|
|
} |
|
|
|
} catch (error) { |
|
|
|
console.error('服务器版本检查失败:', error); |
|
|
|
} |
|
|
|
}, |
|
|
|
|
|
|
|
/** |
|
|
|
* 获取当前平台标识 |
|
|
|
*/ |
|
|
|
getPlatform() { |
|
|
|
// #ifdef MP-WEIXIN |
|
|
|
return 'mp-weixin'; |
|
|
|
// #endif |
|
|
|
|
|
|
|
// #ifdef H5 |
|
|
|
return 'h5'; |
|
|
|
// #endif |
|
|
|
|
|
|
|
// #ifdef APP-PLUS |
|
|
|
return 'app'; |
|
|
|
// #endif |
|
|
|
|
|
|
|
return 'unknown'; |
|
|
|
}, |
|
|
|
|
|
|
|
/** |
|
|
|
* 显示版本更新对话框(微信小程序) |
|
|
|
*/ |
|
|
|
showVersionUpdateDialog(newVersion, updateLog, forceUpdate) { |
|
|
|
const content = `发现新版本 ${newVersion}\n${updateLog}`; |
|
|
|
|
|
|
|
if (forceUpdate) { |
|
|
|
// 强制更新 |
|
|
|
uni.showModal({ |
|
|
|
title: '重要更新', |
|
|
|
content: content + '\n\n此为重要更新,必须立即更新才能继续使用。', |
|
|
|
showCancel: false, |
|
|
|
confirmText: '立即更新', |
|
|
|
success: () => { |
|
|
|
this.triggerWeChatUpdate(); |
|
|
|
} |
|
|
|
}); |
|
|
|
} else { |
|
|
|
// 可选更新 |
|
|
|
uni.showModal({ |
|
|
|
title: '发现新版本', |
|
|
|
content: content, |
|
|
|
confirmText: '立即更新', |
|
|
|
cancelText: '稍后更新', |
|
|
|
success: (res) => { |
|
|
|
if (res.confirm) { |
|
|
|
this.triggerWeChatUpdate(); |
|
|
|
} else { |
|
|
|
// 记录用户选择稍后更新,24小时后再提示 |
|
|
|
uni.setStorageSync('update_reminder_time', Date.now()); |
|
|
|
} |
|
|
|
} |
|
|
|
}); |
|
|
|
} |
|
|
|
}, |
|
|
|
|
|
|
|
/** |
|
|
|
* 触发微信小程序更新检查 |
|
|
|
*/ |
|
|
|
triggerWeChatUpdate() { |
|
|
|
// #ifdef MP-WEIXIN |
|
|
|
try { |
|
|
|
if (typeof uni !== 'undefined' && typeof uni.getUpdateManager === 'function') { |
|
|
|
const updateManager = uni.getUpdateManager(); |
|
|
|
|
|
|
|
if (updateManager && typeof updateManager.checkForUpdate === 'function') { |
|
|
|
updateManager.checkForUpdate(); |
|
|
|
} |
|
|
|
} |
|
|
|
} catch (error) { |
|
|
|
console.error('触发微信更新失败:', error); |
|
|
|
uni.showToast({ |
|
|
|
title: '请手动删除小程序重新进入', |
|
|
|
icon: 'none', |
|
|
|
duration: 3000 |
|
|
|
}); |
|
|
|
} |
|
|
|
// #endif |
|
|
|
}, |
|
|
|
|
|
|
|
/** |
|
|
|
* 显示H5更新对话框 |
|
|
|
*/ |
|
|
|
showH5UpdateDialog() { |
|
|
|
showH5UpdateDialog(newVersion, updateLog, forceUpdate) { |
|
|
|
const content = `发现新版本 ${newVersion}\n${updateLog}\n\n检测到新版本,建议刷新页面以获得最佳体验`; |
|
|
|
|
|
|
|
if (forceUpdate) { |
|
|
|
// 强制更新 |
|
|
|
uni.showModal({ |
|
|
|
title: '重要更新', |
|
|
|
content: content + '\n\n此为重要更新,必须立即刷新才能继续使用。', |
|
|
|
showCancel: false, |
|
|
|
confirmText: '立即刷新', |
|
|
|
success: () => { |
|
|
|
location.reload(); |
|
|
|
} |
|
|
|
}); |
|
|
|
} else { |
|
|
|
// 可选更新 |
|
|
|
uni.showModal({ |
|
|
|
title: '发现新版本', |
|
|
|
content: '检测到新版本,建议刷新页面以获得最佳体验', |
|
|
|
content: content, |
|
|
|
confirmText: '立即刷新', |
|
|
|
cancelText: '稍后刷新', |
|
|
|
success: (res) => { |
|
|
|
if (res.confirm) { |
|
|
|
location.reload(); |
|
|
|
} else { |
|
|
|
// 记录用户选择稍后更新,24小时后再提示 |
|
|
|
uni.setStorageSync('update_reminder_time', Date.now()); |
|
|
|
} |
|
|
|
} |
|
|
|
}); |
|
|
|
} |
|
|
|
}, |
|
|
|
|
|
|
|
/** |
|
|
|
|