huangye-parking/miniapp/unpackage/dist/dev/mp-weixin/utils/auth.js
2026-03-03 23:31:28 +08:00

47 lines
1.3 KiB
JavaScript

"use strict";
const common_vendor = require("../common/vendor.js");
const TOKEN_KEY = "token";
const USER_INFO_KEY = "userInfo";
const ROLE_KEY = "userRole";
function setLoginInfo(token, userInfo) {
common_vendor.index.setStorageSync(TOKEN_KEY, token);
common_vendor.index.setStorageSync(USER_INFO_KEY, JSON.stringify(userInfo));
}
function getToken() {
return common_vendor.index.getStorageSync(TOKEN_KEY) || null;
}
function getUserInfo() {
const info = common_vendor.index.getStorageSync(USER_INFO_KEY);
if (!info)
return null;
try {
return JSON.parse(info);
} catch {
return null;
}
}
function isLoggedIn() {
return !!getToken();
}
function setRole(role) {
common_vendor.index.setStorageSync(ROLE_KEY, role);
}
function clearAuth() {
common_vendor.index.removeStorageSync(TOKEN_KEY);
common_vendor.index.removeStorageSync(USER_INFO_KEY);
common_vendor.index.removeStorageSync(ROLE_KEY);
}
function checkLogin() {
if (isLoggedIn())
return true;
common_vendor.index.navigateTo({ url: "/pages/login/index" });
return false;
}
exports.checkLogin = checkLogin;
exports.clearAuth = clearAuth;
exports.getUserInfo = getUserInfo;
exports.isLoggedIn = isLoggedIn;
exports.setLoginInfo = setLoginInfo;
exports.setRole = setRole;
//# sourceMappingURL=../../.sourcemap/mp-weixin/utils/auth.js.map