"use strict";
const common_vendor = require("../common/vendor.js");
const modules_api_AppServer = require("../modules/api/AppServer.js");
function isLoggedIn() {
  const token = common_vendor.index.getStorageSync("token");
  return !!token;
}
function getCurrentUser() {
  try {
    const userStr = common_vendor.index.getStorageSync("user");
    if (userStr) {
      return JSON.parse(userStr);
    }
  } catch (e) {
    common_vendor.index.__f__("error", "at utils/auth.js:28", "获取用户信息失败:", e);
  }
  return null;
}
function getUserInfo() {
  return getCurrentUser();
}
function saveUserInfo(user) {
  try {
    if (user) {
      const userStr = JSON.stringify(user);
      common_vendor.index.setStorageSync("user", userStr);
      const app = getApp();
      if (app && app.globalData) {
        app.globalData.user = user;
      }
    }
  } catch (error) {
    common_vendor.index.__f__("error", "at utils/auth.js:58", "保存用户信息失败:", error);
    throw error;
  }
}
function saveAuthData(token, refreshToken, user) {
  try {
    common_vendor.index.__f__("log", "at utils/auth.js:79", "保存认证信息:", { token: token ? "exists" : "missing", refreshToken: refreshToken ? "exists" : "missing", user: user ? "exists" : "missing" });
    if (token) {
      common_vendor.index.setStorageSync("token", token);
      common_vendor.index.__f__("log", "at utils/auth.js:84", "Token 已保存");
    }
    if (refreshToken) {
      common_vendor.index.setStorageSync("refreshToken", refreshToken);
      common_vendor.index.__f__("log", "at utils/auth.js:89", "RefreshToken 已保存");
    }
    if (user) {
      const userStr = JSON.stringify(user);
      common_vendor.index.setStorageSync("user", userStr);
      common_vendor.index.__f__("log", "at utils/auth.js:95", "用户信息已保存:", userStr);
    }
    const app = getApp();
    if (app && app.globalData) {
      app.globalData.token = token;
      app.globalData.user = user;
      app.globalData.loginTime = Date.now();
      common_vendor.index.__f__("log", "at utils/auth.js:104", "全局数据已更新");
    }
    common_vendor.index.__f__("log", "at utils/auth.js:107", "认证信息保存成功");
  } catch (error) {
    common_vendor.index.__f__("error", "at utils/auth.js:109", "保存认证信息失败:", error);
    throw error;
  }
}
function clearAuthData() {
  common_vendor.index.removeStorageSync("token");
  common_vendor.index.removeStorageSync("refreshToken");
  common_vendor.index.removeStorageSync("user");
  const app = getApp();
  if (app && app.globalData) {
    app.globalData.token = "";
    app.globalData.user = null;
  }
}
function requireAuth(showToast = true) {
  if (!isLoggedIn()) {
    if (showToast) {
      common_vendor.index.showToast({
        title: "请先登录",
        icon: "none",
        duration: 1500
      });
    }
    setTimeout(() => {
      common_vendor.index.navigateTo({
        url: "/pages/login/login-page"
      });
    }, showToast ? 1e3 : 0);
    return false;
  }
  return true;
}
async function logout() {
  try {
    const appServer = new modules_api_AppServer.AppServer();
    await appServer.Logout();
  } catch (error) {
    common_vendor.index.__f__("error", "at utils/auth.js:193", "登出接口调用失败:", error);
  } finally {
    clearAuthData();
    common_vendor.index.reLaunch({
      url: "/pages/index/index"
    });
  }
}
exports.getCurrentUser = getCurrentUser;
exports.getUserInfo = getUserInfo;
exports.isLoggedIn = isLoggedIn;
exports.logout = logout;
exports.requireAuth = requireAuth;
exports.saveAuthData = saveAuthData;
exports.saveUserInfo = saveUserInfo;
//# sourceMappingURL=../../.sourcemap/mp-weixin/utils/auth.js.map
