初始化工程
This commit is contained in:
74
src/services/commonApi.ts
Normal file
74
src/services/commonApi.ts
Normal file
@@ -0,0 +1,74 @@
|
||||
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) {
|
||||
throw error
|
||||
} finally {
|
||||
Taro.hideLoading()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// ==================== 用户信息接口(基础版本) ====================
|
||||
|
||||
// 更新用户信息
|
||||
async updateUserProfile(data: any): Promise<ApiResponse<any>> {
|
||||
return httpService.put('/user/profile', data, {
|
||||
showLoading: true,
|
||||
loadingText: '保存中...'
|
||||
})
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// 导出通用API服务实例
|
||||
export default new CommonApiService()
|
||||
Reference in New Issue
Block a user