edit.vue 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <script setup lang="ts">
  2. import { reactive, ref } from "vue";
  3. import * as $api from "@/api/agent/index";
  4. import { message } from "@/utils/message";
  5. import type { FormInstance } from "element-plus";
  6. import { cloneDeep } from "@pureadmin/utils";
  7. const title = "";
  8. defineOptions({ name: "templateEdit" });
  9. const isDetail = ref(false);
  10. const formRef = ref<FormInstance>();
  11. // 初始化数据
  12. const initData = {
  13. userName: undefined,
  14. password: undefined,
  15. status: 1
  16. };
  17. // 表单校验规则
  18. const formRules = {};
  19. // 页面数据
  20. const pageData: any = reactive({
  21. dialogVisible: false,
  22. title: "",
  23. formLoading: false,
  24. mode: "add",
  25. isUpdate: false,
  26. // 表单数据
  27. formData: initData,
  28. formRules: formRules
  29. });
  30. // 暴露给父级调用
  31. const emits = defineEmits(["ok", "close"]);
  32. // 打开弹窗
  33. const open = (data: any, dataSource: any, mode: string) => {
  34. pageData.formData = cloneDeep(data) || initData;
  35. if (mode === "edit") {
  36. pageData.title = `${title} - 编辑`;
  37. isDetail.value = false;
  38. } else if (mode === "detail") {
  39. pageData.title = `${title} - 查看`;
  40. isDetail.value = true;
  41. } else {
  42. pageData.title = `${title} - 新增`;
  43. isDetail.value = false;
  44. }
  45. pageData.dataSource = dataSource;
  46. pageData.isUpdate = !!pageData.formData.id;
  47. pageData.dialogVisible = true;
  48. };
  49. // 关闭弹窗
  50. const handleClose = () => {
  51. pageData.dialogVisible = false;
  52. emits("close");
  53. };
  54. // 确定并关闭弹窗
  55. const handleConfirm = () => {
  56. formRef.value!.validate((isValid: boolean) => {
  57. if (isValid) {
  58. const { id } = pageData.formData;
  59. if (id) {
  60. _update();
  61. } else {
  62. _save();
  63. }
  64. } else {
  65. message("表单校验失败", { type: "warning" });
  66. }
  67. });
  68. };
  69. const _save = () => {
  70. pageData.formLoading = true;
  71. const _data = cloneDeep(pageData.formData);
  72. $api
  73. .save(_data)
  74. .then((res: any) => {
  75. if (res.success) {
  76. _confirm();
  77. } else {
  78. message(res.message, { type: "warning" });
  79. }
  80. })
  81. .finally(() => {
  82. pageData.formLoading = false;
  83. });
  84. };
  85. const _update = () => {
  86. pageData.formLoading = true;
  87. const _data = cloneDeep(pageData.formData);
  88. $api
  89. .update(pageData.formData.id, _data)
  90. .then((res: any) => {
  91. if (res.success) {
  92. _confirm();
  93. } else {
  94. message(res.message, { type: "warning" });
  95. }
  96. })
  97. .finally(() => {
  98. pageData.formLoading = false;
  99. });
  100. };
  101. const _confirm = () => {
  102. pageData.dialogVisible = false;
  103. emits("ok");
  104. };
  105. defineExpose({ open });
  106. </script>
  107. <template>
  108. <el-dialog v-model="pageData.dialogVisible" destroy-on-close :width="500">
  109. <template #header>
  110. <el-text class="mx-1" type="primary" size="large">{{
  111. pageData.title
  112. }}</el-text>
  113. </template>
  114. <div class="el-dialog-content">
  115. <el-form
  116. ref="formRef"
  117. :model="pageData.formData"
  118. style="width: 90%; margin: 20px auto 0"
  119. label-width="auto"
  120. :rules="pageData.formRules"
  121. :loading="pageData.formLoading"
  122. >
  123. <el-form-item label="代理名称" prop="userName">
  124. <el-input
  125. v-model="pageData.formData.userName"
  126. clearable
  127. placeholder="请输入名称"
  128. size="small"
  129. :readonly="isDetail"
  130. />
  131. </el-form-item>
  132. <el-form-item label="密码" prop="password">
  133. <el-input
  134. v-model="pageData.formData.password"
  135. type="password"
  136. clearable
  137. placeholder="请输入密码"
  138. size="small"
  139. :readonly="isDetail"
  140. />
  141. </el-form-item>
  142. </el-form>
  143. </div>
  144. <template #footer>
  145. <el-button v-if="!isDetail" @click="handleClose">取消</el-button>
  146. <el-button v-if="!isDetail" type="primary" @click="handleConfirm"
  147. >确认</el-button
  148. >
  149. </template>
  150. </el-dialog>
  151. </template>