Преглед на файлове

feat(PageThree): enhance drag-and-drop functionality for image display and improve touch event handling

Yannay преди 2 месеца
родител
ревизия
a4c452f1de
променени са 1 файла, в които са добавени 52 реда и са изтрити 21 реда
  1. 52 21
      pages/orderDetailRefactored/components/PageThree.vue

+ 52 - 21
pages/orderDetailRefactored/components/PageThree.vue

@@ -83,15 +83,16 @@
83
         <view class="detail-image-content">
83
         <view class="detail-image-content">
84
           <view class="detail-image-list">
84
           <view class="detail-image-list">
85
             <view 
85
             <view 
86
-              v-for="(item, index) in detailImages.slice(0, 6)" 
87
-              :key="`detail-${index}`"
86
+              v-for="(item, index) in displayImages" 
87
+              :key="item.id || `detail-${index}`"
88
               class="detail-image-item"
88
               class="detail-image-item"
89
               :class="{
89
               :class="{
90
                 'dragging': draggingIndex === index,
90
                 'dragging': draggingIndex === index,
91
-                'can-drop': canDropIndex === index
91
+                'can-drop': canDropIndex === index && draggingIndex !== index
92
               }"
92
               }"
93
+              :style="draggingIndex === index ? draggingStyle : ''"
93
               @touchstart.stop="onTouchStart($event, index)"
94
               @touchstart.stop="onTouchStart($event, index)"
94
-              @touchmove.stop="onTouchMove($event)"
95
+              @touchmove.stop="onTouchMove($event, index)"
95
               @touchend.stop="onTouchEnd"
96
               @touchend.stop="onTouchEnd"
96
             >
97
             >
97
               <PicComp 
98
               <PicComp 
@@ -282,6 +283,8 @@ export default {
282
       canDropIndex: -1,
283
       canDropIndex: -1,
283
       startX: 0,
284
       startX: 0,
284
       startY: 0,
285
       startY: 0,
286
+      currentX: 0,
287
+      currentY: 0,
285
       // 模态窗
288
       // 模态窗
286
       unpaidModalVisible: false,
289
       unpaidModalVisible: false,
287
       followUpModalVisible: false,
290
       followUpModalVisible: false,
@@ -290,6 +293,20 @@ export default {
290
       followUpNotes: ''
293
       followUpNotes: ''
291
     }
294
     }
292
   },
295
   },
