feat: Web 版上线 footpr.linyux.cn —— 登录页 + 按用户隔离的命令 API + 静态托管
This commit is contained in:
+47
-5
@@ -6,16 +6,58 @@ import type {
|
||||
|
||||
const isTauri = typeof window !== 'undefined' && '__TAURI_INTERNALS__' in window;
|
||||
|
||||
const TOKEN_KEY = 'fp-token';
|
||||
export function getToken() { return localStorage.getItem(TOKEN_KEY); }
|
||||
export function setToken(t: string) { localStorage.setItem(TOKEN_KEY, t); }
|
||||
export function clearToken() { localStorage.removeItem(TOKEN_KEY); }
|
||||
export const isLocalMode = () => isTauri || !getToken();
|
||||
|
||||
export interface ServerSession { token: string; user_id: string; device_id: string; device_name: string; }
|
||||
|
||||
export async function serverLogin(username: string, password: string): Promise<ServerSession> {
|
||||
const res = await fetch('/auth/login', {
|
||||
method: 'POST', headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ username, password, device_name: 'Web 浏览器' })
|
||||
});
|
||||
const json = await res.json();
|
||||
if (!json.ok) throw new Error(json.error || '登录失败');
|
||||
setToken(json.data.token);
|
||||
return json.data;
|
||||
}
|
||||
|
||||
export async function serverRegister(username: string, password: string, invite_code: string): Promise<ServerSession> {
|
||||
const res = await fetch('/auth/register', {
|
||||
method: 'POST', headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ username, password, invite_code, device_name: 'Web 浏览器' })
|
||||
});
|
||||
const json = await res.json();
|
||||
if (!json.ok) throw new Error(json.error || '注册失败');
|
||||
setToken(json.data.token);
|
||||
return json.data;
|
||||
}
|
||||
|
||||
export interface MeInfo { user_id: string; username: string; device_id: string; }
|
||||
export async function serverMe(): Promise<MeInfo> {
|
||||
const res = await fetch('/auth/me', { headers: { Authorization: 'Bearer ' + getToken() } });
|
||||
const json = await res.json();
|
||||
if (!json.ok) throw new Error(json.error || '会话失效');
|
||||
return json.data;
|
||||
}
|
||||
|
||||
async function call<T>(cmd: string, payload?: unknown): Promise<T> {
|
||||
if (isTauri) {
|
||||
const { invoke } = await import('@tauri-apps/api/core');
|
||||
return invoke<T>(cmd, { payload: payload ?? {} });
|
||||
}
|
||||
const res = await fetch('/api/' + cmd, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify(payload ?? {})
|
||||
});
|
||||
const headers: Record<string, string> = { 'Content-Type': 'application/json' };
|
||||
const token = getToken();
|
||||
if (token) headers.Authorization = 'Bearer ' + token;
|
||||
const res = await fetch('/api/' + cmd, { method: 'POST', headers, body: JSON.stringify(payload ?? {}) });
|
||||
if (res.status === 401) {
|
||||
clearToken();
|
||||
window.dispatchEvent(new CustomEvent('fp-auth-required'));
|
||||
throw new Error('登录已过期,请重新登录');
|
||||
}
|
||||
const json = await res.json();
|
||||
if (!res.ok || !json.ok) throw new Error(json.error || ('HTTP ' + res.status));
|
||||
return json.data as T;
|
||||
|
||||
Reference in New Issue
Block a user