tagging-style.ts 2.7 KB

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