apilist.vue 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609
  1. <template>
  2. <div class="page">
  3. <div class="apihub-container">
  4. <!-- 左侧分组树 -->
  5. <div class="apihub-sidebar">
  6. <el-card shadow="never" class="group-card">
  7. <template #header>
  8. <div class="card-header">
  9. <span>API分组</span>
  10. <div class="header-actions">
  11. <el-button type="primary" size="small" @click="addGroup" v-auth="'add_group'">
  12. <el-icon><ele-Plus /></el-icon>
  13. </el-button>
  14. <el-button type="primary" size="small" @click="refreshGroups">
  15. <el-icon><ele-Refresh /></el-icon>
  16. </el-button>
  17. </div>
  18. </div>
  19. </template>
  20. <div class="group-search">
  21. <el-input v-model="groupSearchKey" placeholder="搜索分组" clearable prefix-icon="ele-Search" @input="filterGroups" />
  22. </div>
  23. <div class="group-tree-container">
  24. <el-tree ref="groupTreeRef" :data="groupTreeData" :props="{ label: 'Name', children: 'Children' }" node-key="Id" highlight-current :expand-on-click-node="true" default-expand-all @node-click="handleGroupClick">
  25. <template #default="{ node, data }">
  26. <div class="custom-tree-node">
  27. <span>{{ node.label }}</span>
  28. <span class="api-count" v-if="data.ApiCount">{{ data.ApiCount }}</span>
  29. <div class="node-actions">
  30. <el-dropdown @command="(command: string) => handleGroupCommand(command, data)" trigger="click">
  31. <el-icon><ele-More /></el-icon>
  32. <template #dropdown>
  33. <el-dropdown-menu>
  34. <el-dropdown-item command="edit" v-auth="'edit_group'">编辑</el-dropdown-item>
  35. <el-dropdown-item command="add_child" v-auth="'add_group'">添加子分组</el-dropdown-item>
  36. <el-dropdown-item command="delete" v-auth="'delete_group'">删除</el-dropdown-item>
  37. </el-dropdown-menu>
  38. </template>
  39. </el-dropdown>
  40. </div>
  41. </div>
  42. </template>
  43. </el-tree>
  44. </div>
  45. </el-card>
  46. </div>
  47. <!-- 右侧API列表 -->
  48. <el-card shadow="never">
  49. <div class="api-header">
  50. <div class="current-group" v-if="currentGroup.Name">
  51. 当前分组: <span class="group-name">{{ currentGroup.Name }}</span>
  52. </div>
  53. <div class="current-group" v-else>全部API</div>
  54. </div>
  55. <el-form :model="params" inline ref="queryRef">
  56. <el-form-item label="API名称" prop="keyWord">
  57. <el-input v-model="params.keyWord" placeholder="请输入API名称" clearable style="width: 180px" @keyup.enter.native="getList(1)" />
  58. </el-form-item>
  59. <el-form-item label="数据源" prop="dataSourceId">
  60. <el-select v-model="params.dataSourceId" placeholder="请选择数据源" clearable style="width: 180px">
  61. <el-option v-for="item in dataSources" :key="item.id" :label="item.name" :value="item.id" />
  62. </el-select>
  63. </el-form-item>
  64. <el-form-item label="状态" prop="status">
  65. <el-select v-model="params.status" placeholder="请选择状态" clearable style="width: 120px">
  66. <el-option label="全部" value="" />
  67. <el-option label="草稿" value="Draft" />
  68. <el-option label="已发布" value="Published" />
  69. <el-option label="已废弃" value="Deprecated" />
  70. </el-select>
  71. </el-form-item>
  72. <!-- <el-form-item label="日期范围" prop="dateRange">-->
  73. <!-- <el-date-picker v-model="params.dateRange" type="daterange" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" value-format="YYYY-MM-DD" style="width: 240px" />-->
  74. <!-- </el-form-item>-->
  75. <el-form-item>
  76. <el-button type="primary" class="ml10" @click="getList(1)">
  77. <el-icon>
  78. <ele-Search />
  79. </el-icon>
  80. 查询
  81. </el-button>
  82. <el-button @click="resetQuery()">
  83. <el-icon>
  84. <ele-Refresh />
  85. </el-icon>
  86. 重置
  87. </el-button>
  88. <el-button type="primary" @click="addOrEdit()" v-auth="'add'">
  89. <el-icon>
  90. <ele-FolderAdd />
  91. </el-icon>
  92. 新增API
  93. </el-button>
  94. </el-form-item>
  95. </el-form>
  96. <el-table :data="tableData" style="width: 100%" v-loading="loading" row-key="id">
  97. <el-table-column type="selection" width="40" align="center" />
  98. <!-- <el-table-column prop="id" label="ID" width="80" align="center" />-->
  99. <el-table-column prop="method" label="请求" width="85" align="center">
  100. <template #default="scope">
  101. <el-tag :type="getMethodTagType(scope.row.method)" size="small">
  102. {{ scope.row.method }}
  103. </el-tag>
  104. </template>
  105. </el-table-column>
  106. <el-table-column prop="name" label="API名称" min-width="140" show-overflow-tooltip></el-table-column>
  107. <el-table-column prop="path" label="API路径" min-width="150" show-overflow-tooltip></el-table-column>
  108. <!-- <el-table-column prop="dataSourceName" label="数据源" width="120" show-overflow-tooltip></el-table-column>-->
  109. <!-- <el-table-column prop="sqlType" label="SQL类型" width="100" align="center">-->
  110. <!-- <template #default="scope">-->
  111. <!-- <el-tag size="small" type="info" v-if="scope.row.sqlType === 'query'">查询</el-tag>-->
  112. <!-- <el-tag size="small" type="warning" v-else-if="scope.row.sqlType === 'procedure'">存储过程</el-tag>-->
  113. <!-- <span v-else>{{ scope.row.sqlType }}</span>-->
  114. <!-- </template>-->
  115. <!-- </el-table-column>-->
  116. <el-table-column prop="version" label="版本" width="80" align="center"></el-table-column>
  117. <el-table-column prop="description" label="描述" width="140" />
  118. <el-table-column prop="status" label="状态" width="100" align="center">
  119. <template #default="scope">
  120. <el-tag size="small" v-if="scope.row.status === 'Draft'">草稿</el-tag>
  121. <el-tag size="small" type="success" v-else-if="scope.row.status === 'Published'">已发布</el-tag>
  122. <el-tag size="small" type="info" v-else-if="scope.row.status === 'Deprecated'">已废弃</el-tag>
  123. <span v-else>{{ scope.row.status }}</span>
  124. </template>
  125. </el-table-column>
  126. <!-- <el-table-column prop="createdAt" label="创建时间" width="160" align="center"></el-table-column>-->
  127. <el-table-column label="操作" width="190" align="center" fixed="right">
  128. <template #default="scope">
  129. <div class="flex-row">
  130. <el-button size="small" text type="primary" @click="viewDetail(scope.row)" v-auth="'view'">查看</el-button>
  131. <el-button size="small" text type="warning" @click="addOrEdit(scope.row)" v-auth="'edit'">编辑</el-button>
  132. <el-button size="small" text type="success" @click="testApi(scope.row)" v-auth="'test'">测试</el-button>
  133. <el-dropdown @command="(command: string) => handleCommand(command, scope.row)">
  134. <el-button size="small" text type="primary" style="margin-left: 12px; vertical-align: top">
  135. 更多<el-icon class="el-icon--right" style="font-size: 12px !important"><arrow-down /></el-icon>
  136. </el-button>
  137. <template #dropdown>
  138. <el-dropdown-menu>
  139. <el-dropdown-item command="publish" v-if="scope.row.status === 'Draft'" v-auth="'publish'">发布</el-dropdown-item>
  140. <el-dropdown-item command="deprecate" v-if="scope.row.status === 'Published'" v-auth="'deprecate'">废弃</el-dropdown-item>
  141. <el-dropdown-item command="delete" v-auth="'delete'">删除</el-dropdown-item>
  142. </el-dropdown-menu>
  143. </template>
  144. </el-dropdown>
  145. </div>
  146. </template>
  147. </el-table-column>
  148. </el-table>
  149. <pagination v-if="params.total" :total="params.total" v-model:page="params.pageNum" v-model:limit="params.pageSize" @pagination="getList()" />
  150. </el-card>
  151. </div>
  152. <!-- 组件 -->
  153. <EditForm ref="editFormRef" @getList="getList(1)"></EditForm>
  154. <ViewDetail ref="viewDetailRef"></ViewDetail>
  155. <TestApi ref="testApiRef"></TestApi>
  156. <GroupForm ref="groupFormRef" @refresh="refreshGroups"></GroupForm>
  157. </div>
  158. </template>
  159. <script lang="ts" setup>
  160. import { ref, onMounted, reactive } from "vue";
  161. import EditForm from "./component/edit.vue";
  162. import ViewDetail from "./component/view.vue";
  163. import TestApi from "./component/test.vue";
  164. import GroupForm from "./component/group.vue";
  165. import { useSearch } from "/@/hooks/useCommon";
  166. import { ElMessageBox, ElMessage } from "element-plus";
  167. import { ArrowDown } from "@element-plus/icons-vue";
  168. import api from "/@/api/modules/apiHub";
  169. // 定义API接口类型
  170. interface ApiDefinition {
  171. id?: number;
  172. name: string;
  173. path: string;
  174. method: string;
  175. dataSourceId: number;
  176. dataSourceName?: string;
  177. sqlType: string;
  178. sqlContent: string;
  179. parameters?: any[];
  180. returnFormat: string;
  181. version: string;
  182. status: string;
  183. plugins?: any[];
  184. description?: string;
  185. createdAt?: string;
  186. updatedAt?: string;
  187. }
  188. // 引用组件
  189. const editFormRef = ref();
  190. const viewDetailRef = ref();
  191. const testApiRef = ref();
  192. const queryRef = ref();
  193. const groupFormRef = ref();
  194. const groupTreeRef = ref();
  195. // 分组相关状态
  196. const groupSearchKey = ref("");
  197. const groupTreeData = ref<any[]>([]);
  198. const currentGroup = reactive<any>({
  199. Id: undefined,
  200. GroupKey: "",
  201. Name: "",
  202. ParentId: 0,
  203. Description: "",
  204. });
  205. const originalGroupTree = ref<any[]>([]);
  206. // 数据源列表
  207. const dataSources = ref<any[]>([]);
  208. // 使用通用搜索钩子
  209. const { params, tableData, getList, loading } = useSearch<ApiDefinition[]>(api.list, "Data", {
  210. keyWord: "",
  211. dataSourceId: "",
  212. status: "",
  213. dateRange: [],
  214. orderBy: "",
  215. });
  216. // 加载数据源列表
  217. const loadDataSources = () => {
  218. api.dataSource.list().then((res: any) => {
  219. dataSources.value = res.list;
  220. });
  221. };
  222. // 页面加载时获取列表数据
  223. onMounted(() => {
  224. // 获取API列表
  225. getList(1);
  226. // 加载数据源列表
  227. loadDataSources();
  228. // 加载分组树
  229. refreshGroups();
  230. });
  231. // 将扁平数组转换为树形结构
  232. const convertToTree = (flatData: any[]) => {
  233. // 创建一个映射表,用于快速查找节点
  234. const map: any = {};
  235. const result: any[] = [];
  236. // 首先创建所有节点的映射
  237. flatData.forEach((item) => {
  238. // 确保每个节点都有Children属性
  239. map[item.Id] = { ...item, Children: [] };
  240. });
  241. // 然后建立父子关系
  242. flatData.forEach((item) => {
  243. const node = map[item.Id];
  244. if (item.ParentId === 0 || !map[item.ParentId]) {
  245. // 如果ParentId为0或者父节点不存在,则为顶级节点
  246. result.push(node);
  247. } else {
  248. // 否则将该节点添加到父节点的Children中
  249. map[item.ParentId].Children.push(node);
  250. }
  251. });
  252. // 清理空的Children数组
  253. flatData.forEach((item) => {
  254. if (map[item.Id].Children.length === 0) {
  255. map[item.Id].Children = null;
  256. }
  257. });
  258. return result;
  259. };
  260. // 检查数据是否已经是树形结构
  261. const isTreeStructure = (data: any[]) => {
  262. // 检查数据中是否有包含非空的Children字段的项
  263. return data.some((item) => item.Children && Array.isArray(item.Children) && item.Children.length > 0);
  264. };
  265. // 刷新分组树
  266. const refreshGroups = async () => {
  267. try {
  268. // 调用API获取分组树
  269. const res: any = await api.group.tree();
  270. // 使用API返回的数据
  271. if (res?.list) {
  272. // 获取原始数据
  273. const apiData = res.list || [];
  274. // 检查数据是否已经是树形结构
  275. const hasTreeStructure = isTreeStructure(apiData);
  276. let treeData: any[];
  277. if (hasTreeStructure) {
  278. // 如果已经是树形结构,直接使用
  279. treeData = apiData;
  280. } else {
  281. // 如果是扁平结构,通过ParentId构建树形结构
  282. treeData = convertToTree(apiData);
  283. }
  284. // 设置到组件中
  285. groupTreeData.value = treeData;
  286. originalGroupTree.value = JSON.parse(JSON.stringify(treeData));
  287. return;
  288. }
  289. // 如果没有数据,初始化为空数组
  290. groupTreeData.value = [];
  291. originalGroupTree.value = [];
  292. } catch (error) {
  293. ElMessage.error("获取分组数据失败");
  294. }
  295. };
  296. // 搜索过滤分组
  297. const filterGroups = () => {
  298. if (!groupSearchKey.value) {
  299. // 如果搜索关键字为空,恢复原始数据
  300. groupTreeData.value = JSON.parse(JSON.stringify(originalGroupTree.value));
  301. return;
  302. }
  303. // 递归搜索函数
  304. const searchTree = (nodes: any[]) => {
  305. return nodes.filter((node) => {
  306. // 当前节点名称匹配
  307. const matchesName = node.Name.toLowerCase().includes(groupSearchKey.value.toLowerCase());
  308. // 递归搜索子节点
  309. if (node.Children && node.Children.length) {
  310. node.Children = searchTree(node.Children);
  311. // 如果子节点有匹配项,则保留父节点
  312. return matchesName || node.Children.length > 0;
  313. }
  314. return matchesName;
  315. });
  316. };
  317. groupTreeData.value = searchTree(JSON.parse(JSON.stringify(originalGroupTree.value)));
  318. };
  319. // 点击分组节点
  320. const handleGroupClick = (data: any) => {
  321. console.log(data);
  322. // 设置当前选中分组
  323. Object.assign(currentGroup, data);
  324. // 更新查询参数,加入分组条件
  325. params.groupKey = data.GroupKey;
  326. // 重新获取列表
  327. getList(1);
  328. };
  329. // 添加分组
  330. const addGroup = () => {
  331. groupFormRef.value.open();
  332. };
  333. // 处理分组操作
  334. const handleGroupCommand = (command: string, data: any) => {
  335. switch (command) {
  336. case "edit":
  337. groupFormRef.value.open(data);
  338. break;
  339. case "add_child":
  340. groupFormRef.value.open(null, data.GroupKey);
  341. break;
  342. case "delete":
  343. deleteGroupConfirm(data);
  344. break;
  345. }
  346. };
  347. // 删除分组确认
  348. const deleteGroupConfirm = (data: any) => {
  349. ElMessageBox.confirm(`确定要删除分组「${data.Name}」吗?如果包含子分组或API,将一并删除。`, "警告", {
  350. confirmButtonText: "确定",
  351. cancelButtonText: "取消",
  352. type: "warning",
  353. })
  354. .then(async () => {
  355. try {
  356. // 实际使用时调用API
  357. await api.group.delete([data.Id]);
  358. ElMessage.success("删除成功");
  359. // 如果当前选中的是要删除的分组,则清空当前分组
  360. if (currentGroup.GroupKey === data.GroupKey) {
  361. Object.assign(currentGroup, {
  362. Id: undefined,
  363. GroupKey: "",
  364. Name: "",
  365. ParentId: 0,
  366. Description: "",
  367. });
  368. params.groupKey = "";
  369. getList(1);
  370. }
  371. // 刷新分组树
  372. refreshGroups();
  373. } catch (error) {
  374. ElMessage.error("删除失败");
  375. }
  376. })
  377. .catch(() => {});
  378. };
  379. // 根据请求方法返回不同的标签类型
  380. const getMethodTagType = (method: string) => {
  381. switch (method.toUpperCase()) {
  382. case "GET":
  383. return "success";
  384. case "POST":
  385. return "primary";
  386. case "PUT":
  387. return "warning";
  388. case "DELETE":
  389. return "danger";
  390. default:
  391. return "info";
  392. }
  393. };
  394. // 重置查询表单
  395. const resetQuery = () => {
  396. queryRef.value.resetFields();
  397. getList(1);
  398. };
  399. // 新增或编辑API
  400. const addOrEdit = (row?: ApiDefinition) => {
  401. if (row) {
  402. // 编辑现有API
  403. // 实际使用时,可能需要先获取详情
  404. editFormRef.value.open(row);
  405. } else {
  406. // 新增API,如果有选中分组,则传递分组标识
  407. editFormRef.value.open(null, currentGroup.GroupKey);
  408. }
  409. };
  410. // 查看API详情
  411. const viewDetail = (row: ApiDefinition) => {
  412. viewDetailRef.value.open(row);
  413. };
  414. // 测试API
  415. const testApi = (row: ApiDefinition) => {
  416. testApiRef.value.open(row);
  417. };
  418. // 处理下拉菜单命令
  419. const handleCommand = (command: string, row: ApiDefinition) => {
  420. switch (command) {
  421. case "publish":
  422. publishApi(row);
  423. break;
  424. case "deprecate":
  425. deprecateApi(row);
  426. break;
  427. case "delete":
  428. deleteApi(row);
  429. break;
  430. }
  431. };
  432. // 发布API
  433. const publishApi = (row: ApiDefinition) => {
  434. ElMessageBox.confirm(`确定要发布API「${row.name}」吗?`, "提示", {
  435. confirmButtonText: "确定",
  436. cancelButtonText: "取消",
  437. type: "warning",
  438. }).then(async () => {
  439. await api.publish(row.id!);
  440. ElMessage.success("发布成功");
  441. getList();
  442. });
  443. };
  444. // 废弃API
  445. const deprecateApi = (row: ApiDefinition) => {
  446. ElMessageBox.confirm(`确定要废弃API「${row.name}」吗?`, "提示", {
  447. confirmButtonText: "确定",
  448. cancelButtonText: "取消",
  449. type: "warning",
  450. }).then(async () => {
  451. await api.deprecate(row.id!);
  452. ElMessage.success("废弃成功");
  453. getList();
  454. });
  455. };
  456. // 删除API
  457. const deleteApi = (row: ApiDefinition) => {
  458. ElMessageBox.confirm(`确定要删除API「${row.name}」吗?此操作不可恢复!`, "警告", {
  459. confirmButtonText: "确定",
  460. cancelButtonText: "取消",
  461. type: "error",
  462. }).then(async () => {
  463. await api.delete([row.id!]);
  464. ElMessage.success("删除成功");
  465. getList();
  466. });
  467. };
  468. </script>
  469. <style scoped>
  470. .ml10 {
  471. margin-left: 10px;
  472. }
  473. .apihub-container {
  474. display: flex;
  475. height: 100%;
  476. align-items: stretch;
  477. }
  478. .apihub-sidebar {
  479. width: 280px;
  480. min-width: 280px;
  481. margin-right: 16px;
  482. overflow-y: auto;
  483. height: 100%;
  484. }
  485. .apihub-content {
  486. flex: 1;
  487. overflow-y: auto;
  488. height: 100%;
  489. }
  490. .group-card {
  491. height: 100%;
  492. display: flex;
  493. flex-direction: column;
  494. }
  495. .card-header {
  496. display: flex;
  497. justify-content: space-between;
  498. align-items: center;
  499. }
  500. .header-actions {
  501. display: flex;
  502. gap: 8px;
  503. }
  504. .group-search {
  505. margin-bottom: 12px;
  506. }
  507. .group-tree-container {
  508. overflow-y: auto;
  509. flex: 1;
  510. min-height: 200px;
  511. /* 确保容器有最小高度 */
  512. border: 1px solid #ebeef5;
  513. /* 添加边框以便于调试 */
  514. padding: 10px;
  515. }
  516. .custom-tree-node {
  517. flex: 1;
  518. display: flex;
  519. align-items: center;
  520. justify-content: space-between;
  521. font-size: 14px;
  522. padding-right: 8px;
  523. }
  524. .api-count {
  525. padding: 0 6px;
  526. background-color: #f0f0f0;
  527. border-radius: 10px;
  528. font-size: 12px;
  529. color: #606266;
  530. }
  531. .node-actions {
  532. margin-left: 8px;
  533. visibility: hidden;
  534. }
  535. .custom-tree-node:hover .node-actions {
  536. visibility: visible;
  537. }
  538. .api-header {
  539. margin-bottom: 16px;
  540. font-size: 16px;
  541. }
  542. .group-name {
  543. font-weight: bold;
  544. color: #409eff;
  545. }
  546. </style>