tagging-style.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. import axios from "./instance";
  2. import {
  3. TAGGING_STYLE_LIST,
  4. INSERT_TAGGING_STYLE,
  5. DELETE_TAGGING_STYLE,
  6. UPLOAD_HEADS,
  7. TAGGING_STYLE_TREE,
  8. } from "./constant";
  9. import { jsonToForm } from "@/utils";
  10. import { params } from "@/env";
  11. import { fetchMaterialGroups } from "./material";
  12. interface ServiceStyle {
  13. iconId: number;
  14. iconTitle: string;
  15. iconUrl: string;
  16. isSystem: number;
  17. lastUse: 1 | 0;
  18. }
  19. export const defStyleType = {
  20. id: -999,
  21. name: "其他",
  22. dictId: -1
  23. };
  24. export const styleTypes = [
  25. {
  26. id: 1,
  27. name: "痕迹",
  28. dictId: -1,
  29. children: [
  30. { id: 2, name: "手印" },
  31. { id: 3, name: "足迹" },
  32. { id: 4, name: "血迹 " },
  33. { id: 5, name: "尸体" },
  34. { id: 6, name: "其他" },
  35. ],
  36. },
  37. ];
  38. export const getStyleTypeName = (id: number, all: any = styleTypes): string => {
  39. for (const item of all) {
  40. if (id === item.id) {
  41. return item.name;
  42. } else if ("children" in item && item.children) {
  43. const cname = getStyleTypeName(id, item.children);
  44. if (cname) {
  45. return cname;
  46. }
  47. }
  48. }
  49. return "";
  50. };
  51. export const getStyleTypeId = (dictId: number, all: any = styleTypes): number => {
  52. for (const item of all) {
  53. if (dictId === item.dictId) {
  54. return item.id;
  55. } else if ("children" in item && item.children) {
  56. const cid = getStyleTypeId(dictId, item.children);
  57. if (cid) {
  58. return cid;
  59. }
  60. }
  61. }
  62. return -1;
  63. };
  64. export const getStyleDictId = (id: number, all: any = styleTypes): number => {
  65. return __map[id]
  66. };
  67. export interface TaggingStyle {
  68. id: string;
  69. icon: string;
  70. typeId: number;
  71. lastUse: 1 | 0;
  72. default: boolean;
  73. }
  74. const toLocal = (serviceStyle: ServiceStyle): TaggingStyle => {
  75. return {
  76. id: serviceStyle.iconId.toString(),
  77. lastUse: serviceStyle.lastUse,
  78. typeId: Number(serviceStyle.iconTitle),
  79. icon: serviceStyle.iconUrl,
  80. default: Boolean(serviceStyle.isSystem),
  81. };
  82. };
  83. const toService = (style: TaggingStyle): ServiceStyle => ({
  84. iconId: Number(style.id),
  85. iconTitle: style.typeId.toString(),
  86. lastUse: style.lastUse,
  87. iconUrl: style.icon,
  88. isSystem: Number(style.default),
  89. });
  90. export type TaggingStyles = TaggingStyle[];
  91. const __map: Record<string, number> = {}
  92. export const fetchTaggingStyles = async () => {
  93. const reqParams = params.share ? { fusionId: params.caseId } : {};
  94. const treeData = await axios.get<any>(TAGGING_STYLE_TREE, {
  95. params: reqParams,
  96. });
  97. const styles: any[] = [];
  98. const genTree = (tree: any, parent?: any) => {
  99. for (const item of tree) {
  100. if (item.iconUrl) {
  101. delete parent.children;
  102. __map[item.iconId] = item.dictId || parent.dictId
  103. styles.push(toLocal({ ...item, iconTitle: parent.id }));
  104. } else {
  105. const data = {
  106. id: item.iconId,
  107. dictId: item.dictId,
  108. name: item.iconTitle,
  109. children: [],
  110. };
  111. parent.children.push(data);
  112. genTree(item.childrenList, data)
  113. }
  114. }
  115. };
  116. const tree: any = { children: [] };
  117. genTree(treeData, tree);
  118. styleTypes.length = 0
  119. styleTypes.push(...tree.children)
  120. Object.assign(defStyleType, tree.children[tree.children.length - 1])
  121. console.error('StyleT', styleTypes)
  122. // const data = await axios.get<ServiceStyle[]>(TAGGING_STYLE_LIST, {
  123. // params: reqParams,
  124. // });
  125. return styles;
  126. };
  127. export const postAddTaggingStyle = async (props: {
  128. file: Blob;
  129. iconTitle: string;
  130. }) => {
  131. const data = await axios<ServiceStyle>({
  132. method: "POST",
  133. headers: UPLOAD_HEADS,
  134. url: INSERT_TAGGING_STYLE,
  135. data: jsonToForm({
  136. file: new File([props.file], `${props.iconTitle}.png`),
  137. iconTitle: props.iconTitle.toString(),
  138. parentId: props.iconTitle,
  139. fusionId: params.caseId,
  140. }),
  141. });
  142. return toLocal(data);
  143. };
  144. export const postDeleteTaggingStyle = async (id: TaggingStyle["id"]) => {
  145. await axios.post(DELETE_TAGGING_STYLE, { iconId: Number(id) });
  146. };