feat: 云端优先架构 — 桌面/Web/PWA 全走服务器 API

This commit is contained in:
root
2026-10-16 17:34:26 +08:00
parent 629c7e4073
commit aea94de9e5
22 changed files with 472 additions and 445 deletions
+43 -27
View File
@@ -4,21 +4,21 @@ import type {
StatsOverview, SyncStatus, TaxonomyItem, TimelineGroup
} from './types';
const isTauri = typeof window !== 'undefined' && '__TAURI_INTERNALS__' in window;
export function isTauriRef() { return isTauri; }
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, baseUrl = ''): Promise<ServerSession> {
const res = await fetch(baseUrl + '/auth/login', {
export async function serverLogin(username: string, password: string, baseUrl?: string): Promise<ServerSession> {
const base = baseUrl || getServerUrl();
const res = await fetch(base + '/auth/login', {
method: 'POST', headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ username, password, device_name: isTauri ? '桌面应用' : 'Web 浏览器' })
body: JSON.stringify({ username, password, device_name: '桌面应用' })
});
const json = await res.json();
if (!json.ok) throw new Error(json.error || '登录失败');
@@ -26,10 +26,11 @@ export async function serverLogin(username: string, password: string, baseUrl =
return json.data;
}
export async function serverRegister(username: string, password: string, invite_code: string, baseUrl = ''): Promise<ServerSession> {
const res = await fetch(baseUrl + '/auth/register', {
export async function serverRegister(username: string, password: string, invite_code: string, baseUrl?: string): Promise<ServerSession> {
const base = baseUrl || getServerUrl();
const res = await fetch(base + '/auth/register', {
method: 'POST', headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ username, password, invite_code, device_name: isTauri ? '桌面应用' : 'Web 浏览器' })
body: JSON.stringify({ username, password, invite_code, device_name: '桌面应用' })
});
const json = await res.json();
if (!json.ok) throw new Error(json.error || '注册失败');
@@ -38,32 +39,47 @@ export async function serverRegister(username: string, password: string, invite_
}
export interface MeInfo { user_id: string; username: string; is_admin?: boolean; device_id: string; }
export async function serverMe(baseUrl = ''): Promise<MeInfo> {
const res = await fetch(baseUrl + '/auth/me', { headers: { Authorization: 'Bearer ' + getToken() } });
export async function serverMe(baseUrl?: string): Promise<MeInfo> {
const base = baseUrl || getServerUrl();
const res = await fetch(base + '/auth/me', { headers: { Authorization: 'Bearer ' + getToken() } });
const json = await res.json();
if (!json.ok) throw new Error(json.error || '会话失效');
return json.data;
}
// ---------- 云端传输层:所有数据存服务器 ----------
const SERVER_KEY = 'fp-server-url';
export function getServerUrl() { return localStorage.getItem(SERVER_KEY) || 'https://footpr.linyux.cn'; }
export function setServerUrl(url: string) { localStorage.setItem(SERVER_KEY, url.replace(/\/+$/, '')); }
export class AuthError extends Error {
constructor(msg: string) { super(msg); this.name = 'AuthError'; }
}
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 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 (!token) throw new AuthError('请先登录');
const base = getServerUrl();
const res = await fetch(base + '/api/' + cmd, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: 'Bearer ' + token
},
body: JSON.stringify(payload ?? {})
});
if (res.status === 401) {
clearToken();
window.dispatchEvent(new CustomEvent('fp-auth-required'));
throw new Error('登录已过期,请重新登录');
throw new AuthError('登录已过期,请重新登录');
}
const json = await res.json();
if (!res.ok || !json.ok) throw new Error(json.error || ('HTTP ' + res.status));
return json.data as T;
}
// ---- app ----
export const appInfo = () => call<AppInfo>('app_info');
export const syncStatus = () => call<SyncStatus>('sync_status');
@@ -128,25 +144,25 @@ export const viewUpsert = (p: { id?: number; name: string; filter: Record<string
export const viewDelete = (id: number) => call<{ ok: true }>('view_delete', { id });
// ---- 管理端(is_admin) ----
export async function adminListInvites(baseUrl = '') {
const res = await fetch(baseUrl + '/admin/invites', { headers: { Authorization: 'Bearer ' + getToken() } });
export async function adminListInvites(baseUrl?: string) {
const res = await fetch((baseUrl || getServerUrl()) + '/admin/invites', { headers: { Authorization: 'Bearer ' + getToken() } });
return res.json();
}
export async function adminCreateInvites(count: number, baseUrl = '') {
const res = await fetch(baseUrl + '/admin/invites/generate', {
export async function adminCreateInvites(count: number, baseUrl?: string) {
const res = await fetch((baseUrl || getServerUrl()) + '/admin/invites/generate', {
method: 'POST', headers: { 'Content-Type': 'application/json', Authorization: 'Bearer ' + getToken() },
body: JSON.stringify({ count })
});
return res.json();
}
export async function adminRevokeInvite(id: number, baseUrl = '') {
const res = await fetch(baseUrl + '/admin/invites/revoke', {
export async function adminRevokeInvite(id: number, baseUrl?: string) {
const res = await fetch((baseUrl || getServerUrl()) + '/admin/invites/revoke', {
method: 'POST', headers: { 'Content-Type': 'application/json', Authorization: 'Bearer ' + getToken() },
body: JSON.stringify({ id })
});
return res.json();
}
export async function adminListUsers(baseUrl = '') {
const res = await fetch(baseUrl + '/admin/users', { headers: { Authorization: 'Bearer ' + getToken() } });
export async function adminListUsers(baseUrl?: string) {
const res = await fetch((baseUrl || getServerUrl()) + '/admin/users', { headers: { Authorization: 'Bearer ' + getToken() } });
return res.json();
}