ny
昨天 282fbc6488f4e8ceb5fda759f963ee88fbf7b999
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
<script lang="ts" setup>
import type { FormInstance } from 'ant-design-vue';
 
import { reactive, ref, toRefs } from 'vue';
 
import { useMessage } from '@jnpf/hooks';
import { ModalClose } from '@jnpf/ui/modal';
 
import { $t } from '@vben/locales';
 
import { globalShareState } from '@vben-core/shared/global-state';
 
import { Modal as AModal, Form, FormItem } from 'ant-design-vue';
 
interface State {
  categoryList: any[];
  dataForm: any;
  formRules: any;
  type: Number;
}
 
defineOptions({ inheritAttrs: false, name: 'I18nFormModal' });
const emit = defineEmits(['register', 'reload']);
const { createBaseLang } = globalShareState.getApi();
const { useBaseStore } = globalShareState.getStores();
 
defineExpose({ openModal });
 
const defaultForm = {
  enCode: '',
  id: '',
  map: {},
  type: 0,
};
const baseStore = useBaseStore();
const formElRef = ref<FormInstance>();
const visible = ref(false);
const loading = ref(false);
const { createMessage } = useMessage();
const state = reactive<State>({
  categoryList: [],
  dataForm: {
    enCode: '',
    id: '',
    map: {},
    type: 0,
  },
  formRules: {
    enCode: [
      { message: '必填', required: true, trigger: 'change' },
      { message: '只能输入字母、数字、点、横线和下划线,且以字母开头', pattern: /^[a-z][\w.-]*$/i, trigger: 'change' },
    ],
  },
  type: 0,
});
const { categoryList, dataForm, formRules } = toRefs(state);
 
async function openModal() {
  visible.value = true;
  loading.value = false;
  state.dataForm = { ...defaultForm };
  formElRef.value?.resetFields();
  state.categoryList = categoryList.value = (await baseStore.getDictionaryData('Language')) as any[];
}
function handleCancel() {
  visible.value = false;
  emit('reload');
}
async function handleSubmit() {
  loading.value = true;
  try {
    const values = await formElRef.value?.validate();
    if (!values) return;
    if (!state.dataForm.map) state.dataForm.map = {};
    for (let i = 0; i < state.categoryList.length; i++) {
      state.dataForm.map[state.categoryList[i].enCode] = state.dataForm[state.categoryList[i].enCode] || '';
    }
    createBaseLang(state.dataForm)
      .then((res) => {
        createMessage.success(res.msg);
        loading.value = false;
        visible.value = false;
        emit('reload', true);
      })
      .catch(() => {
        loading.value = false;
      });
  } catch {
    loading.value = false;
  }
}
</script>
<template>
  <div class="common-container">
    <AModal v-model:open="visible" :title="$t('common.addText')" :width="600" class="common-container-modal i18n-form-modal" centered :mask-closable="false">
      <template #closeIcon>
        <ModalClose :can-fullscreen="false" @cancel="handleCancel" />
      </template>
      <template #footer>
        <a-button @click="handleCancel()">{{ $t('common.cancelText') }}</a-button>
        <a-button type="primary" v-loading="loading" @click="handleSubmit()">{{ $t('common.okText') }}</a-button>
      </template>
      <Form :colon="false" :label-col="{ style: { width: '80px' } }" :model="dataForm" :rules="formRules" ref="formElRef">
        <FormItem label="翻译标记" name="enCode">
          <a-input v-model:value="dataForm.enCode" placeholder="请输入" />
        </FormItem>
        <FormItem :label="item.fullName" :name="item.enCode" v-for="item in categoryList" :key="item.enCode">
          <a-input v-model:value="dataForm[item.enCode]" placeholder="请输入" />
        </FormItem>
      </Form>
    </AModal>
  </div>
</template>
<style lang="scss">
.i18n-form-modal {
  .ant-modal-body {
    height: auto !important;
    padding: 30px 30px 10px !important;
  }
}
</style>