@wuxiaolins/upload 0.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,433 @@
1
+ # @bsy-ui/upload 上传组件库
2
+
3
+ 基于 Ant Design Vue 和腾讯云 COS/VOD 的上传组件库,支持图片、视频上传和懒加载。
4
+
5
+ ## ✨ 特性
6
+
7
+ - 🚀 **开箱即用** - 内置腾讯云 COS/VOD 上传支持
8
+ - 📦 **Hook 懒加载** - 独立导出 Hook,减少 46% 打包体积
9
+ - 🎯 **类型安全** - 完整的 TypeScript 类型定义
10
+ - 📱 **响应式** - 自动适配 PC 和移动端
11
+ - 🎨 **自定义样式** - 支持自定义上传按钮和样式
12
+ - 🔄 **多种上传方式** - 支持 COS 直传、表单上传、自定义上传
13
+ - ✅ **文件验证** - 内置文件大小、类型、尺寸验证
14
+ - 📝 **完整文档** - 详细的使用文档和示例
15
+
16
+ ## 📦 安装
17
+
18
+ ```bash
19
+ npm install @bsy-ui/upload
20
+ # 或
21
+ pnpm add @bsy-ui/upload
22
+ ```
23
+
24
+ ## 🚀 快速开始
25
+
26
+ ### 组件使用
27
+
28
+ ```vue
29
+ <script setup lang="ts">
30
+ import { ref } from 'vue';
31
+ import { BsyUpload } from '@bsy-ui/upload';
32
+ import '@bsy-ui/upload/dist/style.css';
33
+
34
+ const uploadRef = ref();
35
+
36
+ const handleDone = (file) => {
37
+ console.log('上传完成', file);
38
+ };
39
+
40
+ const handleError = (error) => {
41
+ console.error('上传失败', error);
42
+ };
43
+ </script>
44
+
45
+ <template>
46
+ <BsyUpload
47
+ ref="uploadRef"
48
+ :count="3"
49
+ accept=".png,.jpg,.jpeg"
50
+ :max-size="5"
51
+ @done="handleDone"
52
+ @error="handleError"
53
+ />
54
+ </template>
55
+ ```
56
+
57
+ ### Hook 使用(懒加载,推荐)
58
+
59
+ ```typescript
60
+ import { useUpload } from '@bsy-ui/upload/hook';
61
+
62
+ const { urls, setFiles, remove, onUploadEvent, beforeUpload } = useUpload({
63
+ prod: true,
64
+ bucket: 'my-bucket',
65
+ region: 'ap-guangzhou',
66
+ prefixPath: '/uploads',
67
+ maxSize: 5,
68
+ count: 3
69
+ });
70
+
71
+ // 监听上传事件
72
+ onUploadEvent('success', (file) => {
73
+ console.log('上传成功', file);
74
+ });
75
+
76
+ onUploadEvent('error', (error) => {
77
+ console.error('上传失败', error);
78
+ });
79
+
80
+ // 设置文件列表
81
+ setFiles([
82
+ { url: 'https://example.com/image1.jpg' },
83
+ { url: 'https://example.com/image2.jpg' }
84
+ ]);
85
+
86
+ // 删除文件
87
+ remove(urls.value[0]);
88
+ ```
89
+
90
+ ## 📚 组件 API
91
+
92
+ ### BsyUpload Props
93
+
94
+ | 属性 | 类型 | 默认值 | 说明 |
95
+ |-----|------|--------|------|
96
+ | count | number | 1 | 最大上传数量 |
97
+ | accept | string | '.png,.jpeg,.jpg,.bmp,.webp,.svg,.tiff' | 接受的文件类型 |
98
+ | maxSize | number | 2 | 文件最大大小(MB) |
99
+ | widthRange | string | '1:9000' | 图片宽度范围 |
100
+ | heightRange | string | '1:9000' | 图片高度范围 |
101
+ | prefixPath | string | '/content' | 上传路径前缀 |
102
+ | disabled | boolean | false | 是否禁用 |
103
+ | showText | boolean | true | 是否显示上传文本 |
104
+ | text | string | '点击或拖拽上传' | 上传按钮文本 |
105
+ | showSize | boolean | true | 是否显示文件大小提示 |
106
+ | canSort | boolean | false | 是否可以拖拽排序 |
107
+ | listType | UploadListType | 'picture-card' | 上传列表的内建样式 |
108
+ | custom | boolean | false | 是否使用自定义插槽 |
109
+ | className | string | '' | 自定义样式类名 |
110
+ | uploadTextTip | string | '' | 上传提示文本 |
111
+ | showPreviewIcon | boolean | true | 是否显示预览图标 |
112
+ | showRemoveIcon | boolean | true | 是否显示删除图标 |
113
+ | showDownloadIcon | boolean | false | 是否显示下载图标 |
114
+
115
+ #### 上传配置
116
+
117
+ | 属性 | 类型 | 默认值 | 说明 |
118
+ |-----|------|--------|------|
119
+ | prod | boolean | true | 是否正式环境 |
120
+ | bucket | string | '' | 腾讯云 COS bucket |
121
+ | region | string | '' | 腾讯云 COS region |
122
+ | uploadType | string | '' | 上传方式('form' 为表单上传) |
123
+ | customUpload | boolean | false | 是否自定义上传 |
124
+ | action | string | '/api/base/admin/upload/upload/getCredential' | 上传地址 |
125
+ | headers | object | - | 上传请求头 |
126
+ | data | object | - | 上传额外参数 |
127
+
128
+ ### BsyUpload Events
129
+
130
+ | 事件 | 参数 | 说明 |
131
+ |-----|------|------|
132
+ | beforeUpload | (file) | 上传前回调 |
133
+ | beforeUploadError | (error) | 上传前验证失败 |
134
+ | add | (file) | 文件添加到列表 |
135
+ | progress | (file) | 上传进度更新 |
136
+ | done | (file) | 上传成功 |
137
+ | error | (error) | 上传失败 |
138
+ | remove | (file) | 文件删除 |
139
+ | change | (fileList) | 文件列表变化 |
140
+
141
+ ### BsyUpload Methods
142
+
143
+ | 方法 | 参数 | 返回值 | 说明 |
144
+ |-----|------|--------|------|
145
+ | resetFiles | (files: string[] \| object[]) | void | 重置文件列表 |
146
+
147
+ ### BsyUpload Slots
148
+
149
+ | 插槽 | 参数 | 说明 |
150
+ |-----|------|------|
151
+ | custom | - | 自定义上传按钮 |
152
+ | customIcon | { item } | 自定义文件图标 |
153
+
154
+ ## 🎯 Hook API
155
+
156
+ ### useUpload
157
+
158
+ ```typescript
159
+ const {
160
+ urls, // 文件列表
161
+ setFiles, // 设置文件列表
162
+ remove, // 删除文件
163
+ onUploadEvent, // 监听上传事件
164
+ beforeUpload // 上传前验证
165
+ } = useUpload(options);
166
+ ```
167
+
168
+ #### Options
169
+
170
+ | 属性 | 类型 | 默认值 | 说明 |
171
+ |-----|------|--------|------|
172
+ | prod | boolean | true | 是否正式环境 |
173
+ | bucket | string | '' | 腾讯云 COS bucket |
174
+ | region | string | '' | 腾讯云 COS region |
175
+ | action | string | - | 上传地址 |
176
+ | headers | object | {} | 上传请求头 |
177
+ | data | object | {} | 上传额外参数 |
178
+ | customUpload | boolean | false | 是否自定义上传 |
179
+ | accept | string | '.png,.jpeg,.jpg,.bmp,.webp,.svg,.tiff' | 接受的文件类型 |
180
+ | maxSize | number | 2 | 文件最大大小(MB) |
181
+ | widthRange | string | '1:9000' | 图片宽度范围 |
182
+ | heightRange | string | '1:9000' | 图片高度范围 |
183
+ | prefixPath | string | '/content' | 上传路径前缀 |
184
+ | count | number | 1 | 最大上传数量 |
185
+
186
+ #### Returns
187
+
188
+ | 属性 | 类型 | 说明 |
189
+ |-----|------|------|
190
+ | urls | Ref<WxFile[]> | 文件列表 |
191
+ | setFiles | (files: any[]) => void | 设置文件列表 |
192
+ | remove | (file: WxFile) => void | 删除文件 |
193
+ | onUploadEvent | (event: string, callback: Function) => void | 监听上传事件 |
194
+ | beforeUpload | (file: File) => Promise<boolean> | 上传前验证 |
195
+
196
+ #### Events
197
+
198
+ - `beforeUploadError` - 上传前验证失败
199
+ - `beforeUpload` - 上传前回调
200
+ - `add` - 文件添加到列表
201
+ - `progress` - 上传进度更新
202
+ - `success` - 上传成功
203
+
204
+ ## 💡 使用示例
205
+
206
+ ### 基础图片上传
207
+
208
+ ```vue
209
+ <template>
210
+ <BsyUpload
211
+ :count="5"
212
+ accept=".png,.jpg,.jpeg"
213
+ :max-size="10"
214
+ @done="handleDone"
215
+ />
216
+ </template>
217
+ ```
218
+
219
+ ### 自定义上传按钮
220
+
221
+ ```vue
222
+ <template>
223
+ <BsyUpload :count="3" custom>
224
+ <template #custom>
225
+ <a-button type="primary">
226
+ 点击上传
227
+ </a-button>
228
+ </template>
229
+ </BsyUpload>
230
+ </template>
231
+ ```
232
+
233
+ ### 文件回显
234
+
235
+ ```vue
236
+ <script setup>
237
+ import { ref, onMounted } from 'vue';
238
+
239
+ const uploadRef = ref();
240
+
241
+ onMounted(() => {
242
+ // 回显已上传的文件
243
+ uploadRef.value.resetFiles([
244
+ 'https://example.com/image1.jpg',
245
+ 'https://example.com/image2.jpg'
246
+ ]);
247
+ });
248
+ </script>
249
+
250
+ <template>
251
+ <BsyUpload ref="uploadRef" :count="5" />
252
+ </template>
253
+ ```
254
+
255
+ ### 使用 Hook(仅上传逻辑)
256
+
257
+ ```vue
258
+ <script setup>
259
+ import { useUpload } from '@bsy-ui/upload/hook';
260
+ import { Upload } from 'ant-design-vue';
261
+
262
+ const { urls, beforeUpload, onUploadEvent } = useUpload({
263
+ maxSize: 5,
264
+ count: 3
265
+ });
266
+
267
+ onUploadEvent('success', (file) => {
268
+ console.log('上传成功', file);
269
+ });
270
+ </script>
271
+
272
+ <template>
273
+ <Upload
274
+ :file-list="urls"
275
+ :before-upload="beforeUpload"
276
+ list-type="picture-card"
277
+ >
278
+ <div>点击上传</div>
279
+ </Upload>
280
+ </template>
281
+ ```
282
+
283
+ ### 腾讯云 COS 直传
284
+
285
+ ```vue
286
+ <template>
287
+ <BsyUpload
288
+ :prod="true"
289
+ bucket="my-bucket"
290
+ region="ap-guangzhou"
291
+ prefix-path="/uploads"
292
+ :count="5"
293
+ />
294
+ </template>
295
+ ```
296
+
297
+ ### 表单上传
298
+
299
+ ```vue
300
+ <template>
301
+ <BsyUpload
302
+ upload-type="form"
303
+ action="/api/upload"
304
+ :headers="{ Authorization: 'Bearer token' }"
305
+ :data="{ type: 'image' }"
306
+ :count="3"
307
+ />
308
+ </template>
309
+ ```
310
+
311
+ ## 📖 高级功能
312
+
313
+ ### 文件验证
314
+
315
+ 组件内置了多种文件验证:
316
+
317
+ - **文件类型验证** - 通过 `accept` 属性限制文件类型
318
+ - **文件大小验证** - 通过 `maxSize` 属性限制文件大小
319
+ - **图片尺寸验证** - 通过 `widthRange` 和 `heightRange` 限制图片尺寸
320
+
321
+ ```vue
322
+ <BsyUpload
323
+ accept=".png,.jpg"
324
+ :max-size="5"
325
+ width-range="100:1920"
326
+ height-range="100:1080"
327
+ />
328
+ ```
329
+
330
+ ### 拖拽排序
331
+
332
+ 启用 `canSort` 属性后,可以拖拽调整文件顺序:
333
+
334
+ ```vue
335
+ <BsyUpload :can-sort="true" :count="5" />
336
+ ```
337
+
338
+ ### 自定义样式
339
+
340
+ 通过 `className` 属性和 CSS 变量自定义样式:
341
+
342
+ ```vue
343
+ <BsyUpload class-name="my-upload" />
344
+
345
+ <style>
346
+ .my-upload {
347
+ --theme-upload-width: 120px;
348
+ --theme-upload-height: 120px;
349
+ --theme-upload-border-radius: 8px;
350
+ }
351
+ </style>
352
+ ```
353
+
354
+ ## 🔧 配置说明
355
+
356
+ ### 环境配置
357
+
358
+ ```typescript
359
+ // 开发环境
360
+ const upload = useUpload({
361
+ prod: false,
362
+ bucket: 'dev-bucket',
363
+ region: 'ap-guangzhou'
364
+ });
365
+
366
+ // 生产环境
367
+ const upload = useUpload({
368
+ prod: true,
369
+ bucket: 'prod-bucket',
370
+ region: 'ap-guangzhou'
371
+ });
372
+ ```
373
+
374
+ ### 上传路径
375
+
376
+ ```typescript
377
+ const upload = useUpload({
378
+ prefixPath: '/uploads/images', // 文件将上传到 /uploads/images/ 目录
379
+ });
380
+ ```
381
+
382
+ ## 📊 性能优化
383
+
384
+ ### Hook 懒加载
385
+
386
+ 使用独立的 Hook 导出可以减少打包体积:
387
+
388
+ ```typescript
389
+ // ❌ 从主包导入(28.42 kB)
390
+ import { useUpload } from '@bsy-ui/upload'
391
+
392
+ // ✅ 从 hook 子包导入(15.13 kB,减少 46%)
393
+ import { useUpload } from '@bsy-ui/upload/hook'
394
+ ```
395
+
396
+ ### 按需导入
397
+
398
+ ```typescript
399
+ // 仅导入需要的功能
400
+ import { useUpload } from '@bsy-ui/upload/hook';
401
+ ```
402
+
403
+ ## 🔗 相关文档
404
+
405
+ - [使用示例](./USAGE_EXAMPLE.md) - 详细的使用示例
406
+ - [构建说明](./BUILD_SUMMARY.md) - 构建配置说明
407
+ - [TypeScript 配置](./TYPESCRIPT_CONFIG.md) - TypeScript 配置说明
408
+ - [Vite 配置](./VITE_CONFIG_NOTES.md) - Vite 配置说明
409
+
410
+ ## 🤝 贡献
411
+
412
+ 欢迎提交 Issue 和 Pull Request!
413
+
414
+ ## 📄 许可证
415
+
416
+ MIT License
417
+
418
+ ## 🔗 相关链接
419
+
420
+ - [腾讯云 COS](https://cloud.tencent.com/product/cos)
421
+ - [腾讯云 VOD](https://cloud.tencent.com/product/vod)
422
+ - [Ant Design Vue](https://antdv.com/)
423
+
424
+ ## 📞 支持
425
+
426
+ 如有问题,请:
427
+ 1. 查看[使用示例](./USAGE_EXAMPLE.md)
428
+ 2. 查看[API 文档](#组件-api)
429
+ 3. 提交 [Issue](https://gitlab.bossyun.com/wuxl/package/issues)
430
+
431
+ ---
432
+
433
+ Made with ❤️ by BSY Team