2026-05-01 20:39:09 +08:00
|
|
|
|
import { computed, readonly, shallowRef } from 'vue'
|
2026-04-19 14:18:20 +08:00
|
|
|
|
|
2026-05-01 20:39:09 +08:00
|
|
|
|
import { ApiClientError, createApiClient } from '@geo/http-client'
|
2026-04-19 14:18:20 +08:00
|
|
|
|
import type {
|
|
|
|
|
|
AuthTokens,
|
|
|
|
|
|
DesktopClientInfo,
|
|
|
|
|
|
DesktopClientRegisterRequest,
|
|
|
|
|
|
DesktopClientRegisterResponse,
|
|
|
|
|
|
DesktopRuntimeSessionSyncRequest,
|
|
|
|
|
|
LoginRequest,
|
|
|
|
|
|
LoginResponse,
|
|
|
|
|
|
RefreshResponse,
|
|
|
|
|
|
UserInfo,
|
2026-05-01 20:39:09 +08:00
|
|
|
|
} from '@geo/shared-types'
|
2026-04-19 14:18:20 +08:00
|
|
|
|
|
2026-05-01 20:39:09 +08:00
|
|
|
|
type SessionMode = 'authenticated' | 'preview'
|
2026-04-19 14:18:20 +08:00
|
|
|
|
|
|
|
|
|
|
interface DesktopSession {
|
2026-05-01 20:39:09 +08:00
|
|
|
|
mode: SessionMode
|
|
|
|
|
|
accessToken: string | null
|
|
|
|
|
|
refreshToken: string | null
|
|
|
|
|
|
user: UserInfo | null
|
|
|
|
|
|
clientToken: string | null
|
|
|
|
|
|
clientTokenExpiresAt: number | null
|
|
|
|
|
|
desktopClient: DesktopClientInfo | null
|
2026-04-19 14:18:20 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-04-27 20:06:37 +08:00
|
|
|
|
interface ResolvedDeviceInfo {
|
2026-05-01 20:39:09 +08:00
|
|
|
|
device_name: string
|
|
|
|
|
|
os: string
|
|
|
|
|
|
cpu_arch: string
|
2026-04-27 20:06:37 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-05-01 20:39:09 +08:00
|
|
|
|
const sessionStorageKey = 'geo.desktop.session.v1'
|
|
|
|
|
|
const apiBaseURLStorageKey = 'geo.desktop.api-base-url'
|
|
|
|
|
|
export const DEFAULT_API_BASE_URL =
|
|
|
|
|
|
import.meta.env.VITE_DESKTOP_API_BASE_URL ?? 'https://api.shengxintui.com'
|
2026-04-19 14:18:20 +08:00
|
|
|
|
|
2026-05-01 20:39:09 +08:00
|
|
|
|
const session = shallowRef<DesktopSession | null>(readStoredSession())
|
|
|
|
|
|
const apiBaseURL = shallowRef(readStoredApiBaseURL())
|
|
|
|
|
|
const pending = shallowRef(false)
|
|
|
|
|
|
const error = shallowRef<string | null>(null)
|
|
|
|
|
|
let loginPromise: Promise<void> | null = null
|
|
|
|
|
|
let logoutPromise: Promise<void> | null = null
|
2026-04-19 14:18:20 +08:00
|
|
|
|
|
|
|
|
|
|
function readStoredSession(): DesktopSession | null {
|
2026-05-01 20:39:09 +08:00
|
|
|
|
if (typeof window === 'undefined') {
|
|
|
|
|
|
return null
|
2026-04-19 14:18:20 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
2026-05-01 20:39:09 +08:00
|
|
|
|
const raw = window.localStorage.getItem(sessionStorageKey)
|
2026-04-19 14:18:20 +08:00
|
|
|
|
if (!raw) {
|
2026-05-01 20:39:09 +08:00
|
|
|
|
return null
|
2026-04-19 14:18:20 +08:00
|
|
|
|
}
|
2026-05-01 20:39:09 +08:00
|
|
|
|
return JSON.parse(raw) as DesktopSession
|
2026-04-19 14:18:20 +08:00
|
|
|
|
} catch {
|
2026-05-01 20:39:09 +08:00
|
|
|
|
return null
|
2026-04-19 14:18:20 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function persistSession(next: DesktopSession | null): void {
|
2026-05-01 20:39:09 +08:00
|
|
|
|
session.value = next
|
2026-04-19 14:18:20 +08:00
|
|
|
|
|
2026-05-01 20:39:09 +08:00
|
|
|
|
if (typeof window === 'undefined') {
|
|
|
|
|
|
return
|
2026-04-19 14:18:20 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (!next) {
|
2026-05-01 20:39:09 +08:00
|
|
|
|
window.localStorage.removeItem(sessionStorageKey)
|
2026-04-19 14:18:20 +08:00
|
|
|
|
} else {
|
2026-05-01 20:39:09 +08:00
|
|
|
|
window.localStorage.setItem(sessionStorageKey, JSON.stringify(next))
|
2026-04-19 14:18:20 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-05-01 20:39:09 +08:00
|
|
|
|
void syncRuntimeSessionToMain(next)
|
2026-04-19 14:18:20 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function readStoredApiBaseURL(): string {
|
2026-05-01 20:39:09 +08:00
|
|
|
|
if (typeof window === 'undefined') {
|
|
|
|
|
|
return DEFAULT_API_BASE_URL
|
2026-04-19 14:18:20 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-05-01 20:39:09 +08:00
|
|
|
|
return window.localStorage.getItem(apiBaseURLStorageKey) ?? DEFAULT_API_BASE_URL
|
2026-04-19 14:18:20 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function persistApiBaseURL(value: string): void {
|
2026-05-01 20:39:09 +08:00
|
|
|
|
const trimmed = value.trim() || DEFAULT_API_BASE_URL
|
|
|
|
|
|
apiBaseURL.value = trimmed
|
2026-04-19 14:18:20 +08:00
|
|
|
|
|
2026-05-01 20:39:09 +08:00
|
|
|
|
if (typeof window !== 'undefined') {
|
|
|
|
|
|
window.localStorage.setItem(apiBaseURLStorageKey, trimmed)
|
2026-04-19 14:18:20 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-05-01 20:39:09 +08:00
|
|
|
|
void syncRuntimeSessionToMain()
|
2026-04-19 14:18:20 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function createPublicClient() {
|
|
|
|
|
|
return createApiClient({
|
|
|
|
|
|
baseURL: apiBaseURL.value,
|
|
|
|
|
|
timeoutMs: 15000,
|
2026-05-01 20:39:09 +08:00
|
|
|
|
})
|
2026-04-19 14:18:20 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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) {
|
2026-05-01 20:39:09 +08:00
|
|
|
|
return
|
2026-04-19 14:18:20 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
persistSession({
|
|
|
|
|
|
...session.value,
|
|
|
|
|
|
accessToken: tokens.accessToken,
|
|
|
|
|
|
refreshToken: tokens.refreshToken,
|
2026-05-01 20:39:09 +08:00
|
|
|
|
})
|
2026-04-19 14:18:20 +08:00
|
|
|
|
},
|
|
|
|
|
|
clearTokens: () => persistSession(null),
|
|
|
|
|
|
async refresh(refreshToken: string): Promise<AuthTokens> {
|
|
|
|
|
|
const next = await createPublicClient().post<RefreshResponse, { refresh_token: string }>(
|
2026-05-01 20:39:09 +08:00
|
|
|
|
'/api/auth/refresh',
|
2026-04-19 14:18:20 +08:00
|
|
|
|
{ refresh_token: refreshToken },
|
2026-05-01 20:39:09 +08:00
|
|
|
|
)
|
2026-04-19 14:18:20 +08:00
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
|
accessToken: next.access_token,
|
|
|
|
|
|
refreshToken: next.refresh_token,
|
2026-05-01 20:39:09 +08:00
|
|
|
|
}
|
2026-04-19 14:18:20 +08:00
|
|
|
|
},
|
|
|
|
|
|
},
|
2026-05-01 20:39:09 +08:00
|
|
|
|
})
|
2026-04-19 14:18:20 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-04-30 20:56:03 +08:00
|
|
|
|
function createSessionBoundAuthenticatedClient(source: DesktopSession) {
|
2026-05-01 20:39:09 +08:00
|
|
|
|
let accessToken = source.accessToken
|
|
|
|
|
|
let refreshToken = source.refreshToken
|
2026-04-30 20:56:03 +08:00
|
|
|
|
|
|
|
|
|
|
return createApiClient({
|
|
|
|
|
|
baseURL: apiBaseURL.value,
|
|
|
|
|
|
timeoutMs: 15000,
|
|
|
|
|
|
auth: {
|
|
|
|
|
|
getAccessToken: () => accessToken,
|
|
|
|
|
|
getRefreshToken: () => refreshToken,
|
|
|
|
|
|
setTokens: (tokens: AuthTokens) => {
|
2026-05-01 20:39:09 +08:00
|
|
|
|
accessToken = tokens.accessToken
|
|
|
|
|
|
refreshToken = tokens.refreshToken
|
2026-04-30 20:56:03 +08:00
|
|
|
|
},
|
|
|
|
|
|
clearTokens: () => {
|
2026-05-01 20:39:09 +08:00
|
|
|
|
accessToken = null
|
|
|
|
|
|
refreshToken = null
|
2026-04-30 20:56:03 +08:00
|
|
|
|
},
|
|
|
|
|
|
async refresh(refreshTokenValue: string): Promise<AuthTokens> {
|
|
|
|
|
|
const next = await createPublicClient().post<RefreshResponse, { refresh_token: string }>(
|
2026-05-01 20:39:09 +08:00
|
|
|
|
'/api/auth/refresh',
|
2026-04-30 20:56:03 +08:00
|
|
|
|
{ refresh_token: refreshTokenValue },
|
2026-05-01 20:39:09 +08:00
|
|
|
|
)
|
2026-04-30 20:56:03 +08:00
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
|
accessToken: next.access_token,
|
|
|
|
|
|
refreshToken: next.refresh_token,
|
2026-05-01 20:39:09 +08:00
|
|
|
|
}
|
2026-04-30 20:56:03 +08:00
|
|
|
|
},
|
|
|
|
|
|
},
|
2026-05-01 20:39:09 +08:00
|
|
|
|
})
|
2026-04-30 20:56:03 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-04-20 16:04:32 +08:00
|
|
|
|
function resolveFallbackPlatform(): string {
|
|
|
|
|
|
const currentNavigator = globalThis.navigator as Navigator & {
|
2026-05-01 20:39:09 +08:00
|
|
|
|
userAgentData?: { platform?: string }
|
|
|
|
|
|
}
|
|
|
|
|
|
return currentNavigator.userAgentData?.platform || currentNavigator.platform || 'unknown'
|
2026-04-20 16:04:32 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function resolveFallbackOS(platform: string): string {
|
2026-05-01 20:39:09 +08:00
|
|
|
|
const normalized = platform.toLowerCase()
|
|
|
|
|
|
if (normalized.includes('mac')) {
|
|
|
|
|
|
return 'darwin'
|
2026-04-20 16:04:32 +08:00
|
|
|
|
}
|
2026-05-01 20:39:09 +08:00
|
|
|
|
if (normalized.includes('win')) {
|
|
|
|
|
|
return 'windows'
|
2026-04-20 16:04:32 +08:00
|
|
|
|
}
|
2026-05-01 20:39:09 +08:00
|
|
|
|
if (normalized.includes('linux')) {
|
|
|
|
|
|
return 'linux'
|
2026-04-20 16:04:32 +08:00
|
|
|
|
}
|
2026-05-01 20:39:09 +08:00
|
|
|
|
return normalized || 'unknown'
|
2026-04-20 16:04:32 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function resolveFallbackDeviceName(platform: string): string {
|
2026-05-01 20:39:09 +08:00
|
|
|
|
const normalized = platform.toLowerCase()
|
|
|
|
|
|
if (normalized.includes('mac')) {
|
|
|
|
|
|
return 'Current Mac'
|
2026-04-20 16:04:32 +08:00
|
|
|
|
}
|
2026-05-01 20:39:09 +08:00
|
|
|
|
if (normalized.includes('win')) {
|
|
|
|
|
|
return 'Current Windows PC'
|
2026-04-20 16:04:32 +08:00
|
|
|
|
}
|
2026-05-01 20:39:09 +08:00
|
|
|
|
if (normalized.includes('linux')) {
|
|
|
|
|
|
return 'Current Linux Desktop'
|
2026-04-20 16:04:32 +08:00
|
|
|
|
}
|
2026-05-01 20:39:09 +08:00
|
|
|
|
return 'Current Desktop'
|
2026-04-20 16:04:32 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-04-27 20:06:37 +08:00
|
|
|
|
async function resolveDeviceInfo(): Promise<ResolvedDeviceInfo> {
|
2026-04-20 11:59:35 +08:00
|
|
|
|
try {
|
2026-05-01 20:39:09 +08:00
|
|
|
|
const info = await window.desktopBridge?.app?.deviceInfo?.()
|
2026-04-20 11:59:35 +08:00
|
|
|
|
if (info?.device_name && info.os && info.cpu_arch) {
|
2026-05-01 20:39:09 +08:00
|
|
|
|
return info
|
2026-04-20 11:59:35 +08:00
|
|
|
|
}
|
|
|
|
|
|
} catch (err) {
|
2026-05-01 20:39:09 +08:00
|
|
|
|
console.warn('[desktop-session] deviceInfo bridge failed', err)
|
2026-04-20 11:59:35 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-05-01 20:39:09 +08:00
|
|
|
|
const platform = resolveFallbackPlatform()
|
2026-04-27 20:06:37 +08:00
|
|
|
|
return {
|
|
|
|
|
|
device_name: resolveFallbackDeviceName(platform),
|
|
|
|
|
|
os: resolveFallbackOS(platform),
|
2026-05-01 20:39:09 +08:00
|
|
|
|
cpu_arch: 'unknown',
|
|
|
|
|
|
}
|
2026-04-27 20:06:37 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async function resolveDesktopClientID(user: UserInfo): Promise<string> {
|
|
|
|
|
|
const clientID = await window.desktopBridge?.app?.clientId?.({
|
|
|
|
|
|
tenant_id: user.tenant_id,
|
|
|
|
|
|
workspace_id: user.primary_workspace_id,
|
|
|
|
|
|
user_id: user.id,
|
2026-05-01 20:39:09 +08:00
|
|
|
|
})
|
2026-04-27 20:06:37 +08:00
|
|
|
|
if (!clientID) {
|
2026-05-01 20:39:09 +08:00
|
|
|
|
throw new Error('无法生成本机账号 client_id,请重启桌面客户端后重试')
|
2026-04-27 20:06:37 +08:00
|
|
|
|
}
|
2026-05-01 20:39:09 +08:00
|
|
|
|
return clientID
|
2026-04-20 11:59:35 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-04-20 16:04:32 +08:00
|
|
|
|
async function syncStoredDesktopClientDeviceInfo(): Promise<void> {
|
2026-05-01 20:39:09 +08:00
|
|
|
|
const current = session.value
|
|
|
|
|
|
if (
|
|
|
|
|
|
typeof window === 'undefined' ||
|
|
|
|
|
|
current?.mode !== 'authenticated' ||
|
|
|
|
|
|
!current.desktopClient ||
|
|
|
|
|
|
!current.user
|
|
|
|
|
|
) {
|
|
|
|
|
|
return
|
2026-04-20 16:04:32 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-04-27 20:06:37 +08:00
|
|
|
|
const [device, clientID] = await Promise.all([
|
|
|
|
|
|
resolveDeviceInfo(),
|
|
|
|
|
|
resolveDesktopClientID(current.user),
|
2026-05-01 20:39:09 +08:00
|
|
|
|
])
|
2026-04-20 16:04:32 +08:00
|
|
|
|
if (session.value !== current) {
|
2026-05-01 20:39:09 +08:00
|
|
|
|
return
|
2026-04-20 16:04:32 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-05-01 20:39:09 +08:00
|
|
|
|
const desktopClient = current.desktopClient
|
2026-04-27 20:06:37 +08:00
|
|
|
|
if (desktopClient.id !== clientID) {
|
|
|
|
|
|
try {
|
2026-05-01 20:39:09 +08:00
|
|
|
|
const registered = await registerDesktopClient(current.user, device)
|
|
|
|
|
|
const latest = session.value
|
|
|
|
|
|
if (latest?.mode !== 'authenticated' || latest.user?.id !== current.user?.id) {
|
|
|
|
|
|
return
|
2026-04-27 20:06:37 +08:00
|
|
|
|
}
|
|
|
|
|
|
persistSession({
|
|
|
|
|
|
...latest,
|
|
|
|
|
|
clientToken: registered.client_token,
|
|
|
|
|
|
clientTokenExpiresAt: registered.expires_at,
|
|
|
|
|
|
desktopClient: registered.client,
|
2026-05-01 20:39:09 +08:00
|
|
|
|
})
|
2026-04-27 20:06:37 +08:00
|
|
|
|
} catch (err) {
|
2026-05-01 20:39:09 +08:00
|
|
|
|
console.warn('[desktop-session] stable client migration failed', err)
|
2026-04-27 20:06:37 +08:00
|
|
|
|
}
|
2026-05-01 20:39:09 +08:00
|
|
|
|
return
|
2026-04-27 20:06:37 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-04-20 16:04:32 +08:00
|
|
|
|
if (
|
2026-05-01 20:39:09 +08:00
|
|
|
|
desktopClient.device_name === device.device_name &&
|
|
|
|
|
|
desktopClient.os === device.os &&
|
|
|
|
|
|
desktopClient.cpu_arch === device.cpu_arch
|
2026-04-20 16:04:32 +08:00
|
|
|
|
) {
|
2026-05-01 20:39:09 +08:00
|
|
|
|
return
|
2026-04-20 16:04:32 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
persistSession({
|
|
|
|
|
|
...current,
|
|
|
|
|
|
desktopClient: {
|
|
|
|
|
|
...desktopClient,
|
|
|
|
|
|
device_name: device.device_name,
|
|
|
|
|
|
os: device.os,
|
|
|
|
|
|
cpu_arch: device.cpu_arch,
|
|
|
|
|
|
},
|
2026-05-01 20:39:09 +08:00
|
|
|
|
})
|
2026-04-20 16:04:32 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-04-27 20:06:37 +08:00
|
|
|
|
async function registerPayload(
|
|
|
|
|
|
user: UserInfo,
|
|
|
|
|
|
device?: ResolvedDeviceInfo,
|
|
|
|
|
|
): Promise<DesktopClientRegisterRequest> {
|
2026-05-01 20:39:09 +08:00
|
|
|
|
const resolvedDevice = device ?? (await resolveDeviceInfo())
|
2026-04-20 11:59:35 +08:00
|
|
|
|
return {
|
2026-04-27 20:06:37 +08:00
|
|
|
|
client_id: await resolveDesktopClientID(user),
|
|
|
|
|
|
device_name: resolvedDevice.device_name,
|
|
|
|
|
|
os: resolvedDevice.os,
|
|
|
|
|
|
cpu_arch: resolvedDevice.cpu_arch,
|
2026-05-01 20:39:09 +08:00
|
|
|
|
client_version: '0.1.0-dev',
|
|
|
|
|
|
channel: 'dev',
|
|
|
|
|
|
}
|
2026-04-19 14:18:20 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-04-27 20:06:37 +08:00
|
|
|
|
async function registerDesktopClient(
|
|
|
|
|
|
user: UserInfo,
|
|
|
|
|
|
device?: ResolvedDeviceInfo,
|
|
|
|
|
|
): Promise<DesktopClientRegisterResponse> {
|
2026-05-01 20:39:09 +08:00
|
|
|
|
const resolvedDevice = device ?? (await resolveDeviceInfo())
|
2026-04-27 20:06:37 +08:00
|
|
|
|
return createAuthenticatedClient().post<
|
|
|
|
|
|
DesktopClientRegisterResponse,
|
|
|
|
|
|
DesktopClientRegisterRequest
|
2026-05-01 20:39:09 +08:00
|
|
|
|
>('/api/desktop/clients/register', await registerPayload(user, resolvedDevice))
|
2026-04-27 20:06:37 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-04-19 14:18:20 +08:00
|
|
|
|
function createPreviewUser(): UserInfo {
|
|
|
|
|
|
return {
|
|
|
|
|
|
id: 0,
|
2026-05-01 20:39:09 +08:00
|
|
|
|
email: 'preview@geo-rankly.local',
|
|
|
|
|
|
phone: '',
|
|
|
|
|
|
name: '开发预览账号',
|
2026-04-19 14:18:20 +08:00
|
|
|
|
avatar_url: null,
|
|
|
|
|
|
tenant_id: 0,
|
|
|
|
|
|
primary_workspace_id: 0,
|
2026-05-01 20:39:09 +08:00
|
|
|
|
tenant_role: 'owner',
|
|
|
|
|
|
permissions: ['desktop.preview'],
|
2026-04-19 14:18:20 +08:00
|
|
|
|
membership: null,
|
|
|
|
|
|
kol_profile: null,
|
2026-05-01 20:39:09 +08:00
|
|
|
|
}
|
2026-04-19 14:18:20 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-04-20 11:59:35 +08:00
|
|
|
|
async function createPreviewClient(): Promise<DesktopClientInfo> {
|
2026-05-01 20:39:09 +08:00
|
|
|
|
const now = new Date().toISOString()
|
|
|
|
|
|
const device = await resolveDeviceInfo()
|
2026-04-19 14:18:20 +08:00
|
|
|
|
return {
|
2026-05-01 20:39:09 +08:00
|
|
|
|
id: 'preview-client',
|
2026-04-19 14:18:20 +08:00
|
|
|
|
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-05-01 20:39:09 +08:00
|
|
|
|
client_version: '0.1.0-preview',
|
|
|
|
|
|
channel: 'dev',
|
2026-04-19 14:18:20 +08:00
|
|
|
|
created_at: now,
|
|
|
|
|
|
last_seen_at: now,
|
|
|
|
|
|
last_rotated_at: now,
|
|
|
|
|
|
revoked_at: null,
|
2026-05-01 20:39:09 +08:00
|
|
|
|
}
|
2026-04-19 14:18:20 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-05-01 20:39:09 +08:00
|
|
|
|
function buildRuntimeSessionPayload(
|
|
|
|
|
|
next: DesktopSession | null,
|
|
|
|
|
|
): DesktopRuntimeSessionSyncRequest | null {
|
2026-04-19 14:18:20 +08:00
|
|
|
|
if (!next) {
|
2026-05-01 20:39:09 +08:00
|
|
|
|
return null
|
2026-04-19 14:18:20 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
|
api_base_url: apiBaseURL.value,
|
|
|
|
|
|
mode: next.mode,
|
|
|
|
|
|
client_token: next.clientToken,
|
|
|
|
|
|
desktop_client: next.desktopClient,
|
2026-05-01 20:39:09 +08:00
|
|
|
|
}
|
2026-04-19 14:18:20 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-05-01 20:39:09 +08:00
|
|
|
|
async function syncRuntimeSessionToMain(
|
|
|
|
|
|
next: DesktopSession | null = session.value,
|
|
|
|
|
|
): Promise<void> {
|
|
|
|
|
|
if (typeof window === 'undefined') {
|
|
|
|
|
|
return
|
2026-04-19 14:18:20 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (!window.desktopBridge?.app?.syncRuntimeSession) {
|
2026-05-01 20:39:09 +08:00
|
|
|
|
return
|
2026-04-19 14:18:20 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-05-01 20:39:09 +08:00
|
|
|
|
await window.desktopBridge.app.syncRuntimeSession(buildRuntimeSessionPayload(next))
|
2026-04-19 14:18:20 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-04-30 01:07:32 +08:00
|
|
|
|
function formatLoginError(err: unknown): string {
|
|
|
|
|
|
if (err instanceof ApiClientError) {
|
|
|
|
|
|
const messages: Record<string, string> = {
|
2026-05-01 20:39:09 +08:00
|
|
|
|
invalid_credentials: '账号或密码错误',
|
|
|
|
|
|
login_rate_limited: '登录请求过于频繁',
|
|
|
|
|
|
login_locked: '登录已被临时保护',
|
|
|
|
|
|
login_in_progress: '登录请求正在处理中',
|
|
|
|
|
|
login_guard_unavailable: '登录保护暂时不可用',
|
|
|
|
|
|
}
|
2026-05-02 19:25:15 +08:00
|
|
|
|
if (err.message in messages) {
|
|
|
|
|
|
const message = messages[err.message]!
|
|
|
|
|
|
return err.message === 'login_locked' && err.detail ? `${message},${err.detail}` : message
|
|
|
|
|
|
}
|
|
|
|
|
|
if (err.status === 404) return '登录接口不存在,请确认服务器地址'
|
|
|
|
|
|
if (err.status && err.status >= 500) return '服务器内部错误,请稍后重试'
|
|
|
|
|
|
if (err.status && err.status >= 400) return '请求被服务器拒绝,请检查账号信息'
|
|
|
|
|
|
if (!err.status) return '网络连接失败,请检查服务器地址或网络'
|
|
|
|
|
|
return err.message.replaceAll('_', ' ')
|
|
|
|
|
|
}
|
|
|
|
|
|
if (err instanceof Error) {
|
|
|
|
|
|
const msg = err.message
|
|
|
|
|
|
if (msg.includes('Invalid URL') || msg.includes('Failed to construct')) {
|
|
|
|
|
|
return '服务器地址无效,请检查网关 API 地址设置'
|
|
|
|
|
|
}
|
2026-05-01 20:39:09 +08:00
|
|
|
|
}
|
2026-05-02 19:25:15 +08:00
|
|
|
|
return '登录失败,请稍后重试'
|
2026-04-30 01:07:32 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async function performLogin(payload: LoginRequest): Promise<void> {
|
2026-05-01 20:39:09 +08:00
|
|
|
|
pending.value = true
|
|
|
|
|
|
error.value = null
|
2026-04-19 14:18:20 +08:00
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
const authData = await createPublicClient().post<LoginResponse, LoginRequest>(
|
2026-05-01 20:39:09 +08:00
|
|
|
|
'/api/auth/login',
|
2026-04-19 14:18:20 +08:00
|
|
|
|
payload,
|
2026-05-01 20:39:09 +08:00
|
|
|
|
)
|
2026-04-19 14:18:20 +08:00
|
|
|
|
|
|
|
|
|
|
persistSession({
|
2026-05-01 20:39:09 +08:00
|
|
|
|
mode: 'authenticated',
|
2026-04-19 14:18:20 +08:00
|
|
|
|
accessToken: authData.access_token,
|
|
|
|
|
|
refreshToken: authData.refresh_token,
|
|
|
|
|
|
user: authData.user,
|
|
|
|
|
|
clientToken: null,
|
|
|
|
|
|
clientTokenExpiresAt: null,
|
|
|
|
|
|
desktopClient: null,
|
2026-05-01 20:39:09 +08:00
|
|
|
|
})
|
2026-04-19 14:18:20 +08:00
|
|
|
|
|
2026-05-01 20:39:09 +08:00
|
|
|
|
const registered = await registerDesktopClient(authData.user)
|
2026-04-19 14:18:20 +08:00
|
|
|
|
|
|
|
|
|
|
persistSession({
|
2026-05-01 20:39:09 +08:00
|
|
|
|
mode: 'authenticated',
|
2026-04-19 14:18:20 +08:00
|
|
|
|
accessToken: authData.access_token,
|
|
|
|
|
|
refreshToken: authData.refresh_token,
|
|
|
|
|
|
user: authData.user,
|
|
|
|
|
|
clientToken: registered.client_token,
|
|
|
|
|
|
clientTokenExpiresAt: registered.expires_at,
|
|
|
|
|
|
desktopClient: registered.client,
|
2026-05-01 20:39:09 +08:00
|
|
|
|
})
|
2026-04-19 14:18:20 +08:00
|
|
|
|
} catch (err) {
|
2026-05-01 20:39:09 +08:00
|
|
|
|
error.value = formatLoginError(err)
|
|
|
|
|
|
persistSession(null)
|
|
|
|
|
|
throw err
|
2026-04-19 14:18:20 +08:00
|
|
|
|
} finally {
|
2026-05-01 20:39:09 +08:00
|
|
|
|
pending.value = false
|
2026-04-19 14:18:20 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-04-30 01:07:32 +08:00
|
|
|
|
async function login(payload: LoginRequest): Promise<void> {
|
|
|
|
|
|
if (loginPromise) {
|
2026-05-01 20:39:09 +08:00
|
|
|
|
return loginPromise
|
2026-04-30 01:07:32 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
loginPromise = performLogin({ ...payload }).finally(() => {
|
2026-05-01 20:39:09 +08:00
|
|
|
|
loginPromise = null
|
|
|
|
|
|
})
|
|
|
|
|
|
return loginPromise
|
2026-04-30 01:07:32 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-04-20 11:59:35 +08:00
|
|
|
|
async function enterPreview(): Promise<void> {
|
2026-05-01 20:39:09 +08:00
|
|
|
|
error.value = null
|
2026-04-19 14:18:20 +08:00
|
|
|
|
persistSession({
|
2026-05-01 20:39:09 +08:00
|
|
|
|
mode: 'preview',
|
2026-04-19 14:18:20 +08:00
|
|
|
|
accessToken: null,
|
|
|
|
|
|
refreshToken: null,
|
|
|
|
|
|
user: createPreviewUser(),
|
2026-05-01 20:39:09 +08:00
|
|
|
|
clientToken: 'preview-client-token',
|
2026-04-19 14:18:20 +08:00
|
|
|
|
clientTokenExpiresAt: Date.now() + 7 * 24 * 60 * 60 * 1000,
|
2026-04-20 11:59:35 +08:00
|
|
|
|
desktopClient: await createPreviewClient(),
|
2026-05-01 20:39:09 +08:00
|
|
|
|
})
|
2026-04-19 14:18:20 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-04-30 20:56:03 +08:00
|
|
|
|
async function releaseSessionForLogout(current: DesktopSession | null): Promise<void> {
|
2026-05-01 20:39:09 +08:00
|
|
|
|
if (current?.mode === 'authenticated') {
|
2026-04-20 09:52:48 +08:00
|
|
|
|
try {
|
2026-05-01 20:39:09 +08:00
|
|
|
|
await window.desktopBridge?.app?.releaseRuntimeSession?.(true)
|
2026-04-20 09:52:48 +08:00
|
|
|
|
} catch (err) {
|
2026-05-01 20:39:09 +08:00
|
|
|
|
console.warn('[desktop-session] releaseRuntimeSession failed', err)
|
2026-04-20 09:52:48 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
2026-05-01 20:39:09 +08:00
|
|
|
|
await createSessionBoundAuthenticatedClient(current).post<null, Record<string, never>>(
|
|
|
|
|
|
'/api/auth/logout',
|
|
|
|
|
|
{},
|
|
|
|
|
|
)
|
2026-04-20 09:52:48 +08:00
|
|
|
|
} catch (err) {
|
2026-05-01 20:39:09 +08:00
|
|
|
|
console.warn('[desktop-session] auth logout failed', err)
|
2026-04-20 09:52:48 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2026-04-30 20:56:03 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async function transitionWindowForLogout(): Promise<void> {
|
|
|
|
|
|
try {
|
2026-05-01 20:39:09 +08:00
|
|
|
|
await window.desktopBridge?.app?.setWindowMode?.('login', { source: 'logout', animate: true })
|
2026-04-30 20:56:03 +08:00
|
|
|
|
} catch (err) {
|
2026-05-01 20:39:09 +08:00
|
|
|
|
console.warn('[desktop-session] logout window transition failed', err)
|
2026-04-30 20:56:03 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async function performLogout(): Promise<void> {
|
2026-05-01 20:39:09 +08:00
|
|
|
|
error.value = null
|
|
|
|
|
|
const current = session.value
|
|
|
|
|
|
const releasePromise = releaseSessionForLogout(current)
|
2026-04-20 09:52:48 +08:00
|
|
|
|
|
2026-05-01 20:39:09 +08:00
|
|
|
|
persistSession(null)
|
|
|
|
|
|
await transitionWindowForLogout()
|
2026-04-30 20:56:03 +08:00
|
|
|
|
|
|
|
|
|
|
void releasePromise.catch((err) => {
|
2026-05-01 20:39:09 +08:00
|
|
|
|
console.warn('[desktop-session] background logout release failed', err)
|
|
|
|
|
|
})
|
2026-04-30 20:56:03 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async function logout(): Promise<void> {
|
|
|
|
|
|
if (logoutPromise) {
|
2026-05-01 20:39:09 +08:00
|
|
|
|
return logoutPromise
|
2026-04-30 20:56:03 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
logoutPromise = performLogout().finally(() => {
|
2026-05-01 20:39:09 +08:00
|
|
|
|
logoutPromise = null
|
|
|
|
|
|
})
|
|
|
|
|
|
return logoutPromise
|
2026-04-19 14:18:20 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-05-01 20:39:09 +08:00
|
|
|
|
if (typeof window !== 'undefined') {
|
|
|
|
|
|
window.addEventListener('storage', (event) => {
|
2026-04-30 22:00:01 +08:00
|
|
|
|
if (event.key === apiBaseURLStorageKey) {
|
2026-05-01 20:39:09 +08:00
|
|
|
|
apiBaseURL.value = event.newValue?.trim() || DEFAULT_API_BASE_URL
|
|
|
|
|
|
void syncRuntimeSessionToMain()
|
2026-04-30 22:00:01 +08:00
|
|
|
|
}
|
2026-05-01 20:39:09 +08:00
|
|
|
|
})
|
|
|
|
|
|
void syncStoredDesktopClientDeviceInfo()
|
2026-04-20 16:04:32 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-04-19 14:18:20 +08:00
|
|
|
|
export function useDesktopSession() {
|
|
|
|
|
|
return {
|
|
|
|
|
|
session: readonly(session),
|
|
|
|
|
|
apiBaseURL: readonly(apiBaseURL),
|
|
|
|
|
|
pending: readonly(pending),
|
|
|
|
|
|
error: readonly(error),
|
|
|
|
|
|
isAuthenticated: computed(() => Boolean(session.value?.clientToken)),
|
2026-05-01 20:39:09 +08:00
|
|
|
|
isPreviewMode: computed(() => session.value?.mode === 'preview'),
|
2026-04-19 14:18:20 +08:00
|
|
|
|
setApiBaseURL: persistApiBaseURL,
|
|
|
|
|
|
syncRuntimeSession: syncRuntimeSessionToMain,
|
|
|
|
|
|
login,
|
|
|
|
|
|
logout,
|
|
|
|
|
|
enterPreview,
|
2026-05-01 20:39:09 +08:00
|
|
|
|
}
|
2026-04-19 14:18:20 +08:00
|
|
|
|
}
|