智慧教务系统
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

451 lines
12 KiB

<script>
import {
WxToken
} from './common/wx_token.js'
import Api_url from '@/common/config.js'
var wxtoken = new WxToken();
export default {
async onLaunch() {
// 初始化版本信息
this.initializeVersionInfo();
// 检查更新
this.checkForUpdate();
// #ifdef MP-WEIXIN
uni.login({
provider: 'weixin',
success: function(loginRes) {
let data = {
'code': loginRes.code
}
// user.get_xcx_token(data).then(res => {
// uni.setStorageSync('token', res.data.token)
// user.get_info().then(res=>{
// uni.setStorageSync('sfm',res.data.invite_code)
// })
// })
}
})
// #endif
// #ifdef H5
var ua = navigator.userAgent.toLowerCase();
var isWeixin = ua.indexOf('micromessenger') != -1;
if (!isWeixin) {
console.log("非公众号-暂不登陆")
return false;
} else {
wxtoken.verify('userinfo'); //静默获取openid
}
//微信公众号获取token -必须是认证的服务号
// #endif
},
onShow: function() {
// 应用切换到前台时再次检查更新
this.checkForUpdate();
},
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}`);
}
},
/**
* 检查小程序更新
*/
checkForUpdate() {
// #ifdef MP-WEIXIN
try {
// 双重检查确保在微信小程序环境中
if (typeof uni !== 'undefined' && typeof uni.getUpdateManager === 'function') {
const updateManager = uni.getUpdateManager();
// 确保updateManager对象存在且有所需方法
if (updateManager && typeof updateManager.checkForUpdate === 'function') {
// 监听向微信后台请求检查更新结果事件
updateManager.onCheckForUpdate((res) => {
console.log('检查更新结果:', res);
if (res.hasUpdate) {
console.log('发现新版本,准备下载');
uni.showToast({
title: '发现新版本',
icon: 'none',
duration: 2000
});
}
});
// 监听小程序有版本更新事件
updateManager.onUpdateReady(() => {
console.log('新版本下载完成,准备重启应用');
this.showUpdateDialog();
});
// 监听小程序更新失败事件
updateManager.onUpdateFailed(() => {
console.log('新版本下载失败');
uni.showToast({
title: '更新失败,请检查网络',
icon: 'none',
duration: 2000
});
});
// 主动检查更新
updateManager.checkForUpdate();
} else {
console.log('updateManager 或 checkForUpdate 方法不可用');
}
} else {
console.log('uni.getUpdateManager 不可用,可能不在微信小程序环境中');
}
} catch (error) {
console.error('更新管理器初始化失败:', error);
}
// #endif
// #ifdef H5
// H5环境下的更新检查(可选)
this.checkH5Update();
// #endif
},
/**
* 显示更新对话框
*/
showUpdateDialog() {
uni.showModal({
title: '更新提示',
content: '新版本已下载完成,是否立即重启应用以更新到最新版本?',
confirmText: '立即重启',
cancelText: '稍后重启',
success: (res) => {
if (res.confirm) {
console.log('用户选择立即重启');
this.applyUpdate();
} else {
console.log('用户选择稍后重启');
// 可以设置定时器在一定时间后自动重启
this.scheduleDelayedUpdate();
}
}
});
},
/**
* 应用更新并重启
*/
applyUpdate() {
// #ifdef MP-WEIXIN
try {
// 双重检查确保在微信小程序环境中
if (typeof uni !== 'undefined' && typeof uni.getUpdateManager === 'function') {
const updateManager = uni.getUpdateManager();
// 确保updateManager对象存在且有所需方法
if (updateManager && typeof updateManager.applyUpdate === 'function') {
updateManager.applyUpdate();
} else {
console.log('updateManager 或 applyUpdate 方法不可用');
uni.showToast({
title: '重启功能不可用',
icon: 'none',
duration: 2000
});
}
} else {
console.log('uni.getUpdateManager 不可用,可能不在微信小程序环境中');
uni.showToast({
title: '重启功能不可用',
icon: 'none',
duration: 2000
});
}
} catch (error) {
console.error('应用更新失败:', error);
uni.showToast({
title: '重启失败,请手动重启',
icon: 'none',
duration: 2000
});
}
// #endif
},
/**
* 安排延迟更新
*/
scheduleDelayedUpdate() {
// 5分钟后自动重启
setTimeout(() => {
console.log('自动应用更新');
this.applyUpdate();
}, 5 * 60 * 1000);
uni.showToast({
title: '将在5分钟后自动重启',
icon: 'none',
duration: 3000
});
},
/**
* 统一的版本检查方法(支持多平台)
*/
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环境下的更新检查(可选功能)
*/
checkH5Update() {
// #ifdef H5
try {
// 检查页面缓存版本
const currentVersion = uni.getStorageSync('app_version') || '1.0.0';
const buildTime = uni.getStorageSync('build_time') || Date.now();
const now = Date.now();
// 每30分钟检查一次更新
if (now - buildTime > 30 * 60 * 1000) {
console.log('H5环境检查更新');
this.checkVersionUpdate();
}
} catch (error) {
console.error('H5更新检查失败:', error);
}
// #endif
// #ifdef MP-WEIXIN
// 微信小程序也进行服务器版本检查
this.checkVersionUpdate();
// #endif
},
/**
* 从服务器检查版本信息(通用)
*/
async checkVersionFromServer() {
try {
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(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,
confirmText: '立即刷新',
cancelText: '稍后刷新',
success: (res) => {
if (res.confirm) {
location.reload();
} else {
// 记录用户选择稍后更新,24小时后再提示
uni.setStorageSync('update_reminder_time', Date.now());
}
}
});
}
},
/**
* 版本号比较工具
*/
compareVersion(version1, version2) {
const v1 = version1.split('.').map(Number);
const v2 = version2.split('.').map(Number);
for (let i = 0; i < Math.max(v1.length, v2.length); i++) {
const a = v1[i] || 0;
const b = v2[i] || 0;
if (a > b) return 1;
if (a < b) return -1;
}
return 0;
}
}
}
</script>
<style lang="scss">
// 样式文件已迁移到后端,如需使用请通过网络请求获取
@import '@/static/styles/app.scss';
@import "@/uni.scss";
// 引入FirstUI主题样式
@import "@/components/firstui/fui-theme/fui-theme.css";
</style>