mirror of
https://github.com/vastsa/FileCodeBoxFronted.git
synced 2026-11-24 21:58:01 +08:00
feat: support first-run setup flow
This commit is contained in:
1 parent
5bdd9197bc
commit
00b3ad7fc4
8 files changed
+78
-11
No files matched your search
Generated
+2
-2
@@ -1,12 +1,12 @@
|
||||
{
|
||||
"name": "filecodeboxfronted",
|
||||
"version": "0.0.0",
|
||||
"version": "2.4.0",
|
||||
"lockfileVersion": 3,
|
||||
"requires": true,
|
||||
"packages": {
|
||||
"": {
|
||||
"name": "filecodeboxfronted",
|
||||
"version": "0.0.0",
|
||||
"version": "2.4.0",
|
||||
"dependencies": {
|
||||
"@tailwindcss/typography": "^0.5.16",
|
||||
"axios": "^1.13.2",
|
||||
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "filecodeboxfronted",
|
||||
"version": "0.0.0",
|
||||
"version": "2.4.0",
|
||||
"private": true,
|
||||
"type": "module",
|
||||
"scripts": {
|
||||
|
||||
@@ -22,6 +22,7 @@ export function useAppShell() {
|
||||
)
|
||||
|
||||
let cleanupThemeListener: (() => void) | null = null
|
||||
let setupRedirecting = false
|
||||
|
||||
const handleUnauthorized = () => {
|
||||
if (router.currentRoute.value.path !== ROUTES.LOGIN) {
|
||||
@@ -34,16 +35,32 @@ export function useAppShell() {
|
||||
}
|
||||
}
|
||||
|
||||
const handleSetupRequired = (event: Event) => {
|
||||
const setupPath =
|
||||
event instanceof CustomEvent && typeof event.detail?.setupPath === 'string'
|
||||
? event.detail.setupPath
|
||||
: '/setup'
|
||||
|
||||
if (!setupPath || setupRedirecting || window.location.pathname.replace(/\/+$/, '') === setupPath) {
|
||||
return
|
||||
}
|
||||
|
||||
setupRedirecting = true
|
||||
window.location.assign(setupPath)
|
||||
}
|
||||
|
||||
onMounted(() => {
|
||||
cleanupThemeListener = initTheme()
|
||||
setupRouteLoading()
|
||||
window.addEventListener(AUTH_EVENTS.UNAUTHORIZED, handleUnauthorized)
|
||||
window.addEventListener(AUTH_EVENTS.SETUP_REQUIRED, handleSetupRequired)
|
||||
void syncPublicConfig()
|
||||
})
|
||||
|
||||
onUnmounted(() => {
|
||||
cleanupThemeListener?.()
|
||||
window.removeEventListener(AUTH_EVENTS.UNAUTHORIZED, handleUnauthorized)
|
||||
window.removeEventListener(AUTH_EVENTS.SETUP_REQUIRED, handleSetupRequired)
|
||||
})
|
||||
|
||||
provide('isDarkMode', isDarkMode)
|
||||
|
||||
@@ -6,7 +6,7 @@ import { useAlertStore } from '@/stores/alertStore'
|
||||
import { useFileDataStore } from '@/stores/fileData'
|
||||
import type { ReceivedFileRecord, ShareMetadataResponse, ShareSelectResponse } from '@/types'
|
||||
import { copyToClipboard } from '@/utils/clipboard'
|
||||
import { getErrorMessage } from '@/utils/common'
|
||||
import { getErrorMessage, getResponseMessage } from '@/utils/common'
|
||||
import { renderMarkdownPreview } from '@/utils/content-preview'
|
||||
import { downloadReceivedRecord } from '@/utils/download-action'
|
||||
|
||||
@@ -99,7 +99,7 @@ export function useRetrieveFlow() {
|
||||
inspectedFile.value = res.detail
|
||||
} else {
|
||||
inspectedFile.value = null
|
||||
error.value = String(res.detail || res.message || '')
|
||||
error.value = getResponseMessage(res, t('retrieve.messages.unknownError'))
|
||||
alertStore.showAlert(t('retrieve.messages.retrieveFailure') + error.value, 'error')
|
||||
}
|
||||
} catch {
|
||||
@@ -142,7 +142,7 @@ export function useRetrieveFlow() {
|
||||
inspectedFile.value = null
|
||||
alertStore.showAlert(t('retrieve.messages.retrieveSuccess'), 'success')
|
||||
} else {
|
||||
error.value = String(res.detail || '')
|
||||
error.value = getResponseMessage(res, t('retrieve.messages.unknownError'))
|
||||
alertStore.showAlert(t('retrieve.messages.retrieveFailure') + error.value, 'error')
|
||||
}
|
||||
} catch (err: unknown) {
|
||||
|
||||
@@ -42,6 +42,7 @@ export const UPLOAD_STATUS = {
|
||||
export const API_STATUS_CODES = {
|
||||
SUCCESS: 200,
|
||||
UNAUTHORIZED: 401,
|
||||
SETUP_REQUIRED: 428,
|
||||
FORBIDDEN: 403,
|
||||
NOT_FOUND: 404,
|
||||
SERVER_ERROR: 500
|
||||
|
||||
+18
-1
@@ -4,7 +4,8 @@ import type { ApiErrorPayload } from '@/types'
|
||||
import { clearStoredToken, readStoredToken } from '@/utils/auth-storage'
|
||||
|
||||
export const AUTH_EVENTS = {
|
||||
UNAUTHORIZED: 'filecodebox:auth:unauthorized'
|
||||
UNAUTHORIZED: 'filecodebox:auth:unauthorized',
|
||||
SETUP_REQUIRED: 'filecodebox:setup:required'
|
||||
} as const
|
||||
|
||||
const rawBaseURL =
|
||||
@@ -33,7 +34,23 @@ const attachAuthToken = (config: InternalAxiosRequestConfig) => {
|
||||
return config
|
||||
}
|
||||
|
||||
const getSetupPath = (payload?: ApiErrorPayload) => {
|
||||
const detail = payload?.detail
|
||||
if (detail && typeof detail === 'object' && typeof detail.setup === 'string') {
|
||||
return detail.setup
|
||||
}
|
||||
return ''
|
||||
}
|
||||
|
||||
const handleAuthError = (error: AxiosError<ApiErrorPayload>) => {
|
||||
if (error.response?.status === API_STATUS_CODES.SETUP_REQUIRED) {
|
||||
window.dispatchEvent(
|
||||
new CustomEvent(AUTH_EVENTS.SETUP_REQUIRED, {
|
||||
detail: { setupPath: getSetupPath(error.response.data) }
|
||||
})
|
||||
)
|
||||
}
|
||||
|
||||
if (error.response?.status === API_STATUS_CODES.UNAUTHORIZED) {
|
||||
clearStoredToken()
|
||||
window.dispatchEvent(new CustomEvent(AUTH_EVENTS.UNAUTHORIZED))
|
||||
|
||||
+2
-1
@@ -6,6 +6,7 @@ export interface ApiResponse<T = unknown> {
|
||||
}
|
||||
|
||||
export interface ApiErrorPayload {
|
||||
detail?: string
|
||||
detail?: string | { setup?: string }
|
||||
message?: string
|
||||
msg?: string
|
||||
}
|
||||
+34
-3
@@ -1,7 +1,7 @@
|
||||
/**
|
||||
* 通用工具函数
|
||||
*/
|
||||
import type { ApiErrorPayload } from '@/types'
|
||||
import type { ApiErrorPayload, ApiResponse } from '@/types'
|
||||
|
||||
/**
|
||||
* 格式化时间戳为可读格式
|
||||
@@ -221,15 +221,46 @@ type ErrorWithResponse = {
|
||||
message?: string
|
||||
}
|
||||
|
||||
const getReadableMessage = (value: unknown): string => {
|
||||
if (typeof value === 'string') {
|
||||
return value
|
||||
}
|
||||
|
||||
if (!value || typeof value !== 'object') {
|
||||
return ''
|
||||
}
|
||||
|
||||
const payload = value as Record<string, unknown>
|
||||
for (const key of ['message', 'msg', 'detail', 'error']) {
|
||||
const fieldValue = payload[key]
|
||||
if (typeof fieldValue === 'string' && fieldValue.trim()) {
|
||||
return fieldValue
|
||||
}
|
||||
}
|
||||
|
||||
return ''
|
||||
}
|
||||
|
||||
export function getResponseMessage(response: ApiResponse, fallback: string): string {
|
||||
return (
|
||||
getReadableMessage(response.detail) ||
|
||||
getReadableMessage(response.message) ||
|
||||
getReadableMessage(response.msg) ||
|
||||
fallback
|
||||
)
|
||||
}
|
||||
|
||||
export function getErrorMessage(error: unknown, fallback: string): string {
|
||||
if (!error || typeof error !== 'object') {
|
||||
return fallback
|
||||
}
|
||||
|
||||
const errorWithResponse = error as ErrorWithResponse
|
||||
const responseData = errorWithResponse.response?.data
|
||||
return (
|
||||
errorWithResponse.response?.data?.detail ||
|
||||
errorWithResponse.response?.data?.message ||
|
||||
getReadableMessage(responseData?.detail) ||
|
||||
getReadableMessage(responseData?.message) ||
|
||||
getReadableMessage(responseData?.msg) ||
|
||||
errorWithResponse.message ||
|
||||
fallback
|
||||
)
|
||||
|
||||
Reference in new issue
Block a user