index.vue 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. <route lang="yaml">
  2. meta:
  3. enabled: false
  4. </route>
  5. <script lang="ts" setup name="apiIndex">
  6. import { ElMessage, ElMessageBox } from 'element-plus'
  7. import PSApi from './components/Api.vue'
  8. import { getBizData } from '@/api/biz/bizApi'
  9. import { deleteApiById, getApiByList, updateApiStatus } from '@/api/api/apiApi'
  10. import useSettingsStore from '@/store/modules/settings'
  11. import { httpMethod } from '@/utils/tool'
  12. const settingsStore = useSettingsStore()
  13. const router = useRouter()
  14. const tabbar = useTabbar()
  15. interface TreeNode {
  16. label: string
  17. isLeaf: boolean
  18. value: number
  19. bizId: number
  20. objectId: number
  21. children?: TreeNode[]
  22. }
  23. interface Api {
  24. bizId: number
  25. objectId: number
  26. oamId: number
  27. apiId: number
  28. apiMethod: number
  29. apiType: number
  30. apiTitle: string
  31. apiCode: string
  32. apiDesc: string
  33. apiParams: string
  34. status: number
  35. }
  36. const state = reactive({
  37. loading: false,
  38. keywords: '',
  39. treeData: [] as TreeNode[],
  40. curBizId: 0,
  41. curObjectId: 0,
  42. apiList: [] as Api[],
  43. dialogState: 1,
  44. search: {
  45. keywords: '',
  46. },
  47. })
  48. const leftTreeRef = ref()
  49. const getApiData = async () => {
  50. const curBizId = state.curBizId
  51. const curObjectId = state.curObjectId
  52. if (curBizId > 0 && curObjectId > 0) {
  53. state.loading = true
  54. const { data } = await getApiByList({ bizId: curBizId, objectId: curObjectId })
  55. state.apiList = data
  56. state.loading = false
  57. }
  58. }
  59. const getData = async () => {
  60. const { data } = await getBizData()
  61. state.treeData = data
  62. if (data && data.length > 0) {
  63. state.curBizId = data[0].bizId
  64. data.forEach((biz: { label: string; bizTitle: string; objects: any; children: any; isLeaf: boolean }) => {
  65. biz.label = biz.bizTitle
  66. biz.isLeaf = false
  67. const objects = biz.objects
  68. objects.forEach((object: { label: string; objectTitle: string; isLeaf: boolean }) => {
  69. object.label = object.objectTitle
  70. object.isLeaf = true
  71. })
  72. biz.children = objects
  73. })
  74. await getApiData()
  75. await nextTick(() => {
  76. leftTreeRef.value.setCurrentKey(data[0].bizId)
  77. })
  78. }
  79. }
  80. onMounted(() => {
  81. getData()
  82. })
  83. // 模块选择
  84. const onTreeNodeClick = (node: TreeNode) => {
  85. if (node.isLeaf) {
  86. state.curObjectId = node.objectId
  87. getApiData()
  88. }
  89. }
  90. const apiRef = ref()
  91. const apiTableRef = ref()
  92. // 检索对象
  93. const onSearch = () => {
  94. getApiData()
  95. }
  96. // 编辑API
  97. const onApiEdit = (data: { apiId: number }) => {
  98. const apiId = data.apiId
  99. apiRef.value.showEditApi(apiId)
  100. }
  101. // 编辑对象
  102. const onApiDebug = (row: { apiId: number }) => {
  103. if (settingsStore.settings.tabbar.enable && settingsStore.settings.tabbar.mergeTabsBy !== 'activeMenu') {
  104. tabbar.open({
  105. name: 'apiDebug',
  106. params: {
  107. apiId: row.apiId,
  108. },
  109. })
  110. }
  111. else {
  112. router.push({
  113. name: 'apiDebug',
  114. params: {
  115. apiId: row.apiId,
  116. },
  117. })
  118. }
  119. }
  120. async function onChangeStatus(row: { apiId: number; status: number }) {
  121. const res = await updateApiStatus(row.apiId, row.status)
  122. ElMessage.success({
  123. message: res.msg,
  124. center: true,
  125. })
  126. }
  127. </script>
  128. <template>
  129. <div class="absolute-container">
  130. <tool-header title="API管理">
  131. <template #right>
  132. </template>
  133. </tool-header>
  134. <div class="page-main">
  135. <LayoutContainer left-side-width="250px" hide-left-side-toggle>
  136. <template #leftSide>
  137. <el-scrollbar class="tree">
  138. <ElTree ref="leftTreeRef" :data="state.treeData" default-expand-all node-key="moduleId" @node-click="onTreeNodeClick">
  139. <template #default="{ node, data }">
  140. <div class="custom-tree-node">
  141. <div class="label" :title="node.label">
  142. <el-icon v-if="data.isLeaf" size="16px" style="margin-right: 5px;">
  143. <svg-icon name="uim:box" />
  144. </el-icon>
  145. <div class="text">
  146. {{ data.label }}
  147. </div>
  148. </div>
  149. </div>
  150. </template>
  151. </ElTree>
  152. </el-scrollbar>
  153. </template>
  154. <div class="container">
  155. <tool-bar>
  156. <template #right>
  157. <el-input v-model="state.search.keywords" placeholder="请输入关键字" style="padding-right: 10px;" clearable />
  158. <el-button type="primary" @click="onSearch">
  159. <template #icon>
  160. <el-icon>
  161. <svg-icon name="i-ep:search" />
  162. </el-icon>
  163. </template>
  164. </el-button>
  165. </template>
  166. </tool-bar>
  167. <el-table ref="apiTableRef" v-loading="state.loading" class="list-table" :data="state.apiList" size="small" border stripe highlight-current-row height="100%">
  168. <el-table-column type="index" label="序号" header-align="center" align="center" width="70" />
  169. <el-table-column prop="apiTitle" label="名称" header-align="center" align="center" width="150">
  170. <template #default="scope">
  171. <span> {{ scope.row.apiTitle }} </span>
  172. </template>
  173. </el-table-column>
  174. <el-table-column prop="apiMethod" label="请求" header-align="center" align="center" width="100">
  175. <template #default="scope">
  176. <span> {{ httpMethod(scope.row.apiMethod) }} </span>
  177. </template>
  178. </el-table-column>
  179. <el-table-column prop="apiCode" label="url" header-align="left" align="left" width="120">
  180. <template #default="scope">
  181. <span> /{{ scope.row.objectCode }}/{{ scope.row.oamCode }} </span>
  182. </template>
  183. </el-table-column>
  184. <el-table-column prop="apiDesc" label="说明" header-align="center" align="left">
  185. <template #default="scope">
  186. <span> {{ scope.row.apiDesc }} </span>
  187. </template>
  188. </el-table-column>
  189. <el-table-column prop="status" label="状态" header-align="center" align="center" width="100">
  190. <template #default="scope">
  191. <el-switch
  192. v-model="scope.row.status"
  193. :active-value="1"
  194. :inactive-value="0"
  195. :loading="scope.row.loading"
  196. inline-prompt
  197. active-text="启用"
  198. inactive-text="禁用"
  199. @change="(status) => onChangeStatus(scope.row)"
  200. />
  201. </template>
  202. </el-table-column>
  203. <el-table-column label="操作" header-align="center" align="center" width="120">
  204. <template #default="scope">
  205. <el-button type="primary" size="small" plain @click="onApiDebug(scope.row)">在线调试</el-button>
  206. </template>
  207. </el-table-column>
  208. </el-table>
  209. </div>
  210. </LayoutContainer>
  211. </div>
  212. <PSApi ref="apiRef" @success="getApiData" />
  213. </div>
  214. </template>
  215. <style lang="scss" scoped>
  216. .container {
  217. height: 100%;
  218. display: flex;
  219. flex-direction: column;
  220. }
  221. .absolute-container {
  222. :deep(.left-side) {
  223. display: flex;
  224. flex-direction: column;
  225. height: 100%;
  226. .btns {
  227. display: inline-flex;
  228. width: 100%;
  229. margin-bottom: var(--container-padding);
  230. .add {
  231. width: 100%;
  232. }
  233. }
  234. .tree {
  235. flex: 1;
  236. overflow-y: auto;
  237. .el-tree {
  238. .el-tree-node__content {
  239. height: 40px;
  240. }
  241. .is-current > .el-tree-node__content {
  242. background-color: var(--el-color-primary-light-9);
  243. }
  244. .custom-tree-node {
  245. flex: 1;
  246. position: relative;
  247. width: 0;
  248. height: 100%;
  249. display: flex;
  250. flex-direction: column;
  251. justify-content: center;
  252. .label {
  253. display: inline-flex;
  254. align-items: center;
  255. width: calc(100% - 10px);
  256. color: var(--el-text-color-primary);
  257. .text {
  258. @include text-overflow;
  259. }
  260. }
  261. &:hover {
  262. .actions {
  263. display: block;
  264. }
  265. }
  266. .actions {
  267. display: none;
  268. position: absolute;
  269. right: 10px;
  270. top: 50%;
  271. transform: translateY(-50%);
  272. .el-button {
  273. padding: 5px 8px;
  274. }
  275. }
  276. }
  277. }
  278. }
  279. }
  280. }
  281. </style>