accessory.ts 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import type { AxiosResponse } from 'axios'
  2. import download from 'js-file-download'
  3. import config from '@/config'
  4. import request from '@/utils/request'
  5. import type { Accessory } from '@/types/accessory'
  6. import { SuccessResultCode, type Result } from '@cacp/ui'
  7. const contextPath = '/accessory'
  8. export function getUploadUrl(): string {
  9. if (config.SERVICE_API.endsWith('/')) {
  10. return `${config.SERVICE_API.substring(0, config.SERVICE_API.length - 1)}${contextPath}/upload-file`
  11. } else {
  12. return `${config.SERVICE_API}/${contextPath}/upload-file`
  13. }
  14. }
  15. export function getDownloadUrl(accessoryId: string): string {
  16. if (config.SERVICE_API.endsWith('/')) {
  17. return `${config.SERVICE_API.substring(0, config.SERVICE_API.length - 1)}${contextPath}/download-file?accessoryId=${accessoryId}`
  18. } else {
  19. return `${config.SERVICE_API}${contextPath}/download-file?accessoryId=${accessoryId}`
  20. }
  21. }
  22. export async function downloadFile(accessoryId: string, fileName: string): Promise<void> {
  23. const res: AxiosResponse<Result<any>> = await request.get(`${contextPath}/download-file`, {
  24. params: { accessoryId: accessoryId },
  25. responseType: 'arraybuffer'
  26. })
  27. if (res.data.code === SuccessResultCode) {
  28. download(res.data.data, fileName, 'application/octet-stream')
  29. }
  30. }
  31. export async function deleteFile(accessoryId: string): Promise<Result<number>> {
  32. const res: AxiosResponse<Result<number>> = await request.post(`${contextPath}/delete-file`, {
  33. params: { accessoryId: accessoryId }
  34. })
  35. return res.data
  36. }
  37. export async function getPersistListByBizId(bizId: string): Promise<Result<Array<Accessory>>> {
  38. const res: AxiosResponse<Result<Array<Accessory>>> = await request.get(`${contextPath}/get-persist-list-by-biz-id`, {
  39. params: { bizId: bizId }
  40. })
  41. return res.data
  42. }
  43. export async function getPersistListByRelId(bizId: string, relId: string): Promise<Result<Array<Accessory>>> {
  44. const res: AxiosResponse<Result<Array<Accessory>>> = await request.get(`${contextPath}/get-persist-list-by-rel-id`, {
  45. params: { bizId: bizId, relId: relId }
  46. })
  47. return res.data
  48. }