提交 b46aeb16 编写于 作者: jlj05024111@163.com's avatar jlj05024111@163.com

feat: 更新文件下载时对文件url做权限认证处理;更新文件上传支持白名单类型,更新文件上传支持自定义上传大小限制

上级 4dd4b77f
...@@ -36,7 +36,7 @@ export const IBizFileUploadOne = defineComponent({ ...@@ -36,7 +36,7 @@ export const IBizFileUploadOne = defineComponent({
const handleBeforeUpload = (_files: IData) => { const handleBeforeUpload = (_files: IData) => {
// 先清除之前的文件 todo // 先清除之前的文件 todo
fileUpload.value.clearFiles(); fileUpload.value.clearFiles();
return beforeUpload(); return beforeUpload(_files);
}; };
return { return {
...@@ -101,12 +101,21 @@ export const IBizFileUploadOne = defineComponent({ ...@@ -101,12 +101,21 @@ export const IBizFileUploadOne = defineComponent({
)} )}
</div> </div>
) : ( ) : (
[
<i-button <i-button
icon='ios-cloud-upload-outline' icon='ios-cloud-upload-outline'
class={this.ns.b('button')} class={this.ns.b('button')}
> >
上传文件 上传文件
</i-button> </i-button>,
this.c.uploadDescription && (
<div
class={this.ns.be('button-upload', 'accept-description')}
>
{this.c.uploadDescription}
</div>
),
]
), ),
], ],
)} )}
......
...@@ -92,12 +92,21 @@ export const IBizFileUpload = defineComponent({ ...@@ -92,12 +92,21 @@ export const IBizFileUpload = defineComponent({
)} )}
</div> </div>
) : ( ) : (
[
<i-button <i-button
icon='ios-cloud-upload-outline' icon='ios-cloud-upload-outline'
class={this.ns.b('button')} class={this.ns.b('button')}
> >
上传文件 上传文件
</i-button> </i-button>,
this.c.uploadDescription && (
<div
class={this.ns.be('button-upload', 'accept-description')}
>
{this.c.uploadDescription}
</div>
),
]
), ),
], ],
)} )}
......
import { useClickOutside, useNamespace } from '@ibiz-template/vue-util'; import { useClickOutside, useNamespace } from '@ibiz-template/vue-util';
import { defineComponent, ref, toRefs, watch } from 'vue'; import { defineComponent, PropType, Ref, ref, toRefs, watch } from 'vue';
import { UploadEditorController } from '@ibiz-template/controller';
import { import {
FileInfo, FileInfo,
openImagePreview, openImagePreview,
...@@ -30,6 +31,14 @@ export const IBizGridFileUploadPopover = defineComponent({ ...@@ -30,6 +31,14 @@ export const IBizGridFileUploadPopover = defineComponent({
type: Number, type: Number,
default: 300, default: 300,
}, },
controller: {
type: Object as PropType<Ref<UploadEditorController>>,
required: true,
},
data: {
type: Object as PropType<Ref<IData>>,
required: true,
},
}, },
emits: { emits: {
close: (_result: { isModified: boolean; resultFiles: FileInfo[] }) => true, close: (_result: { isModified: boolean; resultFiles: FileInfo[] }) => true,
...@@ -43,6 +52,8 @@ export const IBizGridFileUploadPopover = defineComponent({ ...@@ -43,6 +52,8 @@ export const IBizGridFileUploadPopover = defineComponent({
downloadUrl, downloadUrl,
value, value,
uploadUrl, uploadUrl,
controller: props.controller,
data: props.data,
}); });
const componentRef = ref(); const componentRef = ref();
...@@ -138,6 +149,9 @@ export const IBizGridFileUploadPopover = defineComponent({ ...@@ -138,6 +149,9 @@ export const IBizGridFileUploadPopover = defineComponent({
<i-icon type='md-add' /> <i-icon type='md-add' />
添加文件 添加文件
</div> </div>
<div class={this.ns.e('description')}>
{this.controller.uploadDescription}
</div>
</div> </div>
); );
}, },
......
...@@ -58,6 +58,8 @@ export const IBizGridFileUpload = defineComponent({ ...@@ -58,6 +58,8 @@ export const IBizGridFileUpload = defineComponent({
uploadUrl: uploadUrl.value, uploadUrl: uploadUrl.value,
downloadUrl: downloadUrl.value, downloadUrl: downloadUrl.value,
width, width,
controller,
data,
}, },
on: { on: {
close: (result: { close: (result: {
......
/* eslint-disable array-callback-return */
/* eslint-disable no-param-reassign */ /* eslint-disable no-param-reassign */
import { UploadEditorController } from '@ibiz-template/controller'; import {
FormItemController,
GridEditItemController,
UploadEditorController,
} from '@ibiz-template/controller';
import { import {
HttpError, HttpError,
HttpResponse, HttpResponse,
...@@ -37,8 +42,26 @@ export type FileInfo = { ...@@ -37,8 +42,26 @@ export type FileInfo = {
* @date 2022-11-18 15:11:38 * @date 2022-11-18 15:11:38
* @param {FileInfo} file * @param {FileInfo} file
*/ */
export function formatFileInfo(file: FileInfo, downloadUrl: string): FileInfo { export function formatFileInfo(
file.url = downloadUrl.replace('%fileId%', file.id!); file: FileInfo,
downloadUrl: string,
c: IData,
data: IData,
): FileInfo {
const url = downloadUrl.replace('%fileId%', file.id);
const ctrl =
(c.parent as FormItemController)?.form ||
(c.parent as GridEditItemController)?.grid;
if (ctrl) {
const entityName = ctrl.model.appEntity.deName;
const base64 = `${file.id}|${entityName}|${data.srfkey}|${
c.context.srfpersonid || c.context.srfuserid
}`;
file.url = `${url}?key=${window.btoa(base64)}${Math.floor(
1000 + Math.random() * 9000,
)}`;
}
if (!file.status) { if (!file.status) {
// 不存在时为回填回来的数据默认给他finished // 不存在时为回填回来的数据默认给他finished
file.status = 'finished'; file.status = 'finished';
...@@ -99,7 +122,12 @@ export function useIBizUploadInit(props: { ...@@ -99,7 +122,12 @@ export function useIBizUploadInit(props: {
valueList.value = !newVal ? [] : JSON.parse(newVal); valueList.value = !newVal ? [] : JSON.parse(newVal);
if (valueList.value.length && downloadUrl.value) { if (valueList.value.length && downloadUrl.value) {
valueList.value.forEach((file: FileInfo) => { valueList.value.forEach((file: FileInfo) => {
formatFileInfo(file, downloadUrl.value); formatFileInfo(
file,
downloadUrl.value,
props.controller.value,
props.data.value,
);
}); });
} }
}, },
...@@ -112,7 +140,12 @@ export function useIBizUploadInit(props: { ...@@ -112,7 +140,12 @@ export function useIBizUploadInit(props: {
// 下载基础路径变更时全部url重算 // 下载基础路径变更时全部url重算
if (newVal && valueList.value.length) { if (newVal && valueList.value.length) {
valueList.value.forEach((file: FileInfo) => { valueList.value.forEach((file: FileInfo) => {
formatFileInfo(file, newVal); formatFileInfo(
file,
newVal,
props.controller.value,
props.data.value,
);
}); });
} }
}, },
...@@ -155,12 +188,14 @@ export function useIBizUpload(opts: { ...@@ -155,12 +188,14 @@ export function useIBizUpload(opts: {
url?: string; url?: string;
}[] }[]
>; >;
controller: UploadEditorController;
data: IData;
multiple?: boolean; multiple?: boolean;
accept?: string; accept?: string;
}) { }) {
const uploadState = ref<'undo' | 'loading' | 'done'>('undo'); const uploadState = ref<'undo' | 'loading' | 'done'>('undo');
const fileList = ref<FileInfo[]>([]); const fileList = ref<FileInfo[]>([]);
const { downloadUrl, value, uploadUrl } = opts; const { downloadUrl, value, uploadUrl, controller, data } = opts;
// 初始化fileList // 初始化fileList
watch( watch(
...@@ -169,7 +204,9 @@ export function useIBizUpload(opts: { ...@@ -169,7 +204,9 @@ export function useIBizUpload(opts: {
if (newVal.length > 0) { if (newVal.length > 0) {
fileList.value = []; fileList.value = [];
newVal.forEach(item => { newVal.forEach(item => {
fileList.value.push(formatFileInfo(item, downloadUrl.value)); fileList.value.push(
formatFileInfo(item, downloadUrl.value, controller, data),
);
}); });
} }
}, },
...@@ -181,6 +218,24 @@ export function useIBizUpload(opts: { ...@@ -181,6 +218,24 @@ export function useIBizUpload(opts: {
// 开始上传后记录文件 // 开始上传后记录文件
const beforeUpload = (fileData: File[], files: IUploadFile[]) => { const beforeUpload = (fileData: File[], files: IUploadFile[]) => {
const tag = fileData.some(item => {
if (item.size > controller.defaultSize * 1024 * 1024) {
ibiz.message.error(
`${item.name} 超出限制,请选择小于${controller.defaultSize}MB的文件`,
2.5,
true,
);
return true;
}
if (controller.whiteList && !controller.whiteList.includes(item.name)) {
ibiz.message.error(`${item.name} 文件格式不正确`, 2.5, true);
return true;
}
return false;
});
if (tag) {
return false;
}
files.forEach(file => { files.forEach(file => {
fileList.value.push({ fileList.value.push({
name: file.name, name: file.name,
...@@ -221,7 +276,7 @@ export function useIBizUpload(opts: { ...@@ -221,7 +276,7 @@ export function useIBizUpload(opts: {
item.status = file.status; item.status = file.status;
item.id = res.data.fileid; item.id = res.data.fileid;
item.name = res.data.filename; item.name = res.data.filename;
formatFileInfo(item, downloadUrl.value); formatFileInfo(item, downloadUrl.value, controller, data);
return true; return true;
} }
return false; return false;
......
...@@ -123,7 +123,21 @@ export function useIViewUpload( ...@@ -123,7 +123,21 @@ export function useIViewUpload(
}; };
// 上传前回调 // 上传前回调
const beforeUpload = () => { const beforeUpload = (file: IData) => {
const type = file.name.split('.').pop() || '';
if (c.whiteList && !c.whiteList.includes(type)) {
ibiz.message.error(`${file.name} 文件格式不正确`, 2.5, true);
return false;
}
if (file.size > c.defaultSize * 1024 * 1024) {
// 超出限制,不上传
ibiz.message.error(
`${file.name} 超出限制,请选择小于${c.defaultSize}MB的文件`,
2.5,
true,
);
return false;
}
uploadCache.count += 1; uploadCache.count += 1;
}; };
......
...@@ -86,7 +86,7 @@ export const GridFieldEditColumn = defineComponent({ ...@@ -86,7 +86,7 @@ export const GridFieldEditColumn = defineComponent({
nativeondblclick={this.onStopPropagation} nativeondblclick={this.onStopPropagation}
nativeonClick={this.onStopPropagation} nativeonClick={this.onStopPropagation}
style={this.alignStyle} style={this.alignStyle}
class={[...this.dynamicClass, this.ns.e('item')]} class={[...(this.dynamicClass || []), this.ns.e('item')]}
> >
{this.editItemC.editorProvider && {this.editItemC.editorProvider &&
h(this.editItemC.editorProvider.gridEditor, { h(this.editItemC.editorProvider.gridEditor, {
......
Markdown 格式
0% or
您添加了 0 到此讨论。请谨慎行事。
先完成此消息的编辑!
想要评论请 注册