editDialog.vue.vm 8.4 KB

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