151 lines
4.1 KiB
JavaScript
151 lines
4.1 KiB
JavaScript
// app.js - 拆约小程序入口
|
|
const { config } = require('./config');
|
|
|
|
App({
|
|
globalData: {
|
|
// API 基础地址(从 config.js 统一读取)
|
|
apiBaseUrl: config.apiBaseUrl,
|
|
// 用户信息
|
|
userInfo: null,
|
|
// token
|
|
token: null,
|
|
// 用户ID
|
|
userId: null,
|
|
// 是否为活动管理员(可创建/编辑/删除活动)
|
|
isAdmin: false,
|
|
},
|
|
|
|
onLaunch(options) {
|
|
// 小程序启动时执行
|
|
console.log('App launched', options);
|
|
// 检查版本更新
|
|
this.checkUpdate();
|
|
// 检查登录状态
|
|
this.checkLoginStatus();
|
|
// 验证当前用户是否在数据库中存在
|
|
this.verifyUserExists();
|
|
},
|
|
|
|
onShow(options) {
|
|
// 小程序显示时执行
|
|
console.log('App showed', options);
|
|
},
|
|
|
|
// 验证当前登录用户是否在数据库中存在,不存在则清除登录状态
|
|
async verifyUserExists() {
|
|
const token = wx.getStorageSync('token');
|
|
const userId = wx.getStorageSync('userId');
|
|
if (!token || !userId) return;
|
|
|
|
try {
|
|
const { config } = require('./config');
|
|
const res = await new Promise((resolve, reject) => {
|
|
wx.request({
|
|
url: `${config.apiBaseUrl}/user/profile`,
|
|
method: 'GET',
|
|
header: {
|
|
'Authorization': `Bearer ${token}`,
|
|
'x-user-id': userId,
|
|
},
|
|
timeout: 5000,
|
|
success: resolve,
|
|
fail: reject,
|
|
});
|
|
});
|
|
|
|
if (res.statusCode !== 200 || !res.data || !res.data.success) {
|
|
console.log('用户不存在于当前数据库,清除登录状态');
|
|
this.clearLoginInfo();
|
|
} else if (res.data.data) {
|
|
// 同步管理员身份(后台可随时调整用户角色,每次启动刷新)
|
|
const isAdmin = !!res.data.data.isAdmin;
|
|
if (this.globalData.isAdmin !== isAdmin) {
|
|
console.log('管理员状态已更新:', isAdmin);
|
|
}
|
|
this.globalData.isAdmin = isAdmin;
|
|
wx.setStorageSync('isAdmin', isAdmin);
|
|
}
|
|
} catch (err) {
|
|
console.log('验证用户存在性失败,清除登录状态', err);
|
|
this.clearLoginInfo();
|
|
}
|
|
},
|
|
|
|
onHide() {
|
|
// 小程序隐藏时执行
|
|
},
|
|
|
|
// 检查小程序版本更新
|
|
checkUpdate() {
|
|
if (!wx.getUpdateManager) return;
|
|
const updateManager = wx.getUpdateManager();
|
|
updateManager.onCheckForUpdate((res) => {
|
|
if (res.hasUpdate) console.log('检测到新版本');
|
|
});
|
|
updateManager.onUpdateReady(() => {
|
|
wx.showModal({
|
|
title: '更新提示',
|
|
content: '新版本已经准备好,是否重启应用?',
|
|
success: (res) => {
|
|
if (res.confirm) updateManager.applyUpdate();
|
|
},
|
|
});
|
|
});
|
|
updateManager.onUpdateFailed(() => {
|
|
wx.showModal({
|
|
title: '更新提示',
|
|
content: '新版本下载失败,请删除当前小程序后重新搜索打开',
|
|
showCancel: false,
|
|
});
|
|
});
|
|
},
|
|
|
|
// 检查登录状态
|
|
checkLoginStatus() {
|
|
const token = wx.getStorageSync('token');
|
|
const userId = wx.getStorageSync('userId');
|
|
if (token && userId) {
|
|
this.globalData.token = token;
|
|
this.globalData.userId = userId;
|
|
}
|
|
this.globalData.isAdmin = wx.getStorageSync('isAdmin') || false;
|
|
},
|
|
|
|
// 设置用户信息
|
|
setUserInfo(userInfo) {
|
|
this.globalData.userInfo = userInfo;
|
|
if (userInfo && typeof userInfo.isAdmin !== 'undefined') {
|
|
this.globalData.isAdmin = !!userInfo.isAdmin;
|
|
wx.setStorageSync('isAdmin', !!userInfo.isAdmin);
|
|
}
|
|
},
|
|
|
|
// 设置 token
|
|
setToken(token) {
|
|
this.globalData.token = token;
|
|
wx.setStorageSync('token', token);
|
|
},
|
|
|
|
// 设置用户ID
|
|
setUserId(userId) {
|
|
this.globalData.userId = userId;
|
|
wx.setStorageSync('userId', userId);
|
|
},
|
|
|
|
// 清除登录信息
|
|
clearLoginInfo() {
|
|
this.globalData.token = null;
|
|
this.globalData.userId = null;
|
|
this.globalData.userInfo = null;
|
|
this.globalData.isAdmin = false;
|
|
wx.removeStorageSync('token');
|
|
wx.removeStorageSync('userId');
|
|
wx.removeStorageSync('isAdmin');
|
|
},
|
|
|
|
// 检查是否已登录
|
|
isLoggedIn() {
|
|
return !!this.globalData.token && !!this.globalData.userId;
|
|
},
|
|
});
|