import { HttpResponse } from '@angular/common/http' import { AnyObject } from 'app/shared/components/server-paginated-table' import { NzTreeNodeOptions } from 'ng-zorro-antd/tree' export * from './form' export * from './storage' export class Utils { static flatListToTree(data: any[]): any[] { const res: any[] = [] const map = data.reduce((a, c) => { a[c.id] = c return a }, Object.create(null)) for (let i of data) { if (i.parentId) { if (map[i.parentId]['children']) { map[i.parentId]['children'].push(i) } else { map[i.parentId]['children'] = [i] } map[i.parentId]['isLeaf'] = !map[i.parentId]['children'] map[i.id]['isLeaf'] = !map[i.id]['children'] } else { i.isLeaf = true res.push(i) } } return res } static treeToFlatList(tree: any[]) { tree = JSON.parse(JSON.stringify(tree)) const res: any[] = [] const loop = (arr: any[], id?: number) => { arr.forEach((i) => { if (Array.isArray(i.children) && i.children.length > 0) { loop(i.children, i.id) // 如果有children 就递归 Reflect.deleteProperty(i, 'children') // 删除children,然后push 不能结束 } res.push({ ...i, parentId: id } as any) }) } loop(tree) return res } static isEmpty(v: any) { return [null, '', void 0].includes(v) } static objectToURLSearchParams(obj: {}): URLSearchParams { const params = new URLSearchParams() Object.entries(obj).forEach(([k, v]: [string, any]) => { if (!Utils.isEmpty(v)) { params.append(k, v) } }) return params } static buildTree( organizations: T[], keyName: string = 'key', titleName: string = 'title', ): NzTreeNodeOptions[] { const map: { [parentId: number]: NzTreeNodeOptions[] } = {} organizations.forEach((org) => { if (!map[org.parentId]) { map[org.parentId] = [] } const treeNode: NzTreeNodeOptions = { ...org, title: org[titleName], key: org[keyName].toString(), } map[org.parentId].push(treeNode) }) const build = (parentId: number): NzTreeNodeOptions[] => { if (!map[parentId]) { return [] } return map[parentId].map((node) => { const children = build(parseInt(node.key)) return { ...node, isLeaf: children.length === 0, children, } }) } return build(0) } static downLoadFile(response: HttpResponse, type: string, ext: string = 'pdf') { const fileNameFromHeader = response.headers.get('Content-Disposition') let fileName = '' if (fileNameFromHeader) { fileName = fileNameFromHeader.trim().split('=')[1].replace(/"/g, '') } console.log('fileName', fileName) const blob = new Blob([response.body as any], { type }) const downloadLink = document.createElement('a') downloadLink.href = URL.createObjectURL(blob) downloadLink.download = decodeURIComponent(fileName) document.body.appendChild(downloadLink) downloadLink.click() document.body.removeChild(downloadLink) } }