index.vue 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. <template>
  2. <view class="detail-image-content">
  3. <view class="detail-image-list">
  4. <view v-for="(item, index) in displayImages" :key="`detail-${index}`" class="detail-image-item"
  5. :class="{
  6. 'dragging': draggingIndex === index,
  7. 'can-drop': canDropIndex === index && draggingIndex !== index
  8. }" :style="draggingIndex === index ? draggingStyle : ''" @touchstart.stop="onTouchStart($event, index)"
  9. @touchmove.stop="onTouchMove($event, index)" @touchend.stop="onTouchEnd">
  10. <PicComp :src="item" @needPreviewPic="previewImageDetail" />
  11. <!-- <view class="image-type-tag">{{ getImageType(index) }}</view> -->
  12. <view class="detail-delete-btn" @click.stop="handleHideImage(item, index)" v-if="isDelete">
  13. ×
  14. </view>
  15. </view>
  16. <view class="detail-upload-btn" @click="handleUploadImage">
  17. <u-icon name="plus" size="40rpx" color="#999" />
  18. </view>
  19. </view>
  20. </view>
  21. </template>
  22. <script>
  23. import PicComp from './PicComp.vue'
  24. import imageUpload from './imageUpload.js'
  25. export default {
  26. name: 'ComponentName',
  27. components: {
  28. PicComp
  29. },
  30. data() {
  31. return {
  32. draggingIndex: -1,
  33. canDropIndex: -1,
  34. startX: 0,
  35. startY: 0,
  36. currentX: 0,
  37. currentY: 0,
  38. currentImgs: []
  39. }
  40. },
  41. watch: {
  42. images: {
  43. immediate: true,
  44. handler(newVal) {
  45. this.currentImgs = [...newVal]
  46. }
  47. }
  48. },
  49. computed: {
  50. displayImages() {
  51. return this.currentImgs
  52. },
  53. // 拖拽时的样式
  54. draggingStyle() {
  55. if (this.draggingIndex === -1) return ''
  56. return {
  57. transform: `translate(${this.currentX - this.startX}px, ${this.currentY - this.startY}px)`,
  58. zIndex: 1000
  59. }
  60. },
  61. },
  62. props: {
  63. images: {
  64. type: Array,
  65. default: () => []
  66. },
  67. isDelete: {
  68. type: Boolean,
  69. default: false
  70. },
  71. isPreview: {
  72. type: Boolean,
  73. default: false
  74. }
  75. },
  76. emits: ['imagesChanged', 'uploadComplete'],
  77. methods: {
  78. // 触摸开始
  79. onTouchStart(event, index) {
  80. if (this.draggingIndex !== -1) return
  81. this.draggingIndex = index
  82. const touch = event.touches[0]
  83. this.startX = touch.clientX
  84. this.startY = touch.clientY
  85. this.currentX = touch.clientX
  86. this.currentY = touch.clientY
  87. },
  88. // 触摸移动
  89. onTouchMove(event, index) {
  90. if (this.draggingIndex === -1 || this.draggingIndex !== index) return
  91. const touch = event.touches[0]
  92. this.currentX = touch.clientX
  93. this.currentY = touch.clientY
  94. this.findTargetIndex(touch.clientX, touch.clientY)
  95. },
  96. // 触摸结束
  97. async onTouchEnd() {
  98. if (this.draggingIndex === -1) return
  99. if (this.canDropIndex !== -1 && this.canDropIndex !== this.draggingIndex) {
  100. // 交换位置
  101. const images = [...this.currentImgs]
  102. const temp = images[this.draggingIndex]
  103. images[this.draggingIndex] = images[this.canDropIndex]
  104. images[this.canDropIndex] = temp
  105. this.currentImgs = images
  106. }
  107. this.resetDragState()
  108. //每次拖拽结束后把新的顺序传给父组件
  109. this.$emit('imagesChanged', [...this.currentImgs])
  110. },
  111. // 查找目标索引
  112. findTargetIndex(x, y) {
  113. const query = uni.createSelectorQuery().in(this)
  114. query.selectAll('.detail-image-item').boundingClientRect((rects) => {
  115. if (!rects || rects.length === 0) return
  116. let targetIndex = -1
  117. for (let i = 0; i < rects.length; i++) {
  118. if (i === this.draggingIndex) continue
  119. const rect = rects[i]
  120. if (x >= rect.left && x <= rect.right && y >= rect.top && y <= rect.bottom) {
  121. targetIndex = i
  122. break
  123. }
  124. }
  125. this.canDropIndex = targetIndex
  126. }).exec()
  127. },
  128. // 重置拖拽状态
  129. resetDragState() {
  130. this.draggingIndex = -1
  131. this.canDropIndex = -1
  132. this.startX = 0
  133. this.startY = 0
  134. this.currentX = 0
  135. this.currentY = 0
  136. },
  137. // 预览图片
  138. previewImageDetail(src) {
  139. if(!this.isPreview){
  140. return false
  141. }
  142. const urlList = this.currentImgs.map(item => item)
  143. uni.previewImage({
  144. urls: urlList,
  145. current: src
  146. })
  147. },
  148. // 隐藏图片
  149. handleHideImage(item, index) {
  150. const itemIndex = this.currentImgs.findIndex(img => item === img)
  151. if (itemIndex !== -1) {
  152. this.currentImgs.splice(itemIndex, 1)
  153. // 将更新后的图片数组传给父组件
  154. this.$emit('imagesChanged', [...this.currentImgs])
  155. uni.$u.toast('图片已隐藏')
  156. }
  157. },
  158. // 上传图片
  159. async handleUploadImage() {
  160. try {
  161. const filePaths = await imageUpload.chooseImage(9)
  162. const uploadResults = await imageUpload.uploadFiles(filePaths)
  163. uploadResults.forEach(item => {
  164. this.currentImgs.push(item.fileUrl)
  165. })
  166. this.$emit('uploadComplete', this.currentImgs)
  167. } catch (error) {
  168. console.error('上传失败:', error)
  169. }
  170. },
  171. }
  172. }
  173. </script>
  174. <style lang="scss" scoped>
  175. .detail-image-list {
  176. display: flex;
  177. flex-wrap: wrap;
  178. gap: 20rpx;
  179. position: relative;
  180. .detail-image-item {
  181. position: relative;
  182. width: 200rpx;
  183. height: 200rpx;
  184. touch-action: none;
  185. transition: transform 0.2s ease, opacity 0.2s ease;
  186. z-index: 1;
  187. box-sizing: border-box;
  188. &.dragging {
  189. opacity: 0.6;
  190. transform: scale(1.05);
  191. z-index: 999;
  192. box-shadow: 0 8rpx 24rpx rgba(0, 0, 0, 0.2);
  193. }
  194. &.can-drop {
  195. outline: 2rpx dashed #108cff;
  196. outline-offset: -2rpx;
  197. background-color: rgba(16, 140, 255, 0.1);
  198. border-radius: 8rpx;
  199. }
  200. // .image-type-tag {
  201. // position: absolute;
  202. // top: 10rpx;
  203. // left: 10rpx;
  204. // background-color: rgba(0, 0, 0, 0.6);
  205. // color: white;
  206. // padding: 5rpx 10rpx;
  207. // border-radius: 12rpx;
  208. // font-size: 22rpx;
  209. // z-index: 1;
  210. // }
  211. .detail-delete-btn {
  212. position: absolute;
  213. top: -10rpx;
  214. right: -10rpx;
  215. width: 40rpx;
  216. height: 40rpx;
  217. background-color: #ff4d4f;
  218. color: #fff;
  219. border-radius: 50%;
  220. display: flex;
  221. align-items: center;
  222. justify-content: center;
  223. font-weight: bold;
  224. z-index: 10;
  225. cursor: pointer;
  226. }
  227. }
  228. .detail-upload-btn {
  229. width: 200rpx;
  230. height: 200rpx;
  231. border: 8rpx dashed #ddd;
  232. border-radius: 30rpx;
  233. display: flex;
  234. align-items: center;
  235. justify-content: center;
  236. background-color: #f9f9f9;
  237. cursor: pointer;
  238. }
  239. }
  240. </style>