Files
WXProgram/miniprogram/app.js
2025-10-16 21:32:16 +08:00

132 lines
5.1 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
// app.ts
const userService_1 = __importDefault(require("./services/userService"));
const helpers_1 = require("./utils/helpers");
// 定义应用类
class MyApp {
constructor() {
this.globalData = {
userInfo: null,
isLoggedIn: false,
openid: undefined,
session_key: undefined,
};
// 绑定方法确保this指向正确
this.onLaunch = this.onLaunch.bind(this);
this.doGlobalLogin = this.doGlobalLogin.bind(this);
this.doGlobalLogout = this.doGlobalLogout.bind(this);
}
// 应用初始化
async onLaunch() {
console.log("=== 应用启动 ===");
try {
// 初始化日志存储
const logs = wx.getStorageSync("logs") || [];
logs.unshift(Date.now());
wx.setStorageSync("logs", logs);
console.log("初始化日志存储完成");
// 检查本地存储的登录信息
const token = wx.getStorageSync("token");
const userInfo = wx.getStorageSync("userInfo");
const openid = wx.getStorageSync("openid");
const session_key = wx.getStorageSync("session_key");
if (token && openid) {
console.log("检测到本地登录信息,恢复登录状态");
this.globalData.isLoggedIn = true;
this.globalData.userInfo = userInfo;
this.globalData.openid = openid;
this.globalData.session_key = session_key;
}
// 静默登录,无论是否已有登录信息都重新验证
console.log("开始静默登录流程");
const loginSuc = await this.doGlobalLogin();
// 静默登录后跳转到首页
if (loginSuc) {
wx.switchTab({
url: "/pages/index/index",
});
}
else {
//TODO:退出小程序或者是重试逻辑
(0, helpers_1.showToast)("登录失败,请重试");
}
}
catch (error) {
console.error("应用启动失败:", error);
this.globalData.isLoggedIn = false;
(0, helpers_1.showToast)("应用启动失败,请重试");
}
}
// 全局静默登录方法 - 仅保留核心登录逻辑
async doGlobalLogin() {
console.log("=== 开始全局登录流程 ===");
try {
// 调用userService进行登录分离请求逻辑
const loginResult = await userService_1.default.wxLogin();
if (loginResult.success) {
// 登录成功,更新全局状态
this.globalData.isLoggedIn = true;
this.globalData.userInfo = loginResult.userInfo || null;
this.globalData.openid = loginResult.openid || undefined;
this.globalData.session_key = loginResult.session_key || undefined;
console.log("=== 全局登录流程完成,登录成功 ===");
return true;
}
else {
console.error("=== 全局登录流程失败 ===");
this.globalData.isLoggedIn = false;
// 如果返回了openid保存openid
if (loginResult.openid) {
this.globalData.openid = loginResult.openid;
}
(0, helpers_1.showToast)("登录失败,请重试");
return false;
}
}
catch (error) {
console.error("=== 全局登录流程异常 ===", error);
this.globalData.isLoggedIn = false;
// 清空存储的登录信息
wx.removeStorageSync("userInfo");
wx.removeStorageSync("token");
wx.removeStorageSync("openid");
wx.removeStorageSync("session_key");
(0, helpers_1.showToast)("登录失败,请重试");
return false;
}
}
// 全局退出登录方法
doGlobalLogout() {
this.globalData.isLoggedIn = false;
this.globalData.userInfo = null;
this.globalData.openid = undefined;
this.globalData.session_key = undefined;
// 清空存储的登录信息
wx.removeStorageSync("userInfo");
wx.removeStorageSync("token");
wx.removeStorageSync("openid");
wx.removeStorageSync("session_key");
console.log("用户已退出登录");
(0, helpers_1.showToast)("已退出登录");
// 退出登录后跳转到首页
wx.switchTab({
url: "/pages/index/index",
});
}
}
// 创建应用实例并注册
const app = new MyApp();
// 注册小程序
App({
globalData: app.globalData,
onLaunch: app.onLaunch,
doGlobalLogin: app.doGlobalLogin,
doGlobalLogout: app.doGlobalLogout,
});
// 导出应用实例,以便在其他地方使用
exports.default = app;