editDialog.vue.vm 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <script lang="ts" name="@(Model.LowerClassName)" setup>
  2. import { ref, reactive, onMounted } from "vue";
  3. import { ElMessage } from "element-plus";
  4. import type { FormRules } from "element-plus";
  5. @if(Model.TableField.Any(x => x.EffectType == "DatePicker")) {
  6. @:import { formatDate } from '/@@/utils/formatTime';
  7. }
  8. @if(Model.UploadFieldList.Count > 0) {
  9. @:import { Plus } from "@@element-plus/icons-vue";
  10. @:import { UploadRequestOptions } from "element-plus";
  11. }
  12. @if(Model.HasConstField) {
  13. @:import { useUserInfo } from "/@@/stores/userInfo";
  14. }
  15. import { use@(Model.ClassName)Api } from '/@@/api/@(Model.PagePath)/@(Model.LowerClassName)';
  16. //父级传递来的函数,用于回调
  17. const emit = defineEmits(["reloadTable"]);
  18. const @(Model.LowerClassName)Api = use@(Model.ClassName)Api();
  19. const ruleFormRef = ref();
  20. const state = reactive({
  21. title: '',
  22. loading: false,
  23. showDialog: false,
  24. ruleForm: {} as any,
  25. stores: @(Model.HasConstField ? "useUserInfo()" : "{}"),
  26. dropdownData: {} as any,
  27. });
  28. // 自行添加其他规则
  29. const rules = ref<FormRules>({
  30. @foreach (var column in Model.AddUpdateFieldList.Where(col => col.WhetherRequired == "Y")) {
  31. var trigger = column.EffectType == "DatePicker" || column.EffectType == "DictSelector" || column.EffectType == "EnumSelector" || column.EffectType == "ApiTreeSelector" ? "change" : "blur";
  32. @:@column.LowerPropertyName: [{required: true, message: '请选择@(column.ColumnComment)!', trigger: '@trigger',},],
  33. }
  34. });
  35. // 页面加载时
  36. onMounted(async () => {
  37. @if (Model.DropdownFieldList.Count > 0) {
  38. @:const data = await @(Model.LowerClassName)Api.getDropdownData(false).then(res => res.data.result) ?? {};
  39. @foreach (var column in Model.DropdownFieldList) {
  40. @:state.dropdownData.@(column.LowerPropertyName) = data.@(column.LowerPropertyName) ?? [];
  41. }
  42. }
  43. });
  44. // 打开弹窗
  45. const openDialog = async (row: any, title: string) => {
  46. state.title = title;
  47. row = row ?? { @(Model.GetAddDefaultValue()) };
  48. state.ruleForm = row.id ? await @(Model.LowerClassName)Api.detail(row.id).then(res => res.data.result) : JSON.parse(JSON.stringify(row));
  49. state.showDialog = true;
  50. };
  51. // 关闭弹窗
  52. const closeDialog = () => {
  53. emit("reloadTable");
  54. state.showDialog = false;
  55. };
  56. // 提交
  57. const submit = async () => {
  58. ruleFormRef.value.validate(async (isValid: boolean, fields?: any) => {
  59. if (isValid) {
  60. let values = state.ruleForm;
  61. await @(Model.LowerClassName)Api[state.ruleForm.@(Model.PrimaryKeyFieldList.First().LowerPropertyName) ? 'update' : 'add'](values);
  62. closeDialog();
  63. } else {
  64. ElMessage({
  65. message: `表单有${Object.keys(fields).length}处验证失败,请修改后再提交`,
  66. type: "error",
  67. });
  68. }
  69. });
  70. };
  71. @foreach (var column in Model.UploadFieldList) {
  72. @:const upload@(column.PropertyName)Handle = async (options: UploadRequestOptions) => {
  73. @:const res = await @(Model.LowerClassName)Api.upload@(column.PropertyName)(options);
  74. @:state.ruleForm.@(column.LowerPropertyName) = res.data.result?.url;
  75. @:};
  76. @:
  77. }
  78. //将属性或者函数暴露给父组件
  79. defineExpose({ openDialog });
  80. </script>
  81. <template>
  82. <div class="@(Model.LowerClassName)-container">
  83. <el-dialog v-model="state.showDialog" :width="800" draggable :close-on-click-modal="false">
  84. <template #header>
  85. <div style="color: #fff">
  86. <span>{{ state.title }}</span>
  87. </div>
  88. </template>
  89. <el-form :model="state.ruleForm" ref="ruleFormRef" label-width="auto" :rules="rules">
  90. <el-row :gutter="35">
  91. @foreach (var column in Model.PrimaryKeyFieldList) {
  92. @:<el-form-item v-show="false">
  93. @:<el-input v-model="state.ruleForm.@(column.LowerPropertyName)" />
  94. @:</el-form-item>
  95. }
  96. @foreach (var column in Model.AddUpdateFieldList) {
  97. var showStatus = Model.IsStatus(column) ? $"v-if=\"state.ruleForm.{Model.PrimaryKeyFieldList.First().LowerPropertyName}\" " : "";
  98. @:<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" @showStatus>
  99. @:<el-form-item label="@column.ColumnComment" prop="@(column.LowerPropertyName)">
  100. if (column.IsSelectorEffectType) {
  101. if (column.EffectType == "ApiTreeSelector") {
  102. @:<el-cascader
  103. @::options="state.dropdownData.@(column.LowerPropertyName)"
  104. @::props="{ checkStrictly: true, emitPath: false }"
  105. @:v-model="state.ruleForm.@(column.LowerPropertyName)"
  106. @:placeholder="请选择@(column.ColumnComment)"
  107. @:clearable
  108. @:filterable
  109. @:class="w100">
  110. @:<template #default="{ node, data }">
  111. @:<span>{{ data.label }}</span>
  112. @:<span v-if="!node.isLeaf"> ({{ data.children.length }}) </span>
  113. @:</template>
  114. @:</el-cascader>
  115. } else if (column.EffectType == "DictSelector" || column.EffectType == "EnumSelector") {
  116. @:<g-sys-dict v-model="state.ruleForm.@(column.LowerPropertyName)" code="@(column.DictTypeCode)" render-as="select" placeholder="请选择@(column.ColumnComment)" clearable filterable />
  117. } else {
  118. @:<el-select clearable filterable v-model="state.ruleForm.@(column.LowerPropertyName)" placeholder="请选择@(column.ColumnComment)">
  119. if (column.EffectType == "ForeignKey") {
  120. @:<el-option v-for="(item,index) in state.dropdownData.@(column.LowerPropertyName)" :key="index" :value="item.value" :label="item.label" />
  121. } else if (column.EffectType == "ConstSelector") {
  122. @:<el-option v-for="(item, index) in state.stores.getConstDataByTypeCode('@column.DictTypeCode')" :key="index" :label="item.name" :value="item.code" />
  123. }
  124. @:</el-select>
  125. }
  126. } else if (column.EffectType == "Upload") {
  127. @:<el-upload
  128. @:list-type="picture-card"
  129. @::show-file-list="false"
  130. @::http-request="upload@(column.PropertyName)Handle">
  131. @:<img
  132. @:v-if="state.ruleForm.@(column.LowerPropertyName)"
  133. @::src="state.ruleForm.@(column.LowerPropertyName)"
  134. @:@@click="state.ruleForm.@(column.LowerPropertyName)=''"
  135. @:style="width: 100%; height: 100%; object-fit: contain"/>
  136. @:<el-icon v-else><Plus /></el-icon>
  137. @:</el-upload>
  138. } else if (column.EffectType == "InputNumber") {
  139. @:<el-input-number v-model="state.ruleForm.@(column.LowerPropertyName)" placeholder="请输入@(column.ColumnComment)" clearable />
  140. } else if (column.EffectType == "Switch") {
  141. @:<el-switch v-model="state.ruleForm.@(column.LowerPropertyName)" active-text="是" inactive-text="否" />
  142. } else if (column.EffectType == "DatePicker") {
  143. @:<el-date-picker v-model="state.ruleForm.@(column.LowerPropertyName)" type="date" placeholder="@(column.ColumnComment)" />
  144. } else {
  145. var inputType = column.EffectType == "InputTextArea" ? "type=\"textarea\" " : "";
  146. var maxlength = column.ColumnLength > 0 ? $"maxlength=\"{column.ColumnLength}\" " : "";
  147. @:<el-input v-model="state.ruleForm.@(column.LowerPropertyName)" placeholder="请输入@(column.ColumnComment)" @(maxlength)@(inputType)show-word-limit clearable />
  148. }
  149. @:</el-form-item>
  150. @:</el-col>
  151. }
  152. </el-row>
  153. </el-form>
  154. <template #footer>
  155. <span class="dialog-footer">
  156. <el-button @@click="() => state.showDialog = false">取 消</el-button>
  157. <el-button @@click="submit" type="primary" v-reclick="1000">确 定</el-button>
  158. </span>
  159. </template>
  160. </el-dialog>
  161. </div>
  162. </template>
  163. <style lang="scss" scoped>
  164. :deep(.el-select), :deep(.el-input-number) {
  165. width: 100%;
  166. }
  167. </style>