🔀 修复上传状态与详情交互细节

This commit is contained in:
orion committed 2026-09-11 08:59:02 +08:00
8 files changed
+97 -31

No files matched your search

@@ -30,7 +30,7 @@
type="button"
aria-label="关闭文件详情"
@click="emit('close')"
class="rounded-[30px] p-1.5 transition-colors hover:bg-gray-100 dark:hover:bg-gray-800 sm:p-2"
class="min-h-11 min-w-11 rounded-[30px] p-1.5 transition-colors hover:bg-gray-100 dark:hover:bg-gray-800 sm:p-2"
>
<XIcon
class="w-4 h-4 sm:w-5 sm:h-5"
@@ -140,11 +140,13 @@
</button>
</div>
<p
v-if="!showWget"
class="text-xs sm:text-sm font-mono break-all line-clamp-2"
:class="[isDarkMode ? 'text-gray-300' : 'text-gray-600']"
>
点击复制 wget 命令
<button type="button" class="min-h-11 rounded-[30px] px-3 text-left hover:bg-gray-200 dark:hover:bg-gray-700" @click="showWget = true">显示 wget 命令</button>
</p>
<p v-else class="text-xs sm:text-sm font-mono break-all" :class="[isDarkMode ? 'text-gray-300' : 'text-gray-600']">{{ wgetCommand }}</p>
</div>
</div>
@@ -156,8 +158,10 @@
type="button"
class="bg-white p-3 sm:p-4 rounded-lg shadow-sm mb-3 sm:mb-4"
aria-label="取件二维码"
@click="showQr = !showQr"
>
<QRCode :value="qrValue" :size="140" level="M" class="sm:w-[160px] sm:h-[160px]" />
<span v-if="!showQr" class="flex h-[140px] w-[140px] items-center justify-center text-sm text-gray-500 sm:h-[160px] sm:w-[160px]">显示二维码</span>
<QRCode v-else :value="qrValue" :size="140" level="M" class="sm:w-[160px] sm:h-[160px]" />
</button>
<p
class="text-xs sm:text-sm truncate max-w-full"
@@ -199,6 +203,7 @@ import {
import { useI18n } from 'vue-i18n'
import type { SentFileRecord } from '@/types'
import { buildAbsoluteUrl } from '@/utils/share-url'
import { lockBodyScroll, unlockBodyScroll } from './scrollLock'
const props = defineProps<{
@@ -221,6 +226,10 @@ const modalRef = ref<HTMLElement>()
const modalTitleId = 'sent-record-detail-title'
let returnFocusElement: HTMLElement | null = null
let scrollLockOwner: ReturnType<typeof lockBodyScroll> | null = null
const showQr = ref(false)
const showWget = ref(false)
const wgetCommand = computed(() => `wget ${buildAbsoluteUrl(`/share/select?code=${props.record?.retrieveCode ?? ''}`)} -O "${props.record?.filename ?? ''}"`)
const focusableSelector = 'button:not([disabled]), [href], [tabindex]:not([tabindex="-1"])'
const handleKeydown = (event: KeyboardEvent) => {
if (event.key === 'Escape') { event.preventDefault(); emit('close'); return }
@@ -232,14 +241,20 @@ const handleKeydown = (event: KeyboardEvent) => {
}
}
watch(() => props.record, async (record) => {
if (scrollLockOwner) {
unlockBodyScroll(scrollLockOwner)
scrollLockOwner = null
}
showQr.value = false
showWget.value = false
if (record) {
returnFocusElement = document.activeElement instanceof HTMLElement ? document.activeElement : null
lockBodyScroll(); await nextTick(); modalRef.value?.focus()
scrollLockOwner = lockBodyScroll(); await nextTick(); modalRef.value?.focus()
} else {
unlockBodyScroll(); await nextTick(); returnFocusElement?.focus(); returnFocusElement = null
await nextTick(); returnFocusElement?.focus(); returnFocusElement = null
}
}, { immediate: true })
onBeforeUnmount(() => { unlockBodyScroll(); returnFocusElement = null })
onBeforeUnmount(() => { unlockBodyScroll(scrollLockOwner); scrollLockOwner = null; returnFocusElement = null })
</script>
<style scoped>
+6 -3
View File
@@ -57,11 +57,13 @@ const { t } = useI18n()
const isDarkMode = inject('isDarkMode')
const drawerRef = ref<HTMLElement>()
let returnFocusElement: HTMLElement | null = null
let scrollLockOwner: ReturnType<typeof lockBodyScroll> | null = null
const focusableSelector =
'button:not([disabled]), [href], input:not([disabled]), select:not([disabled]), textarea:not([disabled]), [tabindex]:not([tabindex="-1"])'
const handleKeydown = (event: KeyboardEvent) => {
if (event.key === 'Escape') {
if (event.target !== event.currentTarget && (event.target as HTMLElement).closest('[role="dialog"]') !== event.currentTarget) return
event.preventDefault()
emit('close')
return
@@ -84,20 +86,21 @@ watch(
() => props.visible,
async (visible) => {
if (visible) {
lockBodyScroll()
scrollLockOwner = lockBodyScroll()
returnFocusElement = document.activeElement instanceof HTMLElement ? document.activeElement : null
await nextTick()
const first = drawerRef.value?.querySelector<HTMLElement>(focusableSelector)
;(first || drawerRef.value)?.focus()
} else {
unlockBodyScroll()
unlockBodyScroll(scrollLockOwner)
scrollLockOwner = null
await nextTick()
returnFocusElement?.focus()
returnFocusElement = null
}
}
)
onBeforeUnmount(() => unlockBodyScroll())
onBeforeUnmount(() => { unlockBodyScroll(scrollLockOwner); scrollLockOwner = null })
</script>
<style scoped>
+11 -8
View File
@@ -1,18 +1,21 @@
let lockCount = 0
type ScrollLockOwner = symbol
let owners = new Set<ScrollLockOwner>()
let previousOverflow = ''
export function lockBodyScroll(): void {
if (lockCount === 0) {
export function lockBodyScroll(): ScrollLockOwner {
const owner = Symbol('scroll-lock-owner')
if (owners.size === 0) {
previousOverflow = document.body.style.overflow
document.body.style.overflow = 'hidden'
}
lockCount += 1
owners.add(owner)
return owner
}
export function unlockBodyScroll(): void {
if (lockCount === 0) return
lockCount -= 1
if (lockCount === 0) {
export function unlockBodyScroll(owner: ScrollLockOwner | null | undefined): void {
if (!owner || !owners.delete(owner)) return
if (owners.size === 0) {
document.body.style.overflow = previousOverflow
previousOverflow = ''
}
+9
View File
@@ -37,6 +37,7 @@ type PresignedUploadNotifier = (message: string, type: AlertType) => void
type UsePresignedUploadOptions = {
getMaxFileSize?: () => number
notify?: PresignedUploadNotifier
onStatus?: (status: PresignUploadStatus) => void
}
/**
@@ -66,6 +67,7 @@ export function usePresignedUpload(options: UsePresignedUploadOptions = {}) {
const notify: PresignedUploadNotifier = (message, type) => {
options.notify?.(message, type)
}
const onStatus = options.onStatus
/**
* 文件大小验证
@@ -110,6 +112,7 @@ export function usePresignedUpload(options: UsePresignedUploadOptions = {}) {
*/
const handleUploadError = (error: unknown): void => {
presignStatus.value = PRESIGN_UPLOAD_STATUS.ERROR
onStatus?.(PRESIGN_UPLOAD_STATUS.ERROR)
const status = (error as ErrorWithResponse)?.response?.status
const fallback =
status === 404
@@ -131,6 +134,7 @@ export function usePresignedUpload(options: UsePresignedUploadOptions = {}) {
): Promise<PresignInitResponse | null> => {
try {
presignStatus.value = PRESIGN_UPLOAD_STATUS.INITIALIZING
onStatus?.(PRESIGN_UPLOAD_STATUS.INITIALIZING)
const response = await PresignUploadService.initUpload({
file_name: file.name,
@@ -161,6 +165,7 @@ export function usePresignedUpload(options: UsePresignedUploadOptions = {}) {
): Promise<string | null> => {
try {
presignStatus.value = PRESIGN_UPLOAD_STATUS.UPLOADING
onStatus?.(PRESIGN_UPLOAD_STATUS.UPLOADING)
// 上传到 S3
const progressHandler = createProgressHandler(options?.onProgress)
@@ -176,6 +181,7 @@ export function usePresignedUpload(options: UsePresignedUploadOptions = {}) {
// 确认上传
presignStatus.value = PRESIGN_UPLOAD_STATUS.CONFIRMING
onStatus?.(PRESIGN_UPLOAD_STATUS.CONFIRMING)
const confirmResponse = await PresignUploadService.confirmUpload(session.upload_id, {
expire_value: options?.expireValue ?? DEFAULT_EXPIRE_VALUE,
expire_style: options?.expireStyle ?? DEFAULT_EXPIRE_STYLE
@@ -183,6 +189,7 @@ export function usePresignedUpload(options: UsePresignedUploadOptions = {}) {
if (confirmResponse.code === 200 && confirmResponse.detail?.code) {
presignStatus.value = PRESIGN_UPLOAD_STATUS.SUCCESS
onStatus?.(PRESIGN_UPLOAD_STATUS.SUCCESS)
uploadedCode.value = String(confirmResponse.detail.code)
// 确保进度为 100%
uploadProgress.value = {
@@ -211,6 +218,7 @@ export function usePresignedUpload(options: UsePresignedUploadOptions = {}) {
): Promise<string | null> => {
try {
presignStatus.value = PRESIGN_UPLOAD_STATUS.UPLOADING
onStatus?.(PRESIGN_UPLOAD_STATUS.UPLOADING)
const progressHandler = createProgressHandler(options?.onProgress)
const uploadUrl =
@@ -224,6 +232,7 @@ export function usePresignedUpload(options: UsePresignedUploadOptions = {}) {
if (response.code === 200 && response.detail?.code) {
presignStatus.value = PRESIGN_UPLOAD_STATUS.SUCCESS
onStatus?.(PRESIGN_UPLOAD_STATUS.SUCCESS)
uploadedCode.value = String(response.detail.code)
// 确保进度为 100%
uploadProgress.value = {
+9 -6
View File
@@ -31,7 +31,7 @@ export function useSendFlow() {
const selectedRecord = ref<SentFileRecord | null>(null)
const isSubmitting = ref(false)
const fileHash = ref('')
const uploadStatus = ref<'idle' | 'uploading' | 'success' | 'error'>('idle')
const uploadStatus = ref<'idle' | 'initializing' | 'uploading' | 'confirming' | 'success' | 'error'>('idle')
const uploadedBytes = ref(0)
const totalBytes = ref(0)
const uploadError = ref('')
@@ -65,10 +65,9 @@ export function useSendFlow() {
notify: (message, type) => alertStore.showAlert(message, type),
translate: t,
onProgress: (progress) => {
uploadProgress.value = progress
const total = selectedFile.value?.size || selectedFiles.value.reduce((sum, file) => sum + file.size, 0)
totalBytes.value = total
uploadedBytes.value = Math.round((progress / 100) * total)
uploadProgress.value = progress.percentage
totalBytes.value = progress.total
uploadedBytes.value = progress.loaded
},
onStatus: (status) => { uploadStatus.value = status },
onHashCalculated: (hash) => {
@@ -215,7 +214,7 @@ export function useSendFlow() {
const handleSubmit = async () => {
if (isSubmitting.value) return
isSubmitting.value = true
uploadStatus.value = 'uploading'
uploadError.value = ''
try {
if (sendType.value === 'file' && !selectedFile.value && selectedFiles.value.length === 0) {
@@ -297,6 +296,10 @@ export function useSendFlow() {
alertStore.showAlert(getErrorMessage(error, t('send.messages.sendFailed')), 'error')
} finally {
uploadProgress.value = 0
if (uploadStatus.value !== 'success' && uploadStatus.value !== 'error') {
uploadStatus.value = 'idle'
}
if (uploadStatus.value === 'success') uploadStatus.value = 'idle'
isSubmitting.value = false
}
}
+8 -8
View File
@@ -12,9 +12,9 @@ type UseSendSubmitOptions = {
getMaxFileSize: () => number
notify: (message: string, type: AlertType) => void
translate: Translate
onProgress: (progress: number) => void
onProgress: (progress: UploadProgress) => void
onHashCalculated: (hash: string) => void
onStatus?: (status: 'idle' | 'uploading' | 'success' | 'error') => void
onStatus?: (status: 'idle' | 'initializing' | 'uploading' | 'confirming' | 'success' | 'error') => void
}
type SubmitFileOptions = {
@@ -35,7 +35,8 @@ type SubmitTextOptions = {
export function useSendSubmit(options: UseSendSubmitOptions) {
const { uploadFile: presignUploadFile, reset: resetPresignUpload } = usePresignedUpload({
getMaxFileSize: options.getMaxFileSize,
notify: options.notify
notify: options.notify,
onStatus: options.onStatus
})
const handleChunkUpload = async (
@@ -47,9 +48,8 @@ export function useSendSubmit(options: UseSendSubmitOptions) {
expireValue,
expireStyle,
onHashCalculated: options.onHashCalculated,
onProgress: (progress: UploadProgress) => {
options.onProgress(progress.percentage)
},
onProgress: options.onProgress,
onStatus: options.onStatus,
messages: {
initFailed: options.translate('send.messages.initChunkUploadFailed'),
chunkFailed: (index) => options.translate('send.messages.chunkUploadFailed', { index }),
@@ -67,7 +67,7 @@ export function useSendSubmit(options: UseSendSubmitOptions) {
expireValue,
expireStyle: expireStyle as ExpireStyle,
onProgress: (progress) => {
options.onProgress(progress.percentage)
options.onProgress(progress)
}
})
@@ -107,7 +107,7 @@ export function useSendSubmit(options: UseSendSubmitOptions) {
throw new Error(options.translate('send.messages.selectFile'))
}
options.onStatus?.('uploading')
try {
const response = enableChunk
? await handleChunkUpload(fileToUpload, expireValue, expireStyle)
+6
View File
@@ -14,6 +14,7 @@ type ChunkedUploadOptions = {
chunkFailed?: (index: number) => string
completeFailed?: string
}
onStatus?: (status: 'initializing' | 'uploading' | 'confirming' | 'success' | 'error') => void
}
type ChunkedUploadResult = ChunkUploadInitResponse | FileUploadResponse
@@ -29,6 +30,7 @@ export const uploadChunkedFile = async (
file: File,
options: ChunkedUploadOptions
): Promise<ApiResponse<ChunkedUploadResult>> => {
options.onStatus?.('initializing')
const fileHash = await calculateFileHash(file)
options.onHashCalculated?.(fileHash)
@@ -55,6 +57,7 @@ export const uploadChunkedFile = async (
}
const uploadedChunks = new Set(initDetail.uploaded_chunks || [])
options.onStatus?.('uploading')
for (let index = 0; index < chunks; index++) {
if (uploadedChunks.has(index)) {
continue
@@ -84,14 +87,17 @@ export const uploadChunkedFile = async (
uploadedChunks.add(index)
}
options.onStatus?.('confirming')
const completeResponse = await FileService.completeChunkUpload(uploadId, {
expire_value: options.expireValue,
expire_style: options.expireStyle
})
if (completeResponse.code !== 200) {
options.onStatus?.('error')
throw new Error(options.messages?.completeFailed || 'Complete chunk upload failed')
}
options.onStatus?.('success')
return completeResponse
}
+27
View File
@@ -0,0 +1,27 @@
import assert from 'node:assert/strict'
import test from 'node:test'
const body = { style: { overflow: 'scroll' } }
globalThis.document = { body }
const { lockBodyScroll, unlockBodyScroll } = await import('../src/components/common/scrollLock.ts')
test('restores overflow only after the owning lock is released', () => {
const first = lockBodyScroll()
const second = lockBodyScroll()
unlockBodyScroll(Symbol('unrelated'))
assert.equal(body.style.overflow, 'hidden')
unlockBodyScroll(first)
assert.equal(body.style.overflow, 'hidden')
unlockBodyScroll(second)
assert.equal(body.style.overflow, 'scroll')
})
test('releasing an owner twice cannot unlock another owner', () => {
const first = lockBodyScroll()
const second = lockBodyScroll()
unlockBodyScroll(first)
unlockBodyScroll(first)
assert.equal(body.style.overflow, 'hidden')
unlockBodyScroll(second)
assert.equal(body.style.overflow, 'scroll')
})