114 lines
3.1 KiB
JavaScript
114 lines
3.1 KiB
JavaScript
"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) {
|
|
console.error("获取用户信息失败:", 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) {
|
|
console.error("保存用户信息失败:", error);
|
|
throw error;
|
|
}
|
|
}
|
|
function saveAuthData(token, refreshToken, user) {
|
|
try {
|
|
console.log("保存认证信息:", { token: token ? "exists" : "missing", refreshToken: refreshToken ? "exists" : "missing", user: user ? "exists" : "missing" });
|
|
if (token) {
|
|
common_vendor.index.setStorageSync("token", token);
|
|
console.log("Token 已保存");
|
|
}
|
|
if (refreshToken) {
|
|
common_vendor.index.setStorageSync("refreshToken", refreshToken);
|
|
console.log("RefreshToken 已保存");
|
|
}
|
|
if (user) {
|
|
const userStr = JSON.stringify(user);
|
|
common_vendor.index.setStorageSync("user", userStr);
|
|
console.log("用户信息已保存:", userStr);
|
|
}
|
|
const app = getApp();
|
|
if (app && app.globalData) {
|
|
app.globalData.token = token;
|
|
app.globalData.user = user;
|
|
app.globalData.loginTime = Date.now();
|
|
console.log("全局数据已更新");
|
|
}
|
|
console.log("认证信息保存成功");
|
|
} catch (error) {
|
|
console.error("保存认证信息失败:", 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) {
|
|
console.error("登出接口调用失败:", 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;
|