first
This commit is contained in:
54
admin-web/src/utils/auth.ts
Normal file
54
admin-web/src/utils/auth.ts
Normal file
@@ -0,0 +1,54 @@
|
||||
const TOKEN_KEY = "anxinyan_admin_token";
|
||||
const ADMIN_INFO_KEY = "anxinyan_admin_info";
|
||||
|
||||
export interface AdminSessionInfo {
|
||||
id: number;
|
||||
name: string;
|
||||
mobile: string;
|
||||
email: string;
|
||||
status: string;
|
||||
role_names: string[];
|
||||
permission_codes: string[];
|
||||
}
|
||||
|
||||
export function getAdminToken() {
|
||||
return localStorage.getItem(TOKEN_KEY) || "";
|
||||
}
|
||||
|
||||
export function setAdminToken(token: string) {
|
||||
localStorage.setItem(TOKEN_KEY, token);
|
||||
}
|
||||
|
||||
export function clearAdminToken() {
|
||||
localStorage.removeItem(TOKEN_KEY);
|
||||
}
|
||||
|
||||
export function getAdminInfo(): AdminSessionInfo | null {
|
||||
const raw = localStorage.getItem(ADMIN_INFO_KEY);
|
||||
if (!raw) return null;
|
||||
try {
|
||||
return JSON.parse(raw) as AdminSessionInfo;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
export function setAdminInfo(info: AdminSessionInfo) {
|
||||
localStorage.setItem(ADMIN_INFO_KEY, JSON.stringify(info));
|
||||
}
|
||||
|
||||
export function clearAdminInfo() {
|
||||
localStorage.removeItem(ADMIN_INFO_KEY);
|
||||
}
|
||||
|
||||
export function clearAdminSession() {
|
||||
clearAdminToken();
|
||||
clearAdminInfo();
|
||||
}
|
||||
|
||||
export function hasPermission(code?: string) {
|
||||
if (!code) return true;
|
||||
const info = getAdminInfo();
|
||||
if (!info) return false;
|
||||
return info.permission_codes.includes(code);
|
||||
}
|
||||
24
admin-web/src/utils/env.ts
Normal file
24
admin-web/src/utils/env.ts
Normal file
@@ -0,0 +1,24 @@
|
||||
const LOCAL_API_BASE_URL = "http://127.0.0.1:8787";
|
||||
|
||||
function isLocalLikeHostname(hostname: string) {
|
||||
return (
|
||||
hostname === "localhost" ||
|
||||
hostname === "127.0.0.1" ||
|
||||
hostname === "0.0.0.0" ||
|
||||
/^10\./.test(hostname) ||
|
||||
/^192\.168\./.test(hostname) ||
|
||||
/^172\.(1[6-9]|2\d|3[0-1])\./.test(hostname)
|
||||
);
|
||||
}
|
||||
|
||||
export function resolveApiBaseUrl() {
|
||||
if (import.meta.env.DEV) {
|
||||
return LOCAL_API_BASE_URL;
|
||||
}
|
||||
|
||||
if (typeof window !== "undefined" && isLocalLikeHostname(window.location.hostname)) {
|
||||
return LOCAL_API_BASE_URL;
|
||||
}
|
||||
|
||||
return import.meta.env.VITE_API_BASE_URL || LOCAL_API_BASE_URL;
|
||||
}
|
||||
33
admin-web/src/utils/navigation.ts
Normal file
33
admin-web/src/utils/navigation.ts
Normal file
@@ -0,0 +1,33 @@
|
||||
import type { Router } from "vue-router";
|
||||
|
||||
let appRouter: Router | null = null;
|
||||
|
||||
export function setAppRouter(router: Router) {
|
||||
appRouter = router;
|
||||
}
|
||||
|
||||
export function goToAdminLogin() {
|
||||
if (appRouter) {
|
||||
if (appRouter.currentRoute.value.name !== "login") {
|
||||
appRouter.replace({ name: "login" });
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (window.location.hash !== "#/login") {
|
||||
window.history.replaceState({}, "", "/#/login");
|
||||
window.dispatchEvent(new PopStateEvent("popstate"));
|
||||
}
|
||||
}
|
||||
|
||||
export function goToAdminHome() {
|
||||
if (appRouter) {
|
||||
appRouter.replace({ name: "dashboard" });
|
||||
return;
|
||||
}
|
||||
|
||||
if (window.location.hash !== "#/dashboard") {
|
||||
window.history.replaceState({}, "", "/#/dashboard");
|
||||
window.dispatchEvent(new PopStateEvent("popstate"));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user