u-upload.vue 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884
  1. <template>
  2. <view class="u-upload" :style="[addStyle(customStyle)]">
  3. <view class="u-upload__wrap" >
  4. <template v-if="previewImage">
  5. <view
  6. class="u-upload__wrap__preview"
  7. v-for="(item, index) in lists"
  8. :key="index"
  9. >
  10. <image
  11. v-if="item.isImage || (item.type && item.type === 'image')"
  12. :src="item.thumb || item.url"
  13. :mode="imageMode"
  14. class="u-upload__wrap__preview__image"
  15. @tap="onPreviewImage(item, index)"
  16. :style="[{
  17. width: addUnit(width),
  18. height: addUnit(height)
  19. }]"
  20. />
  21. <view class="u-upload__wrap__preview__video"
  22. :style="{
  23. width: addUnit(width),
  24. height: addUnit(height)
  25. }"
  26. v-else-if="(item.isVideo || (item.type && item.type === 'video')) && getVideoThumb">
  27. <image
  28. v-if="item.thumb"
  29. :src="item.thumb"
  30. :mode="imageMode"
  31. class="u-upload__wrap__preview__image"
  32. @tap="onPreviewVideo(item, index)"
  33. :style="[{
  34. width: addUnit(width),
  35. height: addUnit(height)
  36. }]"
  37. />
  38. <u-icon
  39. v-else
  40. color="#80CBF9"
  41. size="26"
  42. :name="item.isVideo || (item.type && item.type === 'video') ? 'movie' : 'folder'"
  43. ></u-icon>
  44. <view v-if="item.status === 'success'"
  45. class="u-upload__wrap__play"
  46. @tap="onPreviewVideo(item, index)">
  47. <slot name="playIcon"></slot>
  48. <up-icon v-if="!$slots['playIcon']"
  49. class="u-upload__wrap__play__icon"
  50. name="play-right" size="22px"></up-icon>
  51. </view>
  52. </view>
  53. <view
  54. v-else
  55. class="u-upload__wrap__preview__other"
  56. @tap="onClickPreview(item, index)"
  57. >
  58. <u-icon
  59. color="#80CBF9"
  60. size="26"
  61. :name="item.isVideo || (item.type && item.type === 'video') ? 'movie' : 'folder'"
  62. ></u-icon>
  63. <text class="u-upload__wrap__preview__other__text">
  64. {{item.isVideo || (item.type && item.type === 'video') ? '视频' : '文件'}}
  65. </text>
  66. </view>
  67. <view
  68. class="u-upload__status"
  69. v-if="item.status === 'uploading' || item.status === 'failed'"
  70. >
  71. <view class="u-upload__status__icon">
  72. <u-icon
  73. v-if="item.status === 'failed'"
  74. name="close-circle"
  75. color="#ffffff"
  76. size="25"
  77. />
  78. <u-loading-icon
  79. size="22"
  80. mode="circle"
  81. color="#ffffff"
  82. v-else
  83. />
  84. </view>
  85. <text
  86. v-if="item.message"
  87. class="u-upload__status__message"
  88. >{{ item.message }}</text>
  89. <up-gap class="u-upload__progress" height="3px"
  90. :style="{width: item.progress + '%'}"></up-gap>
  91. </view>
  92. <view
  93. class="u-upload__deletable"
  94. v-if="item.status !== 'uploading' && (deletable || item.deletable)"
  95. @tap.stop="deleteItem(index)"
  96. >
  97. <view class="u-upload__deletable__icon">
  98. <u-icon
  99. name="close"
  100. color="#ffffff"
  101. size="10"
  102. ></u-icon>
  103. </view>
  104. </view>
  105. <slot name="success">
  106. <view
  107. class="u-upload__success"
  108. v-if="item.status === 'success'"
  109. >
  110. <!-- #ifdef APP-NVUE -->
  111. <image
  112. :src="successIcon"
  113. class="u-upload__success__icon"
  114. ></image>
  115. <!-- #endif -->
  116. <!-- #ifndef APP-NVUE -->
  117. <view class="u-upload__success__icon">
  118. <u-icon
  119. name="checkmark"
  120. color="#ffffff"
  121. size="12"
  122. ></u-icon>
  123. </view>
  124. <!-- #endif -->
  125. </view>
  126. </slot>
  127. </view>
  128. </template>
  129. <canvas id="myCanvas" type="2d"
  130. style="width: 100px; height: 150px;display: none;"></canvas>
  131. <template v-if="isInCount">
  132. <view
  133. v-if="$slots.trigger"
  134. @tap="chooseFile"
  135. >
  136. <slot name="trigger" />
  137. </view>
  138. <view
  139. v-else-if="!$slots.trigger && ($slots.default || $slots.$default)"
  140. @tap="chooseFile"
  141. >
  142. <slot />
  143. </view>
  144. <view
  145. v-else
  146. class="u-upload__button"
  147. :hover-class="!disabled ? 'u-upload__button--hover' : ''"
  148. hover-stay-time="150"
  149. @tap="chooseFile"
  150. :class="[disabled && 'u-upload__button--disabled']"
  151. :style="[{
  152. width: addUnit(width),
  153. height: addUnit(height)
  154. }]"
  155. >
  156. <u-icon
  157. :name="uploadIcon"
  158. size="26"
  159. :color="uploadIconColor"
  160. ></u-icon>
  161. <text
  162. v-if="uploadText"
  163. class="u-upload__button__text"
  164. >{{ uploadText }}</text>
  165. </view>
  166. </template>
  167. </view>
  168. <up-popup
  169. mode="center"
  170. v-model:show="popupShow">
  171. <video id="myVideo"
  172. :src="currentItemIndex >= 0 ? lists[currentItemIndex].url : ''"
  173. @error="videoErrorCallback" show-center-play-btn
  174. object-fit='cover' show-fullscreen-btn='true'
  175. enable-play-gesture controls
  176. :autoplay="true" auto-pause-if-open-native
  177. @loadedmetadata="loadedVideoMetadata"
  178. :initial-time='0.1'>
  179. </video>
  180. </up-popup>
  181. </view>
  182. </template>
  183. <script>
  184. import {
  185. chooseFile
  186. } from './utils';
  187. import { mixinUpload } from './mixin';
  188. import { props } from './props';
  189. import { mpMixin } from '../../libs/mixin/mpMixin';
  190. import { mixin } from '../../libs/mixin/mixin';
  191. import { addStyle, addUnit, toast } from '../../libs/function/index';
  192. import test from '../../libs/function/test';
  193. /**
  194. * upload 上传
  195. * @description 该组件用于上传图片场景
  196. * @tutorial https://uview-plus.jiangruyi.com/components/upload.html
  197. * @property {String} accept 接受的文件类型, 可选值为all media image file video (默认 'image' )
  198. * @property {String | Array} capture 图片或视频拾取模式,当accept为image类型时设置capture可选额外camera可以直接调起摄像头(默认 ['album', 'camera'] )
  199. * @property {Boolean} compressed 当accept为video时生效,是否压缩视频,默认为true(默认 true )
  200. * @property {String} camera 当accept为video时生效,可选值为back或front(默认 'back' )
  201. * @property {Number} maxDuration 当accept为video时生效,拍摄视频最长拍摄时间,单位秒(默认 60 )
  202. * @property {String} uploadIcon 上传区域的图标,只能内置图标(默认 'camera-fill' )
  203. * @property {String} uploadIconColor 上传区域的图标的字体颜色,只能内置图标(默认 #D3D4D6 )
  204. * @property {Boolean} useBeforeRead 是否开启文件读取前事件(默认 false )
  205. * @property {Boolean} previewFullImage 是否显示组件自带的图片预览功能(默认 true )
  206. * @property {String | Number} maxCount 最大上传数量(默认 52 )
  207. * @property {Boolean} disabled 是否启用(默认 false )
  208. * @property {String} imageMode 预览上传的图片时的裁剪模式,和image组件mode属性一致(默认 'aspectFill' )
  209. * @property {String} name 标识符,可以在回调函数的第二项参数中获取
  210. * @property {Array} sizeType 所选的图片的尺寸, 可选值为original compressed(默认 ['original', 'compressed'] )
  211. * @property {Boolean} multiple 是否开启图片多选,部分安卓机型不支持 (默认 false )
  212. * @property {Boolean} deletable 是否展示删除按钮(默认 true )
  213. * @property {String | Number} maxSize 文件大小限制,单位为byte (默认 Number.MAX_VALUE )
  214. * @property {Array} fileList 显示已上传的文件列表
  215. * @property {String} uploadText 上传区域的提示文字
  216. * @property {String | Number} width 内部预览图片区域和选择图片按钮的区域宽度(默认 80 )
  217. * @property {String | Number} height 内部预览图片区域和选择图片按钮的区域高度(默认 80 )
  218. * @property {Object} customStyle 组件的样式,对象形式
  219. * @event {Function} afterRead 读取后的处理函数
  220. * @event {Function} beforeRead 读取前的处理函数
  221. * @event {Function} oversize 文件超出大小限制
  222. * @event {Function} clickPreview 点击预览图片
  223. * @event {Function} delete 删除图片
  224. * @example <u-upload :action="action" :fileList="fileList" ></u-upload>
  225. */
  226. export default {
  227. name: "u-upload",
  228. mixins: [mpMixin, mixin, mixinUpload, props],
  229. data() {
  230. return {
  231. // #ifdef APP-NVUE
  232. 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=',
  233. // #endif
  234. lists: [],
  235. isInCount: true,
  236. popupShow: false,
  237. currentItemIndex: -1
  238. }
  239. },
  240. watch: {
  241. // 监听文件列表的变化,重新整理内部数据
  242. fileList: {
  243. handler() {
  244. this.formatFileList()
  245. },
  246. immediate: true,
  247. deep: true,
  248. },
  249. deletable(newVal) {
  250. this.formatFileList()
  251. },
  252. maxCount(newVal) {
  253. this.formatFileList()
  254. },
  255. accept(newVal) {
  256. this.formatFileList()
  257. },
  258. popupShow(newVal) {
  259. if (!newVal) {
  260. this.currentItemIndex = -1;
  261. }
  262. }
  263. },
  264. // #ifdef VUE3
  265. emits: ['error', 'beforeRead', 'oversize', 'afterRead', 'delete', 'clickPreview', 'update:fileList'],
  266. // #endif
  267. methods: {
  268. addUnit,
  269. addStyle,
  270. videoErrorCallback() {},
  271. loadedVideoMetadata(e) {
  272. if (this.currentItemIndex < 0) {
  273. return;
  274. }
  275. if (this.autoUploadDriver != 'local') {
  276. return;
  277. }
  278. if (!this.getVideoThumb) {
  279. return;
  280. }
  281. // 截取第一帧作为封面,oss等云存储场景直接使用拼接参数。
  282. let w = this.lists[this.currentItemIndex].width;
  283. let h = this.lists[this.currentItemIndex].height;
  284. const dpr = uni.getSystemInfoSync().pixelRatio;
  285. uni.createSelectorQuery().select('#myVideo').context(res => {
  286. console.log('select video', res)
  287. const myVideo = res.context
  288. uni.createSelectorQuery()
  289. .select('#myCanvas')
  290. .fields({ node: true, size: true })
  291. .exec(([res]) => {
  292. console.log('select canvas', res)
  293. const ctx1 = res[0].node.getContext('2d')
  294. res[0].node.width = w * dpr
  295. res[0].node.height = h * dpr
  296. // Draw the first frame and export it as an image
  297. // myVideo.onPlay(() => {
  298. setTimeout(() => {
  299. captureFirstFrame()
  300. }, 500)
  301. // })
  302. const captureFirstFrame = () => {
  303. ctx1.drawImage(myVideo, 0, 0, w * dpr, h * dpr)
  304. wx.canvasToTempFilePath({
  305. canvas: res[0].node,
  306. success: (result) => {
  307. console.log('First frame image path:', result
  308. .tempFilePath)
  309. // Now you can use the image path (result.tempFilePath)
  310. this.fileList['currentItemIndex'].thumb = result.tempFilePath
  311. },
  312. fail: (err) => {
  313. console.error('Failed to export image:', err)
  314. }
  315. })
  316. }
  317. // Capture the first frame
  318. setInterval(() => {
  319. ctx1.drawImage(myVideo, 0, 0, w * dpr, h * dpr);
  320. }, 1000 / 24)
  321. }).exec()
  322. }).exec()
  323. },
  324. formatFileList() {
  325. const {
  326. fileList = [], maxCount
  327. } = this;
  328. const lists = fileList.map((item) =>
  329. Object.assign(Object.assign({}, item), {
  330. // 如果item.url为本地选择的blob文件的话,无法判断其为video还是image,此处优先通过accept做判断处理
  331. isImage: this.accept === 'image' || test.image(item.url || item.thumb),
  332. isVideo: this.accept === 'video' || test.video(item.url || item.thumb),
  333. deletable: typeof(item.deletable) === 'boolean' ? item.deletable : this.deletable,
  334. })
  335. );
  336. this.lists = lists
  337. this.isInCount = lists.length < maxCount
  338. },
  339. chooseFile() {
  340. const {
  341. maxCount,
  342. multiple,
  343. lists,
  344. disabled
  345. } = this;
  346. if (disabled) return;
  347. // 如果用户传入的是字符串,需要格式化成数组
  348. let capture;
  349. try {
  350. capture = test.array(this.capture) ? this.capture : this.capture.split(',');
  351. }catch(e) {
  352. capture = [];
  353. }
  354. chooseFile(
  355. Object.assign({
  356. accept: this.accept,
  357. extension: this.extension,
  358. multiple: this.multiple,
  359. capture: capture,
  360. compressed: this.compressed,
  361. maxDuration: this.maxDuration,
  362. sizeType: this.sizeType,
  363. camera: this.camera,
  364. }, {
  365. maxCount: maxCount - lists.length,
  366. })
  367. )
  368. .then((res) => {
  369. this.onBeforeRead(multiple ? res : res[0]);
  370. })
  371. .catch((error) => {
  372. this.$emit('error', error);
  373. });
  374. },
  375. // 文件读取之前
  376. onBeforeRead(file) {
  377. const {
  378. beforeRead,
  379. useBeforeRead,
  380. } = this;
  381. let res = true
  382. // beforeRead是否为一个方法
  383. if (test.func(beforeRead)) {
  384. // 如果用户定义了此方法,则去执行此方法,并传入读取的文件回调
  385. res = beforeRead(file, this.getDetail());
  386. }
  387. if (useBeforeRead) {
  388. res = new Promise((resolve, reject) => {
  389. this.$emit(
  390. 'beforeRead',
  391. Object.assign(Object.assign({
  392. file
  393. }, this.getDetail()), {
  394. callback: (ok) => {
  395. ok ? resolve() : reject();
  396. },
  397. })
  398. );
  399. });
  400. }
  401. if (!res) {
  402. return;
  403. }
  404. if (test.promise(res)) {
  405. res.then((data) => this.onAfterRead(data || file));
  406. } else {
  407. this.onAfterRead(file);
  408. }
  409. },
  410. getDetail(index) {
  411. return {
  412. name: this.name,
  413. index: index == null ? this.fileList.length : index,
  414. };
  415. },
  416. async onAfterRead(file) {
  417. const {
  418. maxSize,
  419. afterRead
  420. } = this;
  421. const oversize = Array.isArray(file) ?
  422. file.some((item) => item.size > maxSize) :
  423. file.size > maxSize;
  424. if (oversize) {
  425. uni.showToast({
  426. title: '超过大小限制'
  427. })
  428. this.$emit('oversize', Object.assign({
  429. file
  430. }, this.getDetail()));
  431. return;
  432. }
  433. let len = this.fileList.length;
  434. if (this.autoUpload) {
  435. // 当设置 mutiple 为 true 时, file 为数组格式,否则为对象格式
  436. let lists = [].concat(file);
  437. let fileListLen = this.fileList.length;
  438. lists.map((item) => {
  439. this.fileList.push({
  440. ...item,
  441. status: 'uploading',
  442. message: '上传中',
  443. progress: 0
  444. });
  445. });
  446. let that = this;
  447. this.$emit('update:fileList', this.fileList);
  448. for (let i = 0; i < lists.length; i++) {
  449. let j = i;
  450. let result = '';
  451. switch(this.autoUploadDriver) {
  452. case 'cos': // 腾讯云
  453. break;
  454. case 'kodo': // 七牛云
  455. break;
  456. case 'oss':
  457. case 'upload_oss':
  458. // 阿里云前端直传
  459. // 获取签名
  460. console.log()
  461. let formData = {};
  462. let ret = await uni.request({
  463. url: this.autoUploadAuthUrl,
  464. method: 'get',
  465. header: this.autoUploadHeader,
  466. data: {
  467. filename: lists[j].name
  468. }
  469. });
  470. // console.log(ret);
  471. let res0 = ret.data;
  472. if (res0.code == 200) {
  473. // 路径 + 文件名 + 扩展名
  474. // 不传递filename就要拼接key
  475. // res0.data.params.key = res0.data.params.dir + res0.data.params.uniqidName + fileExt;
  476. formData = res0.data.params;
  477. } else {
  478. uni.showToast({
  479. title: res0.msg,
  480. duration: 1500
  481. });
  482. return;
  483. }
  484. var uploadTask = uni.uploadFile({
  485. url: res0.data.params.host,
  486. filePath: lists[j].url,
  487. name: 'file',
  488. // fileType: 'video', // 仅支付宝小程序,且必填。
  489. // header: header,
  490. formData: formData,
  491. success: (uploadFileRes) => {
  492. result = res0.data.params.host + '/' + res0.data.params.key;
  493. let thumb = '';
  494. if (this.accept === 'video' || test.video(result)) {
  495. thumb = result + '?x-oss-process=video/snapshot,t_10000,m_fast';
  496. }
  497. that.succcessUpload(len + j, result, thumb);
  498. }
  499. });
  500. uploadTask.onProgressUpdate((res) => {
  501. that.updateUpload(len + j, {
  502. progress: res.progress
  503. });
  504. // console.log('上传进度' + res.progress);
  505. // console.log('已经上传的数据长度' + res.totalBytesSent);
  506. // console.log('预期需要上传的数据总长度' + res.totalBytesExpectedToSend);
  507. });
  508. break;
  509. case 'local':
  510. default:
  511. // 服务器本机上传
  512. var uploadTask = uni.uploadFile({
  513. url: this.autoUploadApi,
  514. filePath: lists[j].url,
  515. name: 'file',
  516. // fileType: 'video', // 仅支付宝小程序,且必填。
  517. header: this.autoUploadHeader,
  518. success: (r) => {
  519. result = res0.data.params.host + '/' + res0.data.params.key;
  520. that.succcessUpload(len + j, result);
  521. }
  522. });
  523. uploadTask.onProgressUpdate((res) => {
  524. that.updateUpload(len + j, {
  525. progress: res.progress
  526. });
  527. // console.log('上传进度' + res.progress);
  528. // console.log('已经上传的数据长度' + res.totalBytesSent);
  529. // console.log('预期需要上传的数据总长度' + res.totalBytesExpectedToSend);
  530. });
  531. break;
  532. }
  533. }
  534. } else {
  535. if (typeof afterRead === 'function') {
  536. afterRead(file, this.getDetail());
  537. }
  538. this.$emit('afterRead', Object.assign({
  539. file
  540. }, this.getDetail()));
  541. }
  542. },
  543. updateUpload(index, param) {
  544. let item = this.fileList[index];
  545. this.fileList.splice(index, 1, {
  546. ...item,
  547. status: 'uploading',
  548. message: '',
  549. progress: param.progress
  550. });
  551. this.$emit('update:fileList', this.fileList);
  552. },
  553. succcessUpload(index, url, thumb = '') {
  554. let item = this.fileList[index];
  555. this.fileList.splice(index, 1, {
  556. ...item,
  557. status: 'success',
  558. message: '',
  559. url: url,
  560. progress: 100,
  561. thumb: thumb
  562. });
  563. this.$emit('update:fileList', this.fileList);
  564. },
  565. deleteItem(index) {
  566. if (this.autoDelete) {
  567. this.fileList.splice(index, 1);
  568. this.$emit('update:fileList', this.fileList);
  569. } else {
  570. this.$emit(
  571. 'delete',
  572. Object.assign(Object.assign({}, this.getDetail(index)), {
  573. file: this.fileList[index],
  574. })
  575. );
  576. }
  577. },
  578. // 预览图片
  579. onPreviewImage(previewItem, index) {
  580. if (!previewItem.isImage || !this.previewFullImage) return
  581. let current = 0;
  582. const urls = [];
  583. let imageIndex = 0;
  584. for (var i = 0; i < this.lists.length; i++) {
  585. const item = this.lists[i];
  586. if (item.isImage || (item.type && item.type === 'image')) {
  587. urls.push(item.url || item.thumb);
  588. if (i === index) {
  589. current = imageIndex;
  590. }
  591. imageIndex += 1;
  592. }
  593. }
  594. if (urls.length < 1) {
  595. return;
  596. }
  597. uni.previewImage({
  598. urls: urls,
  599. current: current,
  600. fail() {
  601. toast('预览图片失败')
  602. },
  603. });
  604. },
  605. onPreviewVideo(previewItem, index) {
  606. if (!this.previewFullImage) return;
  607. let current = 0;
  608. const sources = [];
  609. let videoIndex = 0;
  610. for (var i = 0; i < this.lists.length; i++) {
  611. const item = this.lists[i];
  612. if (item.isVideo || (item.type && item.type === 'video')) {
  613. sources.push(Object.assign(Object.assign({}, item), {
  614. type: 'video'
  615. }));
  616. if (i === index) {
  617. current = videoIndex;
  618. }
  619. videoIndex += 1;
  620. }
  621. }
  622. if (sources.length < 1) {
  623. return;
  624. }
  625. // #ifndef MP-WEIXIN
  626. this.popupShow = true;
  627. this.currentItemIndex = index;
  628. console.log(this.lists[this.currentItemIndex])
  629. // #endif
  630. // #ifdef MP-WEIXIN
  631. wx.previewMedia({
  632. sources: sources,
  633. current: current,
  634. fail() {
  635. toast('预览视频失败')
  636. },
  637. });
  638. // #endif
  639. },
  640. onClickPreview(item, index) {
  641. if (!this.previewFullImage) return;
  642. switch (item.type) {
  643. case 'video':
  644. this.onPreviewVideo(index);
  645. break;
  646. default:
  647. break;
  648. }
  649. this.$emit(
  650. 'clickPreview',
  651. Object.assign(Object.assign({}, item), this.getDetail(index))
  652. );
  653. }
  654. }
  655. }
  656. </script>
  657. <style lang="scss" scoped>
  658. @import '../../libs/css/components.scss';
  659. $u-upload-preview-border-radius: 2px !default;
  660. $u-upload-preview-margin: 0 8px 8px 0 !default;
  661. $u-upload-image-width:80px !default;
  662. $u-upload-image-height:$u-upload-image-width;
  663. $u-upload-other-bgColor: rgb(242, 242, 242) !default;
  664. $u-upload-other-flex:1 !default;
  665. $u-upload-text-font-size:11px !default;
  666. $u-upload-text-color:$u-tips-color !default;
  667. $u-upload-text-margin-top:2px !default;
  668. $u-upload-deletable-right:0 !default;
  669. $u-upload-deletable-top:0 !default;
  670. $u-upload-deletable-bgColor:rgb(55, 55, 55) !default;
  671. $u-upload-deletable-height:14px !default;
  672. $u-upload-deletable-width:$u-upload-deletable-height;
  673. $u-upload-deletable-boder-bottom-left-radius:100px !default;
  674. $u-upload-deletable-zIndex:3 !default;
  675. $u-upload-success-bottom:0 !default;
  676. $u-upload-success-right:0 !default;
  677. $u-upload-success-border-style:solid !default;
  678. $u-upload-success-border-top-color:transparent !default;
  679. $u-upload-success-border-left-color:transparent !default;
  680. $u-upload-success-border-bottom-color: $u-success !default;
  681. $u-upload-success-border-right-color:$u-upload-success-border-bottom-color;
  682. $u-upload-success-border-width:9px !default;
  683. $u-upload-icon-top:0px !default;
  684. $u-upload-icon-right:0px !default;
  685. $u-upload-icon-h5-top:1px !default;
  686. $u-upload-icon-h5-right:0 !default;
  687. $u-upload-icon-width:16px !default;
  688. $u-upload-icon-height:$u-upload-icon-width;
  689. $u-upload-success-icon-bottom:-10px !default;
  690. $u-upload-success-icon-right:-10px !default;
  691. $u-upload-status-right:0 !default;
  692. $u-upload-status-left:0 !default;
  693. $u-upload-status-bottom:0 !default;
  694. $u-upload-status-top:0 !default;
  695. $u-upload-status-bgColor:rgba(0, 0, 0, 0.5) !default;
  696. $u-upload-status-icon-Zindex:1 !default;
  697. $u-upload-message-font-size:12px !default;
  698. $u-upload-message-color:#FFFFFF !default;
  699. $u-upload-message-margin-top:5px !default;
  700. $u-upload-button-width:80px !default;
  701. $u-upload-button-height:$u-upload-button-width;
  702. $u-upload-button-bgColor:rgb(244, 245, 247) !default;
  703. $u-upload-button-border-radius:2px !default;
  704. $u-upload-botton-margin: 0 8px 8px 0 !default;
  705. $u-upload-text-font-size:11px !default;
  706. $u-upload-text-color:$u-tips-color !default;
  707. $u-upload-text-margin-top: 2px !default;
  708. $u-upload-hover-bgColor:rgb(230, 231, 233) !default;
  709. $u-upload-disabled-opacity:.5 !default;
  710. .u-upload {
  711. @include flex(column);
  712. flex: 1;
  713. &__wrap {
  714. @include flex;
  715. flex-wrap: wrap;
  716. flex: 1;
  717. &__preview {
  718. border-radius: $u-upload-preview-border-radius;
  719. margin: $u-upload-preview-margin;
  720. position: relative;
  721. overflow: hidden;
  722. @include flex;
  723. &__image {
  724. width: $u-upload-image-width;
  725. height: $u-upload-image-height;
  726. }
  727. &__video,
  728. &__other {
  729. width: $u-upload-image-width;
  730. height: $u-upload-image-height;
  731. background-color: $u-upload-other-bgColor;
  732. flex: $u-upload-other-flex;
  733. @include flex(column);
  734. justify-content: center;
  735. align-items: center;
  736. &__text {
  737. font-size: $u-upload-text-font-size;
  738. color: $u-upload-text-color;
  739. margin-top: $u-upload-text-margin-top;
  740. }
  741. }
  742. }
  743. }
  744. &__wrap__play {
  745. position: absolute;
  746. top: 0px;
  747. left: 0px;
  748. bottom: 0px;
  749. right: 0px;
  750. display: flex;
  751. justify-content: center;
  752. align-items: center;
  753. &__icon {
  754. background: #fff;
  755. border-radius: 100px;
  756. opacity: 0.8;
  757. };
  758. }
  759. &__deletable {
  760. position: absolute;
  761. top: $u-upload-deletable-top;
  762. right: $u-upload-deletable-right;
  763. background-color: $u-upload-deletable-bgColor;
  764. height: $u-upload-deletable-height;
  765. width: $u-upload-deletable-width;
  766. @include flex;
  767. border-bottom-left-radius: $u-upload-deletable-boder-bottom-left-radius;
  768. align-items: center;
  769. justify-content: center;
  770. z-index: $u-upload-deletable-zIndex;
  771. &__icon {
  772. position: absolute;
  773. transform: scale(0.7);
  774. top: $u-upload-icon-top;
  775. right: $u-upload-icon-right;
  776. /* #ifdef H5 */
  777. top: $u-upload-icon-h5-top;
  778. right: $u-upload-icon-h5-right;
  779. /* #endif */
  780. }
  781. }
  782. &__success {
  783. position: absolute;
  784. bottom: $u-upload-success-bottom;
  785. right: $u-upload-success-right;
  786. @include flex;
  787. // 由于weex(nvue)为阿里巴巴的KPI(部门业绩考核)的laji产物,不支持css绘制三角形
  788. // 所以在nvue下使用图片,非nvue下使用css实现
  789. /* #ifndef APP-NVUE */
  790. border-style: $u-upload-success-border-style;
  791. border-top-color: $u-upload-success-border-top-color;
  792. border-left-color: $u-upload-success-border-left-color;
  793. border-bottom-color: $u-upload-success-border-bottom-color;
  794. border-right-color: $u-upload-success-border-right-color;
  795. border-width: $u-upload-success-border-width;
  796. align-items: center;
  797. justify-content: center;
  798. /* #endif */
  799. &__icon {
  800. /* #ifndef APP-NVUE */
  801. position: absolute;
  802. transform: scale(0.7);
  803. bottom: $u-upload-success-icon-bottom;
  804. right: $u-upload-success-icon-right;
  805. /* #endif */
  806. /* #ifdef APP-NVUE */
  807. width: $u-upload-icon-width;
  808. height: $u-upload-icon-height;
  809. /* #endif */
  810. }
  811. }
  812. &__progress {
  813. background-color: $u-primary !important;
  814. position: absolute;
  815. bottom: 0;
  816. left: 0;
  817. }
  818. &__status {
  819. position: absolute;
  820. top: $u-upload-status-top;
  821. bottom: $u-upload-status-bottom;
  822. left: $u-upload-status-left;
  823. right: $u-upload-status-right;
  824. background-color: $u-upload-status-bgColor;
  825. @include flex(column);
  826. align-items: center;
  827. justify-content: center;
  828. &__icon {
  829. position: relative;
  830. z-index: $u-upload-status-icon-Zindex;
  831. }
  832. &__message {
  833. font-size: $u-upload-message-font-size;
  834. color: $u-upload-message-color;
  835. margin-top: $u-upload-message-margin-top;
  836. }
  837. }
  838. &__button {
  839. @include flex(column);
  840. align-items: center;
  841. justify-content: center;
  842. width: $u-upload-button-width;
  843. height: $u-upload-button-height;
  844. background-color: $u-upload-button-bgColor;
  845. border-radius: $u-upload-button-border-radius;
  846. margin: $u-upload-botton-margin;
  847. /* #ifndef APP-NVUE */
  848. box-sizing: border-box;
  849. /* #endif */
  850. &__text {
  851. font-size: $u-upload-text-font-size;
  852. color: $u-upload-text-color;
  853. margin-top: $u-upload-text-margin-top;
  854. }
  855. &--hover {
  856. background-color: $u-upload-hover-bgColor;
  857. }
  858. &--disabled {
  859. opacity: $u-upload-disabled-opacity;
  860. }
  861. }
  862. }
  863. </style>