dialog.vue.vm 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <style scoped lang="scss">
  2. </style>
  3. <template>
  4. <div class="system-dialog-container">
  5. <el-dialog
  6. :title="state.dialog.title"
  7. v-model="state.dialog.visible"
  8. width="820px"
  9. draggable
  10. destroy-on-close
  11. :close-on-click-modal="false"
  12. align-center>
  13. <el-form
  14. inline
  15. :model="state.ruleForm"
  16. :rules="rules"
  17. label-position="top"
  18. ref="formRef"
  19. size="default"
  20. label-width="100px"
  21. class="mt5">
  22. #foreach($ele in $fields)
  23. #if($ele.columnName!="id"&&$ele.columnName!="createTime"&&$ele.columnName!="updateTime" )
  24. <el-form-item label="$ele.columnComment" prop="$ele.columnName">
  25. <el-input
  26. v-model="state.ruleForm.$ele.columnName"
  27. placeholder="$ele.columnComment"
  28. clearable
  29. class="wd350">
  30. </el-input>
  31. </el-form-item>
  32. #end
  33. #end
  34. </el-form>
  35. <template #footer>
  36. <el-row>
  37. <el-col :span="12">
  38. <p class="text-align-left">
  39. <el-checkbox v-model="state.continueAdd" v-if="!state.ruleForm.id">继续创建</el-checkbox>
  40. <template v-else>
  41. <el-button @click="handleActive" type="success" v-if="state.ruleForm.status !=1" size="default">激 活</el-button>
  42. <el-button @click="handleActive" type="danger" v-else size="default">禁 用</el-button>
  43. </template>
  44. </p>
  45. </el-col>
  46. <el-col :span="12">
  47. <p class="dialog-footer">
  48. <el-button @click="onCancel" size="default">取 消</el-button>
  49. <el-button :loading="state.btnLoading" type="primary" @click="onSubmit" size="default">{{ state.dialog.submitTxt }}</el-button>
  50. </p>
  51. </el-col>
  52. </el-row>
  53. </template>
  54. </el-dialog>
  55. </div>
  56. </template>
  57. <script setup lang="ts" name="${pojoName}Dialog">
  58. import {defineAsyncComponent, reactive, onMounted, ref} from 'vue';
  59. import {Msg} from "/@/utils/message";
  60. import {$body, $get} from "/@/utils/request";
  61. import u from '/@/utils/u'
  62. // 引入异步组件
  63. // const ExtDetailForm = defineAsyncComponent(() => import('/@/components/form/ExtDetailForm.vue'));
  64. // 定义子组件向父组件传值/事件
  65. const emit = defineEmits(['refresh']);
  66. const formRef = ref();
  67. //定义初始变量,重置使用
  68. const initState = () => ({
  69. ruleForm: {
  70. id: 0
  71. },
  72. btnLoading: false,
  73. dialog: {
  74. visible: false,
  75. type: '',
  76. title: '',
  77. submitTxt: '',
  78. },
  79. rules: {},
  80. continueAdd: false
  81. })
  82. // 定义变量内容
  83. const state = reactive(initState());
  84. // 打开弹窗
  85. const open = (action: string = 'add', row: any) => {
  86. state.dialog.title = u.dialog.actions[action].title + "『${pojoComment}』"
  87. state.dialog.submitTxt = u.dialog.actions[action].btn + "『${pojoComment}』"
  88. state.dialog.visible = true;
  89. if (action !== 'add') {
  90. loadData(row.id);
  91. } else {
  92. state.ruleForm = Object.assign(state.ruleForm, row);
  93. }
  94. };
  95. // 关闭弹窗
  96. const onClose = () => {
  97. state.dialog.visible = false;
  98. Object.assign(state, initState())
  99. };
  100. // 取消
  101. const onCancel = () => {
  102. onClose();
  103. };
  104. // 提交
  105. const onSubmit = () => {
  106. formRef.value.validate((v: boolean) => {
  107. if (v) {
  108. state.btnLoading = true;
  109. const url = state.ruleForm.id > 0 ? "${firstLowPojoName}/modify" : "${firstLowPojoName}/add"
  110. $body(url, state.ruleForm).then(() => {
  111. state.btnLoading = false;
  112. Msg.message('操作成功');
  113. console.log('submit!')
  114. if (!state.continueAdd) {
  115. onClose();
  116. emit('refresh');
  117. }
  118. })
  119. } else {
  120. state.btnLoading = false;
  121. Msg.message('请先完整填写表单', 'error');
  122. }
  123. })
  124. };
  125. const handleFormChange = (formData: any) => {
  126. console.log(formData)
  127. }
  128. // 初始化详情页
  129. const loadData = (id: number) => {
  130. $get(`${firstLowPojoName}/detail/${id}`).then((res: any) => {
  131. state.ruleForm = res;
  132. })
  133. }
  134. // 暴露变量
  135. defineExpose({
  136. open
  137. });
  138. </script>