296
+  computed: {
297
+    // 显示前6个图片用于拖拽
298
+    displayImages() {
299
+      return this.detailImages.slice(0, 6)
300
+    },
301
+    // 拖拽时的样式
302
+    draggingStyle() {
303
+      if (this.draggingIndex === -1) return ''
304
+      return {
305
+        transform: `translate(${this.currentX - this.startX}px, ${this.currentY - this.startY}px)`,
306
+        zIndex: 1000
307
+      }
308
+    }
309
+  },
293
   watch: {
310
   watch: {
294
     orderDetail: {
311
     orderDetail: {
295
       handler(newVal) {
312
       handler(newVal) {
@@ -417,18 +434,22 @@ export default {
417
     onTouchStart(event, index) {
434
     onTouchStart(event, index) {
418
       if (this.draggingIndex !== -1) return
435
       if (this.draggingIndex !== -1) return
419
       this.draggingIndex = index
436
       this.draggingIndex = index
420
-      this.startX = event.touches[0].clientX
421
-      this.startY = event.touches[0].clientY
437
+      const touch = event.touches[0]
438
+      this.startX = touch.clientX
439
+      this.startY = touch.clientY
440
+      this.currentX = touch.clientX
441
+      this.currentY = touch.clientY
422
     },
442
     },
423
 
443
 
424
     /**
444
     /**
425
      * 触摸移动
445
      * 触摸移动
426
      */
446
      */
427
-    onTouchMove(event) {
428
-      if (this.draggingIndex === -1) return
429
-      const moveX = event.touches[0].clientX
430
-      const moveY = event.touches[0].clientY
431
-      this.findTargetIndex(moveX, moveY)
447
+    onTouchMove(event, index) {
448
+      if (this.draggingIndex === -1 || this.draggingIndex !== index) return
449
+      const touch = event.touches[0]
450
+      this.currentX = touch.clientX
451
+      this.currentY = touch.clientY
452
+      this.findTargetIndex(touch.clientX, touch.clientY)
432
     },
453
     },
433
 
454
 
434
     /**
455
     /**
@@ -438,9 +459,12 @@ export default {
438
       if (this.draggingIndex === -1) return
459
       if (this.draggingIndex === -1) return
439
       
460
       
440
       if (this.canDropIndex !== -1 && this.canDropIndex !== this.draggingIndex) {
461
       if (this.canDropIndex !== -1 && this.canDropIndex !== this.draggingIndex) {
441
-        const temp = this.detailImages[this.draggingIndex]
442
-        this.detailImages[this.draggingIndex] = this.detailImages[this.canDropIndex]
443
-        this.detailImages[this.canDropIndex] = temp
462
+        // 交换位置
463
+        const images = [...this.detailImages]
464
+        const temp = images[this.draggingIndex]
465
+        images[this.draggingIndex] = images[this.canDropIndex]
466
+        images[this.canDropIndex] = temp
467
+        this.detailImages = images
444
       }
468
       }
445
       
469
       
446
       this.resetDragState()
470
       this.resetDragState()
@@ -451,7 +475,8 @@ export default {
451
      */
475
      */
452
     findTargetIndex(x, y) {
476
     findTargetIndex(x, y) {
453
       const query = uni.createSelectorQuery().in(this)
477
       const query = uni.createSelectorQuery().in(this)
454
-      query.selectAll('.detail-image-item').boundingClientRect(rects => {
478
+      query.selectAll('.detail-image-item').boundingClientRect((rects) => {
479
+        if (!rects || rects.length === 0) return
455
         let targetIndex = -1
480
         let targetIndex = -1
456
         for (let i = 0; i < rects.length; i++) {
481
         for (let i = 0; i < rects.length; i++) {
457
           if (i === this.draggingIndex) continue
482
           if (i === this.draggingIndex) continue
@@ -473,6 +498,8 @@ export default {
473
       this.canDropIndex = -1
498
       this.canDropIndex = -1
474
       this.startX = 0
499
       this.startX = 0
475
       this.startY = 0
500
       this.startY = 0
501
+      this.currentX = 0
502
+      this.currentY = 0
476
     },
503
     },
477
 
504
 
478
     /**
505
     /**
@@ -498,7 +525,7 @@ export default {
498
      * 隐藏图片
525
      * 隐藏图片
499
      */
526
      */
500
     handleHideImage(item, index) {
527
     handleHideImage(item, index) {
501
-      const itemIndex = this.detailImages.findIndex(img => img.fileUrl === item.fileUrl)
528
+      const itemIndex = this.detailImages.findIndex(img => img.id === item.id || img.fileUrl === item.fileUrl)
502
       if (itemIndex !== -1) {
529
       if (itemIndex !== -1) {
503
         this.detailImages.splice(itemIndex, 1)
530
         this.detailImages.splice(itemIndex, 1)
504
         uni.$u.toast('图片已隐藏')
531
         uni.$u.toast('图片已隐藏')
@@ -718,19 +745,22 @@ export default {
718
   width: 200rpx;
745
   width: 200rpx;
719
   height: 200rpx;
746
   height: 200rpx;
720
   touch-action: none;
747
   touch-action: none;
721
-  transition: all 0.2s ease;
748
+  transition: transform 0.2s ease, opacity 0.2s ease;
722
   z-index: 1;
749
   z-index: 1;
723
-  cursor: move;
750
+  box-sizing: border-box;
724
   
751
   
725
   &.dragging {
752
   &.dragging {
726
-    opacity: 0.5;
753
+    opacity: 0.6;
727
     transform: scale(1.05);
754
     transform: scale(1.05);
728
-    z-index: 100;
755
+    z-index: 999;
756
+    box-shadow: 0 8rpx 24rpx rgba(0, 0, 0, 0.2);
729
   }
757
   }
730
   
758
   
731
   &.can-drop {
759
   &.can-drop {
732
-    border: 2rpx dashed #108cff;
760
+    outline: 2rpx dashed #108cff;
761
+    outline-offset: -2rpx;
733
     background-color: rgba(16, 140, 255, 0.1);
762
     background-color: rgba(16, 140, 255, 0.1);
763
+    border-radius: 8rpx;
734
   }
764
   }
735
 }
765
 }
736
 
766
 
@@ -896,5 +926,6 @@ export default {
896
   line-height: 80rpx;
926
   line-height: 80rpx;
897
   text-align: center;
927
   text-align: center;
898
   border-radius: 20rpx;
928
   border-radius: 20rpx;
929
+  z-index: 1000;
899
 }
930
 }
900
 </style>
931
 </style>