tagging-style.ts 4.3 KB

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