Commit 9bb40ca3 authored by 田翔's avatar 田翔

fix: 附件默认值优化

parent b895689e
{
"name": "panda-xform",
"version": "6.10.17",
"description": "6.10.17 补充分组标题说明",
"version": "6.10.18",
"description": "6.10.18 附件默认值优化",
"keywords": [
"panda-xform"
],
......
......@@ -1190,6 +1190,7 @@ const baseWidgets = [
},
presetValue: {
title: '默认值',
widget: 'PresetValueFileUpload',
type: 'string',
},
isHidden: {
......
......@@ -340,6 +340,7 @@ const FileUpload = (props) => {
setList()
} else {
addons?.setValue(addons?.dataPath, '')
setShowList([])
}
}, [presetValue])
......
import React, { useEffect, useState } from 'react'
import { Upload, Button, message, Input } from 'antd'
import { UploadOutlined, FileOutlined } from '@ant-design/icons'
import { uploadFileUrl, downloadFileUrl, downloadFile } from '../../../../../apis/process'
import { filenameVerification } from '../../../../../utils'
const PresetValueFileUpload = (props) => {
const site = window.globalConfig?.userInfo?.site || window.globalConfig?.userInfo?.LocalSite
const { value, onChange, addons } = props
const [showList, setShowList] = useState([])
const valueToList = async (presetValue) => {
let fileList = []
if (presetValue) {
let list = presetValue ? presetValue.split(',') : []
for (let i = 0; i < list.length; i++) {
if (list[i]) { // @Tips: 直接过滤掉名字中有异常字符的文件
let uid = i + '_' + Math.random()
let _obj = {
uid: uid,
name: list[i].split('/').reverse()[0],
type: 'file',
status: 'done',
url: list[i]?.includes('http') ? list[i] : `${window.origin}${downloadFileUrl}?filePath=${list[i]}${site ? `&_site=${site}` : ''}`,
sourcePath: list[i],
originFileObj: { "uid": uid },
"response": { "code": 0, "msg": "Ok", "data": list[i], "stackTrace": null },
"xhr": {},
};
fileList.push(_obj);
}
}
}
return fileList
}
const option = {
name: 'file',
action: `${window.location.origin}${uploadFileUrl}`,
listType: 'picture',
withCredentials: true,
showUploadList: {
showRemoveIcon: true,
showDownloadIcon: true,
},
beforeUpload(file, fileList) {
/** @Tips: 解决提交文件中存在特殊字符的问题 */
let _continueUpload = true;
let _msg = {
type: 'success',
content: '上传成功!',
};
fileList.forEach(item => {
let _msgObject = filenameVerification(item);
if (_msgObject.type === 'error') {
_continueUpload = false;
_msg = {
type: 'error',
content: '上传失败!文件名不符合规则!',
};
}
});
_msg.type === 'error' ? message[_msg.type](_msg.content) : ''
return _continueUpload;
},
onChange: ({ file, fileList, event }) => {
// 检验名字,名字不通过不允许显示
if (filenameVerification(file).type === 'error') return false;
// 返回的链接在file.response内;不设置url,预览图表不可点击
if (file.status === 'done' && file.response.code === 0) {
file.url = `${window.origin}${downloadFileUrl}?filePath=${file.response.data}${site ? `&_site=${site}` : ''}`
file.sourcePath = file.response.data;
message.success('上传成功!')
} else if (file.status === 'done' && file.response.code !== 0) {
file.status = 'error';
message.error('上传失败!')
}
if (Array.isArray(fileList)) {
setShowList(fileList)
} else {
setShowList([])
}
if (file.status === 'done') {
onChange(fileList.map(v => v.sourcePath).join(','))
}
},
onPreview: async (file) => {
if (!preview) return message.info('该附件禁止预览')
let fileType = getFileType(file.name)
setShowFile({ name: file.name, fileType: fileType, filePath: file.url })
if (fileType) {
if (['jpg', 'png', 'svg', 'jpeg'].includes(fileType)) {
let index = images.findIndex(v => v.sourcePath === file.sourcePath)
setActiveIndex(index)
setImgVisible(true)
} else {
if (['xlsx', 'xls'].includes(fileType)) {
downloadFile(file.sourcePath)
.then(response => response.arrayBuffer())
.then(buffer => {
const data = new Uint8Array(buffer);
const workbook = XLSX.read(data, { type: 'array' });
workbook?.SheetNames?.forEach(sheetName => {
const worksheet = workbook.Sheets[sheetName];
// 将工作表转换为 JSON 对象
const jsonData = XLSX.utils.sheet_to_json(worksheet, { header: 1, defval: '' });
console.log(sheetName, jsonData)
})
})
}
setVisible(true)
}
} else {
message.info('不支持该类型预览')
}
},
onRemove: async (file) => {
let files = value?.split(",") || [];
let list = files.filter(v => v !== file.sourcePath);
onChange(list.join(","))
setShowList(await valueToList(list.join(",")))
},
}
const setList = async (value) => {
setShowList(await valueToList(value))
}
const inputChange = (e) => {
onChange(e.target.value)
}
useEffect(() => {
if (value) {
setList(value)
}
}, [])
return (
<div>
<Upload
style={{ width: '100%' }}
maxCount={1}
fileList={showList}
className="upload-list-inline"
{...option}
>
<div style={{ display: 'flex' }}>
{/* <Input placeholder='请输入默认值或上传' value={value} style={{ width: '210px' }} onClick={(e) => e.stopPropagation()} onChange={inputChange} /> */}
<Button type='primary' style={{ width: '100%' }} icon={<UploadOutlined />}>{'上传'}</Button>
</div>
</Upload>
</div>
)
}
export default PresetValueFileUpload
\ No newline at end of file
import FieldNames from './FieldNames'
import Placeholder from './Placeholder'
import PresetValueFileUpload from './PresetValueFileUpload'
import InputDefault from './InputDefault'
import NumerDefault from './NumerDefault'
import CascadeDefault from './CascadeDefault'
......@@ -15,6 +16,7 @@ const groupBase = {
FieldNames,
InputDefault,
Placeholder,
PresetValueFileUpload,
NumerDefault,
CascadeDefault,
DateDefault,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment