tagging-style.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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 = (dictId: number, all: any = styleTypes): number => {
  56. for (const item of all) {
  57. if (dictId === item.dictId) {
  58. return item.id;
  59. } else if ("children" in item && item.children) {
  60. const cid = getStyleTypeId(dictId, item.children);
  61. if (cid) {
  62. return cid;
  63. }
  64. }
  65. }
  66. return -1;
  67. };
  68. export const getStyleDictId = (id: number, all: any = styleTypes): number => {
  69. return __map[id]
  70. };
  71. export interface TaggingStyle {
  72. id: string;
  73. icon: string;
  74. typeId: number;
  75. lastUse: 1 | 0;
  76. default: boolean;
  77. }
  78. const toLocal = (serviceStyle: ServiceStyle): TaggingStyle => {
  79. return {
  80. id: serviceStyle.iconId.toString(),
  81. lastUse: serviceStyle.lastUse,
  82. typeId: Number(serviceStyle.iconTitle),
  83. icon: serviceStyle.iconUrl,
  84. default: Boolean(serviceStyle.isSystem),
  85. };
  86. };
  87. const toService = (style: TaggingStyle): ServiceStyle => ({
  88. iconId: Number(style.id),
  89. iconTitle: style.typeId.toString(),
  90. lastUse: style.lastUse,
  91. iconUrl: style.icon,
  92. isSystem: Number(style.default),
  93. });
  94. export type TaggingStyles = TaggingStyle[];
  95. const __map: Record<string, number> = {}
  96. export const fetchTaggingStyles = async () => {
  97. const reqParams = params.share ? { fusionId: params.caseId } : {};
  98. const treeData = await axios.get<any>(TAGGING_STYLE_TREE, {
  99. params: reqParams,
  100. });
  101. const styles: any[] = [];
  102. const genTree = (tree: any, parent?: any) => {
  103. for (const item of tree) {
  104. if (item.iconUrl) {
  105. delete parent.children;
  106. __map[item.iconId] = item.dictId || parent.dictId
  107. styles.push(toLocal({ ...item, iconTitle: parent.id }));
  108. } else {
  109. const data = {
  110. id: item.iconId,
  111. dictId: item.dictId,
  112. name: item.iconTitle,
  113. children: [],
  114. };
  115. parent.children.push(data);
  116. genTree(item.childrenList, data)
  117. }
  118. }
  119. };
  120. const tree: any = { children: [] };
  121. genTree(treeData, tree);
  122. styleTypes.length = 0
  123. styleTypes.push(...tree.children)
  124. Object.assign(defStyleType, tree.children[tree.children.length - 1])
  125. console.error('StyleT', styleTypes)
  126. // const data = await axios.get<ServiceStyle[]>(TAGGING_STYLE_LIST, {
  127. // params: reqParams,
  128. // });
  129. return styles;
  130. };
  131. export const postAddTaggingStyle = async (props: {
  132. file: Blob;
  133. iconTitle: string;
  134. }) => {
  135. const data = await axios<ServiceStyle>({
  136. method: "POST",
  137. headers: UPLOAD_HEADS,
  138. url: INSERT_TAGGING_STYLE,
  139. data: jsonToForm({
  140. file: new File([props.file], `${props.iconTitle}.png`),
  141. iconTitle: props.iconTitle.toString(),
  142. parentId: props.iconTitle,
  143. fusionId: params.caseId,
  144. }),
  145. });
  146. return toLocal(data);
  147. };
  148. export const postDeleteTaggingStyle = async (id: TaggingStyle["id"]) => {
  149. await axios.post(DELETE_TAGGING_STYLE, { iconId: Number(id) });
  150. };