Files
WXProgram/dist/app.js

173 lines
7.5 KiB
JavaScript
Raw Normal View History

2025-10-26 13:15:04 +08:00
"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 locationTrackingService_1 = __importDefault(require("./services/locationTrackingService"));
const helpers_1 = require("./utils/helpers");
// 定义应用类
class MyApp {
constructor() {
this.globalData = {
userInfo: null,
isLoggedIn: false,
openid: undefined,
session_key: undefined,
token: undefined,
};
// 绑定方法确保this指向正确
this.onLaunch = this.onLaunch.bind(this);
this.doGlobalLogin = this.doGlobalLogin.bind(this);
this.doGlobalLogout = this.doGlobalLogout.bind(this);
}
// 应用初始化
async onLaunch() {
console.log("=== 应用启动 ===");
console.log("调试信息 - 开始执行onLaunch");
try {
// 初始化位置追踪服务
console.log("调试信息 - 初始化位置追踪服务");
await locationTrackingService_1.default.initialize();
console.log("调试信息 - 位置追踪服务初始化完成");
// 初始化日志存储
const logs = wx.getStorageSync("logs") || [];
logs.unshift(Date.now());
wx.setStorageSync("logs", logs);
console.log("初始化日志存储完成");
// 检查用户是否已签退(避免自动重新登录)
const userStatus = wx.getStorageSync("userStatus");
console.log("调试信息 - 检查本地存储的userStatus:", userStatus);
if (userStatus === 'signed_out') {
console.log("调试信息 - 检测到用户已签退,跳过自动登录");
this.globalData.isLoggedIn = false;
// 直接跳转到首页,不进行登录
console.log("调试信息 - 直接跳转到首页,不进行登录");
wx.switchTab({
url: "/pages/index/index",
});
return;
}
else {
console.log("调试信息 - 用户状态不是signed_out继续登录流程");
}
// 检查本地存储的登录信息
const token = wx.getStorageSync("token");
const userInfo = wx.getStorageSync("userInfo");
const openid = wx.getStorageSync("openid");
const session_key = wx.getStorageSync("session_key");
console.log("调试信息 - 本地存储检查结果:");
console.log(" - token:", token ? "存在" : "不存在");
console.log(" - openid:", openid ? "存在" : "不存在");
console.log(" - userInfo:", userInfo ? "存在" : "不存在");
if (token && openid) {
console.log("调试信息 - 检测到本地登录信息,恢复登录状态");
this.globalData.isLoggedIn = true;
this.globalData.userInfo = userInfo;
this.globalData.openid = openid;
this.globalData.session_key = session_key;
this.globalData.token = token;
}
else {
console.log("调试信息 - 本地登录信息不完整,需要重新登录");
}
// 静默登录,无论是否已有登录信息都重新验证
console.log("调试信息 - 开始静默登录流程");
const loginSuc = await this.doGlobalLogin();
// 静默登录后跳转到首页
if (loginSuc) {
console.log("调试信息 - 登录成功,跳转到首页");
wx.switchTab({
url: "/pages/index/index",
});
}
else {
console.log("调试信息 - 登录失败");
//TODO:退出小程序或者是重试逻辑
(0, helpers_1.showToast)("登录失败,请重试");
}
}
catch (error) {
console.error("应用启动失败:", error);
this.globalData.isLoggedIn = false;
(0, helpers_1.showToast)("应用启动失败,请重试");
}
}
// 全局静默登录方法 - 仅保留核心登录逻辑
async doGlobalLogin() {
console.log("=== 开始全局登录流程 ===");
try {
// 注意不再清除签退状态让onLaunch中的签退状态检查逻辑正常工作
// 只有在登录成功后才清除签退状态
// 调用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;
this.globalData.token = loginResult.token || undefined;
// 登录成功后清除签退状态
wx.removeStorageSync("userStatus");
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");
wx.removeStorageSync("userStatus");
(0, helpers_1.showToast)("登录失败,请重试");
return false;
}
}
// 全局退出登录方法
doGlobalLogout() {
this.globalData.isLoggedIn = false;
this.globalData.userInfo = null;
this.globalData.openid = undefined;
this.globalData.session_key = undefined;
this.globalData.token = undefined;
// 清空存储的登录信息,但保留签退状态
wx.removeStorageSync("userInfo");
wx.removeStorageSync("token");
wx.removeStorageSync("openid");
wx.removeStorageSync("session_key");
// 注意不删除userStatus以便签退状态能够保持
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;