1
1
<script lang="ts" setup>
2
- import { h , ref } from ' vue' ;
2
+ import type { UploadFile } from ' ant-design-vue' ;
3
+
4
+ import { h , ref , toRaw } from ' vue' ;
3
5
4
6
import { Page } from ' @vben/common-ui' ;
5
7
@@ -9,6 +11,8 @@ import dayjs from 'dayjs';
9
11
10
12
import { useVbenForm , z } from ' #/adapter/form' ;
11
13
import { getAllMenusApi } from ' #/api' ;
14
+ import { upload_file } from ' #/api/examples/upload' ;
15
+ import { $t } from ' #/locales' ;
12
16
13
17
import DocButton from ' ../doc-button.vue' ;
14
18
@@ -329,12 +333,56 @@ const [BaseForm, baseFormApi] = useVbenForm({
329
333
fieldName: ' treeSelect' ,
330
334
label: ' 树选择' ,
331
335
},
336
+ {
337
+ component: ' Upload' ,
338
+ componentProps: {
339
+ // 更多属性见:https://ant.design/components/upload-cn
340
+ accept: ' .png,.jpg,.jpeg' ,
341
+ // 自动携带认证信息
342
+ customRequest: upload_file ,
343
+ disabled: false ,
344
+ maxCount: 1 ,
345
+ multiple: false ,
346
+ showUploadList: true ,
347
+ // 上传列表的内建样式,支持四种基本样式 text, picture, picture-card 和 picture-circle
348
+ listType: ' picture-card' ,
349
+ },
350
+ fieldName: ' files' ,
351
+ label: $t (' examples.form.file' ),
352
+ renderComponentContent : () => {
353
+ return {
354
+ default : () => $t (' examples.form.upload-image' ),
355
+ };
356
+ },
357
+ rules: ' required' ,
358
+ },
332
359
],
333
360
// 大屏一行显示3个,中屏一行显示2个,小屏一行显示1个
334
361
wrapperClass: ' grid-cols-1 md:grid-cols-2 lg:grid-cols-3' ,
335
362
});
336
363
337
364
function onSubmit(values : Record <string , any >) {
365
+ const files = toRaw (values .files ) as UploadFile [];
366
+ const doneFiles = files .filter ((file ) => file .status === ' done' );
367
+ const failedFiles = files .filter ((file ) => file .status !== ' done' );
368
+
369
+ const msg = [
370
+ ... doneFiles .map ((file ) => file .response ?.url || file .url ),
371
+ ... failedFiles .map ((file ) => file .name ),
372
+ ].join (' , ' );
373
+
374
+ if (failedFiles .length === 0 ) {
375
+ message .success ({
376
+ content: ` ${$t (' examples.form.upload-urls' )}: ${msg } ` ,
377
+ });
378
+ } else {
379
+ message .error ({
380
+ content: ` ${$t (' examples.form.upload-error' )}: ${msg } ` ,
381
+ });
382
+ return ;
383
+ }
384
+ // 如果需要可提交前替换为需要的urls
385
+ values .files = doneFiles .map ((file ) => file .response ?.url || file .url );
338
386
message .success ({
339
387
content: ` form values: ${JSON .stringify (values )} ` ,
340
388
});
@@ -347,6 +395,14 @@ function handleSetFormValue() {
347
395
baseFormApi .setValues ({
348
396
checkboxGroup: [' 1' ],
349
397
datePicker: dayjs (' 2022-01-01' ),
398
+ files: [
399
+ {
400
+ name: ' example.png' ,
401
+ status: ' done' ,
402
+ uid: ' -1' ,
403
+ url: ' https://unpkg.com/@vbenjs/[email protected] /source/logo-v1.webp' ,
404
+ },
405
+ ],
350
406
mentions: ' @afc163' ,
351
407
number: 3 ,
352
408
options: ' 1' ,
0 commit comments