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