2026-04-19 14:18:20 +08:00
|
|
|
import { computed, readonly, shallowRef } from "vue";
|
|
|
|
|
|
|
|
|
|
import { createApiClient } from "@geo/http-client";
|
|
|
|
|
import type {
|
|
|
|
|
AuthTokens,
|
|
|
|
|
DesktopClientInfo,
|
|
|
|
|
DesktopClientRegisterRequest,
|
|
|
|
|
DesktopClientRegisterResponse,
|
|
|
|
|
DesktopRuntimeSessionSyncRequest,
|
|
|
|
|
LoginRequest,
|
|
|
|
|
LoginResponse,
|
|
|
|
|
RefreshResponse,
|
|
|
|
|
UserInfo,
|
|
|
|
|
} from "@geo/shared-types";
|
|
|
|
|
|
|
|
|
|
type SessionMode = "authenticated" | "preview";
|
|
|
|
|
|
|
|
|
|
interface DesktopSession {
|
|
|
|
|
mode: SessionMode;
|
|
|
|
|
accessToken: string | null;
|
|
|
|
|
refreshToken: string | null;
|
|
|
|
|
user: UserInfo | null;
|
|
|
|
|
clientToken: string | null;
|
|
|
|
|
clientTokenExpiresAt: number | null;
|
|
|
|
|
desktopClient: DesktopClientInfo | null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const sessionStorageKey = "geo.desktop.session.v1";
|
|
|
|
|
const apiBaseURLStorageKey = "geo.desktop.api-base-url";
|
2026-04-20 09:52:48 +08:00
|
|
|
const clientRegistrationStoragePrefix = "geo.desktop.client-registration.v1";
|
2026-04-19 14:18:20 +08:00
|
|
|
|
|
|
|
|
const session = shallowRef<DesktopSession | null>(readStoredSession());
|
|
|
|
|
const apiBaseURL = shallowRef(readStoredApiBaseURL());
|
|
|
|
|
const pending = shallowRef(false);
|
|
|
|
|
const error = shallowRef<string | null>(null);
|
|
|
|
|
|
|
|
|
|
function readStoredSession(): DesktopSession | null {
|
|
|
|
|
if (typeof window === "undefined") {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const raw = window.localStorage.getItem(sessionStorageKey);
|
|
|
|
|
if (!raw) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
return JSON.parse(raw) as DesktopSession;
|
|
|
|
|
} catch {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function persistSession(next: DesktopSession | null): void {
|
|
|
|
|
session.value = next;
|
|
|
|
|
|
|
|
|
|
if (typeof window === "undefined") {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!next) {
|
|
|
|
|
window.localStorage.removeItem(sessionStorageKey);
|
|
|
|
|
} else {
|
|
|
|
|
window.localStorage.setItem(sessionStorageKey, JSON.stringify(next));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void syncRuntimeSessionToMain(next);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function readStoredApiBaseURL(): string {
|
|
|
|
|
if (typeof window === "undefined") {
|
|
|
|
|
return "http://localhost:8080";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return window.localStorage.getItem(apiBaseURLStorageKey) ?? "http://localhost:8080";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function persistApiBaseURL(value: string): void {
|
|
|
|
|
const trimmed = value.trim() || "http://localhost:8080";
|
|
|
|
|
apiBaseURL.value = trimmed;
|
|
|
|
|
|
|
|
|
|
if (typeof window !== "undefined") {
|
|
|
|
|
window.localStorage.setItem(apiBaseURLStorageKey, trimmed);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void syncRuntimeSessionToMain();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function createPublicClient() {
|
|
|
|
|
return createApiClient({
|
|
|
|
|
baseURL: apiBaseURL.value,
|
|
|
|
|
timeoutMs: 15000,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function createAuthenticatedClient() {
|
|
|
|
|
return createApiClient({
|
|
|
|
|
baseURL: apiBaseURL.value,
|
|
|
|
|
timeoutMs: 15000,
|
|
|
|
|
auth: {
|
|
|
|
|
getAccessToken: () => session.value?.accessToken ?? null,
|
|
|
|
|
getRefreshToken: () => session.value?.refreshToken ?? null,
|
|
|
|
|
setTokens: (tokens: AuthTokens) => {
|
|
|
|
|
if (!session.value) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
persistSession({
|
|
|
|
|
...session.value,
|
|
|
|
|
accessToken: tokens.accessToken,
|
|
|
|
|
refreshToken: tokens.refreshToken,
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
clearTokens: () => persistSession(null),
|
|
|
|
|
async refresh(refreshToken: string): Promise<AuthTokens> {
|
|
|
|
|
const next = await createPublicClient().post<RefreshResponse, { refresh_token: string }>(
|
|
|
|
|
"/api/auth/refresh",
|
|
|
|
|
{ refresh_token: refreshToken },
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
accessToken: next.access_token,
|
|
|
|
|
refreshToken: next.refresh_token,
|
|
|
|
|
};
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-20 09:52:48 +08:00
|
|
|
function clientRegistrationStorageKey(user: UserInfo): string {
|
|
|
|
|
return `${clientRegistrationStoragePrefix}:${user.primary_workspace_id}:${user.id}`;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function readStoredClientRegistrationID(user: UserInfo): string | null {
|
|
|
|
|
if (typeof window === "undefined") {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
return window.localStorage.getItem(clientRegistrationStorageKey(user));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function persistClientRegistrationID(user: UserInfo, clientID: string): void {
|
|
|
|
|
if (typeof window === "undefined") {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
window.localStorage.setItem(clientRegistrationStorageKey(user), clientID);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function generateUUID(): string {
|
|
|
|
|
if (globalThis.crypto?.randomUUID) {
|
|
|
|
|
return globalThis.crypto.randomUUID();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, (char) => {
|
|
|
|
|
const random = Math.floor(Math.random() * 16);
|
|
|
|
|
const value = char === "x" ? random : (random & 0x3) | 0x8;
|
|
|
|
|
return value.toString(16);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getOrCreateClientRegistrationID(user: UserInfo): string {
|
|
|
|
|
const existing = readStoredClientRegistrationID(user)?.trim();
|
|
|
|
|
if (existing) {
|
|
|
|
|
return existing;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const generated = generateUUID();
|
|
|
|
|
persistClientRegistrationID(user, generated);
|
|
|
|
|
return generated;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-20 11:59:35 +08:00
|
|
|
async function resolveDeviceInfo(): Promise<{ device_name: string; os: string; cpu_arch: string }> {
|
|
|
|
|
const fallback = {
|
2026-04-19 14:18:20 +08:00
|
|
|
device_name: `Desktop ${navigator.platform || "unknown"}`,
|
|
|
|
|
os: navigator.platform || "unknown",
|
2026-04-20 11:59:35 +08:00
|
|
|
cpu_arch: "unknown",
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const info = await window.desktopBridge?.app?.deviceInfo?.();
|
|
|
|
|
if (info?.device_name && info.os && info.cpu_arch) {
|
|
|
|
|
return info;
|
|
|
|
|
}
|
|
|
|
|
} catch (err) {
|
|
|
|
|
console.warn("[desktop-session] deviceInfo bridge failed", err);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return fallback;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function registerPayload(user: UserInfo): Promise<DesktopClientRegisterRequest> {
|
|
|
|
|
const device = await resolveDeviceInfo();
|
|
|
|
|
return {
|
|
|
|
|
client_id: getOrCreateClientRegistrationID(user),
|
|
|
|
|
device_name: device.device_name,
|
|
|
|
|
os: device.os,
|
|
|
|
|
cpu_arch: device.cpu_arch,
|
2026-04-19 14:18:20 +08:00
|
|
|
client_version: "0.1.0-dev",
|
|
|
|
|
channel: "dev",
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function createPreviewUser(): UserInfo {
|
|
|
|
|
return {
|
|
|
|
|
id: 0,
|
|
|
|
|
email: "preview@geo-rankly.local",
|
|
|
|
|
name: "开发预览账号",
|
|
|
|
|
avatar_url: null,
|
|
|
|
|
tenant_id: 0,
|
|
|
|
|
primary_workspace_id: 0,
|
|
|
|
|
tenant_role: "owner",
|
|
|
|
|
permissions: ["desktop.preview"],
|
|
|
|
|
membership: null,
|
|
|
|
|
kol_profile: null,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-20 11:59:35 +08:00
|
|
|
async function createPreviewClient(): Promise<DesktopClientInfo> {
|
2026-04-19 14:18:20 +08:00
|
|
|
const now = new Date().toISOString();
|
2026-04-20 11:59:35 +08:00
|
|
|
const device = await resolveDeviceInfo();
|
2026-04-19 14:18:20 +08:00
|
|
|
return {
|
|
|
|
|
id: "preview-client",
|
|
|
|
|
tenant_id: 0,
|
|
|
|
|
workspace_id: 0,
|
|
|
|
|
user_id: 0,
|
2026-04-20 11:59:35 +08:00
|
|
|
device_name: `Preview ${device.device_name}`,
|
|
|
|
|
os: device.os,
|
|
|
|
|
cpu_arch: device.cpu_arch,
|
2026-04-19 14:18:20 +08:00
|
|
|
client_version: "0.1.0-preview",
|
|
|
|
|
channel: "dev",
|
|
|
|
|
created_at: now,
|
|
|
|
|
last_seen_at: now,
|
|
|
|
|
last_rotated_at: now,
|
|
|
|
|
revoked_at: null,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function buildRuntimeSessionPayload(next: DesktopSession | null): DesktopRuntimeSessionSyncRequest | null {
|
|
|
|
|
if (!next) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
api_base_url: apiBaseURL.value,
|
|
|
|
|
mode: next.mode,
|
|
|
|
|
client_token: next.clientToken,
|
|
|
|
|
desktop_client: next.desktopClient,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function syncRuntimeSessionToMain(next: DesktopSession | null = session.value): Promise<void> {
|
|
|
|
|
if (typeof window === "undefined") {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!window.desktopBridge?.app?.syncRuntimeSession) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await window.desktopBridge.app.syncRuntimeSession(buildRuntimeSessionPayload(next));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function login(payload: LoginRequest): Promise<void> {
|
|
|
|
|
pending.value = true;
|
|
|
|
|
error.value = null;
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const authData = await createPublicClient().post<LoginResponse, LoginRequest>(
|
|
|
|
|
"/api/auth/login",
|
|
|
|
|
payload,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
persistSession({
|
|
|
|
|
mode: "authenticated",
|
|
|
|
|
accessToken: authData.access_token,
|
|
|
|
|
refreshToken: authData.refresh_token,
|
|
|
|
|
user: authData.user,
|
|
|
|
|
clientToken: null,
|
|
|
|
|
clientTokenExpiresAt: null,
|
|
|
|
|
desktopClient: null,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const registered = await createAuthenticatedClient().post<
|
|
|
|
|
DesktopClientRegisterResponse,
|
|
|
|
|
DesktopClientRegisterRequest
|
2026-04-20 11:59:35 +08:00
|
|
|
>("/api/desktop/clients/register", await registerPayload(authData.user));
|
2026-04-20 09:52:48 +08:00
|
|
|
|
|
|
|
|
persistClientRegistrationID(authData.user, registered.client.id);
|
2026-04-19 14:18:20 +08:00
|
|
|
|
|
|
|
|
persistSession({
|
|
|
|
|
mode: "authenticated",
|
|
|
|
|
accessToken: authData.access_token,
|
|
|
|
|
refreshToken: authData.refresh_token,
|
|
|
|
|
user: authData.user,
|
|
|
|
|
clientToken: registered.client_token,
|
|
|
|
|
clientTokenExpiresAt: registered.expires_at,
|
|
|
|
|
desktopClient: registered.client,
|
|
|
|
|
});
|
|
|
|
|
} catch (err) {
|
|
|
|
|
error.value = err instanceof Error ? err.message : "登录失败,请稍后重试";
|
|
|
|
|
persistSession(null);
|
|
|
|
|
throw err;
|
|
|
|
|
} finally {
|
|
|
|
|
pending.value = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-20 11:59:35 +08:00
|
|
|
async function enterPreview(): Promise<void> {
|
2026-04-19 14:18:20 +08:00
|
|
|
error.value = null;
|
|
|
|
|
persistSession({
|
|
|
|
|
mode: "preview",
|
|
|
|
|
accessToken: null,
|
|
|
|
|
refreshToken: null,
|
|
|
|
|
user: createPreviewUser(),
|
|
|
|
|
clientToken: "preview-client-token",
|
|
|
|
|
clientTokenExpiresAt: Date.now() + 7 * 24 * 60 * 60 * 1000,
|
2026-04-20 11:59:35 +08:00
|
|
|
desktopClient: await createPreviewClient(),
|
2026-04-19 14:18:20 +08:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-20 09:52:48 +08:00
|
|
|
async function logout(): Promise<void> {
|
2026-04-19 14:18:20 +08:00
|
|
|
error.value = null;
|
2026-04-20 09:52:48 +08:00
|
|
|
const current = session.value;
|
|
|
|
|
|
|
|
|
|
if (current?.mode === "authenticated") {
|
|
|
|
|
try {
|
|
|
|
|
await window.desktopBridge?.app?.releaseRuntimeSession?.(true);
|
|
|
|
|
} catch (err) {
|
|
|
|
|
console.warn("[desktop-session] releaseRuntimeSession failed", err);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
await createAuthenticatedClient().post<null, Record<string, never>>("/api/auth/logout", {});
|
|
|
|
|
} catch (err) {
|
|
|
|
|
console.warn("[desktop-session] auth logout failed", err);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-19 14:18:20 +08:00
|
|
|
persistSession(null);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function useDesktopSession() {
|
|
|
|
|
return {
|
|
|
|
|
session: readonly(session),
|
|
|
|
|
apiBaseURL: readonly(apiBaseURL),
|
|
|
|
|
pending: readonly(pending),
|
|
|
|
|
error: readonly(error),
|
|
|
|
|
isAuthenticated: computed(() => Boolean(session.value?.clientToken)),
|
|
|
|
|
isPreviewMode: computed(() => session.value?.mode === "preview"),
|
|
|
|
|
setApiBaseURL: persistApiBaseURL,
|
|
|
|
|
syncRuntimeSession: syncRuntimeSessionToMain,
|
|
|
|
|
login,
|
|
|
|
|
logout,
|
|
|
|
|
enterPreview,
|
|
|
|
|
};
|
|
|
|
|
}
|