tagging.ts 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. import axios from './instance'
  2. import {
  3. TAGGING_LIST,
  4. DELETE_TAGGING,
  5. INSERT_TAGGING,
  6. UPDATE_TAGGING
  7. } from './constant'
  8. import type { Model } from './model'
  9. interface ServerTagging {
  10. "hotIconId": number,
  11. "hotIconUrl": string,
  12. "getMethod": string,
  13. "getUser": string,
  14. "id": number,
  15. "meta": { "name": string, "url": string } [],
  16. "remark": string,
  17. "tagDescribe": string,
  18. "tagTitle": string,
  19. }
  20. export interface TaggingPosition {
  21. modelId: Model['id']
  22. localPos: SceneLocalPos
  23. }
  24. export interface Tagging {
  25. id: string
  26. styleId: string
  27. title: string,
  28. desc: string
  29. part: string
  30. method: string
  31. principal: string
  32. images: string[],
  33. positions: TaggingPosition[]
  34. }
  35. export type Taggings = Tagging[]
  36. const serviceToLocal = (serviceTagging: ServerTagging): Tagging => ({
  37. id: serviceTagging.id.toString(),
  38. styleId: serviceTagging.hotIconId.toString(),
  39. title: serviceTagging.tagTitle,
  40. desc: serviceTagging.tagDescribe,
  41. part: serviceTagging.remark,
  42. method: serviceTagging.getMethod,
  43. principal: serviceTagging.getUser,
  44. images: serviceTagging.meta.map(({url}) => url),
  45. positions: []
  46. })
  47. const localToService = (tagging: Tagging): ServerTagging => ({
  48. "hotIconId": Number(tagging.styleId),
  49. "hotIconUrl": tagging.styleId,
  50. "getMethod": tagging.method,
  51. "getUser": tagging.principal,
  52. "id": Number(tagging.id),
  53. "meta": tagging.images.map(((item, i) => ({ "name": item, "url": item }))),
  54. "remark": tagging.part,
  55. "tagDescribe": tagging.desc,
  56. "tagTitle": tagging.title,
  57. })
  58. export const fetchTaggings = async () => {
  59. const staggings = await axios.post<ServerTagging[]>(TAGGING_LIST, {})
  60. return staggings.map(serviceToLocal)
  61. }
  62. export const postAddTagging = async (tagging: Tagging) => {
  63. const stagging = await axios.post<ServerTagging>(INSERT_TAGGING, tagging)
  64. return serviceToLocal(stagging)
  65. }
  66. export const postUpdateTagging = (tagging: Tagging) => {
  67. return axios.post<undefined>(UPDATE_TAGGING, localToService(tagging))
  68. }
  69. export const postDeleteTagging = (id: Tagging['id']) => {
  70. return axios.post<undefined>(DELETE_TAGGING, { ids: [id] })
  71. }