hook.tsx 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. import {
  2. reactive,
  3. onMounted,
  4. ref,
  5. PaginationProps
  6. } from "@/utils/importUsed";
  7. import { requestSearch, requestDelete } from "@/utils/handlePort";
  8. // 表单实例
  9. const ruleFormRef = ref();
  10. export function useApiInfo() {
  11. // 获取当前板块接口列表
  12. onMounted(async () => {
  13. onSearch();
  14. groupIdQuery();
  15. });
  16. const form = reactive({
  17. // 根据字段生成对象
  18. groupId: "", //分组
  19. apiName: "" //接口名称
  20. });
  21. // 渲染表单组件和 form 字段相对应
  22. const formData = ref({
  23. groupId: {
  24. label: "分组",
  25. type: "select",
  26. disposition: {},
  27. options: [],
  28. rules: []
  29. },
  30. apiName: {
  31. label: "接口名称",
  32. required: false,
  33. type: "input",
  34. disposition: {}
  35. }
  36. });
  37. // const formData = {
  38. // username: {
  39. // label: "用户名",
  40. // required: true,
  41. // type: "select",
  42. // disposition: { multiple: false },
  43. // options: [
  44. // { label: "id1", value: 1 },
  45. // { label: "id2", value: 2 }
  46. // ],
  47. // rules: []
  48. // },
  49. // phone: {
  50. // label: "电话号码",
  51. // required: false,
  52. // type: "picker",
  53. // disposition: {
  54. // type: "datetimerange"
  55. // }
  56. // },
  57. // deptId: {
  58. // label: "所属部门ID",
  59. // required: false,
  60. // type: "input",
  61. // disposition: {},
  62. // rules: [{ required: true, message: "请输入名称", trigger: "blur" }]
  63. // }
  64. // };
  65. const dataList = ref([]);
  66. const loading = ref(false);
  67. const pagination = reactive<PaginationProps>({
  68. total: 0,
  69. pageSize: 10,
  70. currentPage: 1,
  71. background: true
  72. });
  73. const columns: TableColumnList = [
  74. {
  75. type: "selection",
  76. width: 55,
  77. align: "left",
  78. hide: ({ checkList }) => !checkList.includes("勾选列")
  79. },
  80. {
  81. label: "序号",
  82. type: "index",
  83. width: 70,
  84. hide: ({ checkList }) => !checkList.includes("序号列")
  85. },
  86. {
  87. label: "分组名称",
  88. prop: "apiGroupInfo.groupName",
  89. minWidth: 200
  90. },
  91. {
  92. label: "分组说明",
  93. prop: "apiGroupInfo.groupRemark",
  94. minWidth: 200
  95. },
  96. {
  97. label: "分组版本号",
  98. prop: "apiGroupInfo.groupVersion",
  99. minWidth: 200
  100. },
  101. {
  102. label: "接口名称",
  103. prop: "apiName",
  104. minWidth: 200
  105. },
  106. {
  107. label: "接口关键字",
  108. prop: "apiKey",
  109. minWidth: 200
  110. },
  111. {
  112. label: "接口主机头",
  113. prop: "apiHost",
  114. minWidth: 200
  115. },
  116. {
  117. label: "接口端口号",
  118. prop: "apiPort",
  119. minWidth: 200
  120. },
  121. {
  122. label: "接口路由",
  123. prop: "apiRouter",
  124. minWidth: 200
  125. },
  126. {
  127. label: "ID",
  128. prop: "id",
  129. minWidth: 200
  130. },
  131. {
  132. label: "操作",
  133. fixed: "right",
  134. width: 200,
  135. slot: "operation"
  136. }
  137. ];
  138. // 当前页数量切换
  139. function handleSizeChange(val: number) {
  140. if (typeof val === "number") {
  141. pagination.pageSize = val;
  142. onSearch();
  143. }
  144. }
  145. // 当前页码切换
  146. function handleCurrentChange(val: number) {
  147. console.log(`current page: ${val}`);
  148. if (typeof val === "number") {
  149. pagination.currentPage = val;
  150. onSearch();
  151. }
  152. }
  153. // 选择表格项
  154. function handleSelectionChange(val) {
  155. console.log(`SelectionChange: ${val}`);
  156. onSearch();
  157. }
  158. // 搜索列表
  159. async function onSearch() {
  160. // 状态调整为加载中
  161. loading.value = true;
  162. // 调用模块及方法(需动态生成接口)
  163. const { status, msg, data }: any = await requestSearch({
  164. module: "kxsConfigServer",
  165. method: "apiInfolist",
  166. params: form,
  167. pageSize: pagination.pageSize,
  168. pageNum: pagination.currentPage
  169. });
  170. dataList.value = data.records;
  171. pagination.total = data.total;
  172. setTimeout(() => {
  173. loading.value = false;
  174. }, 500);
  175. }
  176. //获取分组数据
  177. async function groupIdQuery() {
  178. const { status, data }: any = await requestSearch({
  179. module: "kxsConfigServer",
  180. method: "apiGroupselectList",
  181. params: {}
  182. });
  183. if (status === 1) {
  184. formData.value.groupId.options = data.records.map(item => {
  185. return { value: item.id, label: item.groupName };
  186. });
  187. }
  188. }
  189. // 删除
  190. function handleDelete(row) {
  191. requestDelete(
  192. {
  193. module: "kxsConfigServer",
  194. method: "apiInfodelete",
  195. msg: "是否删除该api接口?",
  196. id: row.id
  197. },
  198. onSearch
  199. );
  200. }
  201. return {
  202. form,
  203. loading,
  204. columns,
  205. dataList,
  206. pagination,
  207. onSearch,
  208. handleSizeChange,
  209. handleCurrentChange,
  210. handleSelectionChange,
  211. ruleFormRef,
  212. groupIdQuery,
  213. handleDelete,
  214. formData
  215. };
  216. }