合同小程序前端代码仓库
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.

261 lines
8.3 KiB

1 week ago
  1. # lime-upload
  2. - 文件上传组件,可以选择图片视频等任意文件,支持指定服务或当前uniCloud,兼容uniapp/uniappx
  3. - 插件依赖`lime-style`、`lime-shared`、`lime-icon`、`lime-svg`。不喜勿下
  4. ## 安装
  5. 插件市场导入即可,首次导入需要重新编译
  6. **注意**
  7. * 🔔 本插件依赖的[lime-svg](https://ext.dcloud.net.cn/plugin?id=18519)在 uniapp x app中是原生插件,如果购买(收费为5元)则需要自定义基座,才能使用!uniapp可忽略。
  8. * 🔔 不需要[lime-svg](https://ext.dcloud.net.cn/plugin?id=18519)在lime-icon代码中注释掉即可
  9. ```html
  10. // lime-icon/components/l-icon.uvue 第4行 注释掉即可。
  11. <!-- <l-svg class="l-icon" :class="classes" :style="styles" :color="color" :src="iconUrl" v-else :web="web" @error="imageError" @load="imageload" @click="$emit('click')"></l-svg> -->
  12. ```
  13. ## 文档
  14. [upload](https://limex.qcoon.cn/components/upload.html)
  15. ## 代码演示
  16. ### 基础使用
  17. 选择文件会触发 `add` 事件,获取到对应的 file 对象。
  18. ```html
  19. <l-upload @add="handleAdd"/>
  20. ```
  21. ```js
  22. import {UploadFile} from '@/uni_modules/lime-upload';
  23. const handleAdd = (files: UploadFile[])=>{
  24. console.log(files)
  25. }
  26. ```
  27. ### 单选
  28. 通过设置`max`为`1`,`multiple`为`false`关闭多选,只允许单选
  29. ```html
  30. <l-upload :max="1" :multiple="false" v-model="files">
  31. ```
  32. ```js
  33. import {UploadFile} from '@/uni_modules/lime-upload';
  34. const files = ref<UploadFile[]>([])
  35. ```
  36. ### 文件预览
  37. 通过设置`default-files`或`v-model`可以绑定已经上传的文件列表,并展示文件列表的预览图
  38. ```html
  39. <l-upload :column="4" :default-files="files">
  40. ```
  41. ```js
  42. import {UploadFile} from '@/uni_modules/lime-upload';
  43. const files = ref<UploadFile[]>([{
  44. url: 'https://img-cdn-tx.dcloud.net.cn/stream/plugin_screens/lime-barcode_0.png',
  45. name: 'uploaded4.png',
  46. type: 'image',
  47. }])
  48. ```
  49. ### 上传状态
  50. 通过设置`status`属性可以标识上传状态,`loading`表示上传中,`reload`表示重新上传,`failed`表示上传失败,`done `表示上传完成。`percent`属性表示上传进度
  51. ```html
  52. <l-upload :column="4" :default-files="files">
  53. ```
  54. ```js
  55. import {UploadFile} from '@/uni_modules/lime-upload';
  56. const files = ref<UploadFile[]>([{
  57. url: 'https://img-cdn-tx.dcloud.net.cn/stream/plugin_screens/lime-tag_0.png',
  58. name: 'uploaded1.png',
  59. type: 'image',
  60. status: 'loading'
  61. },
  62. {
  63. url: 'https://img-cdn-tx.dcloud.net.cn/stream/plugin_screens/lime-button_0.png',
  64. name: 'uploaded2.png',
  65. type: 'image',
  66. status: 'loading',
  67. percent: 68,
  68. },
  69. {
  70. url: 'https://img-cdn-tx.dcloud.net.cn/stream/plugin_screens/lime-color_0.png',
  71. name: 'uploaded3.png',
  72. type: 'image',
  73. status: 'reload',
  74. },
  75. {
  76. url: 'https://img-cdn-tx.dcloud.net.cn/stream/plugin_screens/lime-barcode_0.png',
  77. name: 'uploaded4.png',
  78. type: 'image',
  79. status: 'failed',
  80. },
  81. {
  82. url: 'https://img-cdn-tx.dcloud.net.cn/stream/plugin_screens/aff13f50-b9d4-11ee-9303-15d888ed69e8_0.png',
  83. name: 'uploaded5.png',
  84. type: 'image',
  85. }])
  86. ```
  87. ### 禁用
  88. 通过设置`disabled`可禁用组件
  89. ```html
  90. <l-upload :disabled="true">
  91. ```
  92. ### 限制上传数量
  93. 通过设置`max`可设置上传的数量
  94. ```html
  95. <l-upload :column="4" :max="10"></l-upload>
  96. ```
  97. ### 自定义样式
  98. 通过插槽可以自定义选择区域的样式。
  99. ```html
  100. <l-upload :multiple="false" :column="1" grid-height="200px" add-bg-color="#fff">
  101. <view style="height: 100%; width: 100%; display: flex; justify-content: center; align-items: center;">
  102. <image style="width: 100%; height: 100%; position: absolute; opacity: 0.5;" src="https://tdesign.gtimg.com/mobile/demos/upload1.png"></image>
  103. <view style="width: 72px; height: 72px; background: #e0eefe; border-radius: 99px; display: flex; justify-content: center; align-items: center; position: relative; z-index: 10;">
  104. <image style="width: 32px; height: 32px;" src="https://tdesign.gtimg.com/mobile/demos/upload3.png" />
  105. </view>
  106. </view>
  107. </l-upload>
  108. ```
  109. ### 上传方式
  110. 通过设置`action`指定服务器地址,如果为`uniCloud`则上传到当前绑定的`uniCloud`,设置`autoUpload`为`true`选文件后就立即上传。
  111. ```html
  112. <!-- 上传到uniCloud -->
  113. <l-upload action="uniCloud" :autoUpload="true">
  114. <!-- 指定服务器 -->
  115. <l-upload action="//service-bv448zsw-1257786608.gz.apigw.tencentcs.com/api/upload-demo" :autoUpload="true">
  116. <!-- 手动上传 -->
  117. <l-upload v-model="files" @add="handleAdd">
  118. ```
  119. ```js
  120. import {UploadFile} from '@/uni_modules/lime-upload';
  121. const files = ref<UploadFile[]>([]);
  122. const handleAdd = (files: UploadFile[])=> {
  123. files.forEach(file =>{
  124. const task = uni.uploadFile({
  125. url: 'https://example.xxx.com/upload', // 仅为示例,非真实的接口地址
  126. filePath: file.url,
  127. name: 'file',
  128. formData: { user: 'test' },
  129. success: (res) => {
  130. file.status = 'done'
  131. file.url = res.data.url
  132. console.log('上传完成')
  133. }
  134. });
  135. task.onProgressUpdate((res) => {
  136. file.status = 'loading'
  137. file.percent = res.progress
  138. console.log('上传进度:', res)
  139. });
  140. })
  141. }
  142. ```
  143. ### 查看示例
  144. - 导入后直接使用这个标签查看演示效果
  145. ```html
  146. <!-- // 代码位于 uni_modules/lime-upload/compoents/lime-upload -->
  147. <lime-upload />
  148. ```
  149. ### 插件标签
  150. - 默认 l-upload 为 component
  151. - 默认 lime-upload 为 demo
  152. ## API
  153. ### Props
  154. | 参数 | 说明 | 类型 | 默认值 |
  155. | --- | --- | --- | --- |
  156. | v-model | 双向绑定值 | _number_ | `-` |
  157. | disabled | 是否禁用文件上传 | _boolean_ | `false` |
  158. | multiple | 支持多文件上传 | _boolean_ | `true` |
  159. | disabled | 禁用全部操作 | _boolean_ | `false` |
  160. | disablePreview | 是否禁用预览 | _boolean_ | `false` |
  161. | autoUpload | 是否自动上传 | _boolean_ | `false` |
  162. | defaultFiles | 默认列表 | _UploadFile[]_ | `-` |
  163. | imageFit | 预览图裁剪模式,可选值参考小程序`image`组件的`mode`属性 | _string_ | `-` |
  164. | gutter | 格子间隔 | _string_ | `8px` |
  165. | column | 列数 | _string_ | `8px` |
  166. | mediatype | 支持上传的文件类型,图片或视频 `'image'\|'video'` | _string_ | `8px` |
  167. | maxDuration | 拍摄视频最长拍摄时间,单位秒 | _number_ | `10` |
  168. | sizeType | original 原图,compressed 压缩图 | _string[]_ | `['original','compressed']` |
  169. | sourceType | album 从相册选图,camera 使用相机,默认二者都有 | _string[]_ | `['album', 'camera']` |
  170. | max | 用于控制文件上传数量,值为 0 则不限制 | _number_ | `100` |
  171. | sizeLimit | 图片文件大小限制,默认单位 KB。 | _number_ | `-` |
  172. | uploadIcon | 上传图标[name](https://ext.dcloud.net.cn/plugin?id=14057) | _number_ | `1` |
  173. | uploadIconSize | 上传图标尺寸 | _string_ | `-` |
  174. | gridWidth | 格子宽度 | _string_ | `80px` |
  175. | gridHeight | 格子高度 | _string_ | `80px` |
  176. | gridBgColor | 格子背景色 | _string_ | `-` |
  177. | gridBorderRadius | 格子圆角 | _string_ | `-` |
  178. | addBgColor | 上传格子背景色 | _string_ | `-` |
  179. | loadingText | 上传文本 | _string_ | `-` |
  180. | reloadText | 重新上传文本 | _string_ | `-` |
  181. | failedText | 错误文本 | _string_ | `-` |
  182. | action | 上传地址 如需使用uniCloud服务,设置为uniCloud即可 | _string_ | `-` |
  183. | headers | 请求中其他额外的 form data | _object_ | `-` |
  184. ### UploadFile
  185. 名称 | 类型 | 默认值 | 说明
  186. -- | -- | -- | -- | --
  187. name | String | - | 文件名称 | N
  188. percent | Number | - | 上传进度 | N
  189. size | Number | - | 文件大小 | N
  190. status | String | - | 文件上传状态:上传成功,上传失败,上传中,等待上传。TS 类型:` 'loading' \| 'reload' \| 'failed' \| 'done'` | N
  191. type | String | - | 文件类型 | N
  192. url | String | - | 文件上传成功后的下载/访问地址 | N
  193. path | String | - | 临时地址 | N
  194. ## 主题定制
  195. ### 样式变量
  196. 组件提供了下列 CSS 变量,可用于自定义样式。uvue app无效
  197. | 名称 | 默认值 | 描述 |
  198. | --- | --- | --- |
  199. --l-upload-radius | $border-radius | -
  200. --l-upload-width | 80px | -
  201. --l-upload-height | 80px | -
  202. --l-upload-background | $upload-add-bg-color | -
  203. --l-upload-delete-icon-color | white | -
  204. --l-upload-add-icon-font-size | 28px | -
  205. --l-upload-add-bg-color | $gray-1 | -
  206. --l-upload-add-color | $text-color-4 | -
  207. ## 打赏
  208. 如果你觉得本插件,解决了你的问题,赠人玫瑰,手留余香。
  209. ![](https://testingcf.jsdelivr.net/gh/liangei/image@1.9/alipay.png)
  210. ![](https://testingcf.jsdelivr.net/gh/liangei/image@1.9/wpay.png)