聊天室创建工单
This commit is contained in:
@@ -7,6 +7,7 @@
|
||||
declare const uni: {
|
||||
getStorageSync: (key: string) => any
|
||||
request: (options: any) => void
|
||||
uploadFile: (options: any) => void
|
||||
}
|
||||
|
||||
import type { ResultDomain } from '../types'
|
||||
@@ -46,4 +47,40 @@ export function request<T>(options: {
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
// 文件上传方法
|
||||
export function uploadFile<T>(options: {
|
||||
url: string
|
||||
filePath: string
|
||||
name?: string
|
||||
formData?: Record<string, any>
|
||||
header?: Record<string, string>
|
||||
}): Promise<ResultDomain<T>> {
|
||||
return new Promise((resolve, reject) => {
|
||||
const token = uni.getStorageSync('token') as string
|
||||
uni.uploadFile({
|
||||
url: BASE_URL + options.url,
|
||||
filePath: options.filePath,
|
||||
name: options.name || 'file',
|
||||
formData: options.formData,
|
||||
header: {
|
||||
...(token ? { 'Authorization': `Bearer ${token}` } : {}),
|
||||
...options.header
|
||||
},
|
||||
success: (res: any) => {
|
||||
try {
|
||||
if (res.statusCode === 200) {
|
||||
const result = typeof res.data === 'string' ? JSON.parse(res.data) : res.data
|
||||
resolve(result as ResultDomain<T>)
|
||||
} else {
|
||||
reject(new Error(`上传失败: ${res.statusCode}`))
|
||||
}
|
||||
} catch (error) {
|
||||
reject(new Error('解析响应失败'))
|
||||
}
|
||||
},
|
||||
fail: (err: any) => {
|
||||
reject(err)
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user