en-upload.vue 18 KB

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