import axios from "./instance"; import { TAGGING_STYLE_LIST, INSERT_TAGGING_STYLE, DELETE_TAGGING_STYLE, UPLOAD_HEADS, TAGGING_STYLE_TREE, } from "./constant"; import { jsonToForm } from "@/utils"; import { params } from "@/env"; import { ui18n } from "@/lang"; import { fetchMaterialGroups } from "./material"; interface ServiceStyle { iconId: number; iconTitle: string; iconUrl: string; isSystem: number; lastUse: 1 | 0; } export const defStyleType = { id: 8, name: ui18n.t("sys.other"), }; export const styleTypes = [ { id: 1, name: ui18n.t("tagging.type.1"), children: [ { id: 2, name: ui18n.t("tagging.type.2") }, { id: 3, name: ui18n.t("tagging.type.3") }, { id: 4, name: ui18n.t("tagging.type.4") }, { id: 5, name: ui18n.t("tagging.type.5") }, { id: 6, name: ui18n.t("tagging.type.6") }, ], }, { id: 7, name: ui18n.t("tagging.type.7"), }, defStyleType, ]; export const getStyleTypeName = (id: number, all: any = styleTypes): string => { for (const item of all) { if (id === item.id) { return item.name; } else if ("children" in item && item.children) { const cname = getStyleTypeName(id, item.children); if (cname) { return cname; } } } return ""; }; export const getStyleTypeId = ( dictId: number, all: any = styleTypes ): number => { for (const item of all) { if (dictId === item.dictId) { return item.id; } else if ("children" in item && item.children) { const cid = getStyleTypeId(dictId, item.children); if (cid) { return cid; } } } return -1; }; export const getStyleDictId = (id: number, all: any = styleTypes): number => { return __map[id]; }; export interface TaggingStyle { id: string; icon: string; typeId: number; lastUse: 1 | 0; default: boolean; } const toLocal = (serviceStyle: ServiceStyle): TaggingStyle => { return { id: serviceStyle.iconId.toString(), lastUse: serviceStyle.lastUse, typeId: Number(serviceStyle.iconTitle), icon: serviceStyle.iconUrl, default: Boolean(serviceStyle.isSystem), }; }; const toService = (style: TaggingStyle): ServiceStyle => ({ iconId: Number(style.id), iconTitle: style.typeId.toString(), lastUse: style.lastUse, iconUrl: style.icon, isSystem: Number(style.default), }); export type TaggingStyles = TaggingStyle[]; const __map: Record = {}; export const fetchTaggingStyles = async () => { const reqParams = params.share ? { fusionId: params.caseId } : {}; let treeData = await axios.get(TAGGING_STYLE_TREE, { params: reqParams, }); const styles: any[] = []; const genTree = (tree: any, parent?: any, level = 0) => { for (const item of tree) { if (item.iconUrl) { if (level !== 0) { // parent.children.filter(item => item.) // console.log(parent.children) // delete parent.children; } __map[item.iconId] = item.dictId || parent.dictId; console.error(parent.id) styles.push(toLocal({ ...item, iconTitle: parent.id })); } else { const data = { id: item.iconId, dictId: item.dictId, name: ui18n.t(`sys.${item.iconTitle}`), children: [], }; parent.children.push(data); genTree(item.childrenList, data, ++level); } } }; const tree: any = { children: [] }; genTree(treeData, tree); console.error("treeData", tree, styles); styleTypes.length = 0; styleTypes.push(...tree.children) Object.assign(defStyleType, tree.children[tree.children.length - 1]); console.error("StyleT", styleTypes); // const data = await axios.get(TAGGING_STYLE_LIST, { // params: reqParams, // }); return styles; }; export const postAddTaggingStyle = async (props: { file: Blob; iconTitle: string; }) => { const data = await axios({ method: "POST", headers: UPLOAD_HEADS, url: INSERT_TAGGING_STYLE, data: jsonToForm({ file: new File([props.file], `${props.iconTitle}.png`), iconTitle: props.iconTitle.toString(), parentId: props.iconTitle, fusionId: params.caseId, }), }); return toLocal(data); }; export const postDeleteTaggingStyle = async (id: TaggingStyle["id"]) => { await axios.post(DELETE_TAGGING_STYLE, { iconId: Number(id) }); };