117 lines
3.9 KiB
TypeScript
117 lines
3.9 KiB
TypeScript
import Taro from '@tarojs/taro'
|
||
import envConfig from '@/config/env'
|
||
import { API_CONFIG } from '@/config/api'
|
||
import httpService from './httpService'
|
||
import tokenManager from '../utils/tokenManager'
|
||
|
||
// 用户接口
|
||
export interface UploadFilesData {
|
||
id: string,
|
||
filePath: string,
|
||
description?: string,
|
||
tags?: string,
|
||
is_public?: 0 | 1,
|
||
}
|
||
|
||
// {"code":0,"message":"请求成功!","data":{"tags":["test"],"create_time":"2025-08-24 22:51:03","last_modify_time":"2025-08-24 22:51:03","duration":"0","thumbnail_url":"","view_count":"0","download_count":"0","id":16,"user_id":1,"resource_type":"image","file_name":"front/ball/images/63f56978-ffe9-4397-b897-8aca6f4fdcd8.png","original_name":"QyoUvEsLG6ci57c7e25cca0845dafed3ee1fde07876d.png","file_path":"http://bimwe.oss-cn-shanghai.aliyuncs.com/front/ball/images/63f56978-ffe9-4397-b897-8aca6f4fdcd8.png","file_url":"http://bimwe.oss-cn-shanghai.aliyuncs.com/front/ball/images/63f56978-ffe9-4397-b897-8aca6f4fdcd8.png","file_size":17756,"mime_type":"image/png","description":"test","is_public":"1","status":"active","width":0,"height":0,"uploadInfo":{"success":true,"name":"front/ball/images/63f56978-ffe9-4397-b897-8aca6f4fdcd8.png","path":"http://bimwe.oss-cn-shanghai.aliyuncs.com/front/ball/images/63f56978-ffe9-4397-b897-8aca6f4fdcd8.png","ossPath":"http://bimwe.oss-cn-shanghai.aliyuncs.com/front/ball/images/63f56978-ffe9-4397-b897-8aca6f4fdcd8.png","fileType":"image/png","fileSize":17756,"originalName":"QyoUvEsLG6ci57c7e25cca0845dafed3ee1fde07876d.png","suffix":"png","storagePath":"front/ball/images/63f56978-ffe9-4397-b897-8aca6f4fdcd8.png"}}}
|
||
|
||
export interface uploadFileResponse {
|
||
code: number,
|
||
message: string,
|
||
data: uploadFileResponseData,
|
||
}
|
||
|
||
export interface uploadFileResponseData {
|
||
id: number,
|
||
user_id: number,
|
||
file_name: string,
|
||
original_name: string,
|
||
file_path: string,
|
||
file_url: string,
|
||
file_size: number,
|
||
resource_type: string,
|
||
mime_type: string,
|
||
description: string,
|
||
tags: string[],
|
||
is_public: string,
|
||
view_count: number,
|
||
download_count: number,
|
||
created_at: string,
|
||
updated_at: string,
|
||
}
|
||
|
||
// 发布球局类
|
||
class UploadApi {
|
||
async upload(req: UploadFilesData): Promise<{ id: string, data: uploadFileResponseData }> {
|
||
// return httpService.post('/files/upload', req, {
|
||
// showLoading: true,
|
||
// })
|
||
|
||
let fullUrl = `${envConfig.apiBaseURL}/api/gallery/upload`
|
||
// 后门id,用于调试
|
||
let userid = httpService.getHashParam("userIdTest")
|
||
if (userid) {
|
||
if (fullUrl.indexOf("?") > -1) {
|
||
fullUrl += `&userIdTest45=${userid}`
|
||
}
|
||
else {
|
||
fullUrl += `?userIdTest45=${userid}`
|
||
}
|
||
}
|
||
|
||
const { id, ...rest } = req
|
||
return Taro.uploadFile({
|
||
url: fullUrl,
|
||
filePath: rest.filePath,
|
||
name: 'file',
|
||
formData: {
|
||
description: rest.description,
|
||
tags: rest.tags,
|
||
is_public: rest.is_public,
|
||
}
|
||
}).then(res => {
|
||
return {
|
||
id,
|
||
data: JSON.parse(res.data).data,
|
||
}
|
||
})
|
||
}
|
||
|
||
async batchUpload(req: UploadFilesData[]): Promise<{ id: string, data: uploadFileResponseData }[]> {
|
||
return Promise.all(req.map(item => this.upload(item)))
|
||
}
|
||
|
||
// 上传单张图片到OSS
|
||
async upload_oss_img(file_path: string): Promise<any> {
|
||
try {
|
||
let fullUrl = `${envConfig.apiBaseURL}/api${API_CONFIG.UPLOAD.OSS_IMG}`
|
||
|
||
const authHeader = tokenManager.getAuthHeader()
|
||
|
||
const response = await Taro.uploadFile({
|
||
url: fullUrl,
|
||
filePath: file_path,
|
||
name: 'file',
|
||
header: authHeader,
|
||
|
||
});
|
||
|
||
|
||
const result = JSON.parse(response.data);
|
||
|
||
if (result.code === 0) {
|
||
return result.data;
|
||
} else {
|
||
throw new Error(result.message || '上传失败');
|
||
}
|
||
} catch (error) {
|
||
console.error('上传图片失败:', error);
|
||
throw error;
|
||
}
|
||
}
|
||
|
||
|
||
}
|
||
|
||
// 导出认证服务实例
|
||
export default new UploadApi() |