| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493 |
- const n = `<template>
- <s-modal
- v-model:value="isVisible"
- :title="$l(title)"
- :width="width"
- :mask-closable="false"
- :show-full-screen="false"
- @handCancel="handleClose"
- >
- <div class="header-steps">
- <a-steps v-model:current="activeStep" type="navigation" size="small">
- <a-step :title="$l('上传文件')" :disabled="true" />
- <a-step :title="$l('数据预览')" :disabled="true" />
- <a-step :title="$l('导入数据')" :disabled="true" />
- </a-steps>
- </div>
- <!-- 第一步:上传文件 -->
- <div class="import-main" v-show="activeStep == 0">
- <div class="upload">
- <div class="up_right">
- <p class="title">{{ $l('上传填好的数据表') }}</p>
- <p class="tip">
- {{ $l('文件后缀名必须是xls或xlsx,文件大小不超过500KB,最多支持导入1000条数据') }}
- </p>
- <a-upload
- v-model:file-list="fileList"
- class="upload-area"
- accept=".xls,.xlsx"
- :max-count="1"
- :custom-request="customRequest"
- :before-upload="beforeUpload"
- @remove="handleFileRemove"
- >
- <a-button type="link">{{ $l('上传文件') }}</a-button>
- </a-upload>
- </div>
- </div>
- <div class="upload">
- <div class="up_right">
- <p class="title">{{ $l('填写导入数据信息') }}</p>
- <p class="tip">
- {{ $l('请按照数据模板的格式准备导入数据,模板中的表头名称不可更改,表头行不能删除') }}
- </p>
- <div class="extra-select-wrapper">
- <!-- 业务自定义参数(如设备分类)由外部传入选项,选中值会随各导入接口一起提交 -->
- <a-select
- v-if="typeSelect"
- v-model:value="typeValue"
- :options="typeSelect.options"
- :loading="typeSelect.loading"
- :placeholder="typeSelect.placeholder || $l('请选择')"
- style="width: 200px; margin-right: 12px"
- @change="handleTypeChange"
- />
- <a-button type="link" :loading="downloading" @click="handleTemplateDownload">{{
- $l('下载模板')
- }}</a-button>
- </div>
- </div>
- </div>
- </div>
- <!-- 第二步:数据预览 -->
- <div class="import-main" v-show="activeStep == 1">
- <a-table
- :data-source="list"
- :columns="previewColumns"
- size="small"
- :pagination="false"
- :scroll="{ x: 'max-content', y: '420px' }"
- class="import-preview-table"
- >
- <template #bodyCell="{ column, record, index }">
- <template v-for="item in localColumns" :key="item.dataIndex">
- <template v-if="column.dataIndex === item.dataIndex">
- <a-input v-model:value="record[column.dataIndex]" />
- </template>
- </template>
- <template v-if="column.dataIndex === 'action'">
- <a-button
- class="action-btn"
- type="link"
- danger
- @click="handleDelItem(index)"
- size="small"
- >{{ $l('删除') }}</a-button
- >
- </template>
- </template>
- </a-table>
- </div>
- <!-- 第三步:导入结果 -->
- <div class="import-main" v-show="activeStep == 2">
- <div class="success" v-if="!result.resultType">
- <p class="success-title">{{ $l('批量导入成功') }}</p>
- <p class="success-tip">{{ $l('您已成功导入') }}{{ result.snum }}{{ $l('条数据') }}</p>
- </div>
- <div class="unsuccess" v-if="result.resultType">
- <div class="upload error-show">
- <div class="up_right">
- <p class="tip success-tip">
- {{ $l('正常数据条数') }}:<span>{{ result.snum }}{{ $l('条') }}</span>
- </p>
- <p class="tip error-tip">
- {{ $l('异常数据条数') }}:<span>{{ result.fnum }}{{ $l('条') }}</span>
- </p>
- </div>
- </div>
- <div class="conTips">
- <p>{{ $l('以下文件数据为导入异常数据') }}</p>
- <a-button type="link" :loading="downloading" @click="handleExportExceptionData">{{
- $l('导出异常数据')
- }}</a-button>
- </div>
- <a-table
- :data-source="resultList"
- :columns="resultColumns"
- size="small"
- :pagination="false"
- :scroll="{ x: 'max-content', y: '205px' }"
- />
- </div>
- </div>
- <template #footer>
- <a-button v-if="activeStep == 0" @click="handleClose">{{ $l('取消') }}</a-button>
- <a-button v-if="activeStep === 1" @click="handlePrev">{{ $l('上一步') }}</a-button>
- <a-button
- type="primary"
- :loading="btnLoading"
- v-if="activeStep < 2"
- :disabled="activeStep === 0 && (!fileName || (typeSelect && !typeValue))"
- @click="handleNext"
- >
- {{ $l('下一步') }}
- </a-button>
- <a-button type="primary" v-else @click="handleClose(true)">{{ $l('关闭') }}</a-button>
- </template>
- </s-modal>
- </template>
- <script setup>
- /**
- * SImportModal 通用批量导入弹窗
- * 三步式:上传文件 -> 数据预览(可编辑/删除行)-> 导入结果(含异常数据导出)
- *
- * 用法(基座重新构建发布后,业务项目可直接使用 <s-import-modal>):
- * <s-import-modal ref="importModalRef" :upload-url="uploadUrl" :columns="columns"
- * :apis="apis" :type-select="typeSelect" @reload="reload" />
- * importModalRef.value.show()
- */
- import { ref, computed, watch } from 'vue'
- defineOptions({ name: 'SImportModal' })
- const props = defineProps({
- // 显隐(v-model:value)
- value: { type: Boolean, default: false },
- // 弹窗标题
- title: { type: String, default: () => window.$l('批量导入') },
- // 弹窗宽度
- width: { type: [String, Number], default: 1000 },
- // 文件上传地址(完整网关地址,如 /monitor/DeviceInfo/Uploader)
- uploadUrl: { type: String, default: '' },
- // 预览/结果表格列:[{ title, dataIndex, width }],作为默认列,接口返回 headerRow 后以接口为准
- columns: { type: Array, default: () => [] },
- // 四个导入接口方法:{ templateDownload, importPreview, importData, exportExceptionData }
- apis: {
- type: Object,
- default: () => ({})
- },
- // 可选的业务参数下拉(如设备分类):{ options: [{ label, value }], value, placeholder, loading }
- // 选中值会以 { extraKey: value } 的形式随各接口一起提交
- typeSelect: { type: Object, default: null },
- // typeSelect 选中值提交时的字段名
- extraKey: { type: String, default: 'deviceTypeId' }
- })
- const emit = defineEmits(['update:value', 'reload'])
- const activeStep = ref(0)
- const fileName = ref('')
- const fileList = ref([])
- const btnLoading = ref(false)
- const downloading = ref(false)
- const list = ref([])
- // 预览/结果表格列(接口返回 headerRow 后由接口决定,避免直接修改 props.columns)
- const localColumns = ref([])
- const result = ref({})
- const resultList = ref([])
- const typeValue = ref(undefined)
- const isVisible = computed({
- get: () => props.value,
- set: (val) => emit('update:value', val)
- })
- const previewColumns = computed(() => [
- { width: 50, title: window.$l('序号'), align: 'center', customRender: ({ index }) => index + 1 },
- ...localColumns.value,
- { title: window.$l('操作'), dataIndex: 'action', key: 'action', width: 50, fixed: 'right' }
- ])
- const resultColumns = computed(() => [
- { width: 50, title: window.$l('序号'), align: 'center', customRender: ({ index }) => index + 1 },
- ...localColumns.value,
- { title: window.$l('异常原因'), dataIndex: 'errorsInfo', width: 150, fixed: 'right' }
- ])
- // 随接口一起提交的额外参数(如设备分类)
- const extraParams = computed(() => {
- if (!props.typeSelect) return {}
- return typeValue.value ? { [props.extraKey]: typeValue.value } : {}
- })
- // 外部传入的选项变化时默认选中第一项
- watch(
- () => props.typeSelect?.options,
- (options) => {
- if (options && options.length) {
- const hasValue = options.some((item) => item.value === typeValue.value)
- if (!hasValue) typeValue.value = options[0].value
- }
- },
- { immediate: true, deep: true }
- )
- const resetState = () => {
- activeStep.value = 0
- fileName.value = ''
- fileList.value = []
- btnLoading.value = false
- list.value = []
- localColumns.value = []
- result.value = {}
- resultList.value = []
- if (props.typeSelect) {
- typeValue.value = props.typeSelect.options?.length
- ? props.typeSelect.options[0].value
- : undefined
- }
- }
- const show = () => {
- isVisible.value = true
- resetState()
- }
- // 统一取数:兼容网关返回 { code, data } 与直接返回数据两种格式
- const unwrap = (res) => {
- return res && Object.prototype.hasOwnProperty.call(res, 'data') ? res.data : res
- }
- // 把接口返回的 headerRow([{ fullName, id }]) 转成表格列配置
- const transformHeaderRow = (headerRow) => {
- return (headerRow || []).map((col) => ({
- title: col.fullName || '',
- dataIndex: col.id || '',
- key: col.id || ''
- }))
- }
- // 上传(复用 window.$utils.http,自动携带网关与 user-token 鉴权)
- const customRequest = ({ file, onSuccess, onError }) => {
- const http = window.$utils?.http
- if (!http) {
- window.$message.error(window.$l('未获取到请求工具,无法上传'))
- onError && onError(new Error('http not found'))
- return
- }
- const formData = new FormData()
- formData.append('file', file)
- return http({
- url: props.uploadUrl,
- method: 'post',
- data: formData,
- headers: { 'Content-Type': 'multipart/form-data' }
- })
- .then((res) => {
- const data = unwrap(res) || {}
- const name = data?.name ?? data?.fileName ?? ''
- if (!name) throw new Error(window.$l('上传返回文件名无效'))
- fileName.value = name
- onSuccess && onSuccess(res)
- })
- .catch((error) => {
- window.$message.error(typeof error?.msg === 'string' ? error.msg : window.$l('上传失败'))
- onError && onError(error)
- })
- }
- const beforeUpload = (file) => {
- const fileType = file.name.replace(/.+\\./, '')
- const isAccept = ['xls', 'xlsx'].indexOf(fileType.toLowerCase()) !== -1
- if (!isAccept) {
- window.$message.error(window.$l('文件格式不正确'))
- return false
- }
- if (file.size / 1024 >= 500) {
- window.$message.error(window.$l('文件大小超过500KB'))
- return false
- }
- return true
- }
- const handleFileRemove = (file) => {
- return new Promise((resolve, reject) => {
- window.$modal.confirm({
- title: window.$l('提示'),
- content: \`\${window.$l('确定移除')}\${file.name}?\`,
- onOk: () => {
- fileName.value = ''
- resolve()
- },
- onCancel: () => reject()
- })
- })
- }
- const handleTypeChange = () => {
- // 切换业务参数时清空已上传的文件与预览数据
- if (fileList.value.length || fileName.value) {
- resetState()
- }
- }
- const handlePrev = () => {
- if (activeStep.value == 0) return
- activeStep.value -= 1
- }
- const handleNext = () => {
- if (activeStep.value == 0) {
- if (!fileList.value.length || !fileName.value) {
- window.$message.warning(window.$l('请先上传文件'))
- return
- }
- if (props.typeSelect && !typeValue.value) {
- window.$message.warning(
- props.typeSelect.placeholder
- ? \`\${window.$l('请先')}\${props.typeSelect.placeholder}\`
- : window.$l('请先选择业务参数')
- )
- return
- }
- btnLoading.value = true
- props.apis
- .importPreview({ fileName: fileName.value, ...extraParams.value })
- .then((res) => {
- const data = unwrap(res) || {}
- list.value = data.dataRow || []
- // 用接口返回的 headerRow 生成表格列(存本地,避免修改 props.columns)
- localColumns.value = transformHeaderRow(data.headerRow)
- btnLoading.value = false
- activeStep.value += 1
- })
- .catch(() => {
- btnLoading.value = false
- })
- return
- }
- if (activeStep.value == 1) {
- if (!list.value.length) {
- window.$message.warning(window.$l('导入数据为空'))
- return
- }
- btnLoading.value = true
- props.apis
- .importData({ list: list.value, ...extraParams.value })
- .then((res) => {
- const data = unwrap(res) || {}
- result.value = data
- resultList.value = data.failResult || []
- btnLoading.value = false
- activeStep.value += 1
- emit('reload')
- })
- .catch(() => {
- btnLoading.value = false
- })
- }
- }
- // 通过返回的 url 触发下载
- const downloadByUrl = (url) => {
- if (!url) {
- window.$message.error(window.$l('未获取到下载地址'))
- return
- }
- const a = document.createElement('a')
- a.href = url
- a.download = ''
- document.body.appendChild(a)
- a.click()
- document.body.removeChild(a)
- }
- const handleTemplateDownload = () => {
- if (props.typeSelect && !typeValue.value) {
- window.$message.warning(
- props.typeSelect.placeholder
- ? \`\${window.$l('请先')}\${window.$l(props.typeSelect.placeholder)}\`
- : window.$l('请先选择业务参数')
- )
- return
- }
- downloading.value = true
- props.apis
- .templateDownload({ ...extraParams.value })
- .then((res) => {
- const data = unwrap(res) || {}
- downloadByUrl(data?.url)
- })
- .catch(() => window.$message.error(window.$l('模板下载失败')))
- .finally(() => {
- downloading.value = false
- })
- }
- const handleExportExceptionData = () => {
- downloading.value = true
- props.apis
- .exportExceptionData({ list: resultList.value, ...extraParams.value })
- .then((res) => {
- const data = unwrap(res) || {}
- downloadByUrl(data?.url)
- })
- .catch(() => window.$message.error(window.$l('异常数据导出失败')))
- .finally(() => {
- downloading.value = false
- })
- }
- const handleDelItem = (index) => {
- list.value.splice(index, 1)
- }
- const handleClose = (reload = false) => {
- isVisible.value = false
- if (reload) emit('reload')
- }
- defineExpose({ show, resetState })
- <\/script>
- <style lang="less" scoped>
- .header-steps {
- margin-bottom: 16px;
- }
- .upload {
- display: flex;
- justify-content: flex-start;
- align-items: flex-start;
- padding: 16px;
- margin-bottom: 16px;
- background-color: var(--s-bg-color-hover);
- border-radius: 6px;
- .up_right {
- flex: 1;
- .title {
- font-weight: 500;
- margin-bottom: 8px;
- }
- .tip {
- color: var(--s-text-color-secondary);
- font-size: 13px;
- margin-bottom: 8px;
- }
- }
- }
- .extra-select-wrapper {
- display: flex;
- align-items: center;
- margin-top: 8px;
- }
- .success {
- text-align: center;
- padding: 40px 0;
- .success-title {
- font-size: 18px;
- font-weight: 500;
- margin-bottom: 8px;
- }
- .success-tip {
- color: var(--s-color-success);
- }
- }
- .conTips {
- display: flex;
- justify-content: space-between;
- align-items: center;
- margin: 8px 0;
- }
- </style>
- `;
- export {
- n as default
- };
|