|
|
@@ -4,9 +4,7 @@
|
|
|
import { getAccessToken, clearAll } from './storage'
|
|
|
import type { ApiResponse } from '../types/user'
|
|
|
import type { UploadResponse, UploadFileInfo } from '../types/workbench'
|
|
|
-
|
|
|
-// 基础 URL
|
|
|
-const BASE_URL = "http://192.168.110.40:8080"
|
|
|
+import { getBaseUrl } from './request'
|
|
|
|
|
|
/**
|
|
|
* 处理 401 未授权错误
|
|
|
@@ -34,7 +32,7 @@ const handle401Error = (): void => {
|
|
|
* 获取文件预览地址
|
|
|
*/
|
|
|
export const getFileViewUrl = (fileId: string): string => {
|
|
|
- return `${BASE_URL}/file/view/${fileId}`
|
|
|
+ return getBaseUrl()+`${fileId}`
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -43,15 +41,14 @@ export const getFileViewUrl = (fileId: string): string => {
|
|
|
* @param businessType 业务类型模块
|
|
|
*/
|
|
|
export const uploadFile = (filePath: string, businessType: string): Promise<UploadResponse> => {
|
|
|
- return new Promise((resolve, reject) => {
|
|
|
+ return new Promise((resolve, reject) => {
|
|
|
const token = getAccessToken()
|
|
|
uni.uploadFile({
|
|
|
- //url: BASE_URL + `/file/uploads?businessType=${businessType}`,
|
|
|
- url: BASE_URL + `/common/upload`,
|
|
|
+ url: getBaseUrl() + `/common/upload`,
|
|
|
filePath: filePath,
|
|
|
- name: 'files',
|
|
|
+ name: 'file',
|
|
|
header: {
|
|
|
- 'accesstoken': token ?? ''
|
|
|
+ 'Authorization': 'Bearer ' + token ?? ''
|
|
|
},
|
|
|
success: (res) => {
|
|
|
// 提取属性(any 类型不能直接访问属性)
|
|
|
@@ -85,44 +82,25 @@ export const uploadFile = (filePath: string, businessType: string): Promise<Uplo
|
|
|
if (statusCode == 200) {
|
|
|
try {
|
|
|
const result = JSON.parse(resData) as UTSJSONObject
|
|
|
- const success = result['success'] as boolean | null
|
|
|
- const code = result['code'] as number | null
|
|
|
- const msg = result['msg'] as string | null
|
|
|
- const data = result['data'] as any[] | null
|
|
|
-
|
|
|
- // 判断是否成功
|
|
|
- if (success == true || code == 0) {
|
|
|
- if (data != null && data.length > 0) {
|
|
|
- // 获取第一个文件信息
|
|
|
- const fileInfo = data[0] as UTSJSONObject
|
|
|
- const fileId = fileInfo['fileId'] as string
|
|
|
- const fileName = fileInfo['fileName'] as string
|
|
|
- const filePath = fileInfo['filePath'] as string
|
|
|
- const fileSize = fileInfo['fileSize'] as number
|
|
|
- const fileExt = fileInfo['fileExt'] as string
|
|
|
- const businessType = fileInfo['businessType'] as string
|
|
|
-
|
|
|
- // 构建完整的 URL
|
|
|
- const url = getFileViewUrl(fileId)
|
|
|
-
|
|
|
- // 返回 UploadResponse
|
|
|
- const uploadResult: UploadResponse = {
|
|
|
- url: url,
|
|
|
- fileId: fileId,
|
|
|
- fileName: fileName,
|
|
|
- filePath: filePath,
|
|
|
- fileSize: fileSize,
|
|
|
- fileExt: fileExt,
|
|
|
- businessType: businessType
|
|
|
- }
|
|
|
-
|
|
|
- resolve(uploadResult)
|
|
|
- } else {
|
|
|
- reject(new Error('上传成功但未返回文件信息'))
|
|
|
- }
|
|
|
- } else {
|
|
|
- reject(new Error(msg ?? '上传失败'))
|
|
|
+ // 获取第一个文件信息
|
|
|
+ const fileInfo = result as UTSJSONObject
|
|
|
+ const fileName = fileInfo['fileName'] as string
|
|
|
+
|
|
|
+ // 构建完整的 URL
|
|
|
+ const url = getFileViewUrl(fileName)
|
|
|
+
|
|
|
+ // 返回 UploadResponse
|
|
|
+ const uploadResult: UploadResponse = {
|
|
|
+ url: url,
|
|
|
+ fileId: fileName,
|
|
|
+ fileName: fileName,
|
|
|
+ filePath: filePath,
|
|
|
+ fileSize: 0,
|
|
|
+ fileExt: '',
|
|
|
+ businessType: businessType
|
|
|
}
|
|
|
+
|
|
|
+ resolve(uploadResult)
|
|
|
} catch (e: any) {
|
|
|
console.error('响应数据解析失败:', e)
|
|
|
reject(new Error('响应数据解析失败'))
|