fix(custom): 修复上传组件上传失败以及补全商户分类icon

This commit is contained in:
2023-11-29 18:53:16 +08:00
parent 3336dd7f96
commit 1207a58801
4 changed files with 1905 additions and 4768 deletions

View File

@@ -1,22 +1,17 @@
<template>
<n-upload
v-model:file-list="List"
action="/admin/upload"
:headers="headers"
:max="max"
list-type="image-card"
:custom-request="customRequest"
@before-upload="beforeUpload"
@finish="handleFinish"
@error="handleError"
>
点击上传
</n-upload>
</template>
<script setup>
import { getToken } from '@/utils'
const token = getToken()
import api from '@/api'
const prop = defineProps({
list: {
@@ -31,10 +26,6 @@ const prop = defineProps({
const emit = defineEmits(['update:list'])
const headers = ref({
Authorization: `Bearer ${token}`,
})
const List = computed({
get() {
return prop.list
@@ -44,24 +35,28 @@ const List = computed({
},
})
const handleFinish = ({ file, event }) => {
const res = JSON.parse(event.target.response)
file.url = res.data.data
return file
}
const handleError = () => {
$message.error('上传失败')
}
// 图片上传限制
const beforeUpload = (data) => {
if (!(data.file.file?.type === 'image/png' || data.file.file?.type === 'image/jpeg')) {
const beforeUpload = ({ file }) => {
if (!(file.file?.type === 'image/png' || file.file?.type === 'image/jpeg')) {
$message.error('只能上传png或jpg格式的图片文件,请重新上传')
return false
}
return true
}
const customRequest = async ({ file, onFinish, onError }) => {
try {
const formData = new FormData()
formData.append('file', file.file)
const res = await api.uploadImg(formData)
$message.success(res.msg)
file.url = res.data.data
onFinish()
} catch (error) {
onError()
throw error
}
}
</script>
<style lang="scss" scoped></style>

View File

@@ -24,6 +24,9 @@
aria-modal="true"
>
<n-form ref="formRef" label-placement="left" :model="formValue" :rules="rules">
<n-form-item label="分类图标:" path="icon">
<Upload v-model:list="formValue.icon" />
</n-form-item>
<n-form-item label="分类名称:" path="name">
<n-input v-model:value="formValue.name" placeholder="请输入商户分类名称" />
</n-form-item>
@@ -53,6 +56,8 @@
import { onMounted, h, withDirectives, resolveDirective } from 'vue'
import api from './api'
import { NButton } from 'naive-ui'
import Upload from '@/components/Upload.vue'
const vPerms = resolveDirective('perms')
const loading = ref(false)
@@ -93,7 +98,19 @@ const columns = ref([
type: 'primary',
size: 'small',
onClick: () => {
formValue.value = row
formValue.value = {
...row,
icon:
row.icon.length > 0
? [
{
url: row.icon,
name: '图片',
status: 'finished',
},
]
: [],
}
handleAdd(2)
},
},
@@ -115,12 +132,18 @@ const rules = {
required: true,
message: '请输入商户分类名称',
},
icon: {
required: true,
type: 'array',
message: '请上传分类图标',
},
}
const formValue = ref({
ID: 0,
name: '',
status: 1,
icon: [],
})
const showModal = ref(false)
@@ -171,6 +194,7 @@ const clear = () => {
ID: 0,
name: '',
status: 1,
icon: [],
}
showModal.value = false
}
@@ -180,7 +204,10 @@ const handleValidateClick = async (e) => {
formRef.value?.validate(async (errors) => {
if (!errors) {
try {
await api.addClass(formValue.value)
await api.addClass({
...formValue.value,
icon: formValue.value.icon[0].url,
})
$message.success('成功')
clear()
getList()