This commit is contained in:
@@ -2,8 +2,28 @@ import { ref, computed, readonly } from 'vue'
|
|||||||
import { FileService } from '@/services'
|
import { FileService } from '@/services'
|
||||||
import { useAlertStore } from '@/stores/alertStore'
|
import { useAlertStore } from '@/stores/alertStore'
|
||||||
import { usePresignedUpload } from '@/composables/usePresignedUpload'
|
import { usePresignedUpload } from '@/composables/usePresignedUpload'
|
||||||
import type { UploadProgress, UploadStatus, PresignUploadOptions, ExpireStyle } from '@/types'
|
import type { UploadProgress, UploadStatus, PresignUploadOptions, ExpireStyle, ConfigState } from '@/types'
|
||||||
import { UPLOAD_STATUS, FILE_SIZE_LIMITS } from '@/constants'
|
import { UPLOAD_STATUS, FILE_SIZE_LIMITS, STORAGE_KEYS } from '@/constants'
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取最大文件大小限制(字节)
|
||||||
|
* 优先从后端配置获取,否则使用默认值
|
||||||
|
*/
|
||||||
|
function getMaxFileSize(): number {
|
||||||
|
try {
|
||||||
|
const configStr = localStorage.getItem(STORAGE_KEYS.CONFIG)
|
||||||
|
if (configStr) {
|
||||||
|
const config = JSON.parse(configStr) as Partial<ConfigState>
|
||||||
|
if (config.uploadSize && config.uploadSize > 0) {
|
||||||
|
// uploadSize 单位是字节
|
||||||
|
return config.uploadSize
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch {
|
||||||
|
// 解析失败时使用默认值
|
||||||
|
}
|
||||||
|
return FILE_SIZE_LIMITS.MAX_FILE_SIZE
|
||||||
|
}
|
||||||
|
|
||||||
export interface FileUploadOptions {
|
export interface FileUploadOptions {
|
||||||
/** 是否使用预签名上传,默认 false 保持向后兼容 */
|
/** 是否使用预签名上传,默认 false 保持向后兼容 */
|
||||||
@@ -66,9 +86,10 @@ export function useFileUpload(options?: { defaultUsePresigned?: boolean }) {
|
|||||||
|
|
||||||
// 文件验证
|
// 文件验证
|
||||||
const validateFile = (file: File): boolean => {
|
const validateFile = (file: File): boolean => {
|
||||||
if (file.size > FILE_SIZE_LIMITS.MAX_FILE_SIZE) {
|
const maxFileSize = getMaxFileSize()
|
||||||
|
if (file.size > maxFileSize) {
|
||||||
alertStore.showAlert(
|
alertStore.showAlert(
|
||||||
`文件大小不能超过 ${Math.round(FILE_SIZE_LIMITS.MAX_FILE_SIZE / 1024 / 1024)}MB`,
|
`文件大小不能超过 ${Math.round(maxFileSize / 1024 / 1024)}MB`,
|
||||||
'error'
|
'error'
|
||||||
)
|
)
|
||||||
return false
|
return false
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import { ref, computed, readonly } from 'vue'
|
import { ref, computed, readonly } from 'vue'
|
||||||
import { PresignUploadService } from '@/services'
|
import { PresignUploadService } from '@/services'
|
||||||
import { useAlertStore } from '@/stores/alertStore'
|
import { useAlertStore } from '@/stores/alertStore'
|
||||||
import { FILE_SIZE_LIMITS } from '@/constants'
|
import { FILE_SIZE_LIMITS, STORAGE_KEYS } from '@/constants'
|
||||||
import type {
|
import type {
|
||||||
PresignUploadStatus,
|
PresignUploadStatus,
|
||||||
PresignUploadMode,
|
PresignUploadMode,
|
||||||
@@ -9,10 +9,31 @@ import type {
|
|||||||
PresignUploadOptions,
|
PresignUploadOptions,
|
||||||
PresignStatusResponse,
|
PresignStatusResponse,
|
||||||
UploadProgress,
|
UploadProgress,
|
||||||
ExpireStyle
|
ExpireStyle,
|
||||||
|
ConfigState
|
||||||
} from '@/types'
|
} from '@/types'
|
||||||
import axios from 'axios'
|
import axios from 'axios'
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取最大文件大小限制(字节)
|
||||||
|
* 优先从后端配置获取,否则使用默认值
|
||||||
|
*/
|
||||||
|
function getMaxFileSize(): number {
|
||||||
|
try {
|
||||||
|
const configStr = localStorage.getItem(STORAGE_KEYS.CONFIG)
|
||||||
|
if (configStr) {
|
||||||
|
const config = JSON.parse(configStr) as Partial<ConfigState>
|
||||||
|
if (config.uploadSize && config.uploadSize > 0) {
|
||||||
|
// uploadSize 单位是字节
|
||||||
|
return config.uploadSize
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch {
|
||||||
|
// 解析失败时使用默认值
|
||||||
|
}
|
||||||
|
return FILE_SIZE_LIMITS.MAX_FILE_SIZE
|
||||||
|
}
|
||||||
|
|
||||||
// 预签名上传状态常量
|
// 预签名上传状态常量
|
||||||
export const PRESIGN_UPLOAD_STATUS = {
|
export const PRESIGN_UPLOAD_STATUS = {
|
||||||
IDLE: 'idle',
|
IDLE: 'idle',
|
||||||
@@ -58,8 +79,9 @@ export function usePresignedUpload() {
|
|||||||
* 文件大小验证
|
* 文件大小验证
|
||||||
*/
|
*/
|
||||||
const validateFileSize = (file: File): boolean => {
|
const validateFileSize = (file: File): boolean => {
|
||||||
if (file.size > FILE_SIZE_LIMITS.MAX_FILE_SIZE) {
|
const maxFileSize = getMaxFileSize()
|
||||||
const maxSizeMB = Math.round(FILE_SIZE_LIMITS.MAX_FILE_SIZE / 1024 / 1024)
|
if (file.size > maxFileSize) {
|
||||||
|
const maxSizeMB = Math.round(maxFileSize / 1024 / 1024)
|
||||||
errorMessage.value = `文件大小不能超过 ${maxSizeMB}MB`
|
errorMessage.value = `文件大小不能超过 ${maxSizeMB}MB`
|
||||||
alertStore.showAlert(errorMessage.value, 'error')
|
alertStore.showAlert(errorMessage.value, 'error')
|
||||||
return false
|
return false
|
||||||
|
|||||||
Reference in New Issue
Block a user