tagging-style.ts 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. import axios from './instance'
  2. import { TAGGING_STYLE_LIST, INSERT_TAGGING_STYLE, DELETE_TAGGING_STYLE, UPLOAD_HEADS } from './constant'
  3. import { jsonToForm } from '@/utils'
  4. import { params } from '@/env'
  5. interface ServiceStyle {
  6. iconId: number,
  7. iconTitle: string,
  8. iconUrl: string,
  9. isSystem: number
  10. lastUse: 1 | 0
  11. }
  12. export interface TaggingStyle {
  13. id: string
  14. icon: string
  15. name: string
  16. lastUse: 1 | 0
  17. default: boolean
  18. }
  19. const toLocal = (serviceStyle: ServiceStyle) : TaggingStyle => ({
  20. id: serviceStyle.iconId.toString(),
  21. lastUse: serviceStyle.lastUse,
  22. name: serviceStyle.iconTitle,
  23. icon: serviceStyle.iconUrl,
  24. default: Boolean(serviceStyle.isSystem)
  25. })
  26. const toService = (style: TaggingStyle): ServiceStyle => ({
  27. iconId: Number(style.id),
  28. iconTitle: style.name,
  29. lastUse: style.lastUse,
  30. iconUrl: style.icon,
  31. isSystem: Number(style.default)
  32. })
  33. export type TaggingStyles = TaggingStyle[]
  34. export const fetchTaggingStyles = async () => {
  35. const reqParams = params.share ? { caseId: params.caseId } : { }
  36. const data = await axios.get<ServiceStyle[]>(TAGGING_STYLE_LIST, { params: reqParams })
  37. return data.map(toLocal)
  38. }
  39. export const postAddTaggingStyle = async (props: {file: Blob, iconTitle: string}) => {
  40. const data = await axios<ServiceStyle>({
  41. method: 'POST',
  42. headers: UPLOAD_HEADS,
  43. url: INSERT_TAGGING_STYLE,
  44. data: jsonToForm({
  45. file: new File([props.file], `${props.iconTitle}.png`),
  46. iconTitle: props.iconTitle,
  47. caseId: params.caseId
  48. })
  49. })
  50. return toLocal(data)
  51. }
  52. export const postDeleteTaggingStyle = async (id: TaggingStyle['id']) => {
  53. await axios.post(DELETE_TAGGING_STYLE, { iconId: Number(id) })
  54. }