addOrEdit.vue 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <template>
  2. <div>
  3. <el-dialog :title="(ruleForm.id ? $t('message.device.tableI18nAction.editCategory') : $t('message.device.tableI18nAction.addCategory'))" v-model="isShowDialog" width="600px">
  4. <el-form ref="formRef" :model="ruleForm" :rules="rules" label-width="90px">
  5. <el-row :gutter="35">
  6. <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
  7. <!-- 上级分类 -->
  8. <el-form-item :label="$t('message.formI18nLabel.parentCategory')" :label-width="currentLocale == 'en' ? '120px' : '90px'">
  9. <el-cascader :options="deptData" :props="{ checkStrictly: true, emitPath: false, value: 'id', label: 'name' }" :placeholder="$t('message.formI18nPlaceholder.parentCategory')" clearable class="w100" v-model="ruleForm.parentId">
  10. <template #default="{ node, data }">
  11. <span>{{ data.name }}</span>
  12. <span v-if="!node.isLeaf"> ({{ data.children.length }}) </span>
  13. </template>
  14. </el-cascader>
  15. </el-form-item>
  16. </el-col>
  17. <el-col :xs="24" :sm="12" :md="16" :lg="16" :xl="16">
  18. <!-- 分类名称 -->
  19. <el-form-item :label="$t('message.formI18nLabel.categoryName')" prop="name" :label-width="currentLocale == 'en' ? '120px' : '90px'">
  20. <el-input v-model="ruleForm.name" :placeholder="$t('message.formI18nPlaceholder.categoryName')" clearable></el-input>
  21. </el-form-item>
  22. </el-col>
  23. <el-col :xs="24" :sm="12" :md="8" :lg="8" :xl="8">
  24. <!-- 排序 -->
  25. <el-form-item :label="$t('message.formI18nLabel.sort')" prop="sort" label-width="40px">
  26. <el-input-number v-model="ruleForm.sort" :min="0" :max="100" step="1" step-strictly="true" controls-position="right" :placeholder="$t('message.formI18nPlaceholder.sort')" class="w100" />
  27. </el-form-item>
  28. </el-col>
  29. <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
  30. <!-- 分类标识 -->
  31. <el-form-item :label="$t('message.formI18nLabel.categoryKey')" prop="key" :label-width="currentLocale == 'en' ? '120px' : '90px'">
  32. <el-input v-model="ruleForm.key" :placeholder="$t('message.formI18nPlaceholder.categoryKey')" clearable></el-input>
  33. </el-form-item>
  34. </el-col>
  35. <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
  36. <!-- 描述 -->
  37. <el-form-item :label="$t('message.formI18nLabel.desc')" prop="desc" :label-width="currentLocale == 'en' ? '120px' : '90px'">
  38. <el-input v-model="ruleForm.desc" type="textarea" :placeholder="$t('message.formI18nPlaceholder.desc')" maxlength="150"></el-input>
  39. </el-form-item>
  40. </el-col>
  41. </el-row>
  42. </el-form>
  43. <template #footer>
  44. <span class="dialog-footer">
  45. <el-button @click="onCancel">{{ $t('message.tableI18nAction.cancel') }}</el-button>
  46. <el-button type="primary" @click="onSubmit" :loading="loading">{{ ruleForm.id ? $t('message.tableI18nAction.edit') : $t('message.tableI18nAction.add') }}</el-button>
  47. </span>
  48. </template>
  49. </el-dialog>
  50. </div>
  51. </template>
  52. <script lang="ts" setup>
  53. import { reactive, toRefs, defineComponent, ref, unref, computed, defineEmits } from 'vue';
  54. import api from '/@/api/device';
  55. import { ElMessage } from 'element-plus';
  56. import { useI18n } from 'vue-i18n';
  57. // 国际化
  58. const { locale, t } = useI18n();
  59. const currentLocale = computed(() => locale.value);
  60. const emit = defineEmits(['getCateList']);
  61. interface RuleFormState {
  62. id?: number;
  63. parentId: number;
  64. name: string;
  65. key: string;
  66. desc: string;
  67. sort: number;
  68. children?: RuleFormState[];
  69. }
  70. const baseForm: RuleFormState = {
  71. parentId: 0, // 上级分类
  72. name: '', // 分类名称
  73. key: '',
  74. desc: '',
  75. sort: 0
  76. };
  77. const formRef = ref<HTMLElement | null>(null);
  78. const loading = ref(false); // 添加loading状态'
  79. const isShowDialog = ref(false);
  80. const deptData = ref([]);// 分类数据
  81. const orgData = ref([]);// 组织数据
  82. // 使用computed自动响应语言变化
  83. const rules = computed(() => ({
  84. name: [{ required: true, message: t('message.formI18nPlaceholder.categoryName'), trigger: 'blur' }],
  85. key: [{ required: true, message: t('message.formI18nPlaceholder.categoryKey'), trigger: 'blur' }],
  86. }));
  87. const ruleForm = ref({
  88. ...baseForm,
  89. })
  90. // // 打开弹窗
  91. const openDialog = (row?: RuleFormState | number) => {
  92. resetForm();
  93. api.category.getList({ status: 1 }).then((res: any) => {
  94. deptData.value = res.category || [];
  95. });
  96. if (row && typeof row === 'object') {
  97. ruleForm.value = row;
  98. } else if (row && typeof row === 'number') {
  99. ruleForm.value.parentId = row;
  100. }
  101. isShowDialog.value = true;
  102. };
  103. // 关闭弹窗
  104. const closeDialog = () => {
  105. isShowDialog.value = false;
  106. };
  107. // 取消
  108. const onCancel = () => {
  109. closeDialog();
  110. };
  111. // 新增
  112. const onSubmit = () => {
  113. const formWrap = unref(formRef) as any;
  114. if (!formWrap) return;
  115. formWrap.validate(async (valid: boolean) => {
  116. if (valid) {
  117. // 禁用按钮
  118. loading.value = true;
  119. if (!ruleForm.value.parentId) {
  120. ruleForm.value.parentId = 0;
  121. }
  122. if (!ruleForm.value.id) {
  123. //添加
  124. try {
  125. // 等待提交完成
  126. await api.category.add(ruleForm.value);
  127. ElMessage.success(t('message.tableI18nConfirm.addSuccess'));
  128. closeDialog(); // 关闭弹窗
  129. emit('getCateList');
  130. } catch (error) {
  131. ElMessage.error(t('message.tableI18nConfirm.addFailed'));
  132. }
  133. } else {
  134. //修改
  135. try {
  136. // 等待提交完成
  137. await api.category.edit(ruleForm.value);
  138. ElMessage.success(t('message.tableI18nConfirm.editSuccess'));
  139. closeDialog(); // 关闭弹窗
  140. emit('getCateList');
  141. } catch (error) { }
  142. }
  143. // 启用按钮
  144. loading.value = false;
  145. } else {
  146. // 表单验证失败时,启用按钮
  147. loading.value = false;
  148. }
  149. });
  150. };
  151. const resetForm = () => {
  152. ruleForm.value = {
  153. ...baseForm,
  154. };
  155. };
  156. defineExpose({ openDialog });
  157. </script>