346 lines
9.6 KiB
JavaScript
346 lines
9.6 KiB
JavaScript
import gulpError from './utils/gulpError';
|
||
|
||
App({
|
||
globalData: {
|
||
userInfo: null,
|
||
authInfo: null
|
||
},
|
||
|
||
onLaunch(options) {
|
||
console.log('小程序启动', options);
|
||
|
||
// 检查登录状态
|
||
this.checkLoginStatus();
|
||
|
||
// 检查更新
|
||
this.checkUpdate();
|
||
},
|
||
|
||
onShow() {
|
||
if (gulpError !== 'gulpErrorPlaceHolder') {
|
||
wx.redirectTo({
|
||
url: `/pages/gulp-error/index?gulpError=${gulpError}`,
|
||
});
|
||
return;
|
||
}
|
||
|
||
// 刷新登录状态
|
||
this.refreshLoginStatus();
|
||
},
|
||
|
||
// ==================== 认证相关方法 ====================
|
||
|
||
/**
|
||
* 检查是否已登录
|
||
*/
|
||
isLoggedIn() {
|
||
const userInfo = wx.getStorageSync('userInfo');
|
||
const authInfo = wx.getStorageSync('authInfo');
|
||
return !!(userInfo && authInfo);
|
||
},
|
||
|
||
/**
|
||
* 获取用户信息
|
||
*/
|
||
getUserInfo() {
|
||
return wx.getStorageSync('userInfo') || this.globalData.userInfo;
|
||
},
|
||
|
||
/**
|
||
* 保存用户信息
|
||
*/
|
||
saveUserInfo(userInfo) {
|
||
wx.setStorageSync('userInfo', userInfo);
|
||
this.globalData.userInfo = userInfo;
|
||
},
|
||
|
||
/**
|
||
* 获取认证信息
|
||
*/
|
||
getAuthInfo() {
|
||
return wx.getStorageSync('authInfo') || this.globalData.authInfo;
|
||
},
|
||
|
||
/**
|
||
* 保存认证信息
|
||
*/
|
||
saveAuthInfo(authInfo) {
|
||
wx.setStorageSync('authInfo', authInfo);
|
||
this.globalData.authInfo = authInfo;
|
||
},
|
||
|
||
/**
|
||
* 用户登录
|
||
*/
|
||
login() {
|
||
return new Promise((resolve, reject) => {
|
||
// 1. 先获取用户信息(必须在同步上下文中)
|
||
wx.getUserProfile({
|
||
desc: '用于完善用户资料',
|
||
success: userProfileRes => {
|
||
const userInfo = userProfileRes.userInfo;
|
||
this.saveUserInfo(userInfo);
|
||
console.log('获取用户信息成功', userInfo);
|
||
|
||
// 2. 再执行 wx.login
|
||
wx.login({
|
||
success: loginRes => {
|
||
console.log('登录成功,code:', loginRes.code);
|
||
|
||
// TODO: 将 code 发送到后端,换取 openId 和 session_key
|
||
// 这里使用模拟数据
|
||
const authInfo = {
|
||
token: 'mock_token_' + Date.now(),
|
||
openId: 'mock_openid_' + Date.now(),
|
||
sessionKey: 'mock_session_key',
|
||
loginTime: Date.now()
|
||
};
|
||
|
||
this.saveAuthInfo(authInfo);
|
||
console.log('保存认证信息成功');
|
||
|
||
resolve({
|
||
success: true,
|
||
userInfo: userInfo,
|
||
authInfo: authInfo
|
||
});
|
||
},
|
||
fail: loginErr => {
|
||
console.error('wx.login 失败', loginErr);
|
||
reject(loginErr);
|
||
}
|
||
});
|
||
},
|
||
fail: userProfileErr => {
|
||
console.error('获取用户信息失败', userProfileErr);
|
||
reject(userProfileErr);
|
||
}
|
||
});
|
||
});
|
||
},
|
||
|
||
/**
|
||
* 静默登录(不需要用户授权)
|
||
*/
|
||
silentLogin() {
|
||
return new Promise((resolve, reject) => {
|
||
wx.login({
|
||
success: res => {
|
||
console.log('静默登录成功,code:', res.code);
|
||
|
||
// TODO: 将 code 发送到后端
|
||
const authInfo = {
|
||
token: 'mock_token_' + Date.now(),
|
||
openId: 'mock_openid_' + Date.now(),
|
||
sessionKey: 'mock_session_key',
|
||
loginTime: Date.now()
|
||
};
|
||
|
||
this.saveAuthInfo(authInfo);
|
||
resolve({ success: true, authInfo });
|
||
},
|
||
fail: reject
|
||
});
|
||
});
|
||
},
|
||
|
||
/**
|
||
* 检查登录状态
|
||
*/
|
||
checkLoginStatus() {
|
||
if (this.isLoggedIn()) {
|
||
this.globalData.userInfo = this.getUserInfo();
|
||
this.globalData.authInfo = this.getAuthInfo();
|
||
console.log('用户已登录', this.globalData.userInfo);
|
||
} else {
|
||
console.log('用户未登录');
|
||
}
|
||
},
|
||
|
||
/**
|
||
* 刷新登录状态
|
||
*/
|
||
async refreshLoginStatus() {
|
||
try {
|
||
// 检查会话是否有效
|
||
const sessionValid = await this.checkSession();
|
||
if (!sessionValid) {
|
||
// 会话过期,静默登录
|
||
await this.silentLogin();
|
||
}
|
||
this.checkLoginStatus();
|
||
} catch (error) {
|
||
console.error('刷新登录状态失败', error);
|
||
}
|
||
},
|
||
|
||
/**
|
||
* 检查会话是否有效
|
||
*/
|
||
checkSession() {
|
||
return new Promise(resolve => {
|
||
wx.checkSession({
|
||
success: () => {
|
||
console.log('会话未过期');
|
||
resolve(true);
|
||
},
|
||
fail: () => {
|
||
console.log('会话已过期');
|
||
resolve(false);
|
||
}
|
||
});
|
||
});
|
||
},
|
||
|
||
/**
|
||
* 退出登录
|
||
*/
|
||
logout() {
|
||
wx.removeStorageSync('userInfo');
|
||
wx.removeStorageSync('authInfo');
|
||
this.globalData.userInfo = null;
|
||
this.globalData.authInfo = null;
|
||
|
||
wx.reLaunch({
|
||
url: '/pages/login/login'
|
||
});
|
||
},
|
||
|
||
// ==================== 消息推送相关方法 ====================
|
||
|
||
/**
|
||
* 订阅消息模板ID
|
||
*/
|
||
TEMPLATE_IDS: {
|
||
INSPECTION_REMINDER: 'YOUR_TEMPLATE_ID_1',
|
||
WARNING_NOTICE: 'YOUR_TEMPLATE_ID_2',
|
||
INSPECTION_COMPLETE: 'YOUR_TEMPLATE_ID_3'
|
||
},
|
||
|
||
/**
|
||
* 请求订阅消息
|
||
*/
|
||
requestSubscribeMessage(tmplIds) {
|
||
return new Promise((resolve, reject) => {
|
||
wx.requestSubscribeMessage({
|
||
tmplIds: tmplIds,
|
||
success: res => {
|
||
console.log('订阅消息成功', res);
|
||
resolve(res);
|
||
},
|
||
fail: err => {
|
||
console.error('订阅消息失败', err);
|
||
reject(err);
|
||
}
|
||
});
|
||
});
|
||
},
|
||
|
||
/**
|
||
* 订阅所有消息
|
||
*/
|
||
async subscribeAllMessages() {
|
||
try {
|
||
const result = await this.requestSubscribeMessage([
|
||
this.TEMPLATE_IDS.INSPECTION_REMINDER,
|
||
this.TEMPLATE_IDS.WARNING_NOTICE,
|
||
this.TEMPLATE_IDS.INSPECTION_COMPLETE
|
||
]);
|
||
|
||
// 保存订阅状态
|
||
const subscriptionStatus = {
|
||
inspectionReminder: result[this.TEMPLATE_IDS.INSPECTION_REMINDER] === 'accept',
|
||
warningNotice: result[this.TEMPLATE_IDS.WARNING_NOTICE] === 'accept',
|
||
inspectionComplete: result[this.TEMPLATE_IDS.INSPECTION_COMPLETE] === 'accept',
|
||
updateTime: Date.now()
|
||
};
|
||
|
||
wx.setStorageSync('subscription_status', subscriptionStatus);
|
||
return { success: true, subscriptionStatus };
|
||
} catch (error) {
|
||
console.error('订阅失败', error);
|
||
return { success: false, error };
|
||
}
|
||
},
|
||
|
||
/**
|
||
* 获取订阅状态
|
||
*/
|
||
getSubscriptionStatus() {
|
||
return wx.getStorageSync('subscription_status') || {
|
||
inspectionReminder: false,
|
||
warningNotice: false,
|
||
inspectionComplete: false
|
||
};
|
||
},
|
||
|
||
/**
|
||
* 检查是否已订阅
|
||
*/
|
||
isSubscribed() {
|
||
const status = this.getSubscriptionStatus();
|
||
return status.inspectionReminder || status.warningNotice || status.inspectionComplete;
|
||
},
|
||
|
||
/**
|
||
* 获取消息推送设置
|
||
*/
|
||
getNotificationSettings() {
|
||
return wx.getStorageSync('notification_settings') || {
|
||
enabled: true,
|
||
inspectionReminder: true,
|
||
warningNotice: true,
|
||
advanceDays: 7
|
||
};
|
||
},
|
||
|
||
/**
|
||
* 保存消息推送设置
|
||
*/
|
||
saveNotificationSettings(settings) {
|
||
wx.setStorageSync('notification_settings', settings);
|
||
},
|
||
|
||
// ==================== 其他方法 ====================
|
||
|
||
/**
|
||
* 检查小程序更新
|
||
*/
|
||
checkUpdate() {
|
||
if (wx.canIUse('getUpdateManager')) {
|
||
const updateManager = wx.getUpdateManager();
|
||
|
||
updateManager.onCheckForUpdate(res => {
|
||
console.log('检查更新', res.hasUpdate);
|
||
});
|
||
|
||
updateManager.onUpdateReady(() => {
|
||
wx.showModal({
|
||
title: '更新提示',
|
||
content: '新版本已经准备好,是否重启应用?',
|
||
success: res => {
|
||
if (res.confirm) {
|
||
updateManager.applyUpdate();
|
||
}
|
||
}
|
||
});
|
||
});
|
||
|
||
updateManager.onUpdateFailed(() => {
|
||
wx.showModal({
|
||
title: '更新失败',
|
||
content: '新版本下载失败,请检查网络后重试',
|
||
showCancel: false
|
||
});
|
||
});
|
||
}
|
||
},
|
||
|
||
/**
|
||
* 设置用户信息
|
||
*/
|
||
setUserInfo(userInfo) {
|
||
this.saveUserInfo(userInfo);
|
||
}
|
||
});
|