uview-pro 0.0.14 → 0.0.16

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.
Files changed (236) hide show
  1. package/changelog.md +232 -223
  2. package/components/u-action-sheet/types.ts +35 -35
  3. package/components/u-action-sheet/u-action-sheet.vue +160 -160
  4. package/components/u-alert-tips/types.ts +39 -39
  5. package/components/u-alert-tips/u-alert-tips.vue +212 -212
  6. package/components/u-avatar/types.ts +34 -34
  7. package/components/u-avatar/u-avatar.vue +193 -193
  8. package/components/u-avatar-cropper/types.ts +23 -23
  9. package/components/u-avatar-cropper/u-avatar-cropper.vue +286 -286
  10. package/components/u-avatar-cropper/weCropper.d.ts +62 -62
  11. package/components/u-avatar-cropper/weCropper.js +1253 -1253
  12. package/components/u-avatar-cropper/weCropper.ts +1255 -1255
  13. package/components/u-back-top/types.ts +39 -39
  14. package/components/u-back-top/u-back-top.vue +125 -125
  15. package/components/u-badge/types.ts +36 -36
  16. package/components/u-badge/u-badge.vue +165 -165
  17. package/components/u-button/types.ts +66 -66
  18. package/components/u-button/u-button.vue +554 -554
  19. package/components/u-calendar/types.ts +63 -63
  20. package/components/u-calendar/u-calendar.vue +592 -592
  21. package/components/u-car-keyboard/types.ts +12 -12
  22. package/components/u-car-keyboard/u-car-keyboard.vue +234 -234
  23. package/components/u-card/types.ts +59 -59
  24. package/components/u-card/u-card.vue +194 -194
  25. package/components/u-cell-group/types.ts +17 -17
  26. package/components/u-cell-group/u-cell-group.vue +50 -50
  27. package/components/u-cell-item/types.ts +54 -54
  28. package/components/u-cell-item/u-cell-item.vue +202 -202
  29. package/components/u-checkbox/types.ts +31 -31
  30. package/components/u-checkbox/u-checkbox.vue +265 -265
  31. package/components/u-checkbox-group/types.ts +32 -32
  32. package/components/u-checkbox-group/u-checkbox-group.vue +79 -79
  33. package/components/u-circle-progress/types.ts +52 -52
  34. package/components/u-circle-progress/u-circle-progress.vue +189 -189
  35. package/components/u-city-select/types.ts +20 -20
  36. package/components/u-city-select/u-city-select.vue +236 -236
  37. package/components/u-col/types.ts +30 -30
  38. package/components/u-col/u-col.vue +123 -123
  39. package/components/u-collapse/types.ts +31 -31
  40. package/components/u-collapse/u-collapse.vue +68 -68
  41. package/components/u-collapse-item/types.ts +25 -25
  42. package/components/u-collapse-item/u-collapse-item.vue +176 -176
  43. package/components/u-column-notice/types.ts +48 -48
  44. package/components/u-column-notice/u-column-notice.vue +176 -176
  45. package/components/u-count-down/types.ts +42 -42
  46. package/components/u-count-down/u-count-down.vue +258 -258
  47. package/components/u-count-to/types.ts +32 -32
  48. package/components/u-count-to/u-count-to.vue +241 -241
  49. package/components/u-divider/types.ts +31 -31
  50. package/components/u-divider/u-divider.vue +121 -121
  51. package/components/u-dropdown/types.ts +32 -32
  52. package/components/u-dropdown/u-dropdown.vue +289 -289
  53. package/components/u-dropdown-item/types.ts +27 -27
  54. package/components/u-dropdown-item/u-dropdown-item.vue +123 -123
  55. package/components/u-empty/types.ts +36 -36
  56. package/components/u-empty/u-empty.vue +88 -88
  57. package/components/u-field/types.ts +69 -69
  58. package/components/u-field/u-field.vue +354 -354
  59. package/components/u-form/types.ts +6 -4
  60. package/components/u-form/u-form.vue +19 -5
  61. package/components/u-form-item/types.ts +2 -2
  62. package/components/u-form-item/u-form-item.vue +7 -6
  63. package/components/u-full-screen/types.ts +14 -14
  64. package/components/u-full-screen/u-full-screen.vue +82 -82
  65. package/components/u-gap/types.ts +18 -18
  66. package/components/u-gap/u-gap.vue +40 -40
  67. package/components/u-grid/types.ts +19 -19
  68. package/components/u-grid/u-grid.vue +93 -93
  69. package/components/u-grid-item/types.ts +16 -16
  70. package/components/u-grid-item/u-grid-item.vue +130 -130
  71. package/components/u-icon/types.ts +62 -62
  72. package/components/u-icon/u-icon.vue +274 -274
  73. package/components/u-image/types.ts +51 -49
  74. package/components/u-image/u-image.vue +222 -218
  75. package/components/u-index-anchor/types.ts +16 -16
  76. package/components/u-index-anchor/u-index-anchor.vue +86 -86
  77. package/components/u-index-list/types.ts +43 -43
  78. package/components/u-index-list/u-index-list.vue +355 -355
  79. package/components/u-input/types.ts +140 -140
  80. package/components/u-input/u-input.vue +255 -255
  81. package/components/u-keyboard/types.ts +40 -40
  82. package/components/u-keyboard/u-keyboard.vue +158 -158
  83. package/components/u-lazy-load/types.ts +37 -37
  84. package/components/u-lazy-load/u-lazy-load.vue +233 -233
  85. package/components/u-line/types.ts +44 -44
  86. package/components/u-line/u-line.vue +59 -59
  87. package/components/u-line-progress/types.ts +58 -58
  88. package/components/u-line-progress/u-line-progress.vue +109 -109
  89. package/components/u-link/types.ts +43 -43
  90. package/components/u-link/u-link.vue +75 -75
  91. package/components/u-loading/types.ts +35 -35
  92. package/components/u-loading/u-loading.vue +90 -90
  93. package/components/u-loading-popup/types.ts +19 -0
  94. package/components/u-loading-popup/u-loading-popup.vue +208 -0
  95. package/components/u-loadmore/types.ts +79 -79
  96. package/components/u-loadmore/u-loadmore.vue +140 -140
  97. package/components/u-mask/types.ts +43 -43
  98. package/components/u-mask/u-mask.vue +106 -106
  99. package/components/u-message-input/types.ts +74 -74
  100. package/components/u-message-input/u-message-input.vue +255 -255
  101. package/components/u-modal/types.ts +118 -118
  102. package/components/u-modal/u-modal.vue +200 -200
  103. package/components/u-navbar/types.ts +103 -103
  104. package/components/u-navbar/u-navbar.vue +226 -226
  105. package/components/u-no-network/image.ts +2 -2
  106. package/components/u-no-network/types.ts +28 -28
  107. package/components/u-no-network/u-no-network.vue +290 -290
  108. package/components/u-notice-bar/types.ts +111 -111
  109. package/components/u-notice-bar/u-notice-bar.vue +174 -174
  110. package/components/u-number-box/types.ts +42 -42
  111. package/components/u-number-box/u-number-box.vue +312 -312
  112. package/components/u-number-keyboard/types.ts +26 -26
  113. package/components/u-number-keyboard/u-number-keyboard.vue +166 -166
  114. package/components/u-picker/types.ts +111 -111
  115. package/components/u-picker/u-picker.vue +637 -637
  116. package/components/u-popup/types.ts +59 -59
  117. package/components/u-popup/u-popup.vue +359 -359
  118. package/components/u-radio/types.ts +25 -25
  119. package/components/u-radio/u-radio.vue +258 -258
  120. package/components/u-radio-group/types.ts +29 -29
  121. package/components/u-radio-group/u-radio-group.vue +98 -98
  122. package/components/u-rate/types.ts +40 -40
  123. package/components/u-rate/u-rate.vue +234 -234
  124. package/components/u-read-more/types.ts +35 -35
  125. package/components/u-read-more/u-read-more.vue +150 -150
  126. package/components/u-row/types.ts +20 -20
  127. package/components/u-row/u-row.vue +87 -87
  128. package/components/u-row-notice/types.ts +39 -39
  129. package/components/u-row-notice/u-row-notice.vue +213 -213
  130. package/components/u-search/types.ts +53 -53
  131. package/components/u-search/u-search.vue +256 -256
  132. package/components/u-section/types.ts +32 -32
  133. package/components/u-section/u-section.vue +125 -125
  134. package/components/u-select/types.ts +43 -43
  135. package/components/u-select/u-select.vue +361 -345
  136. package/components/u-skeleton/types.ts +20 -20
  137. package/components/u-skeleton/u-skeleton.vue +205 -205
  138. package/components/u-slider/types.ts +32 -32
  139. package/components/u-slider/u-slider.vue +238 -238
  140. package/components/u-steps/types.ts +28 -28
  141. package/components/u-steps/u-steps.vue +160 -160
  142. package/components/u-sticky/types.ts +22 -22
  143. package/components/u-sticky/u-sticky.vue +159 -159
  144. package/components/u-subsection/types.ts +36 -36
  145. package/components/u-subsection/u-subsection.vue +328 -328
  146. package/components/u-swipe-action/types.ts +50 -50
  147. package/components/u-swipe-action/u-swipe-action.vue +253 -253
  148. package/components/u-swiper/types.ts +47 -47
  149. package/components/u-swiper/u-swiper.vue +266 -266
  150. package/components/u-switch/types.ts +28 -28
  151. package/components/u-switch/u-switch.vue +128 -128
  152. package/components/u-tabbar/types.ts +36 -36
  153. package/components/u-tabbar/u-tabbar.vue +280 -280
  154. package/components/u-table/types.ts +25 -25
  155. package/components/u-table/u-table.vue +55 -55
  156. package/components/u-tabs/types.ts +51 -51
  157. package/components/u-tabs/u-tabs.vue +284 -284
  158. package/components/u-tabs-swiper/types.ts +53 -53
  159. package/components/u-tabs-swiper/u-tabs-swiper.vue +379 -379
  160. package/components/u-tag/types.ts +37 -37
  161. package/components/u-tag/u-tag.vue +244 -244
  162. package/components/u-td/types.ts +12 -12
  163. package/components/u-td/u-td.vue +69 -69
  164. package/components/u-th/types.ts +12 -12
  165. package/components/u-th/u-th.vue +63 -63
  166. package/components/u-time-line/u-time-line.vue +39 -39
  167. package/components/u-time-line-item/types.ts +14 -14
  168. package/components/u-time-line-item/u-time-line-item.vue +78 -78
  169. package/components/u-toast/types.ts +36 -36
  170. package/components/u-toast/u-toast.vue +233 -233
  171. package/components/u-top-tips/types.ts +14 -14
  172. package/components/u-top-tips/u-top-tips.vue +113 -113
  173. package/components/u-tr/types.ts +8 -8
  174. package/components/u-tr/u-tr.vue +24 -24
  175. package/components/u-upload/types.ts +71 -71
  176. package/components/u-upload/u-upload.vue +545 -545
  177. package/components/u-verification-code/types.ts +22 -22
  178. package/components/u-verification-code/u-verification-code.vue +164 -164
  179. package/components/u-waterfall/types.ts +16 -16
  180. package/components/u-waterfall/u-waterfall.vue +170 -170
  181. package/iconfont.css +910 -910
  182. package/index.scss +22 -22
  183. package/index.ts +202 -202
  184. package/libs/config/config.ts +26 -26
  185. package/libs/config/zIndex.ts +37 -37
  186. package/libs/css/color.scss +155 -155
  187. package/libs/css/common.scss +175 -175
  188. package/libs/css/style.components.scss +6 -6
  189. package/libs/css/style.h5.scss +8 -8
  190. package/libs/css/style.mp.scss +72 -72
  191. package/libs/css/style.nvue.scss +2 -2
  192. package/libs/css/style.vue.scss +175 -175
  193. package/libs/function/$parent.ts +22 -22
  194. package/libs/function/addUnit.ts +13 -13
  195. package/libs/function/color.ts +36 -36
  196. package/libs/function/colorGradient.ts +125 -125
  197. package/libs/function/debounce.ts +28 -28
  198. package/libs/function/deepClone.ts +39 -39
  199. package/libs/function/deepMerge.ts +34 -34
  200. package/libs/function/getParent.ts +58 -58
  201. package/libs/function/getRect.ts +26 -26
  202. package/libs/function/guid.ts +42 -42
  203. package/libs/function/md5.ts +398 -398
  204. package/libs/function/parent.ts +21 -21
  205. package/libs/function/queryParams.ts +60 -60
  206. package/libs/function/random.ts +16 -16
  207. package/libs/function/randomArray.ts +11 -11
  208. package/libs/function/route.ts +118 -118
  209. package/libs/function/sys.ts +15 -15
  210. package/libs/function/test.ts +229 -229
  211. package/libs/function/throttle.ts +31 -31
  212. package/libs/function/timeFormat.ts +54 -54
  213. package/libs/function/timeFrom.ts +48 -48
  214. package/libs/function/toast.ts +14 -14
  215. package/libs/function/trim.ts +21 -21
  216. package/libs/function/type2icon.ts +36 -36
  217. package/libs/hooks/useEmitter.ts +77 -77
  218. package/libs/hooks/useParent.ts +29 -29
  219. package/libs/request/auto-http.ts +76 -76
  220. package/libs/request/index.ts +237 -237
  221. package/libs/request/uni-http.md +156 -156
  222. package/libs/request/uni-http.ts +434 -434
  223. package/libs/store/index.ts +88 -88
  224. package/libs/util/async-validator.d.ts +62 -62
  225. package/libs/util/async-validator.js +1356 -1356
  226. package/libs/util/emitter.ts +112 -112
  227. package/libs/util/mitt.ts +118 -118
  228. package/libs/util/parent.ts +20 -20
  229. package/package.json +107 -107
  230. package/readme.md +231 -237
  231. package/theme.scss +38 -38
  232. package/types/components.d.ts +95 -94
  233. package/types/global.d.ts +221 -208
  234. package/types/ignore-errors.d.ts +30 -30
  235. package/types/index.d.ts +90 -90
  236. package/types/uni-app.d.ts +63 -63
