Skip to content

Commit 26b025a

Browse files
committed
【代码优化】全局:移除 VITE_UPLOAD_URL 环境变量,使用 getUploadUrl 方法替代
1 parent 74e0427 commit 26b025a

File tree

8 files changed

+23
-24
lines changed

8 files changed

+23
-24
lines changed

.env.dev

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ VITE_BASE_URL='http://api-dashboard.yudao.iocoder.cn'
88

99
# 文件上传类型:server - 后端上传, client - 前端直连上传,仅支持S3服务
1010
VITE_UPLOAD_TYPE=server
11-
# 上传路径
12-
VITE_UPLOAD_URL='http://api-dashboard.yudao.iocoder.cn/admin-api/infra/file/upload'
1311

1412
# 接口地址
1513
VITE_API_URL=/admin-api

.env.local

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ VITE_BASE_URL='http://localhost:48080'
88

99
# 文件上传类型:server - 后端上传, client - 前端直连上传,仅支持 S3 服务
1010
VITE_UPLOAD_TYPE=server
11-
# 上传路径
12-
VITE_UPLOAD_URL='http://localhost:48080/admin-api/infra/file/upload'
1311

1412
# 接口地址
1513
VITE_API_URL=/admin-api

.env.prod

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ VITE_BASE_URL='http://localhost:48080'
88

99
# 文件上传类型:server - 后端上传, client - 前端直连上传,仅支持S3服务
1010
VITE_UPLOAD_TYPE=server
11-
# 上传路径
12-
VITE_UPLOAD_URL='http://localhost:48080/admin-api/infra/file/upload'
1311

1412
# 接口地址
1513
VITE_API_URL=/admin-api

.env.stage

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ VITE_BASE_URL='http://api-dashboard.yudao.iocoder.cn'
88

99
# 文件上传类型:server - 后端上传, client - 前端直连上传,仅支持S3服务
1010
VITE_UPLOAD_TYPE=server
11-
# 上传路径
12-
VITE_UPLOAD_URL='http://api-dashboard.yudao.iocoder.cn/admin-api/infra/file/upload'
1311

1412
# 接口地址
1513
VITE_API_URL=/admin-api

.env.test

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ VITE_BASE_URL='http://localhost:48080'
88

99
# 文件上传类型:server - 后端上传, client - 前端直连上传,仅支持S3服务
1010
VITE_UPLOAD_TYPE=server
11-
# 上传路径
12-
VITE_UPLOAD_URL='http://localhost:48080/admin-api/infra/file/upload'
1311

1412
# 接口地址
1513
VITE_API_URL=/admin-api

src/components/Editor/src/Editor.vue

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { isNumber } from '@/utils/is'
77
import { ElMessage } from 'element-plus'
88
import { useLocaleStore } from '@/store/modules/locale'
99
import { getAccessToken, getTenantId } from '@/utils/auth'
10+
import { getUploadUrl } from '@/components/UploadFile/src/useUpload'
1011
1112
defineOptions({ name: 'Editor' })
1213
@@ -88,7 +89,7 @@ const editorConfig = computed((): IEditorConfig => {
8889
scroll: true,
8990
MENU_CONF: {
9091
['uploadImage']: {
91-
server: import.meta.env.VITE_UPLOAD_URL,
92+
server: getUploadUrl(),
9293
// 单个文件的最大体积限制,默认为 2M
9394
maxFileSize: 5 * 1024 * 1024,
9495
// 最多可上传几个文件,默认为 100
@@ -136,7 +137,7 @@ const editorConfig = computed((): IEditorConfig => {
136137
}
137138
},
138139
['uploadVideo']: {
139-
server: import.meta.env.VITE_UPLOAD_URL,
140+
server: getUploadUrl(),
140141
// 单个文件的最大体积限制,默认为 10M
141142
maxFileSize: 10 * 1024 * 1024,
142143
// 最多可上传几个文件,默认为 100

src/components/UploadFile/src/useUpload.ts

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,16 @@ import CryptoJS from 'crypto-js'
33
import { UploadRawFile, UploadRequestOptions } from 'element-plus/es/components/upload/src/upload'
44
import axios from 'axios'
55

6+
/**
7+
* 获得上传 URL
8+
*/
9+
export const getUploadUrl = (): string => {
10+
return import.meta.env.VITE_BASE_URL + import.meta.env.VITE_API_URL + '/infra/file/upload'
11+
}
12+
613
export const useUpload = () => {
714
// 后端上传地址
8-
const uploadUrl = import.meta.env.VITE_UPLOAD_URL
15+
const uploadUrl = getUploadUrl()
916
// 是否使用前端直连上传
1017
const isClientUpload = UPLOAD_TYPE.CLIENT === import.meta.env.VITE_UPLOAD_TYPE
1118
// 重写ElUpload上传方法
@@ -17,16 +24,18 @@ export const useUpload = () => {
1724
// 1.2 获取文件预签名地址
1825
const presignedInfo = await FileApi.getFilePresignedUrl(fileName)
1926
// 1.3 上传文件(不能使用 ElUpload 的 ajaxUpload 方法的原因:其使用的是 FormData 上传,Minio 不支持)
20-
return axios.put(presignedInfo.uploadUrl, options.file, {
21-
headers: {
22-
'Content-Type': options.file.type,
23-
}
24-
}).then(() => {
25-
// 1.4. 记录文件信息到后端(异步)
26-
createFile(presignedInfo, fileName, options.file)
27-
// 通知成功,数据格式保持与后端上传的返回结果一致
28-
return { data: presignedInfo.url }
29-
})
27+
return axios
28+
.put(presignedInfo.uploadUrl, options.file, {
29+
headers: {
30+
'Content-Type': options.file.type
31+
}
32+
})
33+
.then(() => {
34+
// 1.4. 记录文件信息到后端(异步)
35+
createFile(presignedInfo, fileName, options.file)
36+
// 通知成功,数据格式保持与后端上传的返回结果一致
37+
return { data: presignedInfo.url }
38+
})
3039
} else {
3140
// 模式二:后端上传
3241
// 重写 el-upload httpRequest 文件上传成功会走成功的钩子,失败走失败的钩子

types/env.d.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ interface ImportMetaEnv {
1919
readonly VITE_APP_DEFAULT_LOGIN_PASSWORD: string
2020
readonly VITE_APP_DOCALERT_ENABLE: string
2121
readonly VITE_BASE_URL: string
22-
readonly VITE_UPLOAD_URL: string
2322
readonly VITE_API_URL: string
2423
readonly VITE_BASE_PATH: string
2524
readonly VITE_DROP_DEBUGGER: string

0 commit comments

Comments
 (0)