uni-events-helper-wx
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.

536 lines
16 KiB

3 years ago
  1. <template>
  2. <view class="u-upload" :style="[$u.addStyle(customStyle)]">
  3. <view class="u-upload__wrap" v-if="previewImage">
  4. <view
  5. class="u-upload__wrap__preview"
  6. v-for="(item, index) in lists"
  7. :key="index"
  8. >
  9. <image
  10. v-if="item.isImage || (item.type && item.type === 'image')"
  11. :src="item.thumb || item.url"
  12. :mode="imageMode"
  13. class="u-upload__wrap__preview__image"
  14. @tap="onPreviewImage(item)"
  15. :style="{
  16. width: $u.addUnit(width),
  17. height: $u.addUnit(height)
  18. }"
  19. />
  20. <view
  21. v-else
  22. class="u-upload__wrap__preview__other"
  23. >
  24. <u-icon
  25. color="#80CBF9"
  26. size="26"
  27. :name="item.isVideo || (item.type && item.type === 'video') ? 'movie' : 'folder'"
  28. ></u-icon>
  29. <text class="u-upload__wrap__preview__other__text">{{item.isVideo || (item.type && item.type === 'video') ? '视频' : '文件'}}</text>
  30. </view>
  31. <view
  32. class="u-upload__status"
  33. v-if="item.status === 'uploading' || item.status === 'failed'"
  34. >
  35. <view class="u-upload__status__icon">
  36. <u-icon
  37. v-if="item.status === 'failed'"
  38. name="close-circle"
  39. color="#ffffff"
  40. size="25"
  41. />
  42. <u-loading-icon
  43. size="22"
  44. mode="circle"
  45. color="#ffffff"
  46. v-else
  47. />
  48. </view>
  49. <text
  50. v-if="item.message"
  51. class="u-upload__status__message"
  52. >{{ item.message }}</text>
  53. </view>
  54. <view
  55. class="u-upload__deletable"
  56. v-if="item.status !== 'uploading' && (deletable || item.deletable)"
  57. @tap.stop="deleteItem(index)"
  58. >
  59. <view class="u-upload__deletable__icon">
  60. <u-icon
  61. name="close"
  62. color="#ffffff"
  63. size="10"
  64. ></u-icon>
  65. </view>
  66. </view>
  67. <view
  68. class="u-upload__success"
  69. v-if="item.status === 'success'"
  70. >
  71. <!-- #ifdef APP-NVUE -->
  72. <image
  73. :src="successIcon"
  74. class="u-upload__success__icon"
  75. ></image>
  76. <!-- #endif -->
  77. <!-- #ifndef APP-NVUE -->
  78. <view class="u-upload__success__icon">
  79. <u-icon
  80. name="checkmark"
  81. color="#ffffff"
  82. size="12"
  83. ></u-icon>
  84. </view>
  85. <!-- #endif -->
  86. </view>
  87. </view>
  88. </view>
  89. <template v-if="isInCount">
  90. <view
  91. v-if="$slots.default || $slots.$default"
  92. @tap="chooseFile"
  93. >
  94. <slot />
  95. </view>
  96. <view
  97. v-else
  98. class="u-upload__button"
  99. :hover-class="!disabled && 'u-upload__button--hover'"
  100. hover-stay-time="150"
  101. @tap="chooseFile"
  102. :class="[disabled && 'u-upload__button--disabled']"
  103. >
  104. <u-icon
  105. :name="uploadIcon"
  106. size="26"
  107. color="#D3D4D6"
  108. ></u-icon>
  109. <text
  110. v-if="uploadText"
  111. class="u-upload__button__text"
  112. >{{ uploadText }}</text>
  113. </view>
  114. </template>
  115. </view>
  116. </template>
  117. <script>
  118. import {
  119. chooseFile
  120. } from './utils';
  121. import mixin from './mixin.js';
  122. import props from './props.js';
  123. /**
  124. * upload 上传
  125. * @description 该组件用于上传图片场景
  126. * @tutorial https://uviewui.com/components/upload.html
  127. * @property {String} accept 接受的文件类型, 可选值为all media image file video 默认 'image'
  128. * @property {String | Array} capture 图片或视频拾取模式当accept为image类型时设置capture可选额外camera可以直接调起摄像头默认 ['album', 'camera']
  129. * @property {Boolean} compressed 当accept为video时生效是否压缩视频默认为true默认 true
  130. * @property {String} camera 当accept为video时生效可选值为back或front默认 'back'
  131. * @property {Number} maxDuration 当accept为video时生效拍摄视频最长拍摄时间单位秒默认 60
  132. * @property {String} uploadIcon 上传区域的图标只能内置图标默认 'camera-fill'
  133. * @property {Boolean} useBeforeRead 是否开启文件读取前事件默认 false
  134. * @property {Boolean} previewFullImage 是否显示组件自带的图片预览功能默认 true
  135. * @property {String | Number} maxCount 最大上传数量默认 52
  136. * @property {Boolean} disabled 是否启用默认 false
  137. * @property {String} imageMode 预览上传的图片时的裁剪模式和image组件mode属性一致默认 'aspectFill'
  138. * @property {String} name 标识符可以在回调函数的第二项参数中获取
  139. * @property {Array} sizeType 所选的图片的尺寸, 可选值为original compressed默认 ['original', 'compressed']
  140. * @property {Boolean} multiple 是否开启图片多选部分安卓机型不支持 默认 false
  141. * @property {Boolean} deletable 是否展示删除按钮默认 true
  142. * @property {String | Number} maxSize 文件大小限制单位为byte 默认 Number.MAX_VALUE
  143. * @property {Array} fileList 显示已上传的文件列表
  144. * @property {String} uploadText 上传区域的提示文字
  145. * @property {String | Number} width 内部预览图片区域和选择图片按钮的区域宽度默认 80
  146. * @property {String | Number} height 内部预览图片区域和选择图片按钮的区域高度默认 80
  147. * @property {Object} customStyle 组件的样式对象形式
  148. * @event {Function} afterRead 读取后的处理函数
  149. * @event {Function} beforeRead 读取前的处理函数
  150. * @event {Function} oversize 文件超出大小限制
  151. * @event {Function} clickPreview 点击预览图片
  152. * @event {Function} delete 删除图片
  153. * @example <u-upload :action="action" :fileList="fileList" ></u-upload>
  154. */
  155. export default {
  156. name: "u-upload",
  157. mixins: [uni.$u.mpMixin, uni.$u.mixin, mixin,props],
  158. data() {
  159. return {
  160. // #ifdef APP-NVUE
  161. successIcon: 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACgAAAAoCAYAAACM/rhtAAAAAXNSR0IArs4c6QAAAERlWElmTU0AKgAAAAgAAYdpAAQAAAABAAAAGgAAAAAAA6ABAAMAAAABAAEAAKACAAQAAAABAAAAKKADAAQAAAABAAAAKAAAAAB65masAAACP0lEQVRYCc3YXygsURwH8K/dpcWyG3LF5u/6/+dKVylSypuUl6uUPMifKMWL8oKEB1EUT1KeUPdR3uTNUsSLxb2udG/cbvInNuvf2rVnazZ/ZndmZ87snjM1Z+Z3zpzfp9+Z5mEAhlvjRtZgCKs+gnPAOcAkkMOR4jEHfItjDvgRxxSQD8cM0BuOCaAvXNCBQrigAsXgggYUiwsK0B9cwIH+4gIKlIILGFAqLiBAOTjFgXJxigJp4BQD0sIpAqSJow6kjSNAFTnRaHJwLenD6Mud52VQAcrBfTd2oyq+HtGaGGWAcnAVcXWoM3bCZrdi+ncPfaAcXE5UKVpdW/vitGPqqAtn98d0gXJwX7Qp6MmegUYVhvmTIezdmHlxJCjpHRTCFerLkRRu4k0aqdajN3sWOo0BK//msHa+xDuPC/oNFMKRhTtM4xjIX0SCNpXL4+7VIaHuyiWEp2L7ahWLf8fejfPdqPmC3mJicORZUp1CQzm+GiphvljGk+PBvWRbxii+xVTj5M6CiZ/tsDufvaXyxEUDxeLIyvu3m0iOyEFWVAkydcVYdyFrE9tQk9iMq6f/GNlvwt3LjQfh60LUrw9/cFyyMJUW/XkLSNMV4Mi6C5ML+ui4x5ClAX9sB9w0wV6wglJwJCv5fOxcr6EstgbGiEw4XcfUry4cWrcEUW8n+ARKxXEJHhw2WG43UKSvwI/TSZgvl7kh0b3XLZaLEy0QmMgLZAVH7J+ALOE+AVnDvQOyiPMAWcW5gSzjCPAV+78S5WE0GrQAAAAASUVORK5CYII=',
  162. // #endif
  163. lists: [],
  164. isInCount: true,
  165. }
  166. },
  167. watch: {
  168. // 监听文件列表的变化,重新整理内部数据
  169. fileList: {
  170. immediate: true,
  171. handler() {
  172. this.formatFileList()
  173. }
  174. },
  175. },
  176. methods: {
  177. formatFileList() {
  178. const {
  179. fileList = [], maxCount
  180. } = this;
  181. const lists = fileList.map((item) =>
  182. Object.assign(Object.assign({}, item), {
  183. isImage: uni.$u.test.image(item.url),
  184. isVideo: uni.$u.test.video(item.url),
  185. deletable: typeof(item.deletable) === 'boolean' ? item.deletable : true,
  186. })
  187. );
  188. this.lists = lists
  189. this.isInCount = lists.length < maxCount
  190. },
  191. chooseFile() {
  192. const {
  193. maxCount,
  194. multiple,
  195. lists,
  196. disabled
  197. } = this;
  198. if (disabled) return;
  199. chooseFile(
  200. Object.assign({
  201. accept: this.accept,
  202. multiple: this.multiple,
  203. capture: this.capture,
  204. compressed: this.compressed,
  205. maxDuration: this.maxDuration,
  206. sizeType: this.sizeType,
  207. camera: this.camera,
  208. }, {
  209. maxCount: maxCount - lists.length,
  210. })
  211. )
  212. .then((res) => {
  213. this.onBeforeRead(multiple ? res : res[0]);
  214. })
  215. .catch((error) => {
  216. this.$emit('error', error);
  217. });
  218. },
  219. // 文件读取之前
  220. onBeforeRead(file) {
  221. const {
  222. beforeRead,
  223. useBeforeRead,
  224. } = this;
  225. let res = true
  226. // beforeRead是否为一个方法
  227. if (uni.$u.test.func(beforeRead)) {
  228. // 如果用户定义了此方法,则去执行此方法,并传入读取的文件回调
  229. res = beforeRead(file, this.getDetail());
  230. }
  231. if (useBeforeRead) {
  232. res = new Promise((resolve, reject) => {
  233. this.$emit(
  234. 'beforeRead',
  235. Object.assign(Object.assign({
  236. file
  237. }, this.getDetail()), {
  238. callback: (ok) => {
  239. ok ? resolve() : reject();
  240. },
  241. })
  242. );
  243. });
  244. }
  245. if (!res) {
  246. return;
  247. }
  248. if (uni.$u.test.promise(res)) {
  249. res.then((data) => this.onAfterRead(data || file));
  250. } else {
  251. this.onAfterRead(file);
  252. }
  253. },
  254. getDetail(index) {
  255. return {
  256. name: this.name,
  257. index: index == null ? this.fileList.length : index,
  258. };
  259. },
  260. onAfterRead(file) {
  261. const {
  262. maxSize,
  263. afterRead
  264. } = this;
  265. const oversize = Array.isArray(file) ?
  266. file.some((item) => item.size > maxSize) :
  267. file.size > maxSize;
  268. if (oversize) {
  269. this.$emit('oversize', Object.assign({
  270. file
  271. }, this.getDetail()));
  272. return;
  273. }
  274. if (typeof afterRead === 'function') {
  275. afterRead(file, this.getDetail());
  276. }
  277. this.$emit('afterRead', Object.assign({
  278. file
  279. }, this.getDetail()));
  280. },
  281. deleteItem(index) {
  282. this.$emit(
  283. 'delete',
  284. Object.assign(Object.assign({}, this.getDetail(index)), {
  285. file: this.fileList[index],
  286. })
  287. );
  288. },
  289. // 预览图片
  290. onPreviewImage(item) {
  291. if (!item.isImage || !this.previewFullImage) return
  292. uni.previewImage({
  293. // 先filter找出为图片的item,再返回filter结果中的图片url
  294. urls: this.lists.filter((item) => uni.$u.test.image(item.url)).map((item) => item.url),
  295. current: item.url,
  296. fail() {
  297. uni.$u.toast('预览图片失败')
  298. },
  299. });
  300. },
  301. onPreviewVideo(event) {
  302. if (!this.data.previewFullImage) return;
  303. const {
  304. index
  305. } = event.currentTarget.dataset;
  306. const {
  307. lists
  308. } = this.data;
  309. wx.previewMedia({
  310. sources: lists
  311. .filter((item) => isVideoFile(item))
  312. .map((item) =>
  313. Object.assign(Object.assign({}, item), {
  314. type: 'video'
  315. })
  316. ),
  317. current: index,
  318. fail() {
  319. wx.showToast({
  320. title: '预览视频失败',
  321. icon: 'none'
  322. });
  323. },
  324. });
  325. },
  326. onClickPreview(event) {
  327. const {
  328. index
  329. } = event.currentTarget.dataset;
  330. const item = this.data.lists[index];
  331. this.$emit(
  332. 'click-preview',
  333. Object.assign(Object.assign({}, item), this.getDetail(index))
  334. );
  335. }
  336. }
  337. }
  338. </script>
  339. <style lang="scss">
  340. @import '../../libs/css/components.scss';
  341. $u-upload-preview-border-radius: 2px !default;
  342. $u-upload-preview-margin: 0 8px 8px 0 !default;
  343. $u-upload-image-width:80px !default;
  344. $u-upload-image-height:$u-upload-image-width;
  345. $u-upload-other-bgColor: rgb(242, 242, 242) !default;
  346. $u-upload-other-flex:1 !default;
  347. $u-upload-text-font-size:11px !default;
  348. $u-upload-text-color:$u-tips-color !default;
  349. $u-upload-text-margin-top:2px !default;
  350. $u-upload-deletable-right:0 !default;
  351. $u-upload-deletable-top:0 !default;
  352. $u-upload-deletable-bgColor:rgb(55, 55, 55) !default;
  353. $u-upload-deletable-height:14px !default;
  354. $u-upload-deletable-width:$u-upload-deletable-height;
  355. $u-upload-deletable-boder-bottom-left-radius:100px !default;
  356. $u-upload-deletable-zIndex:3 !default;
  357. $u-upload-success-bottom:0 !default;
  358. $u-upload-success-right:0 !default;
  359. $u-upload-success-border-top-color:transparent !default;
  360. $u-upload-success-border-left-color:transparent !default;
  361. $u-upload-success-border-bottom-color: $u-success !default;
  362. $u-upload-success-border-right-color:$u-upload-success-border-bottom-color;
  363. $u-upload-success-border-width:9px !default;
  364. $u-upload-icon-top:0px !default;
  365. $u-upload-icon-right:0px !default;
  366. $u-upload-icon-h5-top:1px !default;
  367. $u-upload-icon-h5-right:0 !default;
  368. $u-upload-icon-width:16px !default;
  369. $u-upload-icon-height:$u-upload-icon-width;
  370. $u-upload-success-icon-bottom:-10px !default;
  371. $u-upload-success-icon-right:-10px !default;
  372. $u-upload-status-right:0 !default;
  373. $u-upload-status-left:0 !default;
  374. $u-upload-status-bottom:0 !default;
  375. $u-upload-status-top:0 !default;
  376. $u-upload-status-bgColor:rgba(0, 0, 0, 0.5) !default;
  377. $u-upload-status-icon-Zindex:1 !default;
  378. $u-upload-message-font-size:12px !default;
  379. $u-upload-message-color:#FFFFFF !default;
  380. $u-upload-message-margin-top:5px !default;
  381. $u-upload-button-width:80px !default;
  382. $u-upload-button-height:$u-upload-button-width;
  383. $u-upload-button-bgColor:rgb(244, 245, 247) !default;
  384. $u-upload-button-border-radius:2px !default;
  385. $u-upload-botton-margin: 0 8px 8px 0 !default;
  386. $u-upload-text-font-size:11px !default;
  387. $u-upload-text-color:$u-tips-color !default;
  388. $u-upload-text-margin-top: 2px !default;
  389. $u-upload-hover-bgColor:rgb(240, 241, 243) !default;
  390. $u-upload-disabled-opacity:.5 !default;
  391. .u-upload {
  392. @include flex;
  393. &__wrap {
  394. @include flex;
  395. &__preview {
  396. border-radius: $u-upload-preview-border-radius;
  397. margin: $u-upload-preview-margin;
  398. position: relative;
  399. overflow: hidden;
  400. @include flex;
  401. &__image {
  402. width: $u-upload-image-width;
  403. height: $u-upload-image-height;
  404. }
  405. &__other {
  406. background-color: $u-upload-other-bgColor;
  407. flex: $u-upload-other-flex;
  408. @include flex(column);
  409. justify-content: center;
  410. align-items: center;
  411. &__text {
  412. font-size: $u-upload-text-font-size;
  413. color: $u-upload-text-color;
  414. margin-top: $u-upload-text-margin-top;
  415. }
  416. }
  417. }
  418. }
  419. &__deletable {
  420. position: absolute;
  421. top: $u-upload-deletable-top;
  422. right: $u-upload-deletable-right;
  423. background-color: $u-upload-deletable-bgColor;
  424. height: $u-upload-deletable-height;
  425. width: $u-upload-deletable-width;
  426. @include flex;
  427. border-bottom-left-radius: $u-upload-deletable-boder-bottom-left-radius;
  428. align-items: center;
  429. justify-content: center;
  430. z-index: $u-upload-deletable-zIndex;
  431. &__icon {
  432. position: absolute;
  433. transform: scale(0.7);
  434. top: $u-upload-icon-top;
  435. right: $u-upload-icon-right;
  436. /* #ifdef H5 */
  437. top: $u-upload-icon-h5-top;
  438. right: $u-upload-icon-h5-right;
  439. /* #endif */
  440. }
  441. }
  442. &__success {
  443. position: absolute;
  444. bottom: $u-upload-success-bottom;
  445. right: $u-upload-success-right;
  446. @include flex;
  447. // 由于weex(nvue)为阿里巴巴的KPI(部门业绩考核)的laji产物,不支持css绘制三角形
  448. // 所以在nvue下使用图片,非nvue下使用css实现
  449. /* #ifndef APP-NVUE */
  450. border-top-color: $u-upload-success-border-top-color;
  451. border-left-color: $u-upload-success-border-left-color;
  452. border-bottom-color: $u-upload-success-border-bottom-color;
  453. border-right-color: $u-upload-success-border-right-color;
  454. border-width: $u-upload-success-border-width;
  455. align-items: center;
  456. justify-content: center;
  457. /* #endif */
  458. &__icon {
  459. /* #ifndef APP-NVUE */
  460. position: absolute;
  461. transform: scale(0.7);
  462. bottom: $u-upload-success-icon-bottom;
  463. right: $u-upload-success-icon-right;
  464. /* #endif */
  465. /* #ifdef APP-NVUE */
  466. width: $u-upload-icon-width;
  467. height: $u-upload-icon-height;
  468. /* #endif */
  469. }
  470. }
  471. &__status {
  472. position: absolute;
  473. top: $u-upload-status-top;
  474. bottom: $u-upload-status-bottom;
  475. left: $u-upload-status-left;
  476. right: $u-upload-status-right;
  477. background-color: $u-upload-status-bgColor;
  478. @include flex(column);
  479. align-items: center;
  480. justify-content: center;
  481. &__icon {
  482. position: relative;
  483. z-index: $u-upload-status-icon-Zindex;
  484. }
  485. &__message {
  486. font-size: $u-upload-message-font-size;
  487. color: $u-upload-message-color;
  488. margin-top: $u-upload-message-margin-top;
  489. }
  490. }
  491. &__button {
  492. @include flex(column);
  493. align-items: center;
  494. justify-content: center;
  495. width: $u-upload-button-width;
  496. height: $u-upload-button-height;
  497. background-color: $u-upload-button-bgColor;
  498. border-radius: $u-upload-button-border-radius;
  499. margin: $u-upload-botton-margin;
  500. /* #ifndef APP-NVUE */
  501. box-sizing: border-box;
  502. /* #endif */
  503. &__text {
  504. font-size: $u-upload-text-font-size;
  505. color: $u-upload-text-color;
  506. margin-top: $u-upload-text-margin-top;
  507. }
  508. &--hover {
  509. background-color: $u-upload-hover-bgColor;
  510. }
  511. &--disabled {
  512. opacity: $u-upload-disabled-opacity;
  513. }
  514. }
  515. }
  516. </style>