feat: invite code full CRUD + demo account seeded with sample data + SW v3 (fix /auth/me cache poisoning)
This commit is contained in:
+35
-18
@@ -144,25 +144,42 @@ 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?: string) {
|
||||
const res = await fetch((baseUrl || getServerUrl()) + '/admin/invites', { headers: { Authorization: 'Bearer ' + getToken() } });
|
||||
return res.json();
|
||||
}
|
||||
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 })
|
||||
async function adminCall<T>(path: string, body?: unknown, baseUrl?: string): Promise<T> {
|
||||
const res = await fetch((baseUrl || getServerUrl()) + path, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json', Authorization: 'Bearer ' + getToken() },
|
||||
body: JSON.stringify(body ?? {}),
|
||||
});
|
||||
return res.json();
|
||||
const json = await res.json().catch(() => ({ ok: false, error: 'HTTP ' + res.status }));
|
||||
if (!json.ok) throw new Error(json.error || '请求失败');
|
||||
return json.data as T;
|
||||
}
|
||||
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();
|
||||
async function adminGet<T>(path: string, baseUrl?: string): Promise<T> {
|
||||
const res = await fetch((baseUrl || getServerUrl()) + path, { headers: { Authorization: 'Bearer ' + getToken() } });
|
||||
const json = await res.json().catch(() => ({ ok: false, error: 'HTTP ' + res.status }));
|
||||
if (!json.ok) throw new Error(json.error || '请求失败');
|
||||
return json.data as T;
|
||||
}
|
||||
export async function adminListUsers(baseUrl?: string) {
|
||||
const res = await fetch((baseUrl || getServerUrl()) + '/admin/users', { headers: { Authorization: 'Bearer ' + getToken() } });
|
||||
return res.json();
|
||||
|
||||
export interface InviteRow {
|
||||
id: number; code: string; remark: string | null; created_at: string;
|
||||
used_by: string | null; used_at: string | null; valid: boolean;
|
||||
used_by_name?: string | null; created_by_name?: string | null;
|
||||
}
|
||||
export interface AdminUserRow {
|
||||
id: string; username: string; is_admin: boolean; created_at: string;
|
||||
devices: number; records: number; attachments: number; attachment_bytes: number;
|
||||
}
|
||||
export interface InviteList { total: number; invites: InviteRow[]; }
|
||||
export interface UserList { total: number; users: AdminUserRow[]; }
|
||||
|
||||
export const adminListInvites = (baseUrl?: string) => adminGet<InviteList>('/admin/invites', baseUrl);
|
||||
export const adminListUsers = (baseUrl?: string) => adminGet<UserList>('/admin/users', baseUrl);
|
||||
export const adminGenerateInvites = (count: number, baseUrl?: string) =>
|
||||
adminCall<{ codes: string[] }>('/admin/invites/generate', { count }, baseUrl);
|
||||
export const adminCreateInvite = (code: string, remark: string, baseUrl?: string) =>
|
||||
adminCall<{ code: string }>('/admin/invites/create', { code, remark }, baseUrl);
|
||||
export const adminUpdateInvite = (id: number, patch: { remark?: string; valid?: boolean }, baseUrl?: string) =>
|
||||
adminCall<{ id: number }>('/admin/invites/update', { id, ...patch }, baseUrl);
|
||||
export const adminDeleteInvite = (id: number, baseUrl?: string) =>
|
||||
adminCall<{ deleted: number }>('/admin/invites/delete', { id }, baseUrl);
|
||||
|
||||
Reference in New Issue
Block a user