feat: 图片上传/回显支持绑定string类型

This commit is contained in:
dap
2024-10-09 22:20:09 +08:00
parent 99a0c63f92
commit 961f65215d
3 changed files with 37 additions and 8 deletions

View File

@@ -1,5 +1,4 @@
export function checkFileType(file: File, accepts: string[]) {
console.log(file.name, accepts);
let reg;
if (!accepts || accepts.length === 0) {
reg = /.(?:jpg|jpeg|png|gif|webp)$/i;

View File

@@ -40,7 +40,7 @@ const props = withDefaults(
// support xxx.xxx.xx
// 返回的字段 默认url
resultField?: 'fileName' | 'ossId' | 'url' | string;
value?: string[];
value?: string | string[];
}>(),
{
value: () => [],
@@ -81,14 +81,18 @@ watch(
isInnerOperate.value = false;
return;
}
let value: string[] = [];
let value: string | string[] = [];
if (v) {
if (isArray(v)) {
value = v;
} else {
value.push(v);
const _fileList: string[] = [];
if (isString(v)) {
_fileList.push(v);
}
fileList.value = value.map((item, i) => {
if (isArray(v)) {
_fileList.push(...v);
}
// 直接赋值 可能为string | string[]
value = v;
fileList.value = _fileList.map((item, i) => {
if (item && isString(item)) {
return {
uid: `${-i}`,
@@ -205,9 +209,21 @@ function getValue() {
if (item?.response && props?.resultField) {
return item?.response?.[props.resultField];
}
// 适用于已经有图片 回显的情况 会默认在init处理为{url: 'xx'}
if (item?.url) {
return item.url;
}
// 注意这里取的key为 url
return item?.response?.url;
});
// 只有一张图片 默认绑定string而非string[]
if (props.maxNumber === 1 && list.length === 1) {
return list[0];
}
// 只有一张图片 && 删除图片时 可自行修改
if (props.maxNumber === 1 && list.length === 0) {
return '';
}
return list;
}
</script>