You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
159 lines
4.4 KiB
159 lines
4.4 KiB
|
2 weeks ago
|
# Upload 上传组件 API 文档
|
||
|
|
|
||
|
|
## 组件路径
|
||
|
|
|
||
|
|
`@/components/uploadFile/index`
|
||
|
|
|
||
|
|
## 说明
|
||
|
|
|
||
|
|
上传接口地址固定为 `app.globalData.upFileUrl`,组件内部自动附加 `loginState`,无需外部传入。
|
||
|
|
|
||
|
|
## Properties 入参
|
||
|
|
|
||
|
|
| 参数 | 说明 | 类型 | 默认值 | 必填 |
|
||
|
|
| --- | --- | --- | --- | --- |
|
||
|
|
| `maxCount` | 最大上传文件数 | `Number` | `9` | 否 |
|
||
|
|
| `maxSize` | 单文件最大限制(byte) | `Number` | `10485760`(10MB) | 否 |
|
||
|
|
| `accept` | 允许的文件类型数组,可选值:`'image'` \| `'video'` \| `'file'` | `Array` | `['image']` | 否 |
|
||
|
|
| `extensions` | 自定义文件后缀(仅 accept 含 `'file'` 时生效),如 `['.pdf', '.doc']` | `Array` | `[]` | 否 |
|
||
|
|
| `readonly` | 只读模式(不显示上传和删除按钮) | `Boolean` | `false` | 否 |
|
||
|
|
| `useSlot` | 是否使用自定义上传区域插槽 | `Boolean` | `false` | 否 |
|
||
|
|
| `fileList` | 已有文件列表(用于回显) | `Array` | `[]` | 否 |
|
||
|
|
|
||
|
|
### fileList 数据结构
|
||
|
|
|
||
|
|
```ts
|
||
|
|
{
|
||
|
|
uid: string // 文件唯一标识
|
||
|
|
url: string // 文件路径
|
||
|
|
type: string // 文件类型: 'image' | 'video' | 'file'
|
||
|
|
name: string // 文件名
|
||
|
|
size: number // 文件大小(byte)
|
||
|
|
}
|
||
|
|
```
|
||
|
|
|
||
|
|
## Events 事件
|
||
|
|
|
||
|
|
| 事件名 | 说明 | 回调参数 |
|
||
|
|
| --- | --- | --- |
|
||
|
|
| `bind:select` | 选中本地文件后触发 | `{ files: UploadFile[] }` |
|
||
|
|
| `bind:success` | 单文件上传完成 | `{ file: UploadFile, response: any }` |
|
||
|
|
| `bind:error` | 上传失败 | `{ file: UploadFile, error: Error }` |
|
||
|
|
| `bind:remove` | 删除文件 | `{ file: UploadFile, fileList: UploadFile[] }` |
|
||
|
|
|
||
|
|
### 事件返回的 UploadFile 结构
|
||
|
|
|
||
|
|
```ts
|
||
|
|
{
|
||
|
|
uid: string // 文件唯一标识
|
||
|
|
url: string // 文件路径(上传成功后为远程 url)
|
||
|
|
type: 'image' | 'video' | 'file' // 文件类型
|
||
|
|
name: string // 文件名
|
||
|
|
size: number // 文件大小(byte)
|
||
|
|
status: 'pending' | 'uploading' | 'success' | 'error' // 上传状态
|
||
|
|
progress: number // 上传进度 0-100
|
||
|
|
}
|
||
|
|
```
|
||
|
|
|
||
|
|
## Slots 插槽
|
||
|
|
|
||
|
|
| 插槽名 | 说明 |
|
||
|
|
| --- | --- |
|
||
|
|
| `upload-area` | 自定义上传区域布局,需同时设置 `useSlot="{{true}}"`。使用时上传触发器尺寸自适应 slot 内容 |
|
||
|
|
|
||
|
|
## CSS 变量
|
||
|
|
|
||
|
|
| 变量名 | 说明 | 默认值 |
|
||
|
|
| --- | --- | --- |
|
||
|
|
| `--upload-bg` | 上传区域背景色 | `#f7f8fa` |
|
||
|
|
| `--upload-border` | 边框颜色 | `#e5e7eb` |
|
||
|
|
| `--upload-text` | 主文字颜色 | `#1f2937` |
|
||
|
|
| `--upload-text-secondary` | 次要文字颜色 | `#9ca3af` |
|
||
|
|
| `--upload-primary` | 主题色 | `#3b82f6` |
|
||
|
|
| `--upload-error` | 错误色 | `#ef4444` |
|
||
|
|
| `--upload-mask` | 遮罩颜色 | `rgba(0, 0, 0, 0.5)` |
|
||
|
|
| `--upload-radius` | 圆角大小 | `16rpx` |
|
||
|
|
| `--upload-size` | 文件项尺寸(仅默认上传框生效,slot 模式自适应) | `160rpx` |
|
||
|
|
|
||
|
|
## 使用案例
|
||
|
|
|
||
|
|
### 案例 1:使用默认上传框
|
||
|
|
|
||
|
|
```json
|
||
|
|
// page.json
|
||
|
|
{
|
||
|
|
"usingComponents": {
|
||
|
|
"upload-file": "@/components/uploadFile/index"
|
||
|
|
}
|
||
|
|
}
|
||
|
|
```
|
||
|
|
|
||
|
|
```xml
|
||
|
|
<!-- page.wxml -->
|
||
|
|
<upload-file
|
||
|
|
maxCount="{{3}}"
|
||
|
|
maxSize="{{5242880}}"
|
||
|
|
accept="{{['image']}}"
|
||
|
|
bind:select="onUploadSelect"
|
||
|
|
bind:success="onUploadSuccess"
|
||
|
|
bind:error="onUploadError"
|
||
|
|
bind:remove="onUploadRemove"
|
||
|
|
/>
|
||
|
|
```
|
||
|
|
|
||
|
|
```ts
|
||
|
|
// page.ts
|
||
|
|
Page({
|
||
|
|
onUploadSelect(e: WechatMiniprogram.CustomEvent) {
|
||
|
|
console.log('选中文件', e.detail.files)
|
||
|
|
},
|
||
|
|
onUploadSuccess(e: WechatMiniprogram.CustomEvent) {
|
||
|
|
console.log('上传成功', e.detail.file, e.detail.response)
|
||
|
|
},
|
||
|
|
onUploadError(e: WechatMiniprogram.CustomEvent) {
|
||
|
|
console.log('上传失败', e.detail.file, e.detail.error)
|
||
|
|
},
|
||
|
|
onUploadRemove(e: WechatMiniprogram.CustomEvent) {
|
||
|
|
console.log('删除文件', e.detail.file, e.detail.fileList)
|
||
|
|
},
|
||
|
|
})
|
||
|
|
```
|
||
|
|
|
||
|
|
### 案例 2:使用 slot 自定义上传区域
|
||
|
|
|
||
|
|
```xml
|
||
|
|
<!-- page.wxml -->
|
||
|
|
<upload-file
|
||
|
|
useSlot="{{true}}"
|
||
|
|
maxCount="{{5}}"
|
||
|
|
accept="{{['image', 'video']}}"
|
||
|
|
bind:success="onUploadSuccess"
|
||
|
|
>
|
||
|
|
<view slot="upload-area" class="custom-upload-area">
|
||
|
|
<image src="/images/upload-icon.png" mode="aspectFit" />
|
||
|
|
<text>点击上传</text>
|
||
|
|
</view>
|
||
|
|
</upload-file>
|
||
|
|
```
|
||
|
|
|
||
|
|
```scss
|
||
|
|
// page.scss
|
||
|
|
.custom-upload-area {
|
||
|
|
display: flex;
|
||
|
|
flex-direction: column;
|
||
|
|
align-items: center;
|
||
|
|
justify-content: center;
|
||
|
|
gap: 12rpx;
|
||
|
|
|
||
|
|
image {
|
||
|
|
width: 64rpx;
|
||
|
|
height: 64rpx;
|
||
|
|
}
|
||
|
|
|
||
|
|
text {
|
||
|
|
font-size: 24rpx;
|
||
|
|
color: #9ca3af;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
```
|