index.vue 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. <template>
  2. <div>
  3. <PlusPage
  4. ref="plusPageInstance"
  5. :columns="tableConfig"
  6. :request="getList"
  7. :is-card="true"
  8. :search="{
  9. labelWidth: 100,
  10. showNumber: 3
  11. }"
  12. :table="{
  13. actionBar: { buttons, type: 'link', width: 180 },
  14. adaptive: { offsetBottom: 50 },
  15. onFormChange: handleTableChange
  16. }"
  17. :pageInfoMap="{ page: 'pageNum', pageSize: 'pageSize' }"
  18. >
  19. <template #table-title>
  20. <el-row class="button-row">
  21. <el-button size="default" type="success" @click="handleCreate">
  22. 新增
  23. </el-button>
  24. </el-row>
  25. </template>
  26. </PlusPage>
  27. <!-- 弹窗编辑 -->
  28. <PlusDialogForm
  29. ref="dialogForm"
  30. v-model="form"
  31. v-model:visible="dialogVisible"
  32. :form="{
  33. columns,
  34. labelPosition: 'right',
  35. labelWidth: 100,
  36. rules
  37. }"
  38. :dialog="{ title: dialogTitle + '菜单', width: 600, confirmLoading }"
  39. @confirm="handleSubmit"
  40. @close="handleClose"
  41. />
  42. <PlusDialog
  43. v-model="detailsVisible"
  44. :hasFooter="false"
  45. title="接口配置"
  46. width="500"
  47. >
  48. <PlusDescriptions
  49. :column="24"
  50. :columns="descriptionsColumns"
  51. :data="descriptionsData"
  52. />
  53. </PlusDialog>
  54. </div>
  55. </template>
  56. <script lang="ts" setup>
  57. import { computed, reactive, ref, toRefs } from "vue";
  58. import type { FormRules } from "element-plus";
  59. import { ElMessage, ElMessageBox } from "element-plus";
  60. import {
  61. type FieldValues,
  62. type PlusColumn,
  63. PlusDescriptions,
  64. PlusDialog,
  65. PlusDialogForm,
  66. PlusPage,
  67. PlusPageInstance,
  68. useTable
  69. } from "plus-pro-components";
  70. import {
  71. addOrEditIpInterface,
  72. deleteIpInterface,
  73. getIpInterfaceList,
  74. switchIpInterfaceStatus
  75. } from "@/api/system/ipInterface";
  76. defineOptions({
  77. name: "IpInterfaceIndex"
  78. });
  79. const plusPageInstance = ref<PlusPageInstance | null>(null);
  80. const getList = async (query: Record<string, any>) => {
  81. let res = await getIpInterfaceList(query);
  82. return {
  83. data: res.list,
  84. total: res.total
  85. };
  86. };
  87. // 重新请求列表接口
  88. const refresh = () => {
  89. plusPageInstance.value?.getList();
  90. };
  91. const handleTableChange = (values: any) => {
  92. console.log(values);
  93. ElMessageBox.confirm("确定修改此数据吗?", "提示", {
  94. confirmButtonText: "确定",
  95. cancelButtonText: "取消",
  96. type: "warning"
  97. })
  98. .then(async () => {
  99. try {
  100. let data = {
  101. id: values.row.id,
  102. status: values.row.status
  103. };
  104. let res = await switchIpInterfaceStatus(data);
  105. if (res.code === 200) {
  106. ElMessage.success("修改成功");
  107. } else {
  108. ElMessage.error(res.msg);
  109. }
  110. } catch (e) {
  111. ElMessage.error("修改失败");
  112. }
  113. })
  114. .finally(() => {
  115. refresh();
  116. });
  117. };
  118. const dialogTitle = computed(() => (state.isCreate ? "新增" : "编辑"));
  119. // 表格数据
  120. const tableConfig: PlusColumn[] = [
  121. {
  122. label: "接口名称",
  123. prop: "interfaceName"
  124. },
  125. {
  126. label: "接口编码",
  127. prop: "interfaceNo",
  128. hideInSearch: true
  129. },
  130. {
  131. label: "接口厂商",
  132. prop: "manufacturer",
  133. hideInSearch: true
  134. },
  135. {
  136. label: "状态",
  137. prop: "status",
  138. valueType: "switch",
  139. fieldProps: {
  140. activeValue: "normal",
  141. inactiveValue: "disable"
  142. },
  143. editable: true,
  144. hideInSearch: true
  145. },
  146. {
  147. label: "状态",
  148. prop: "status",
  149. valueType: "select",
  150. options: [
  151. { label: "正常", value: "normal" },
  152. { label: "停用", value: "disable" }
  153. ],
  154. hideInTable: true
  155. },
  156. {
  157. label: "备注",
  158. prop: "remark",
  159. hideInSearch: true
  160. },
  161. {
  162. label: "创建时间",
  163. prop: "addTime",
  164. valueType: "time-picker",
  165. hideInSearch: true
  166. },
  167. {
  168. label: "创建人",
  169. prop: "addUserName",
  170. hideInSearch: true
  171. }
  172. ];
  173. /*--------------------表单--------------------*/
  174. // 表单实例
  175. const dialogForm = ref(null);
  176. interface State {
  177. dialogVisible: boolean;
  178. detailsVisible: boolean;
  179. confirmLoading: boolean;
  180. selectedIds: number[];
  181. isCreate: boolean;
  182. form: {};
  183. rules: FormRules;
  184. descriptionsData: {};
  185. }
  186. const state = reactive<State>({
  187. dialogVisible: false,
  188. detailsVisible: false,
  189. confirmLoading: false,
  190. selectedIds: [],
  191. isCreate: false,
  192. form: {},
  193. rules: {
  194. interfaceName: [
  195. { required: true, message: "请输入接口名称", trigger: "blur" }
  196. ],
  197. interfaceNo: [
  198. { required: true, message: "请输入接口编码", trigger: "blur" }
  199. ],
  200. manufacturer: [
  201. { required: true, message: "请输入接口厂商", trigger: "blur" }
  202. ],
  203. interfaceConfig: [
  204. { required: true, message: "请输入接口配置", trigger: "blur" }
  205. ]
  206. },
  207. descriptionsData: {}
  208. });
  209. const columns: PlusColumn[] = [
  210. {
  211. label: "接口名称",
  212. prop: "interfaceName",
  213. valueType: "input",
  214. fieldProps: {
  215. clearable: true
  216. }
  217. },
  218. {
  219. label: "接口编码",
  220. prop: "interfaceNo",
  221. valueType: "input",
  222. fieldProps: {
  223. clearable: true
  224. }
  225. },
  226. {
  227. label: "接口厂商",
  228. prop: "manufacturer",
  229. valueType: "input",
  230. fieldProps: {
  231. clearable: true
  232. }
  233. },
  234. {
  235. label: "状态",
  236. prop: "status",
  237. valueType: "radio",
  238. fieldProps: {
  239. options: [
  240. { label: "正常", value: "normal" },
  241. { label: "停用", value: "disable" }
  242. ]
  243. }
  244. },
  245. {
  246. label: "接口配置",
  247. prop: "interfaceConfig",
  248. valueType: "textarea",
  249. fieldProps: {
  250. rows: 4
  251. }
  252. },
  253. {
  254. label: "备注",
  255. prop: "remark",
  256. valueType: "textarea",
  257. fieldProps: {
  258. rows: 4
  259. }
  260. }
  261. ];
  262. const descriptionsColumns: PlusColumn[] = [
  263. {
  264. label: "接口配置",
  265. prop: "interfaceConfig",
  266. valueType: "copy",
  267. descriptionsItemProps: {
  268. span: 24,
  269. className: "break-all"
  270. }
  271. }
  272. ];
  273. // 创建
  274. const handleCreate = (): void => {
  275. form.value = {
  276. status: "normal"
  277. };
  278. state.isCreate = true;
  279. state.dialogVisible = true;
  280. };
  281. const handleSubmit = async (values: FieldValues) => {
  282. console.log(values, "Submit");
  283. confirmLoading.value = true;
  284. if (state.isCreate) {
  285. try {
  286. let params = form.value;
  287. let res = await addOrEditIpInterface(params);
  288. if (res.code === 200) {
  289. ElMessage.success("新增成功");
  290. confirmLoading.value = false;
  291. dialogVisible.value = false;
  292. } else {
  293. ElMessage.error(res.msg);
  294. }
  295. } finally {
  296. confirmLoading.value = false;
  297. refresh();
  298. }
  299. } else {
  300. // 编辑
  301. try {
  302. let params = form.value;
  303. let res = await addOrEditIpInterface(params);
  304. if (res.code === 200) {
  305. ElMessage.success("修改成功");
  306. confirmLoading.value = false;
  307. dialogVisible.value = false;
  308. refresh();
  309. } else {
  310. ElMessage.error(res.msg);
  311. }
  312. } finally {
  313. confirmLoading.value = false;
  314. refresh();
  315. }
  316. }
  317. };
  318. const handleClose = () => {
  319. console.log(dialogForm.value.formInstance);
  320. };
  321. // 参数预期管理
  322. const normalizeParams = (params: Record<string, any>) => {
  323. return {
  324. interfaceName: params.interfaceName ?? null,
  325. interfaceNo: params.interfaceNo ?? null,
  326. manufacturer: params.manufacturer ?? null,
  327. status: params.status ?? null,
  328. remark: params.remark ?? null,
  329. interfaceConfig: params.interfaceConfig ?? null,
  330. id: params.id ?? null
  331. };
  332. };
  333. const { buttons } = useTable();
  334. buttons.value = [
  335. {
  336. // 修改
  337. text: "修改",
  338. code: "edit",
  339. // props v0.1.16 版本新增函数类型
  340. props: {
  341. type: "primary"
  342. },
  343. onClick(params) {
  344. // form.value = params.row;
  345. Object.assign(form.value, normalizeParams(params.row));
  346. state.isCreate = false;
  347. state.dialogVisible = true;
  348. }
  349. },
  350. {
  351. // 修改
  352. text: "查看配置",
  353. code: "config",
  354. // props v0.1.16 版本新增函数类型
  355. props: {
  356. type: "primary"
  357. },
  358. onClick(val) {
  359. console.log(val);
  360. detailsVisible.value = true;
  361. descriptionsData.value = {
  362. interfaceConfig: val.row.interfaceConfig
  363. };
  364. }
  365. },
  366. {
  367. // 删除
  368. text: "删除",
  369. code: "delete",
  370. // props v0.1.16 版本新增计算属性支持
  371. props: computed(() => ({ type: "danger" })),
  372. confirm: {
  373. options: {
  374. draggable: true,
  375. message: "确定删除此数据吗?"
  376. }
  377. },
  378. onConfirm: async params => {
  379. try {
  380. let res = await deleteIpInterface(params.row.id);
  381. if (res.code === 200) {
  382. ElMessage.success("删除成功");
  383. refresh();
  384. } else {
  385. ElMessage.error(res.msg);
  386. }
  387. } catch (e) {
  388. ElMessage.error("删除失败");
  389. }
  390. }
  391. }
  392. ];
  393. const {
  394. form,
  395. confirmLoading,
  396. rules,
  397. dialogVisible,
  398. detailsVisible,
  399. descriptionsData
  400. } = toRefs(state);
  401. </script>