创建项目
This commit is contained in:
209
Web/src/services/commonSERVICE.ts
Normal file
209
Web/src/services/commonSERVICE.ts
Normal file
@@ -0,0 +1,209 @@
|
||||
/**
|
||||
* 通用API服务
|
||||
* 提供字典查询、文件上传等通用接口
|
||||
*/
|
||||
import type { IDictParams, IDictResponse, IUploadParams } from '~/types/api'
|
||||
import type { IUploadResponse, IPageResponse, IPageParams } from '~/types/common'
|
||||
import { RequestEXTEND } from '~/extends/requestEXTEND'
|
||||
|
||||
export class CommonSERVICE {
|
||||
// 私有属性:请求工具实例
|
||||
private request: RequestEXTEND
|
||||
|
||||
/**
|
||||
* 构造函数:初始化请求工具
|
||||
* @param config 自定义请求配置(可选)
|
||||
*/
|
||||
constructor(config?: { timeout?: number; headers?: Record<string, string>; baseURL?: string }) {
|
||||
this.request = new RequestEXTEND(config)
|
||||
}
|
||||
|
||||
/**
|
||||
* 字典查询接口
|
||||
* @param type 字典类型
|
||||
* @returns 字典项列表
|
||||
*/
|
||||
async getDict(type: string): Promise<IDictResponse['data']> {
|
||||
try {
|
||||
const response = await this.request.get<IDictResponse>('/api/common/dict', {
|
||||
params: { type }
|
||||
})
|
||||
if (response.code !== 200 && response.code !== 0) {
|
||||
throw new Error(response.message || '获取字典失败')
|
||||
}
|
||||
return response.data || []
|
||||
} catch (error) {
|
||||
console.error('获取字典接口异常:', error)
|
||||
return []
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 文件上传接口
|
||||
* @param file 要上传的文件
|
||||
* @returns 上传后的文件信息
|
||||
*/
|
||||
async upload(file: File): Promise<IUploadResponse['data']> {
|
||||
try {
|
||||
const formData = new FormData()
|
||||
formData.append('file', file)
|
||||
const response = await this.request.post<IUploadResponse>('/api/common/upload', formData)
|
||||
if (response.code !== 200 && response.code !== 0) {
|
||||
throw new Error(response.message || '文件上传失败')
|
||||
}
|
||||
return response.data
|
||||
} catch (error) {
|
||||
console.error('文件上传接口异常:', error)
|
||||
throw error
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 多文件上传接口
|
||||
* @param files 要上传的文件数组
|
||||
* @returns 上传后的文件信息列表
|
||||
*/
|
||||
async uploadMultiple(files: File[]): Promise<IUploadResponse['data'][]> {
|
||||
try {
|
||||
const formData = new FormData()
|
||||
files.forEach((file, index) => {
|
||||
formData.append(`files_${index}`, file)
|
||||
})
|
||||
const response = await this.request.post<{ code: number; message: string; data: IUploadResponse['data'][] }>(
|
||||
'/api/common/uploadMultiple',
|
||||
formData
|
||||
)
|
||||
if (response.code !== 200 && response.code !== 0) {
|
||||
throw new Error(response.message || '文件上传失败')
|
||||
}
|
||||
return response.data || []
|
||||
} catch (error) {
|
||||
console.error('多文件上传接口异常:', error)
|
||||
throw error
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取上传进度(需要自行实现)
|
||||
* @param file 要上传的文件
|
||||
* @param onProgress 上传进度回调
|
||||
* @returns 上传后的文件信息
|
||||
*/
|
||||
async uploadWithProgress(
|
||||
file: File,
|
||||
onProgress: (percent: number) => void
|
||||
): Promise<IUploadResponse['data']> {
|
||||
return new Promise((resolve, reject) => {
|
||||
const xhr = new XMLHttpRequest()
|
||||
const formData = new FormData()
|
||||
formData.append('file', file)
|
||||
|
||||
xhr.upload.addEventListener('progress', (event) => {
|
||||
if (event.lengthComputable) {
|
||||
const percent = Math.round((event.loaded / event.total) * 100)
|
||||
onProgress(percent)
|
||||
}
|
||||
})
|
||||
|
||||
xhr.addEventListener('load', () => {
|
||||
if (xhr.status >= 200 && xhr.status < 300) {
|
||||
try {
|
||||
const response = JSON.parse(xhr.responseText)
|
||||
if (response.code === 200 || response.code === 0) {
|
||||
resolve(response.data)
|
||||
} else {
|
||||
reject(new Error(response.message || '文件上传失败'))
|
||||
}
|
||||
} catch (error) {
|
||||
reject(error)
|
||||
}
|
||||
} else {
|
||||
reject(new Error('文件上传失败'))
|
||||
}
|
||||
})
|
||||
|
||||
xhr.addEventListener('error', () => {
|
||||
reject(new Error('文件上传失败'))
|
||||
})
|
||||
|
||||
xhr.open('POST', '/api/common/upload')
|
||||
xhr.send(formData)
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取配置信息
|
||||
* @param key 配置键
|
||||
* @returns 配置值
|
||||
*/
|
||||
async getConfig(key: string): Promise<string | null> {
|
||||
try {
|
||||
const response = await this.request.get<{ code: number; message: string; data: { value: string } }>(
|
||||
'/api/common/config',
|
||||
{ params: { key } }
|
||||
)
|
||||
if (response.code === 200 || response.code === 0) {
|
||||
return response.data?.value || null
|
||||
}
|
||||
return null
|
||||
} catch (error) {
|
||||
console.error('获取配置接口异常:', error)
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取应用版本信息
|
||||
* @returns 版本信息
|
||||
*/
|
||||
async getVersion(): Promise<{ version: string; buildTime: string }> {
|
||||
try {
|
||||
const response = await this.request.get<{ code: number; message: string; data: { version: string; buildTime: string } }>('/api/common/version')
|
||||
if (response.code === 200 || response.code === 0) {
|
||||
return response.data || { version: '1.0.0', buildTime: '' }
|
||||
}
|
||||
return { version: '1.0.0', buildTime: '' }
|
||||
} catch (error) {
|
||||
console.error('获取版本信息接口异常:', error)
|
||||
return { version: '1.0.0', buildTime: '' }
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送短信验证码
|
||||
* @param phone 手机号
|
||||
* @param type 验证码类型(login/register/reset)
|
||||
* @returns 发送结果
|
||||
*/
|
||||
async sendSmsCode(phone: string, type: string = 'login'): Promise<boolean> {
|
||||
try {
|
||||
const response = await this.request.post<{ code: number; message: string }>('/api/common/sms/send', {
|
||||
phone,
|
||||
type
|
||||
})
|
||||
return response.code === 200 || response.code === 0
|
||||
} catch (error) {
|
||||
console.error('发送短信验证码接口异常:', error)
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 验证短信验证码
|
||||
* @param phone 手机号
|
||||
* @param code 验证码
|
||||
* @returns 验证结果
|
||||
*/
|
||||
async verifySmsCode(phone: string, code: string): Promise<boolean> {
|
||||
try {
|
||||
const response = await this.request.post<{ code: number; message: string }>('/api/common/sms/verify', {
|
||||
phone,
|
||||
code
|
||||
})
|
||||
return response.code === 200 || response.code === 0
|
||||
} catch (error) {
|
||||
console.error('验证短信验证码接口异常:', error)
|
||||
return false
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user