Files
api-client/src/components/BatchImageAdd/index.vue

152 lines
4.3 KiB
Vue
Raw Normal View History

2026-01-24 10:29:34 +08:00
<template>
<el-dialog title="批量添加" :visible.sync="dialogVisible" width="600px" @close="close">
<el-form ref="form" label-width="80px">
<el-form-item label="图片上传">
<el-upload
ref="upload"
accept="image/*"
:action="uploadUrl"
2026-01-24 23:58:03 +08:00
drag
2026-01-24 10:29:34 +08:00
:file-list="fileList"
:headers="uploadHeaders"
:multiple="true"
:on-preview="handlePreview"
:on-remove="handleRemove"
:on-success="handleSuccess"
>
2026-01-24 23:58:03 +08:00
<i class="el-icon-upload"></i>
<div class="el-upload__text">
将文件拖到此处
<em>点击上传</em>
</div>
2026-01-24 10:29:34 +08:00
</el-upload>
<el-dialog append-to-body :visible.sync="previewVisible">
<img alt="" :src="previewImageUrl" width="100%" />
</el-dialog>
</el-form-item>
2026-01-24 19:26:30 +08:00
<el-form-item v-if="hasType" label="类型">
2026-01-25 11:57:10 +08:00
<el-select
v-if="typeList && typeList.length > 0"
v-model="type"
allow-create
default-first-option
filterable
placeholder="请选择类型"
style="width: 100%"
>
<el-option v-for="item in typeList" :key="item.id" :label="item.name" :value="item.id" />
</el-select>
<el-input v-else v-model.trim="type" placeholder="请输入类型" />
2026-01-24 19:26:30 +08:00
</el-form-item>
2026-01-24 10:29:34 +08:00
<el-form-item label="是否启用">
<el-switch v-model="isEnabled" active-text="启用" :active-value="true" inactive-text="禁用" :inactive-value="false" />
</el-form-item>
</el-form>
<div slot="footer" class="dialog-footer">
<el-button @click="close"> </el-button>
<el-button :loading="loading" type="primary" @click="save"> </el-button>
</div>
</el-dialog>
</template>
<script>
import { getAccessToken } from '@/utils/accessToken'
export default {
name: 'BatchImageAdd',
props: {
doAdd: {
type: Function,
required: true,
},
2026-01-24 19:26:30 +08:00
hasType: {
type: Boolean,
default: false,
},
2026-01-25 11:57:10 +08:00
typeList: {
type: Array,
default: () => [],
},
2026-01-24 10:29:34 +08:00
},
data() {
return {
dialogVisible: false,
isEnabled: true,
2026-01-24 19:26:30 +08:00
type: '',
2026-01-24 10:29:34 +08:00
loading: false,
fileList: [],
previewVisible: false,
previewImageUrl: '',
}
},
computed: {
uploadUrl() {
return `${process.env.VUE_APP_API_BASE_URL}/management/api/common/upload`
},
uploadHeaders() {
return {
Authorization: getAccessToken() || '',
'x-user-id': this.$store.state.user.userId || '',
'x-app-id': this.$store.state.user.appId || '',
}
},
},
methods: {
show() {
this.dialogVisible = true
this.fileList = []
},
close() {
this.isEnabled = true
2026-01-24 19:26:30 +08:00
this.type = ''
2026-01-24 10:29:34 +08:00
this.loading = false
this.fileList = []
this.dialogVisible = false
},
handleSuccess(response, file, fileList) {
if (response.code !== 200) {
this.$baseMessage(response.msg || '上传失败', 'error')
}
this.fileList = fileList
},
handleRemove(file, fileList) {
this.fileList = fileList
},
handlePreview(file) {
this.previewImageUrl = file.url || file.thumbUrl
this.previewVisible = true
},
async save() {
const urls = this.fileList
.filter((f) => f.status === 'success' && f.response && f.response.code === 200)
2026-01-24 19:17:18 +08:00
.map((f) => `https://file.lihailezzc.com/${f.response.data.key}`)
2026-01-24 10:29:34 +08:00
if (urls.length === 0) {
this.$baseMessage('请至少上传一张图片', 'warning')
return
}
this.loading = true
try {
let successCount = 0
for (const url of urls) {
2026-01-24 19:26:30 +08:00
const data = { imageUrl: url, isEnabled: this.isEnabled }
if (this.hasType) {
data.type = this.type
}
await this.doAdd(data)
2026-01-24 10:29:34 +08:00
successCount++
}
this.$emit('fetch-data')
this.close()
} catch (error) {
2026-01-24 19:26:30 +08:00
console.error('批量添加失败:', error)
2026-01-24 10:29:34 +08:00
this.$baseMessage('部分或全部添加失败', 'error')
} finally {
this.loading = false
}
},
},
}
</script>