import companyService from "@/api/company-service"; import EnterpriseHost from "./enterprise-host"; class DepartmentController { private curPoid = ""; private departments4tree = []; private departments = [] public getDepartments(reload = false) { const poid = EnterpriseHost.getPoid(); if (!reload && this.curPoid === poid && this.departments.length > 0) { const p = { departments4tree: this.departments4tree, departments: this.departments }; return Promise.resolve(p); } return new Promise((resolve, reject) => { this.curPoid = poid; companyService.getDepartment(poid).then((res: any) => { const ary = res.departments; this.departments = res.departments; const r = this.parseMarksTree(ary); this.departments4tree = r; const p = { departments4tree: this.departments4tree, departments: this.departments }; resolve(p); }).catch(reject); }); } public reloadDepartments() { this.getDepartments(true); } private parseMarksTree(ary: any[], pid = '0'): any { if (!Array.isArray(ary)) { return null; } const marks = ary.filter(item => item.parentid === pid); return marks.map(item => { const children = this.parseMarksTree(ary, item.id); if (children && children.length > 0) { return { id: item.id, name: item.name, name_pinyin: item.name_pinyin, parentid: item.parentid, order: item.order, children: children }; } else { return { id: item.id, name: item.name, name_pinyin: item.name_pinyin, order: item.order, parentid: item.parentid, }; } }); } } const departmentController = new DepartmentController(); export { departmentController };