80 lines
2.1 KiB
TypeScript
80 lines
2.1 KiB
TypeScript
import Taro from '@tarojs/taro'
|
|
import httpService from './httpService'
|
|
import type { ApiResponse } from './httpService'
|
|
|
|
// 图片上传响应
|
|
export interface UploadResponse {
|
|
url: string
|
|
filename: string
|
|
size: number
|
|
}
|
|
|
|
// 通用API服务类
|
|
class CommonApiService {
|
|
// ==================== 文件上传接口 ====================
|
|
|
|
// 上传单个图片
|
|
async uploadImage(filePath: string): Promise<ApiResponse<UploadResponse>> {
|
|
const uploadTask = Taro.uploadFile({
|
|
url: `${httpService['baseURL']}/upload/image`,
|
|
filePath,
|
|
name: 'file',
|
|
header: {
|
|
...httpService['buildHeaders']({ url: '', needAuth: true })
|
|
}
|
|
})
|
|
|
|
return new Promise((resolve, reject) => {
|
|
uploadTask.then(response => {
|
|
try {
|
|
const data = JSON.parse(response.data)
|
|
resolve(data)
|
|
} catch (error) {
|
|
reject(new Error('上传响应解析失败'))
|
|
}
|
|
}).catch(reject)
|
|
})
|
|
}
|
|
|
|
// 批量上传图片
|
|
async uploadImages(filePaths: string[]): Promise<ApiResponse<UploadResponse[]>> {
|
|
try {
|
|
Taro.showLoading({ title: '上传图片中...', mask: true })
|
|
|
|
const uploadPromises = filePaths.map(filePath => this.uploadImage(filePath))
|
|
const results = await Promise.all(uploadPromises)
|
|
|
|
return {
|
|
code: 200,
|
|
success: true,
|
|
message: '上传成功',
|
|
data: results.map(result => result.data)
|
|
}
|
|
} catch (error) {
|
|
} finally {
|
|
Taro.hideLoading()
|
|
}
|
|
}
|
|
|
|
|
|
// ==================== 用户信息接口(基础版本) ====================
|
|
|
|
// 更新用户信息
|
|
async updateUserProfile(data: any): Promise<ApiResponse<any>> {
|
|
return httpService.put('/user/profile', data, {
|
|
showLoading: true,
|
|
loadingText: '保存中...'
|
|
})
|
|
}
|
|
|
|
// 获取字典数据
|
|
async getDictionaryManyKey(keys: string): Promise<ApiResponse<any>> {
|
|
return httpService.post('/parameter/many_key', { keys }, {
|
|
showLoading: false,
|
|
})
|
|
}
|
|
|
|
}
|
|
|
|
// 导出通用API服务实例
|
|
export default new CommonApiService()
|