@@ -1,545 +1,545 @@
1
- <template>
2
- <view class="u-upload" v-if="!disabled">
3
- <view
4
- v-if="showUploadList"
5
- class="u-list-item u-preview-wrap"
6
- v-for="(item, index) in lists"
7
- :key="index"
8
- :style="{
9
- width: $u.addUnit(width),
10
- height: $u.addUnit(height)
11
- }"
12
- >
13
- <view
14
- v-if="deletable"
15
- class="u-delete-icon"
16
- @tap.stop="deleteItem(index)"
17
- :style="{
18
- background: delBgColor
19
- }"
20
- >
21
- <u-icon class="u-icon" :name="delIcon" size="20" :color="delColor"></u-icon>
22
- </view>
23
- <u-line-progress
24
- v-if="showProgress && item.progress > 0 && item.progress != 100 && !item.error"
25
- :show-percent="false"
26
- height="16"
27
- class="u-progress"
28
- :percent="item.progress"
29
- ></u-line-progress>
30
- <view @tap.stop="retry(index)" v-if="item.error" class="u-error-btn">点击重试</view>
31
- <image @tap.stop="doPreviewImage(item.url || item.path, index)" class="u-preview-image" v-if="!item.isImage" :src="item.url || item.path" :mode="imageMode"></image>
32
- </view>
33
- <slot name="file" :file="lists"></slot>
34
- <view style="display: inline-block" @tap="selectFile" v-if="Number(maxCount) > lists.length">
35
- <slot name="addBtn"></slot>
36
- <view
37
- v-if="!customBtn"
38
- class="u-list-item u-add-wrap"
39
- hover-class="u-add-wrap__hover"
40
- hover-stay-time="150"
41
- :style="{
42
- width: $u.addUnit(width),
43
- height: $u.addUnit(height)
44
- }"
45
- >
46
- <u-icon name="plus" class="u-add-btn" size="40"></u-icon>
47
- <view class="u-add-tips">{{ uploadText }}</view>
48
- </view>
49
- </view>
50
- </view>
51
- </template>
52
-
53
- <script setup lang="ts">
54
- import { ref, watch } from 'vue';
55
- import { $u } from '../..';
56
- import { UploadProps } from './types';
57
-
58
- defineOptions({
59
- name: 'u-upload'
60
- });
61
-
62
- /**
63
- * upload 图片上传
64
- * @description 该组件用于上传图片场景
65
- * @tutorial https://uview-pro.netlify.app/components/upload.html
66
- * @property {String} action 服务器上传地址
67
- * @property {String Number} max-count 最大选择图片的数量(默认99)
68
- * @property {Boolean} custom-btn 如果需要自定义选择图片的按钮,设置为true(默认false)
69
- * @property {Boolean} show-progress 是否显示进度条(默认true)
70
- * @property {Boolean} disabled 是否启用(显示/移仓)组件(默认false)
71
- * @property {String} image-mode 预览图片等显示模式,可选值为uni的image的mode属性值(默认aspectFill)
72
- * @property {String} del-icon 右上角删除图标名称,只能为uView内置图标
73
- * @property {String} del-bg-color 右上角关闭按钮的背景颜色
74
- * @property {String | Number} index 在各个回调事件中的最后一个参数返回,用于区别是哪一个组件的事件
75
- * @property {String} del-color 右上角关闭按钮图标的颜色
76
- * @property {Object} header 上传携带的头信息,对象形式
77
- * @property {Object} form-data 上传额外携带的参数
78
- * @property {String} name 上传文件的字段名,供后端获取使用(默认file)
79
- * @property {Array<String>} size-type original 原图,compressed 压缩图,默认二者都有(默认['original', 'compressed'])
80
- * @property {Array<String>} source-type 选择图片的来源,album-从相册选图,camera-使用相机,默认二者都有(默认['album', 'camera'])
81
- * @property {Boolean} preview-full-image 是否可以通过uni.previewImage预览已选择的图片(默认true)
82
- * @property {Boolean} multiple 是否开启图片多选,部分安卓机型不支持(默认true)
83
- * @property {Boolean} deletable 是否显示删除图片的按钮(默认true)
84
- * @property {String Number} max-size 选择单个文件的最大大小,单位B(byte),默认不限制(默认Number.MAX_VALUE)
85
- * @property {Array<Object>} file-list 默认显示的图片列表,数组元素为对象,必须提供url属性
86
- * @property {Boolean} upload-text 选择图片按钮的提示文字(默认“选择图片”)
87
- * @property {Boolean} auto-upload 选择完图片是否自动上传,见上方说明(默认true)
88
- * @property {Boolean} show-tips 特殊情况下是否自动提示toast,见上方说明(默认true)
89
- * @property {Boolean} show-upload-list 是否显示组件内部的图片预览(默认true)
90
- * @event {Function} on-oversize 图片大小超出最大允许大小
91
- * @event {Function} on-preview 全屏预览图片时触发
92
- * @event {Function} on-remove 移除图片时触发
93
- * @event {Function} on-success 图片上传成功时触发
94
- * @event {Function} on-change 图片上传后,无论成功或者失败都会触发
95
- * @event {Function} on-error 图片上传失败时触发
96
- * @event {Function} on-progress 图片上传过程中的进度变化过程触发
97
- * @event {Function} on-uploaded 所有图片上传完毕触发
98
- * @event {Function} on-choose-complete 每次选择图片后触发,只是让外部可以得知每次选择后,内部的文件列表
99
- * @event {Function} on-choose-fail 文件选择失败时触发
100
- * @event {Function} on-list-change 文件列表发生变化时触发
101
- * @example <u-upload :action="action" :file-list="fileList" ></u-upload>
102
- */
103
-
104
- const props = defineProps(UploadProps);
105
-
106
- const emit = defineEmits([
107
- 'on-list-change',
108
- 'on-oversize',
109
- 'on-exceed',
110
- 'on-choose-complete',
111
- 'on-choose-fail',
112
- 'on-uploaded',
113
- 'on-success',
114
- 'on-error',
115
- 'on-change',
116
- 'on-progress',
117
- 'on-remove',
118
- 'on-preview'
119
- ]);
120
-
121
- const lists = ref<any[]>([]);
122
- const uploading = ref(false);
123
-
124
- // 监听 fileList 变化,自动同步内部 lists
125
- watch(
126
- () => props.fileList,
127
- val => {
128
- val.map((value: any) => {
129
- // 首先检查内部是否已经添加过这张图片,因为外部绑定了一个对象给fileList的话(对象引用),进行修改外部fileList
130
- // 时,会触发watch,导致重新把原来的图片再次添加到this.lists
131
- // 数组的some方法意思是,只要数组元素有任意一个元素条件符合,就返回true,而另一个数组的every方法的意思是数组所有元素都符合条件才返回true
132
- let tmp = lists.value.some(val2 => val2.url == value.url);
133
- // 如果内部没有这个图片(tmp为false),则添加到内部
134
- !tmp && lists.value.push({ url: value.url, error: false, progress: 100 });
135
- });
136
- },
137
- { immediate: true, deep: true }
138
- );
139
-
140
- // 监听 lists 变化,发出事件
141
- watch(
142
- lists,
143
- n => {
144
- emit('on-list-change', n, props.index);
145
- },
146
- { deep: true }
147
- );
148
-
149
- /**
150
- * 清除列表
151
- */
152
- function clear() {
153
- lists.value = [];
154
- }
155
-
156
- /**
157
- * 重新上传队列中上传失败的所有文件
158
- */
159
- function reUpload() {
160
- uploadFile();
161
- }
162
-
163
- /**
164
- * 选择图片
165
- */
166
- function selectFile() {
167
- if (props.disabled) return;
168
- const newMaxCount = Number(props.maxCount) - lists.value.length;
169
- // 设置为只选择图片的时候使用 chooseImage 来实现
170
- let chooseFile: Promise<any>;
171
- chooseFile = new Promise((resolve, reject) => {
172
- uni.chooseImage({
173
- count: props.multiple ? (newMaxCount > 9 ? 9 : newMaxCount) : 1,
174
- sourceType: props.sourceType as string[],
175
- sizeType: props.sizeType as string[],
176
- success: resolve,
177
- fail: reject
178
- });
179
- });
180
- chooseFile
181
- .then((res: any) => {
182
- let listOldLength = lists.value.length;
183
- res.tempFiles.map((val: any, index: number) => {
184
- // 检查文件后缀是否允许,如果不在limitType内,就会返回false
185
- if (!checkFileExt(val)) return;
186
- // 如果是非多选,index大于等于1或者超出最大限制数量时,不处理
187
- if (!props.multiple && index >= 1) return;
188
- if (val.size > Number(props.maxSize)) {
189
- emit('on-oversize', val, lists.value, props.index);
190
- showToast('超出允许的文件大小');
191
- } else {
192
- if (Number(props.maxCount) <= lists.value.length) {
193
- emit('on-exceed', val, lists.value, props.index);
194
- showToast('超出最大允许的文件个数');
195
- return;
196
- }
197
- lists.value.push({ url: val.path, progress: 0, error: false, file: val });
198
- }
199
- });
200
- // 每次图片选择完,抛出一个事件,并将当前内部选择的图片数组抛出去
201
- emit('on-choose-complete', lists.value, props.index);
202
- if (props.autoUpload) uploadFile(listOldLength);
203
- })
204
- .catch((error: any) => {
205
- emit('on-choose-fail', error);
206
- });
207
- }
208
-
209
- /**
210
- * 提示用户消息
211
- */
212
- function showToast(message: string, force = false) {
213
- if (props.showTips || force) {
214
- uni.showToast({ title: message, icon: 'none' });
215
- }
216
- }
217
-
218
- /**
219
- * 该方法供用户通过ref调用,手动上传
220
- */
221
- function upload() {
222
- uploadFile();
223
- }
224
-
225
- /**
226
- * 对失败的图片重新上传
227
- */
228
- function retry(index: number) {
229
- lists.value[index].progress = 0;
230
- lists.value[index].error = false;
231
- lists.value[index].response = null;
232
- uni.showLoading({ title: '重新上传' });
233
- uploadFile(index);
234
- }
235
-
236
- /**
237
- * 上传图片
238
- */
239
- async function uploadFile(index = 0) {
240
- if (props.disabled) return;
241
- if (uploading.value) return;
242
- // 全部上传完成
243
- if (index >= lists.value.length) {
244
- emit('on-uploaded', lists.value, props.index);
245
- return;
246
- }
247
- // 检查是否是已上传或者正在上传中
248
- if (lists.value[index].progress == 100) {
249
- if (props.autoUpload == false) uploadFile(index + 1);
250
- return;
251
- }
252
- // 执行before-upload钩子
253
- if (props.beforeUpload && typeof props.beforeUpload === 'function') {
254
- // 执行回调,同时传入索引和文件列表当作参数
255
- // 在微信,支付宝等环境(H5正常),会导致父组件定义的customBack()函数体中的this变成子组件的this
256
- // 通过bind()方法,绑定父组件的this,让this.customBack()的this为父组件的上下文
257
- // 因为upload组件可能会被嵌套在其他组件内,比如u-form,这时this.$parent其实为u-form的this,
258
- // 非页面的this,所以这里需要往上历遍,一直寻找到最顶端的$parent,这里用了this.$u.$parent.call(this)
259
- // 明白意思即可,无需纠结this.$u.$parent.call(this)的细节
260
- let beforeResponse = props.beforeUpload(index, lists.value);
261
- // 判断是否返回了promise
262
- if (typeof beforeResponse === 'object' && beforeResponse !== null && typeof (beforeResponse as Promise<any>).then === 'function') {
263
- await (beforeResponse as Promise<any>)
264
- .then(() => {
265
- // promise返回成功,不进行动作,继续上传
266
- })
267
- .catch(() => {
268
- // 进入catch回调的话,继续下一张
269
- return uploadFile(index + 1);
270
- });
271
- } else if (beforeResponse === false) {
272
- // 如果返回false,继续下一张图片的上传
273
- return uploadFile(index + 1);
274
- } else {
275
- // 此处为返回"true"的情形,这里不写代码,就跳过此处,继续执行当前的上传逻辑
276
- }
277
- }
278
- // 检查上传地址
279
- if (!props.action) {
280
- showToast('请配置上传地址', true);
281
- return;
282
- }
283
- lists.value[index].error = false;
284
- uploading.value = true;
285
- // 创建上传对象
286
- const task = uni.uploadFile({
287
- url: props.action,
288
- filePath: lists.value[index].url,
289
- name: props.name,
290
- formData: props.formData,
291
- header: props.header,
292
- // #ifdef MP-ALIPAY
293
- fileType: 'image',
294
- // #endif
295
- success: (res: any) => {
296
- // 判断是否json字符串,将其转为json格式
297
- let data = props.toJson && $u.test.jsonString(res.data) ? JSON.parse(res.data) : res.data;
298
- if (![200, 201, 204].includes(res.statusCode)) {
299
- uploadError(index, data);
300
- } else {
301
- // 上传成功
302
- lists.value[index].response = data;
303
- lists.value[index].progress = 100;
304
- lists.value[index].error = false;
305
- emit('on-success', data, index, lists.value, props.index);
306
- }
307
- },
308
- fail: (e: any) => {
309
- uploadError(index, e);
310
- },
311
- complete: (res: any) => {
312
- uni.hideLoading();
313
- uploading.value = false;
314
- uploadFile(index + 1);
315
- emit('on-change', res, index, lists.value, props.index);
316
- }
317
- });
318
- task.onProgressUpdate((res: any) => {
319
- if (res.progress > 0) {
320
- lists.value[index].progress = res.progress;
321
- emit('on-progress', res, index, lists.value, props.index);
322
- }
323
- });
324
- }
325
-
326
- /**
327
- * 上传失败
328
- */
329
- function uploadError(index: number, err: any) {
330
- lists.value[index].progress = 0;
331
- lists.value[index].error = true;
332
- lists.value[index].response = null;
333
- emit('on-error', err, index, lists.value, props.index);
334
- showToast('上传失败,请重试');
335
- }
336
-
337
- /**
338
- * 删除一个图片
339
- */
340
- function deleteItem(index: number) {
341
- uni.showModal({
342
- title: '提示',
343
- content: '您确定要删除此项吗?',
344
- success: async (res: any) => {
345
- if (res.confirm) {
346
- // 先检查是否有定义before-remove移除前钩子
347
- // 执行before-remove钩子
348
- if (props.beforeRemove && typeof props.beforeRemove === 'function') {
349
- // 此处钩子执行 原理同before-remove参数,见上方注释
350
- let beforeResponse = props.beforeRemove(index, lists.value);
351
- // 判断是否返回了promise
352
- if (typeof beforeResponse === 'object' && beforeResponse !== null && typeof (beforeResponse as Promise<any>).then === 'function') {
353
- await (beforeResponse as Promise<any>)
354
- .then(() => {
355
- // promise返回成功,不进行动作,继续上传
356
- handlerDeleteItem(index);
357
- })
358
- .catch(() => {
359
- // 如果进入promise的reject,终止删除操作
360
- showToast('已终止移除');
361
- });
362
- } else if (beforeResponse === false) {
363
- // 返回false,终止删除
364
- showToast('已终止移除');
365
- } else {
366
- // 如果返回true,执行删除操作
367
- handlerDeleteItem(index);
368
- }
369
- } else {
370
- // 如果不存在before-remove钩子,
371
- handlerDeleteItem(index);
372
- }
373
- }
374
- }
375
- });
376
- }
377
-
378
- /**
379
- * 执行移除图片的动作
380
- */
381
- function handlerDeleteItem(index: number) {
382
- // 如果文件正在上传中,终止上传任务,进度在0 < progress < 100则意味着正在上传
383
- if (lists.value[index].progress < 100 && lists.value[index].progress > 0) {
384
- typeof lists.value[index].uploadTask != 'undefined' && lists.value[index].uploadTask.abort();
385
- }
386
- lists.value.splice(index, 1);
387
- emit('on-remove', index, lists.value, props.index);
388
- showToast('移除成功');
389
- }
390
-
391
- /**
392
- * 用户通过ref手动的形式,移除一张图片
393
- */
394
- function remove(index: number) {
395
- // 判断索引的合法范围
396
- if (index >= 0 && index < lists.value.length) {
397
- lists.value.splice(index, 1);
398
- emit('on-list-change', lists.value, props.index);
399
- }
400
- }
401
-
402
- /**
403
- * 预览图片
404
- */
405
- function doPreviewImage(url: string, index: number) {
406
- // 判断是否允许预览
407
- if (!props.previewFullImage) return;
408
- // 获取所有图片的url
409
- const images = lists.value.map(item => item.url || item.path);
410
- uni.previewImage({
411
- urls: images,
412
- current: url,
413
- success: () => {
414
- emit('on-preview', url, lists.value, props.index);
415
- },
416
- fail: () => {
417
- uni.showToast({ title: '预览图片失败', icon: 'none' });
418
- }
419
- });
420
- }
421
-
422
- /**
423
- * 判断文件后缀是否允许
424
- */
425
- function checkFileExt(file: any) {
426
- // 检查是否在允许的后缀中
427
- let noArrowExt = false;
428
- // 获取后缀名
429
- let fileExt = '';
430
- const reg = /.+\./;
431
- // 如果是H5,需要从name中判断
432
- // #ifdef H5
433
- // H5环境下从name中获取后缀
434
- fileExt = file.name.replace(reg, '').toLowerCase();
435
- // #endif
436
-
437
- // 非H5环境下从path中获取后缀
438
- // #ifndef H5
439
- fileExt = file.path.replace(reg, '').toLowerCase();
440
- // #endif
441
- // 使用数组的some方法,只要符合limitType中的一个,就返回true
442
- noArrowExt = (props.limitType as string[]).some((ext: string) => {
443
- // 转为小写
444
- return ext.toLowerCase() === fileExt;
445
- });
446
- if (!noArrowExt) showToast(`不允许选择${fileExt}格式的文件`);
447
- return noArrowExt;
448
- }
449
-
450
- defineExpose({ clear, reUpload, selectFile, upload, retry, remove, doPreviewImage, lists });
451
- </script>
452
-
453
- <style lang="scss" scoped>
454
- @import '../../libs/css/style.components.scss';
455
-
456
- .u-upload {
457
- @include vue-flex;
458
- flex-wrap: wrap;
459
- align-items: center;
460
- }
461
-
462
- .u-list-item {
463
- width: 200rpx;
464
- height: 200rpx;
465
- overflow: hidden;
466
- margin: 10rpx;
467
- background: rgb(244, 245, 246);
468
- position: relative;
469
- border-radius: 10rpx;
470
- /* #ifndef APP-NVUE */
471
- display: flex;
472
- /* #endif */
473
- align-items: center;
474
- justify-content: center;
475
- }
476
-
477
- .u-preview-wrap {
478
- border: 1px solid rgb(235, 236, 238);
479
- }
480
-
481
- .u-add-wrap {
482
- flex-direction: column;
483
- color: $u-content-color;
484
- font-size: 26rpx;
485
- }
486
-
487
- .u-add-tips {
488
- margin-top: 20rpx;
489
- line-height: 40rpx;
490
- }
491
-
492
- .u-add-wrap__hover {
493
- background-color: rgb(235, 236, 238);
494
- }
495
-
496
- .u-preview-image {
497
- display: block;
498
- width: 100%;
499
- height: 100%;
500
- border-radius: 10rpx;
501
- }
502
-
503
- .u-delete-icon {
504
- position: absolute;
505
- top: 10rpx;
506
- right: 10rpx;
507
- z-index: 10;
508
- background-color: $u-type-error;
509
- border-radius: 100rpx;
510
- width: 44rpx;
511
- height: 44rpx;
512
- @include vue-flex;
513
- align-items: center;
514
- justify-content: center;
515
- }
516
-
517
- .u-icon {
518
- @include vue-flex;
519
- align-items: center;
520
- justify-content: center;
521
- }
522
-
523
- .u-progress {
524
- position: absolute;
525
- bottom: 10rpx;
526
- left: 8rpx;
527
- right: 8rpx;
528
- z-index: 9;
529
- width: auto;
530
- }
531
-
532
- .u-error-btn {
533
- color: #ffffff;
534
- background-color: $u-type-error;
535
- font-size: 20rpx;
536
- padding: 4px 0;
537
- text-align: center;
538
- position: absolute;
539
- bottom: 0;
540
- left: 0;
541
- right: 0;
542
- z-index: 9;
543
- line-height: 1;
544
- }
545
- </style>
1
+ <template>
2
+ <view class="u-upload" v-if="!disabled">
3
+ <view
4
+ v-if="showUploadList"
5
+ class="u-list-item u-preview-wrap"
6
+ v-for="(item, index) in lists"
7
+ :key="index"
8
+ :style="{
9
+ width: $u.addUnit(width),
10
+ height: $u.addUnit(height)
11
+ }"
12
+ >
13
+ <view
14
+ v-if="deletable"
15
+ class="u-delete-icon"
16
+ @tap.stop="deleteItem(index)"
17
+ :style="{
18
+ background: delBgColor
19
+ }"
20
+ >
21
+ <u-icon class="u-icon" :name="delIcon" size="20" :color="delColor"></u-icon>
22
+ </view>
23
+ <u-line-progress
24
+ v-if="showProgress && item.progress > 0 && item.progress != 100 && !item.error"
25
+ :show-percent="false"
26
+ height="16"
27
+ class="u-progress"
28
+ :percent="item.progress"
29
+ ></u-line-progress>
30
+ <view @tap.stop="retry(index)" v-if="item.error" class="u-error-btn">点击重试</view>
31
+ <image @tap.stop="doPreviewImage(item.url || item.path, index)" class="u-preview-image" v-if="!item.isImage" :src="item.url || item.path" :mode="imageMode"></image>
32
+ </view>
33
+ <slot name="file" :file="lists"></slot>
34
+ <view style="display: inline-block" @tap="selectFile" v-if="Number(maxCount) > lists.length">
35
+ <slot name="addBtn"></slot>
36
+ <view
37
+ v-if="!customBtn"
38
+ class="u-list-item u-add-wrap"
39
+ hover-class="u-add-wrap__hover"
40
+ hover-stay-time="150"
41
+ :style="{
42
+ width: $u.addUnit(width),
43
+ height: $u.addUnit(height)
44
+ }"
45
+ >
46
+ <u-icon name="plus" class="u-add-btn" size="40"></u-icon>
47
+ <view class="u-add-tips">{{ uploadText }}</view>
48
+ </view>
49
+ </view>
50
+ </view>
51
+ </template>
52
+
53
+ <script setup lang="ts">
54
+ import { ref, watch } from 'vue';
55
+ import { $u } from '../..';
56
+ import { UploadProps } from './types';
57
+
58
+ defineOptions({
59
+ name: 'u-upload'
60
+ });
61
+
62
+ /**
63
+ * upload 图片上传
64
+ * @description 该组件用于上传图片场景
65
+ * @tutorial https://uview-pro.netlify.app/components/upload.html
66
+ * @property {String} action 服务器上传地址
67
+ * @property {String Number} max-count 最大选择图片的数量(默认99)
68
+ * @property {Boolean} custom-btn 如果需要自定义选择图片的按钮,设置为true(默认false)
69
+ * @property {Boolean} show-progress 是否显示进度条(默认true)
70
+ * @property {Boolean} disabled 是否启用(显示/移仓)组件(默认false)
71
+ * @property {String} image-mode 预览图片等显示模式,可选值为uni的image的mode属性值(默认aspectFill)
72
+ * @property {String} del-icon 右上角删除图标名称,只能为uView内置图标
73
+ * @property {String} del-bg-color 右上角关闭按钮的背景颜色
74
+ * @property {String | Number} index 在各个回调事件中的最后一个参数返回,用于区别是哪一个组件的事件
75
+ * @property {String} del-color 右上角关闭按钮图标的颜色
76
+ * @property {Object} header 上传携带的头信息,对象形式
77
+ * @property {Object} form-data 上传额外携带的参数
78
+ * @property {String} name 上传文件的字段名,供后端获取使用(默认file)
79
+ * @property {Array<String>} size-type original 原图,compressed 压缩图,默认二者都有(默认['original', 'compressed'])
80
+ * @property {Array<String>} source-type 选择图片的来源,album-从相册选图,camera-使用相机,默认二者都有(默认['album', 'camera'])
81
+ * @property {Boolean} preview-full-image 是否可以通过uni.previewImage预览已选择的图片(默认true)
82
+ * @property {Boolean} multiple 是否开启图片多选,部分安卓机型不支持(默认true)
83
+ * @property {Boolean} deletable 是否显示删除图片的按钮(默认true)
84
+ * @property {String Number} max-size 选择单个文件的最大大小,单位B(byte),默认不限制(默认Number.MAX_VALUE)
85
+ * @property {Array<Object>} file-list 默认显示的图片列表,数组元素为对象,必须提供url属性
86
+ * @property {Boolean} upload-text 选择图片按钮的提示文字(默认“选择图片”)
87
+ * @property {Boolean} auto-upload 选择完图片是否自动上传,见上方说明(默认true)
88
+ * @property {Boolean} show-tips 特殊情况下是否自动提示toast,见上方说明(默认true)
89
+ * @property {Boolean} show-upload-list 是否显示组件内部的图片预览(默认true)
90
+ * @event {Function} on-oversize 图片大小超出最大允许大小
91
+ * @event {Function} on-preview 全屏预览图片时触发
92
+ * @event {Function} on-remove 移除图片时触发
93
+ * @event {Function} on-success 图片上传成功时触发
94
+ * @event {Function} on-change 图片上传后,无论成功或者失败都会触发
95
+ * @event {Function} on-error 图片上传失败时触发
96
+ * @event {Function} on-progress 图片上传过程中的进度变化过程触发
97
+ * @event {Function} on-uploaded 所有图片上传完毕触发
98
+ * @event {Function} on-choose-complete 每次选择图片后触发,只是让外部可以得知每次选择后,内部的文件列表
99
+ * @event {Function} on-choose-fail 文件选择失败时触发
100
+ * @event {Function} on-list-change 文件列表发生变化时触发
101
+ * @example <u-upload :action="action" :file-list="fileList" ></u-upload>
102
+ */
103
+
104
+ const props = defineProps(UploadProps);
105
+
106
+ const emit = defineEmits([
107
+ 'on-list-change',
108
+ 'on-oversize',
109
+ 'on-exceed',
110
+ 'on-choose-complete',
111
+ 'on-choose-fail',
112
+ 'on-uploaded',
113
+ 'on-success',
114
+ 'on-error',
115
+ 'on-change',
116
+ 'on-progress',
117
+ 'on-remove',
118
+ 'on-preview'
119
+ ]);
120
+
121
+ const lists = ref<any[]>([]);
122
+ const uploading = ref(false);
123
+
124
+ // 监听 fileList 变化,自动同步内部 lists
125
+ watch(
126
+ () => props.fileList,
127
+ val => {
128
+ val.map((value: any) => {
129
+ // 首先检查内部是否已经添加过这张图片,因为外部绑定了一个对象给fileList的话(对象引用),进行修改外部fileList
130
+ // 时,会触发watch,导致重新把原来的图片再次添加到this.lists
131
+ // 数组的some方法意思是,只要数组元素有任意一个元素条件符合,就返回true,而另一个数组的every方法的意思是数组所有元素都符合条件才返回true
132
+ let tmp = lists.value.some(val2 => val2.url == value.url);
133
+ // 如果内部没有这个图片(tmp为false),则添加到内部
134
+ !tmp && lists.value.push({ url: value.url, error: false, progress: 100 });
135
+ });
136
+ },
137
+ { immediate: true, deep: true }
138
+ );
139
+
140
+ // 监听 lists 变化,发出事件
141
+ watch(
142
+ lists,
143
+ n => {
144
+ emit('on-list-change', n, props.index);
145
+ },
146
+ { deep: true }
147
+ );
148
+
149
+ /**
150
+ * 清除列表
151
+ */
152
+ function clear() {
153
+ lists.value = [];
154
+ }
155
+
156
+ /**
157
+ * 重新上传队列中上传失败的所有文件
158
+ */
159
+ function reUpload() {
160
+ uploadFile();
161
+ }
162
+
163
+ /**
164
+ * 选择图片
165
+ */
166
+ function selectFile() {
167
+ if (props.disabled) return;
168
+ const newMaxCount = Number(props.maxCount) - lists.value.length;
169
+ // 设置为只选择图片的时候使用 chooseImage 来实现
170
+ let chooseFile: Promise<any>;
171
+ chooseFile = new Promise((resolve, reject) => {
172
+ uni.chooseImage({
173
+ count: props.multiple ? (newMaxCount > 9 ? 9 : newMaxCount) : 1,
174
+ sourceType: props.sourceType as string[],
175
+ sizeType: props.sizeType as string[],
176
+ success: resolve,
177
+ fail: reject
178
+ });
179
+ });
180
+ chooseFile
181
+ .then((res: any) => {
182
+ let listOldLength = lists.value.length;
183
+ res.tempFiles.map((val: any, index: number) => {
184
+ // 检查文件后缀是否允许,如果不在limitType内,就会返回false
185
+ if (!checkFileExt(val)) return;
186
+ // 如果是非多选,index大于等于1或者超出最大限制数量时,不处理
187
+ if (!props.multiple && index >= 1) return;
188
+ if (val.size > Number(props.maxSize)) {
189
+ emit('on-oversize', val, lists.value, props.index);
190
+ showToast('超出允许的文件大小');
191
+ } else {
192
+ if (Number(props.maxCount) <= lists.value.length) {
193
+ emit('on-exceed', val, lists.value, props.index);
194
+ showToast('超出最大允许的文件个数');
195
+ return;
196
+ }
197
+ lists.value.push({ url: val.path, progress: 0, error: false, file: val });
198
+ }
199
+ });
200
+ // 每次图片选择完,抛出一个事件,并将当前内部选择的图片数组抛出去
201
+ emit('on-choose-complete', lists.value, props.index);
202
+ if (props.autoUpload) uploadFile(listOldLength);
203
+ })
204
+ .catch((error: any) => {
205
+ emit('on-choose-fail', error);
206
+ });
207
+ }
208
+
209
+ /**
210
+ * 提示用户消息
211
+ */
212
+ function showToast(message: string, force = false) {
213
+ if (props.showTips || force) {
214
+ uni.showToast({ title: message, icon: 'none' });
215
+ }
216
+ }
217
+
218
+ /**
219
+ * 该方法供用户通过ref调用,手动上传
220
+ */
221
+ function upload() {
222
+ uploadFile();
223
+ }
224
+
225
+ /**
226
+ * 对失败的图片重新上传
227
+ */
228
+ function retry(index: number) {
229
+ lists.value[index].progress = 0;
230
+ lists.value[index].error = false;
231
+ lists.value[index].response = null;
232
+ uni.showLoading({ title: '重新上传' });
233
+ uploadFile(index);
234
+ }
235
+
236
+ /**
237
+ * 上传图片
238
+ */
239
+ async function uploadFile(index = 0) {
240
+ if (props.disabled) return;
241
+ if (uploading.value) return;
242
+ // 全部上传完成
243
+ if (index >= lists.value.length) {
244
+ emit('on-uploaded', lists.value, props.index);
245
+ return;
246
+ }
247
+ // 检查是否是已上传或者正在上传中
248
+ if (lists.value[index].progress == 100) {
249
+ if (props.autoUpload == false) uploadFile(index + 1);
250
+ return;
251
+ }
252
+ // 执行before-upload钩子
253
+ if (props.beforeUpload && typeof props.beforeUpload === 'function') {
254
+ // 执行回调,同时传入索引和文件列表当作参数
255
+ // 在微信,支付宝等环境(H5正常),会导致父组件定义的customBack()函数体中的this变成子组件的this
256
+ // 通过bind()方法,绑定父组件的this,让this.customBack()的this为父组件的上下文
257
+ // 因为upload组件可能会被嵌套在其他组件内,比如u-form,这时this.$parent其实为u-form的this,
258
+ // 非页面的this,所以这里需要往上历遍,一直寻找到最顶端的$parent,这里用了this.$u.$parent.call(this)
259
+ // 明白意思即可,无需纠结this.$u.$parent.call(this)的细节
260
+ let beforeResponse = props.beforeUpload(index, lists.value);
261
+ // 判断是否返回了promise
262
+ if (typeof beforeResponse === 'object' && beforeResponse !== null && typeof (beforeResponse as Promise<any>).then === 'function') {
263
+ await (beforeResponse as Promise<any>)
264
+ .then(() => {
265
+ // promise返回成功,不进行动作,继续上传
266
+ })
267
+ .catch(() => {
268
+ // 进入catch回调的话,继续下一张
269
+ return uploadFile(index + 1);
270
+ });
271
+ } else if (beforeResponse === false) {
272
+ // 如果返回false,继续下一张图片的上传
273
+ return uploadFile(index + 1);
274
+ } else {
275
+ // 此处为返回"true"的情形,这里不写代码,就跳过此处,继续执行当前的上传逻辑
276
+ }
277
+ }
278
+ // 检查上传地址
279
+ if (!props.action) {
280
+ showToast('请配置上传地址', true);
281
+ return;
282
+ }
283
+ lists.value[index].error = false;
284
+ uploading.value = true;
285
+ // 创建上传对象
286
+ const task = uni.uploadFile({
287
+ url: props.action,
288
+ filePath: lists.value[index].url,
289
+ name: props.name,
290
+ formData: props.formData,
291
+ header: props.header,
292
+ // #ifdef MP-ALIPAY
293
+ fileType: 'image',
294
+ // #endif
295
+ success: (res: any) => {
296
+ // 判断是否json字符串,将其转为json格式
297
+ let data = props.toJson && $u.test.jsonString(res.data) ? JSON.parse(res.data) : res.data;
298
+ if (![200, 201, 204].includes(res.statusCode)) {
299
+ uploadError(index, data);
300
+ } else {
301
+ // 上传成功
302
+ lists.value[index].response = data;
303
+ lists.value[index].progress = 100;
304
+ lists.value[index].error = false;
305
+ emit('on-success', data, index, lists.value, props.index);
306
+ }
307
+ },
308
+ fail: (e: any) => {
309
+ uploadError(index, e);
310
+ },
311
+ complete: (res: any) => {
312
+ uni.hideLoading();
313
+ uploading.value = false;
314
+ uploadFile(index + 1);
315
+ emit('on-change', res, index, lists.value, props.index);
316
+ }
317
+ });
318
+ task.onProgressUpdate((res: any) => {
319
+ if (res.progress > 0) {
320
+ lists.value[index].progress = res.progress;
321
+ emit('on-progress', res, index, lists.value, props.index);
322
+ }
323
+ });
324
+ }
325
+
326
+ /**
327
+ * 上传失败
328
+ */
329
+ function uploadError(index: number, err: any) {
330
+ lists.value[index].progress = 0;
331
+ lists.value[index].error = true;
332
+ lists.value[index].response = null;
333
+ emit('on-error', err, index, lists.value, props.index);
334
+ showToast('上传失败,请重试');
335
+ }
336
+
337
+ /**
338
+ * 删除一个图片
339
+ */
340
+ function deleteItem(index: number) {
341
+ uni.showModal({
342
+ title: '提示',
343
+ content: '您确定要删除此项吗?',
344
+ success: async (res: any) => {
345
+ if (res.confirm) {
346
+ // 先检查是否有定义before-remove移除前钩子
347
+ // 执行before-remove钩子
348
+ if (props.beforeRemove && typeof props.beforeRemove === 'function') {
349
+ // 此处钩子执行 原理同before-remove参数,见上方注释
350
+ let beforeResponse = props.beforeRemove(index, lists.value);
351
+ // 判断是否返回了promise
352
+ if (typeof beforeResponse === 'object' && beforeResponse !== null && typeof (beforeResponse as Promise<any>).then === 'function') {
353
+ await (beforeResponse as Promise<any>)
354
+ .then(() => {
355
+ // promise返回成功,不进行动作,继续上传
356
+ handlerDeleteItem(index);
357
+ })
358
+ .catch(() => {
359
+ // 如果进入promise的reject,终止删除操作
360
+ showToast('已终止移除');
361
+ });
362
+ } else if (beforeResponse === false) {
363
+ // 返回false,终止删除
364
+ showToast('已终止移除');
365
+ } else {
366
+ // 如果返回true,执行删除操作
367
+ handlerDeleteItem(index);
368
+ }
369
+ } else {
370
+ // 如果不存在before-remove钩子,
371
+ handlerDeleteItem(index);
372
+ }
373
+ }
374
+ }
375
+ });
376
+ }
377
+
378
+ /**
379
+ * 执行移除图片的动作
380
+ */
381
+ function handlerDeleteItem(index: number) {
382
+ // 如果文件正在上传中,终止上传任务,进度在0 < progress < 100则意味着正在上传
383
+ if (lists.value[index].progress < 100 && lists.value[index].progress > 0) {
384
+ typeof lists.value[index].uploadTask != 'undefined' && lists.value[index].uploadTask.abort();
385
+ }
386
+ lists.value.splice(index, 1);
387
+ emit('on-remove', index, lists.value, props.index);
388
+ showToast('移除成功');
389
+ }
390
+
391
+ /**
392
+ * 用户通过ref手动的形式,移除一张图片
393
+ */
394
+ function remove(index: number) {
395
+ // 判断索引的合法范围
396
+ if (index >= 0 && index < lists.value.length) {
397
+ lists.value.splice(index, 1);
398
+ emit('on-list-change', lists.value, props.index);
399
+ }
400
+ }
401
+
402
+ /**
403
+ * 预览图片
404
+ */
405
+ function doPreviewImage(url: string, index: number) {
406
+ // 判断是否允许预览
407
+ if (!props.previewFullImage) return;
408
+ // 获取所有图片的url
409
+ const images = lists.value.map(item => item.url || item.path);
410
+ uni.previewImage({
411
+ urls: images,
412
+ current: url,
413
+ success: () => {
414
+ emit('on-preview', url, lists.value, props.index);
415
+ },
416
+ fail: () => {
417
+ uni.showToast({ title: '预览图片失败', icon: 'none' });
418
+ }
419
+ });
420
+ }
421
+
422
+ /**
423
+ * 判断文件后缀是否允许
424
+ */
425
+ function checkFileExt(file: any) {
426
+ // 检查是否在允许的后缀中
427
+ let noArrowExt = false;
428
+ // 获取后缀名
429
+ let fileExt = '';
430
+ const reg = /.+\./;
431
+ // 如果是H5,需要从name中判断
432
+ // #ifdef H5
433
+ // H5环境下从name中获取后缀
434
+ fileExt = file.name.replace(reg, '').toLowerCase();
435
+ // #endif
436
+
437
+ // 非H5环境下从path中获取后缀
438
+ // #ifndef H5
439
+ fileExt = file.path.replace(reg, '').toLowerCase();
440
+ // #endif
441
+ // 使用数组的some方法,只要符合limitType中的一个,就返回true
442
+ noArrowExt = (props.limitType as string[]).some((ext: string) => {
443
+ // 转为小写
444
+ return ext.toLowerCase() === fileExt;
445
+ });
446
+ if (!noArrowExt) showToast(`不允许选择${fileExt}格式的文件`);
447
+ return noArrowExt;
448
+ }
449
+
450
+ defineExpose({ clear, reUpload, selectFile, upload, retry, remove, doPreviewImage, lists });
451
+ </script>
452
+
453
+ <style lang="scss" scoped>
454
+ @import '../../libs/css/style.components.scss';
455
+
456
+ .u-upload {
457
+ @include vue-flex;
458
+ flex-wrap: wrap;
459
+ align-items: center;
460
+ }
461
+
462
+ .u-list-item {
463
+ width: 200rpx;
464
+ height: 200rpx;
465
+ overflow: hidden;
466
+ margin: 10rpx;
467
+ background: rgb(244, 245, 246);
468
+ position: relative;
469
+ border-radius: 10rpx;
470
+ /* #ifndef APP-NVUE */
471
+ display: flex;
472
+ /* #endif */
473
+ align-items: center;
474
+ justify-content: center;
475
+ }
476
+
477
+ .u-preview-wrap {
478
+ border: 1px solid rgb(235, 236, 238);
479
+ }
480
+
481
+ .u-add-wrap {
482
+ flex-direction: column;
483
+ color: $u-content-color;
484
+ font-size: 26rpx;
485
+ }
486
+
487
+ .u-add-tips {
488
+ margin-top: 20rpx;
489
+ line-height: 40rpx;
490
+ }
491
+
492
+ .u-add-wrap__hover {
493
+ background-color: rgb(235, 236, 238);
494
+ }
495
+
496
+ .u-preview-image {
497
+ display: block;
498
+ width: 100%;
499
+ height: 100%;
500
+ border-radius: 10rpx;
501
+ }
502
+
503
+ .u-delete-icon {
504
+ position: absolute;
505
+ top: 10rpx;
506
+ right: 10rpx;
507
+ z-index: 10;
508
+ background-color: $u-type-error;
509
+ border-radius: 100rpx;
510
+ width: 44rpx;
511
+ height: 44rpx;
512
+ @include vue-flex;
513
+ align-items: center;
514
+ justify-content: center;
515
+ }
516
+
517
+ .u-icon {
518
+ @include vue-flex;
519
+ align-items: center;
520
+ justify-content: center;
521
+ }
522
+
523
+ .u-progress {
524
+ position: absolute;
525
+ bottom: 10rpx;
526
+ left: 8rpx;
527
+ right: 8rpx;
528
+ z-index: 9;
529
+ width: auto;
530
+ }
531
+
532
+ .u-error-btn {
533
+ color: #ffffff;
534
+ background-color: $u-type-error;
535
+ font-size: 20rpx;
536
+ padding: 4px 0;
537
+ text-align: center;
538
+ position: absolute;
539
+ bottom: 0;
540
+ left: 0;
541
+ right: 0;
542
+ z-index: 9;
543
+ line-height: 1;
544
+ }
545
+ </style>