wangrong
2026-08-30 4c949c0dc9bcae785c281c9d8125295f09810b15
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
<script lang="ts" setup>
import icon_upload_outlined from '@/assets/svg/icon_upload_outlined.svg'
import { ref, reactive } from 'vue'
import { uploadFontFile } from '@/api/font'
import { useI18n } from '@/hooks/web/useI18n'
import FontInfo from './FontInfo.vue'
import { ElMessage } from 'element-plus-secondary'
import { edit } from '@/api/font'
import { cloneDeep } from 'lodash-es'
 
const state = reactive({
  fileList: null
})
const loading = ref(false)
const upload = ref()
const { t } = useI18n()
const uploadExcel = () => {
  const formData = new FormData()
  formData.append('file', state.fileList.raw)
  loading.value = true
  return uploadFontFile(formData)
    .then(res => {
      ruleForm.name = res.data.name
      ruleForm.size = res.data.size
      ruleForm.sizeType = res.data.sizeType
      ruleForm.fileTransName = res.data.fileTransName
      ruleForm.fileName = state.fileList.raw.name
      upload.value?.clearFiles()
    })
    .catch(error => {
      if (error.code === 'ECONNABORTED') {
        ElMessage({
          type: 'error',
          message: error.message,
          showClose: true
        })
      }
    })
    .finally(() => {
      loading.value = false
    })
}
const dialogTitle = ref('')
const dialogVisible = ref(false)
const action = ref('')
const defaultForm = {
  id: null,
  name: '',
  fileName: '',
  fileTransName: '',
  size: 0,
  sizeType: '',
  isDefault: 0,
  isBuiltin: 0,
  updateTime: 0
}
const ruleForm = reactive(cloneDeep(defaultForm))
 
const init = (val, type, item) => {
  dialogTitle.value = val || t('system.add_font')
  action.value = type
  dialogVisible.value = true
  Object.assign(ruleForm, cloneDeep(defaultForm))
  Object.assign(ruleForm, JSON.parse(JSON.stringify(item)))
}
 
const fontDel = () => {
  state.fileList = null
}
 
const ruleFormRef = ref()
const rules = {
  name: [
    { required: true, message: t('system.the_font_name'), trigger: 'blur' },
    { min: 1, max: 50, message: t('system.character_length_1_50'), trigger: 'blur' }
  ]
}
defineExpose({
  init
})
 
const beforeAvatarUpload = rawFile => {
  if (!rawFile.name.toLocaleLowerCase().endsWith('.ttf')) {
    ElMessage.error(t('system.in_ttf_format'))
    return false
  }
  return true
}
const onChange = file => {
  if (file.raw?.name?.toLocaleLowerCase().endsWith('.ttf')) {
    state.fileList = file
  }
}
 
const uploadFail = response => {
  let myError = response.toString()
  myError.replace('Error: ', '')
}
 
const emits = defineEmits(['finish'])
const cancel = () => {
  Object.assign(ruleForm, cloneDeep(defaultForm))
  ruleFormRef.value.clearValidate()
  state.fileList = null
  dialogVisible.value = false
}
const confirm = () => {
  ruleFormRef.value.validate(val => {
    if (val) {
      if (action.value === 'uploadFile') {
        if (ruleForm.fileTransName === '') {
          ElMessage.error(t('system.upload_font_file_de'))
          return
        }
      }
      edit(ruleForm).then(() => {
        ElMessage.success(dialogTitle.value + t('data_set.success'))
        cancel()
        emits('finish')
      })
    }
  })
}
</script>
 
<template>
  <el-dialog
    class="create-dialog add-form_font_dialog"
    v-model="dialogVisible"
    :before-close="cancel"
    :title="dialogTitle"
    width="420"
  >
    <el-form
      @submit.prevent
      ref="ruleFormRef"
      :model="ruleForm"
      label-position="top"
      :rules="rules"
      label-width="auto"
      class="demo-ruleForm"
    >
      <el-form-item v-if="action !== 'uploadFile'" :label="t('system.font_name')" prop="name">
        <el-input :placeholder="t('system.the_font_name')" v-model.trim="ruleForm.name" />
      </el-form-item>
      <el-form-item
        v-loading="loading"
        v-if="action !== 'rename'"
        :label="t('system.font_file_de')"
      >
        <el-upload
          action=""
          :multiple="false"
          ref="uploadAgain"
          :show-file-list="false"
          accept=".ttf"
          :on-change="onChange"
          :before-upload="beforeAvatarUpload"
          :http-request="uploadExcel"
          :on-error="uploadFail"
          name="file"
          v-show="!state.fileList"
        >
          <template #trigger>
            <el-button secondary>
              <template #icon>
                <Icon name="icon_upload_outlined"><icon_upload_outlined class="svg-icon" /></Icon>
              </template>
              {{ t('system.upload_font_file') }}
            </el-button>
          </template>
        </el-upload>
        <FontInfo
          @del="fontDel"
          v-show="state.fileList"
          :size="ruleForm.size + ' ' + ruleForm.sizeType"
          :name="ruleForm.fileName"
        ></FontInfo>
        <el-upload
          action=""
          :multiple="false"
          ref="uploadAgain"
          :before-upload="beforeAvatarUpload"
          :show-file-list="false"
          accept=".ttf"
          :on-change="onChange"
          :http-request="uploadExcel"
          :on-error="uploadFail"
          name="file"
          v-show="state.fileList"
        >
          <template #trigger>
            <el-button text> {{ t('data_source.reupload') }} </el-button>
          </template>
        </el-upload>
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="cancel">{{ t('userimport.cancel') }}</el-button>
        <el-button v-loading="loading" type="primary" @click="confirm">
          {{ t('userimport.sure') }}
        </el-button>
      </div>
    </template>
  </el-dialog>
</template>
 
<style lang="less">
.add-form_font_dialog {
  .ed-dialog__footer {
    border: none;
    padding-top: 0;
    margin-top: -2px;
  }
}
</style>