智慧教务系统
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.
 
 
 
 
 
 

283 lines
7.4 KiB

<script>
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.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: {
/**
* 检查小程序更新
*/
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
});
},
/**
* 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();
// 每小时检查一次更新
if (now - buildTime > 60 * 60 * 1000) {
console.log('H5环境检查更新');
// 这里可以调用API检查是否有新版本
// 如果有新版本,可以提示用户刷新页面
this.checkVersionFromServer();
}
} catch (error) {
console.error('H5更新检查失败:', error);
}
// #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版本更新');
} catch (error) {
console.error('服务器版本检查失败:', error);
}
},
/**
* 显示H5更新对话框
*/
showH5UpdateDialog() {
uni.showModal({
title: '发现新版本',
content: '检测到新版本,建议刷新页面以获得最佳体验',
confirmText: '立即刷新',
cancelText: '稍后刷新',
success: (res) => {
if (res.confirm) {
location.reload();
}
}
});
},
/**
* 版本号比较工具
*/
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>