|
| 1 | +import { ApiDataInfo, ApiItem } from "@/api/interface/apiData"; |
| 2 | +import { MenuItemType } from "@/components/Menu/src/interface"; |
| 3 | +import { createApiPageKey } from "@/utils"; |
| 4 | +import { ApiAnalysisData } from "@/store/modules/Apidoc/interface"; |
| 5 | +import { MdMenuItem } from "@/api/interface/markdown"; |
| 6 | +import { ConfigAppItem } from "@/api/interface/config"; |
| 7 | + |
| 8 | +interface ReturnHandleApiData { |
| 9 | + apiList: ApiItem[]; |
| 10 | + apiMenus: MenuItemType[]; |
| 11 | + apiObject: ApiObject; |
| 12 | + apiAnalysis: ApiAnalysisData; |
| 13 | +} |
| 14 | + |
| 15 | +interface ApiObject { |
| 16 | + [key: string]: ApiItem; |
| 17 | +} |
| 18 | + |
| 19 | +/** |
| 20 | + * 处理apiData接口响应数据 |
| 21 | + * @param data |
| 22 | + * @param appKey |
| 23 | + * @returns |
| 24 | + */ |
| 25 | +export function handleApiData(data: ApiDataInfo, appKey: string): ReturnHandleApiData { |
| 26 | + const apiData = data.data; |
| 27 | + const res: ReturnHandleApiData = { |
| 28 | + apiList: [], |
| 29 | + apiMenus: [], |
| 30 | + apiObject: {}, |
| 31 | + apiAnalysis: { |
| 32 | + apiCount: 0, |
| 33 | + apiMethodTotal: {}, |
| 34 | + controllerGroupTotal: {}, |
| 35 | + apiGroupTotal: {}, |
| 36 | + apiTagTotal: {}, |
| 37 | + apiAuthorTotal: {}, |
| 38 | + }, |
| 39 | + }; |
| 40 | + // 递归处理数据 |
| 41 | + function renderApiData(list: ApiItem[]): MenuItemType[] { |
| 42 | + const apiMenus = list.map((item) => { |
| 43 | + let key = ""; |
| 44 | + const method = item.method as string; |
| 45 | + let methodList: string[] = []; |
| 46 | + |
| 47 | + let type = "folder"; |
| 48 | + if (item.controller) { |
| 49 | + type = "controller"; |
| 50 | + } else if (method && method.indexOf(",") > -1) { |
| 51 | + type = "multiple"; |
| 52 | + methodList = method.split(","); |
| 53 | + } else if (method) { |
| 54 | + type = "api"; |
| 55 | + methodList = [method]; |
| 56 | + } |
| 57 | + if (type === "controller" && item.group) { |
| 58 | + // 控制器分组 |
| 59 | + const groupName = item.group; |
| 60 | + // 分组下的api数量 |
| 61 | + const childrenApiCount = item.children && item.children.length ? item.children.length : 0; |
| 62 | + if (res.apiAnalysis.apiGroupTotal[groupName]) { |
| 63 | + res.apiAnalysis.apiGroupTotal[groupName] = |
| 64 | + res.apiAnalysis.apiGroupTotal[groupName] + childrenApiCount; |
| 65 | + } else { |
| 66 | + res.apiAnalysis.apiGroupTotal[groupName] = childrenApiCount; |
| 67 | + } |
| 68 | + // 分组下的控制器数量 |
| 69 | + if (res.apiAnalysis.controllerGroupTotal[groupName]) { |
| 70 | + res.apiAnalysis.controllerGroupTotal[groupName] = |
| 71 | + res.apiAnalysis.controllerGroupTotal[groupName] + 1; |
| 72 | + } else { |
| 73 | + res.apiAnalysis.controllerGroupTotal[groupName] = 1; |
| 74 | + } |
| 75 | + } else if (item.url) { |
| 76 | + // api接口存入list和object |
| 77 | + res.apiList.push(item); |
| 78 | + key = createApiPageKey({ |
| 79 | + appKey: appKey as string, |
| 80 | + method: method as string, |
| 81 | + url: item.url as string, |
| 82 | + }); |
| 83 | + res.apiObject[key] = item; |
| 84 | + // 接口总数 |
| 85 | + res.apiAnalysis.apiCount++; |
| 86 | + // 统计请求类型数量 |
| 87 | + if (methodList && methodList.length) { |
| 88 | + for (let i = 0; i < methodList.length; i++) { |
| 89 | + const methodItem = methodList[i]; |
| 90 | + if (res.apiAnalysis.apiMethodTotal[methodItem]) { |
| 91 | + res.apiAnalysis.apiMethodTotal[methodItem] = |
| 92 | + res.apiAnalysis.apiMethodTotal[methodItem] + 1; |
| 93 | + } else { |
| 94 | + res.apiAnalysis.apiMethodTotal[methodItem] = 1; |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | + // tag统计 |
| 99 | + if (item.tag && item.tag.length) { |
| 100 | + for (let i = 0; i < item.tag.length; i++) { |
| 101 | + const tagItem = item.tag[i]; |
| 102 | + if (res.apiAnalysis.apiTagTotal[tagItem]) { |
| 103 | + res.apiAnalysis.apiTagTotal[tagItem] = res.apiAnalysis.apiTagTotal[tagItem] + 1; |
| 104 | + } else { |
| 105 | + res.apiAnalysis.apiTagTotal[tagItem] = 1; |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | + // 作者接口数量统计 |
| 110 | + if (item.author) { |
| 111 | + if (res.apiAnalysis.apiAuthorTotal[item.author]) { |
| 112 | + res.apiAnalysis.apiAuthorTotal[item.author] = |
| 113 | + res.apiAnalysis.apiAuthorTotal[item.author] + 1; |
| 114 | + } else { |
| 115 | + res.apiAnalysis.apiAuthorTotal[item.author] = 1; |
| 116 | + } |
| 117 | + } |
| 118 | + } |
| 119 | + // api接口菜单数据 |
| 120 | + const menuItem: MenuItemType = { |
| 121 | + title: item.title, |
| 122 | + menu_key: item.menu_key, |
| 123 | + type: type, |
| 124 | + method: item.method as string, |
| 125 | + url: item.url, |
| 126 | + tag: item.tag, |
| 127 | + key, |
| 128 | + }; |
| 129 | + if (item.children && item.children.length) { |
| 130 | + menuItem.children = renderApiData(item.children); |
| 131 | + } |
| 132 | + return menuItem; |
| 133 | + }); |
| 134 | + |
| 135 | + return apiMenus; |
| 136 | + } |
| 137 | + res.apiMenus = renderApiData(apiData); |
| 138 | + return res; |
| 139 | +} |
| 140 | + |
| 141 | +export function handleMdMenusData(data: MdMenuItem[]): any { |
| 142 | + let result = { |
| 143 | + menus: [], |
| 144 | + count: 0, |
| 145 | + }; |
| 146 | + function renderMdMenuData(list: MdMenuItem[]): any { |
| 147 | + const mdMenus = list.map((item) => { |
| 148 | + if (item.path) { |
| 149 | + result.count++; |
| 150 | + } |
| 151 | + if (item.children && item.children.length) { |
| 152 | + item.children = renderMdMenuData(item.children); |
| 153 | + } |
| 154 | + return item; |
| 155 | + }); |
| 156 | + return mdMenus; |
| 157 | + } |
| 158 | + result.menus = renderMdMenuData(data); |
| 159 | + return result; |
| 160 | +} |
| 161 | + |
| 162 | +export function handleConfigAppsData(data: ConfigAppItem[]): any { |
| 163 | + let result = { |
| 164 | + count: 0, |
| 165 | + }; |
| 166 | + function renderAppsData(list: ConfigAppItem[]): any { |
| 167 | + const mdMenus = list.map((item) => { |
| 168 | + if (item.folder && !(item.items && item.items.length)) { |
| 169 | + result.count++; |
| 170 | + } |
| 171 | + if (item.items && item.items.length) { |
| 172 | + item.items = renderAppsData(item.items); |
| 173 | + } |
| 174 | + return item; |
| 175 | + }); |
| 176 | + return mdMenus; |
| 177 | + } |
| 178 | + renderAppsData(data); |
| 179 | + return result; |
| 180 | +} |
0 commit comments