tagging-style.ts 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. import axios from "./instance";
  2. import {
  3. TAGGING_STYLE_LIST,
  4. INSERT_TAGGING_STYLE,
  5. DELETE_TAGGING_STYLE,
  6. UPLOAD_HEADS,
  7. } from "./constant";
  8. import { jsonToForm } from "@/utils";
  9. import { params } from "@/env";
  10. interface ServiceStyle {
  11. iconId: number;
  12. iconTitle: string;
  13. iconUrl: string;
  14. isSystem: number;
  15. lastUse: 1 | 0;
  16. }
  17. export const defStyleType = {
  18. id: 8,
  19. name: "其他",
  20. }
  21. export const styleTypes = [
  22. {
  23. id: 1,
  24. name: "痕迹",
  25. children: [
  26. { id: 2, name: "手印" },
  27. { id: 3, name: "足迹" },
  28. { id: 4, name: "血迹 " },
  29. { id: 5, name: "尸体" },
  30. { id: 6, name: "其他" },
  31. ],
  32. },
  33. {
  34. id: 7,
  35. name: "物证",
  36. },
  37. defStyleType
  38. ];
  39. export const getStyleTypeName = (id: number, all = styleTypes): string => {
  40. for (const item of all) {
  41. if (id === item.id) {
  42. return item.name
  43. } else if ('children' in item && item.children) {
  44. const cname = getStyleTypeName(id, item.children)
  45. if (cname) {
  46. return cname
  47. }
  48. }
  49. }
  50. if (all === styleTypes) {
  51. return defStyleType.name
  52. } else {
  53. return ''
  54. }
  55. }
  56. export interface TaggingStyle {
  57. id: string;
  58. icon: string;
  59. typeId: number;
  60. lastUse: 1 | 0;
  61. default: boolean;
  62. }
  63. const toLocal = (serviceStyle: ServiceStyle): TaggingStyle => ({
  64. id: serviceStyle.iconId.toString(),
  65. lastUse: serviceStyle.lastUse,
  66. typeId: Number(serviceStyle.iconTitle) || defStyleType.id,
  67. icon: serviceStyle.iconUrl,
  68. default: Boolean(serviceStyle.isSystem),
  69. });
  70. const toService = (style: TaggingStyle): ServiceStyle => ({
  71. iconId: Number(style.id),
  72. iconTitle: style.typeId.toString(),
  73. lastUse: style.lastUse,
  74. iconUrl: style.icon,
  75. isSystem: Number(style.default),
  76. });
  77. export type TaggingStyles = TaggingStyle[];
  78. export const fetchTaggingStyles = async () => {
  79. const reqParams = params.share ? { caseId: params.caseId } : {};
  80. const data = await axios.get<ServiceStyle[]>(TAGGING_STYLE_LIST, {
  81. params: reqParams,
  82. });
  83. return data.map(toLocal);
  84. };
  85. export const postAddTaggingStyle = async (props: {
  86. file: Blob;
  87. iconTitle: string;
  88. }) => {
  89. const data = await axios<ServiceStyle>({
  90. method: "POST",
  91. headers: UPLOAD_HEADS,
  92. url: INSERT_TAGGING_STYLE,
  93. data: jsonToForm({
  94. file: new File([props.file], `${props.iconTitle}.png`),
  95. iconTitle: props.iconTitle.toString(),
  96. caseId: params.caseId,
  97. }),
  98. });
  99. return toLocal(data);
  100. };
  101. export const postDeleteTaggingStyle = async (id: TaggingStyle["id"]) => {
  102. await axios.post(DELETE_TAGGING_STYLE, { iconId: Number(id) });
  103. };