en-upload.vue 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592
  1. <template>
  2. <view class="con">
  3. <movable-area class="area" :style="{ height: areaHeight }" @mouseenter="mouseenter" @mouseleave="mouseleave">
  4. <block v-for="(item, index) in imageList" :key="item.id">
  5. <movable-view class="view" :x="item.x" :y="item.y" direction="all" :damping="40" :disabled="item.disable"
  6. @change="onChange($event, item)" @touchstart="touchstart(item)" @mousedown="touchstart(item)"
  7. @touchend="touchend(item)" @mouseup="touchend(item)"
  8. :style="{ width: viewWidth + 'px', height: viewWidth + 'px', 'z-index': item.zIndex, opacity: item.opacity }">
  9. <view class="area-con" @click.stop="examineImgOne(index)"
  10. :style="{ width: childWidth, height: childWidth, transform: 'scale(' + item.scale + ')' }">
  11. <image class="pre-image" :src="item.src+(imgType===1?'':'?x-oss-process=video/snapshot,t_7000,f_jpg,w_600,h_0,m_fast')" mode="aspectFill"></image>
  12. <text v-if="imgType===2" class="bf-img iconfont"> &#xea82;</text>
  13. <view class="del-con" @click.stop="delImage(item, index)" @touchstart.stop="delImageMp(item, index)"
  14. @touchend.stop="nothing()" @mousedown.stop="nothing()" @mouseup.stop="nothing()">
  15. <view class="del-wrap">
  16. <!-- <image class="del-image"-->
  17. <!-- src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACEAAAAhCAYAAABX5MJvAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAAhdEVYdENyZWF0aW9uIFRpbWUAMjAyMDowNzoyNSAyMTo1NDoyOU4TkJAAAADcSURBVFhH7ZfRCoMwDEXLvkjwwVf/bH/emmAyN6glTW9WBjsgwm28OeCLpj81Sil7zvlJ90UiONS/yY5VogsO6XrBg3IEQ5a/s8vRSWUAKmLqp2w5jz5BiNQEGMo3GbloDLtFXJ1IkaEuhAiiY6gEIqB4yqACSk9piIBiKQ8VUFpLviKg3C2rESKgWERCBZSWiEfgIfffYvrrsAgoISJ3Apy3zuTxcSxLQkV6ykNEPKVQkZEyiAiiZKgDIaC4upACSlcn5fM/+WuDCAHF1E/Z/N9AhkMZnPNDPI+UDjPIXgAQIGjNAAAAAElFTkSuQmCC">-->
  18. <!-- </image>-->
  19. <text class="del-image iconfont"> &#xe603;</text>
  20. </view>
  21. </view>
  22. </view>
  23. </movable-view>
  24. </block>
  25. <view class="add" v-if="imageList.length < number"
  26. :style="{ top: add.y, left: add.x, width: viewWidth + 'px', height: viewWidth + 'px' }" @click="selectMultimedia">
  27. <view class="add-wrap" :style="{ width: childWidth, height: childWidth }">
  28. <view class="video-data">
  29. <text class="video-img iconfont" >&#xe658;</text>
  30. </view>
  31. </view>
  32. </view>
  33. </movable-area>
  34. </view>
  35. </template>
  36. <script>
  37. import tools from "@/service/tools";
  38. import {upLoadingFileOss} from "@/service/upLoadingFile";
  39. export default {
  40. components:{
  41. },
  42. data() {
  43. return {
  44. imageList: [],
  45. previewList:[],
  46. width: 0,
  47. add: {
  48. x: 0,
  49. y: 0
  50. },
  51. current: 0,
  52. colsValue: 0,
  53. viewWidth: 0,
  54. tempItem: null,
  55. timer: null,
  56. changeStatus: true,
  57. preStatus: true,
  58. ossClient:null
  59. }
  60. },
  61. props: {
  62. // 返回排序后图片
  63. list: {
  64. type: Array,
  65. default: function() {
  66. return []
  67. }
  68. },
  69. imgType: {
  70. type: Number,
  71. default: 1
  72. },
  73. fileNum: {
  74. type: Number,
  75. default: 9
  76. },
  77. // 选择图片数量限制
  78. number: {
  79. type: Number,
  80. default: 100
  81. },
  82. // 图片父容器宽度(实际显示的图片宽度为 imageWidth / 1.1 ),单位 rpx
  83. imageWidth: {
  84. type: Number,
  85. default: 230
  86. },
  87. // 图片列数(cols > 0 则 imageWidth 无效)
  88. cols: {
  89. type: Number,
  90. default: 0
  91. },
  92. // 图片周围空白填充,单位 rpx
  93. padding: {
  94. type: Number,
  95. default: 10
  96. },
  97. // 拖动图片时放大倍数 [0, ∞)
  98. scale: {
  99. type: Number,
  100. default: 1.1
  101. },
  102. // 拖动图片时不透明度
  103. opacity: {
  104. type: Number,
  105. default: 0.7
  106. },
  107. // 自定义添加(需配合 @aaddImage 事件使用)
  108. custom: {
  109. type: Boolean,
  110. default: false
  111. }
  112. },
  113. computed: {
  114. areaHeight() {
  115. if (this.imageList.length < this.number) {
  116. return Math.ceil((this.imageList.length + 1) / this.colsValue) * this.viewWidth + 'px'
  117. } else {
  118. return Math.ceil(this.imageList.length / this.colsValue) * this.viewWidth + 'px'
  119. }
  120. },
  121. childWidth() {
  122. return this.viewWidth - this.rpx2px(this.padding) * 2 + 'px'
  123. },
  124. },
  125. created() {
  126. this.width = uni.getSystemInfoSync().windowWidth
  127. this.viewWidth = this.rpx2px(this.imageWidth)
  128. },
  129. mounted() {
  130. const query = uni.createSelectorQuery().in(this)
  131. query.select('.area').boundingClientRect(data => {
  132. this.colsValue = Math.floor(data.width / this.viewWidth)
  133. if (this.cols > 0) {
  134. this.colsValue = this.cols
  135. this.viewWidth = data.width / this.cols
  136. }
  137. for (let item of this.list) {
  138. this.addProperties(item)
  139. }
  140. })
  141. query.exec()
  142. },
  143. methods: {
  144. examineImgOne( index) {
  145. // this.$emit('examineImgOne',index)
  146. // this.previewList = this.list
  147. // this.current = index
  148. // this.$refs.previewImage.open(index);
  149. let imgList = []
  150. this.imageList.forEach((item)=>{
  151. console.log(item)
  152. imgList.push(item.src)
  153. })
  154. uni.previewImage({
  155. urls: imgList,
  156. current: index,
  157. success: () => {
  158. }
  159. })
  160. },
  161. startList(imgList){
  162. if(imgList.length<=0){
  163. let maxImageNum=this.imageList.length
  164. if(maxImageNum>0){
  165. for (let i=maxImageNum;i>0;--i){
  166. let keyNum=i-1;
  167. this.delImage(this.imageList[keyNum],keyNum)
  168. }
  169. }
  170. }else {
  171. imgList.forEach((item)=>{
  172. this.addProperties(item)
  173. })
  174. }
  175. },
  176. onChange(e, item) {
  177. if (!item) return
  178. item.oldX = e.detail.x
  179. item.oldY = e.detail.y
  180. if (e.detail.source === 'touch') {
  181. if (item.moveEnd) {
  182. item.offset = Math.sqrt(Math.pow(item.oldX - item.absX * this.viewWidth, 2) + Math.pow(item.oldY - item
  183. .absY * this.viewWidth, 2))
  184. }
  185. let x = Math.floor((e.detail.x + this.viewWidth / 2) / this.viewWidth)
  186. if (x >= this.colsValue) return
  187. let y = Math.floor((e.detail.y + this.viewWidth / 2) / this.viewWidth)
  188. let index = this.colsValue * y + x
  189. if (item.index != index && index < this.imageList.length) {
  190. this.changeStatus = false
  191. for (let obj of this.imageList) {
  192. if (item.index > index && obj.index >= index && obj.index < item.index) {
  193. this.change(obj, 1)
  194. } else if (item.index < index && obj.index <= index && obj.index > item.index) {
  195. this.change(obj, -1)
  196. } else if (obj.id != item.id) {
  197. obj.offset = 0
  198. obj.x = obj.oldX
  199. obj.y = obj.oldY
  200. setTimeout(() => {
  201. this.$nextTick(() => {
  202. obj.x = obj.absX * this.viewWidth
  203. obj.y = obj.absY * this.viewWidth
  204. })
  205. }, 0)
  206. }
  207. }
  208. item.index = index
  209. item.absX = x
  210. item.absY = y
  211. this.sortList()
  212. }
  213. }
  214. },
  215. change(obj, i) {
  216. obj.index += i
  217. obj.offset = 0
  218. obj.x = obj.oldX
  219. obj.y = obj.oldY
  220. obj.absX = obj.index % this.colsValue
  221. obj.absY = Math.floor(obj.index / this.colsValue)
  222. setTimeout(() => {
  223. this.$nextTick(() => {
  224. obj.x = obj.absX * this.viewWidth
  225. obj.y = obj.absY * this.viewWidth
  226. })
  227. }, 0)
  228. },
  229. touchstart(item) {
  230. this.imageList.forEach(v => {
  231. v.zIndex = v.index + 9
  232. })
  233. item.zIndex = 99
  234. item.moveEnd = true
  235. this.tempItem = item
  236. this.timer = setTimeout(() => {
  237. item.scale = this.scale
  238. item.opacity = this.opacity
  239. clearTimeout(this.timer)
  240. this.timer = null
  241. }, 200)
  242. },
  243. touchend(item) {
  244. this.previewImage(item)
  245. item.scale = 1
  246. item.opacity = 1
  247. item.x = item.oldX
  248. item.y = item.oldY
  249. item.offset = 0
  250. item.moveEnd = false
  251. setTimeout(() => {
  252. this.$nextTick(() => {
  253. item.x = item.absX * this.viewWidth
  254. item.y = item.absY * this.viewWidth
  255. this.tempItem = null
  256. this.changeStatus = true
  257. })
  258. }, 0)
  259. },
  260. previewImage(item) {
  261. console.log(item)
  262. if (this.timer && this.preStatus && this.changeStatus && item.offset < 28.28) {
  263. clearTimeout(this.timer)
  264. this.timer = null
  265. let src = this.list.findIndex(v => v === item.src)
  266. uni.previewImage({
  267. urls: this.list,
  268. current: src,
  269. success: () => {
  270. this.preStatus = false
  271. setTimeout(() => {
  272. this.preStatus = true
  273. }, 600)
  274. }
  275. })
  276. } else if (this.timer) {
  277. clearTimeout(this.timer)
  278. this.timer = null
  279. }
  280. },
  281. mouseenter() {
  282. //#ifdef H5
  283. this.imageList.forEach(v => {
  284. v.disable = false
  285. })
  286. //#endif
  287. },
  288. mouseleave() {
  289. //#ifdef H5
  290. if (this.tempItem) {
  291. this.imageList.forEach(v => {
  292. v.disable = true
  293. v.zIndex = v.index + 1
  294. v.offset = 0
  295. v.moveEnd = false
  296. if (v.id == this.tempItem.id) {
  297. if (this.timer) {
  298. clearTimeout(this.timer)
  299. this.timer = null
  300. }
  301. v.scale = 1
  302. v.opacity = 1
  303. v.x = v.oldX
  304. v.y = v.oldY
  305. this.$nextTick(() => {
  306. v.x = v.absX * this.viewWidth
  307. v.y = v.absY * this.viewWidth
  308. this.tempItem = null
  309. })
  310. }
  311. })
  312. this.changeStatus = true
  313. }
  314. //#endif
  315. },
  316. selectMultimedia() {
  317. if (this.imgType === 1) {
  318. //图片选择
  319. this.uploadingImg();
  320. } else {
  321. //视频选择
  322. this.uploadingVideo();
  323. }
  324. },
  325. uploadingImg() {
  326. if (this.list.length > 100) {
  327. tools.error('最多上传100张')
  328. return;
  329. }
  330. uni.chooseImage({
  331. count: this.fileNum, //默认9
  332. sizeType: 'original', //可以指定是原图还是压缩图,默认二者都有 'camera' : 'album'
  333. sourceType: ['album'],
  334. success: (res) => {
  335. console.log(res.tempFiles)
  336. if (res.tempFiles !== undefined) {
  337. console.log(res.tempFiles)
  338. console.log('--------------------')
  339. res.tempFiles.forEach((file) => {
  340. if (file.size > (1048576 * 3)) {
  341. // tools.error('上传的图片最大3MB')
  342. } else {
  343. if (this.list.length > 100) {
  344. tools.error('最多上传100张')
  345. } else {
  346. console.log('--aaaaaaaaaa------------------')
  347. if(tools.getPlatform()==='H5'){
  348. this.uploadingFile(file);
  349. }else {
  350. this.uploadingFile(file.path);
  351. }
  352. }
  353. }
  354. })
  355. }
  356. },
  357. fail: (e) => {
  358. console.log(e)
  359. }
  360. });
  361. },
  362. uploadingVideo() {
  363. uni.chooseVideo({
  364. sourceType: ['album'],
  365. maxDuration: 60,
  366. success: (res) => {
  367. console.log(res)
  368. this.uploadingFile(res.tempFile);
  369. }
  370. });
  371. },
  372. uploadingFile(file) {
  373. //图片上传
  374. tools.showLoading();
  375. console.log('----------------asdsa')
  376. upLoadingFileOss(file).then((res) => {
  377. console.log('----------------------上传成功')
  378. console.log(res)
  379. if (res) {
  380. console.log(res)
  381. console.log('------------url')
  382. this.addProperties(res)
  383. tools.hideLoading();
  384. }else {
  385. tools.hideLoading();
  386. tools.error('上传失败')
  387. }
  388. }).catch((err) => {
  389. tools.hideLoading();
  390. })
  391. },
  392. addImages() {
  393. if (this.custom) {
  394. this.$emit('addImage')
  395. } else {
  396. let checkNumber = this.number - this.imageList.length
  397. uni.chooseImage({
  398. count: checkNumber,
  399. sourceType: ['album', 'camera'],
  400. success: res => {
  401. let count = checkNumber <= res.tempFilePaths.length ? checkNumber : res.tempFilePaths.length
  402. for (let i = 0; i < count; i++) {
  403. this.addProperties(res.tempFilePaths[i])
  404. }
  405. }
  406. })
  407. }
  408. },
  409. addImage(image) {
  410. this.addProperties(image)
  411. },
  412. delImage(item, index) {
  413. this.imageList.splice(index, 1)
  414. for (let obj of this.imageList) {
  415. if (obj.index > item.index) {
  416. obj.index -= 1
  417. obj.x = obj.oldX
  418. obj.y = obj.oldY
  419. obj.absX = obj.index % this.colsValue
  420. obj.absY = Math.floor(obj.index / this.colsValue)
  421. this.$nextTick(() => {
  422. obj.x = obj.absX * this.viewWidth
  423. obj.y = obj.absY * this.viewWidth
  424. })
  425. }
  426. }
  427. this.add.x = (this.imageList.length % this.colsValue) * this.viewWidth + 'px'
  428. this.add.y = Math.floor(this.imageList.length / this.colsValue) * this.viewWidth + 'px'
  429. this.sortList()
  430. },
  431. delImageMp(item, index) {
  432. //#ifdef MP
  433. this.delImage(item, index)
  434. //#endif
  435. },
  436. sortList() {
  437. let list = this.imageList.slice()
  438. list.sort((a, b) => {
  439. return a.index - b.index
  440. })
  441. for (let i = 0; i < list.length; i++) {
  442. list[i] = list[i].src
  443. }
  444. // this.$emit('update:list', list)
  445. this.$emit('input', list)
  446. },
  447. addProperties(item) {
  448. console.log('------------------addProperties')
  449. console.log(item)
  450. let absX = this.imageList.length % this.colsValue
  451. let absY = Math.floor(this.imageList.length / this.colsValue)
  452. let x = absX * this.viewWidth
  453. let y = absY * this.viewWidth
  454. this.imageList.push({
  455. src: item,
  456. x,
  457. y,
  458. oldX: x,
  459. oldY: y,
  460. absX,
  461. absY,
  462. scale: 1,
  463. zIndex: 9,
  464. opacity: 1,
  465. index: this.imageList.length,
  466. id: this.guid(),
  467. disable: false,
  468. offset: 0,
  469. moveEnd: false
  470. })
  471. this.add.x = (this.imageList.length % this.colsValue) * this.viewWidth + 'px'
  472. this.add.y = Math.floor(this.imageList.length / this.colsValue) * this.viewWidth + 'px'
  473. this.sortList()
  474. },
  475. nothing() {},
  476. rpx2px(v) {
  477. return this.width * v / 750
  478. },
  479. guid() {
  480. function S4() {
  481. return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
  482. }
  483. return (S4() + S4() + "-" + S4() + "-" + S4() + "-" + S4() + "-" + S4() + S4() + S4());
  484. }
  485. }
  486. }
  487. </script>
  488. <style lang="scss" scoped>
  489. @import url("../../../static/css/en-common.css");
  490. .con {
  491. padding: 30rpx;
  492. .area {
  493. width: 100%;
  494. .view {
  495. display: flex;
  496. justify-content: center;
  497. align-items: center;
  498. .area-con {
  499. position: relative;
  500. .pre-image {
  501. width: 100%;
  502. height: 100%;
  503. border-radius: 10rpx;
  504. }
  505. .bf-img {
  506. width: 40rpx;
  507. height: 40rpx;
  508. position: absolute;
  509. left: 50%;
  510. top: 50%;
  511. //margin: -20rpx 0 0 -20rpx;
  512. z-index: 2;
  513. }
  514. .del-con {
  515. position: absolute;
  516. top: 5rpx;
  517. right: 5rpx;
  518. //padding: 0 0 20rpx 20rpx;
  519. .del-wrap {
  520. width: 36rpx;
  521. height: 36rpx;
  522. //background-color: rgba(0, 0, 0, 0.4);
  523. border-radius: 0 0 0 10rpx;
  524. display: flex;
  525. justify-content: center;
  526. align-items: center;
  527. .del-image {
  528. //width: 20rpx;
  529. //height: 20rpx;
  530. }
  531. }
  532. }
  533. }
  534. }
  535. .add {
  536. position: absolute;
  537. display: flex;
  538. justify-content: center;
  539. align-items: center;
  540. .add-wrap {
  541. // display: flex;
  542. // justify-content: center;
  543. // align-items: center;
  544. background: #F7F7F7;
  545. border-radius: 10rpx;
  546. .add-img {
  547. width: 80rpx;
  548. height: 80rpx;
  549. margin: 0 auto;
  550. padding-top: 44rpx;
  551. }
  552. .names {
  553. color: #6F6F6F;
  554. font-size: 28rpx;
  555. text-align: center;
  556. }
  557. .video-data{
  558. width: 212rpx;
  559. height: 212rpx;
  560. //background-color: #fff;
  561. text-align: center;
  562. line-height: 212rpx;
  563. .video-img {
  564. font-size: 100rpx;
  565. color: #333333;
  566. }
  567. }
  568. }
  569. }
  570. }
  571. }
  572. </style>