contractDialog.vue 8.53 KB
<template>
  <!-- 合同内容弹窗 -->
  <div>
    <div
      v-if="isVisible"
      class="pdf-preview-container"
      :style="containerStyle"
      @mousedown="startDrag"
      @touchstart.passive="startDrag"
    >
      <div class="pdf-header" >
        <span>{{ title }}</span>
        <button class="close-btn" @click="close">×</button>
      </div>
      
      <div class="pdf-content">
        <iframe 
          ref="pdfIframe"
          :src="pdfUrl" 
          frameborder="0"
          :style="iframeStyle"
        ></iframe>
      </div>
      
      <!-- 八个方向的缩放控制点 -->
      <div class="resize-handle n" @mousedown="startResize($event, 'n')"></div>
      <div class="resize-handle e" @mousedown="startResize($event, 'e')"></div>
      <div class="resize-handle s" @mousedown="startResize($event, 's')"></div>
      <div class="resize-handle w" @mousedown="startResize($event, 'w')"></div>
      <div class="resize-handle ne" @mousedown="startResize($event, 'ne')"></div>
      <div class="resize-handle nw" @mousedown="startResize($event, 'nw')"></div>
      <div class="resize-handle se" @mousedown="startResize($event, 'se')"></div>
      <div class="resize-handle sw" @mousedown="startResize($event, 'sw')"></div>
    </div>
    <!-- 功能入口 -->
    <div class="point-btn" @click.capture="handleShow">
      <img src="../../../assets/view-icon.png" alt="">
      <div class="contact_text">查看合同</div>
    </div>
  </div>
</template>

<script>
export default {
  props: {
    pdfUrl: {
      type: String,
      required: true
    },
    title: {
      type: String,
      default: '合同预览'
    },
    initialWidth: {
      type: Number,
      default: 400
    },
    initialHeight: {
      type: Number,
      default: 500
    },
    initialX: {
      type: Number,
      default: 800
    },
    initialY: {
      type: Number,
      default: 100
    },
    isVisible: {
      type: Boolean,
      default: false
    },
  },
  watch: {
    isVisible: {
      handler(n) {
        this.size.width = 400
        this.size.height = 500
        this.position.x = 800
        this.position.y = 100
      }
    }
  },
  data() {
    return {
      position: {
        x: this.initialX,
        y: this.initialY
      },
      size: {
        width: this.initialWidth,
        height: this.initialHeight
      },
      isDragging: false,
      isResizing: false,
      startPos: { x: 0, y: 0 },
      resizeDirection: null,
      startSize: { width: 0, height: 0 }
    }
  },
  computed: {
    containerStyle() {
      return {
        left: `${this.position.x}px`,
        top: `${this.position.y}px`,
        width: `${this.size.width}px`,
        height: `${this.size.height}px`,
        transform: this.isDragging ? 'translateZ(0)' : 'none' // 提升拖拽性能
      }
    },
    iframeStyle() {
      return {
        width: '100%',
        height: 'calc(100% - 40px)',
        'pointer-events': 'auto'
      }
    }
  },
  methods: {
    handleShow() {
      this.$emit('handleContractPdf', this.isVisible)
    },
    startDrag(e) {
      // 忽略缩放控制点的点击
      if (e.target.classList.contains('resize-handle')) return
      
      const clientX = e.clientX || e.touches[0].clientX
      const clientY = e.clientY || e.touches[0].clientY
      
      this.isDragging = true
      this.startPos = {
        x: clientX - this.position.x,
        y: clientY - this.position.y
      }
      
      this.addEventListeners()
      e.preventDefault()
    },
    startResize(e, direction) {
      const clientX = e.clientX || e.touches[0].clientX
      const clientY = e.clientY || e.touches[0].clientY
      
      this.isResizing = true
      this.resizeDirection = direction
      this.startPos = { x: clientX, y: clientY }
      this.startSize = { ...this.size }
      this.startPosition = { ...this.position }

      // 禁用iframe指针事件
      this.$refs.pdfIframe.style.pointerEvents = 'none'
      
      this.addEventListeners()
      e.stopPropagation()
      e.preventDefault()
    },
    addEventListeners() {
      document.addEventListener('mousemove', this.handleMove)
      document.addEventListener('touchmove', this.handleMove, { passive: false })
      document.addEventListener('mouseup', this.stopMove)
      document.addEventListener('touchend', this.stopMove)
    },
    handleMove(e) {
      const clientX = e.clientX || e.touches[0].clientX
      const clientY = e.clientY || e.touches[0].clientY
      
      if (this.isDragging) {
        this.position = {
          x: clientX - this.startPos.x,
          y: clientY - this.startPos.y
        }
      } else if (this.isResizing) {
        this.handleResize(clientX, clientY)
      }
      
      e.preventDefault()
    },
    handleResize(clientX, clientY) {
      const deltaX = clientX - this.startPos.x
      const deltaY = clientY - this.startPos.y
      
      const newSize = { ...this.startSize }
      const newPosition = { ...this.startPosition }
      
      // 根据方向调整大小和位置
      if (this.resizeDirection.includes('e')) {
        newSize.width = Math.max(300, this.startSize.width + deltaX)
      }
      if (this.resizeDirection.includes('w')) {
        const newWidth = Math.max(300, this.startSize.width - deltaX)
        newPosition.x = this.startPosition.x + (this.startSize.width - newWidth)
        newSize.width = newWidth
      }
      if (this.resizeDirection.includes('s')) {
        newSize.height = Math.max(200, this.startSize.height + deltaY)
      }
      if (this.resizeDirection.includes('n')) {
        const newHeight = Math.max(200, this.startSize.height - deltaY)
        newPosition.y = this.startPosition.y + (this.startSize.height - newHeight)
        newSize.height = newHeight
      }
      
      this.size = newSize
      this.position = newPosition
    },
    stopMove() {
      this.isDragging = false
      this.isResizing = false
      this.resizeDirection = null
      if (this.$refs.pdfIframe) {
        this.$refs.pdfIframe.style.pointerEvents = 'auto'
      }
      document.removeEventListener('mousemove', this.handleMove)
      document.removeEventListener('touchmove', this.handleMove)
      document.removeEventListener('mouseup', this.stopMove)
      document.removeEventListener('touchend', this.stopMove)
    },
    close() {
      this.$emit('closeDialog')
    }
  }
}
</script>

<style scoped lang="scss">
.pdf-preview-container {
  position: fixed;
  z-index: 9999;
  background: white;
  border: 1px solid #ccc;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.2);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  user-select: none;
  touch-action: none;
  .pdf-header {
    padding: 10px 15px;
    background: #f5f5f5;
    border-bottom: 1px solid #ddd;
    cursor: move;
    display: flex;
    justify-content: space-between;
    align-items: center;
    .close-btn {
      background: none;
      border: none;
      font-size: 25px;
      cursor: pointer;
      padding: 0 5px;
      line-height: 1;
    }
    .close-btn:hover {
      color: #5850ff;
    }
  }
  .pdf-content {
    flex: 1;
    cursor: move;
    overflow: hidden;
  }
  /* 缩放控制点样式 */
  .resize-handle {
    position: absolute;
    background: transparent;
    z-index: 10;
  }

  .resize-handle.n {
    top: 0;
    left: 10px;
    right: 10px;
    height: 10px;
    cursor: ns-resize;
  }

  .resize-handle.e {
    top: 10px;
    right: 0;
    bottom: 10px;
    width: 10px;
    cursor: ew-resize;
  }

  .resize-handle.s {
    bottom: 0;
    left: 10px;
    right: 10px;
    height: 10px;
    cursor: ns-resize;
  }

  .resize-handle.w {
    top: 10px;
    left: 0;
    bottom: 10px;
    width: 10px;
    cursor: ew-resize;
  }

  .resize-handle.ne {
    top: 0;
    right: 0;
    width: 15px;
    height: 15px;
    cursor: ne-resize;
  }

  .resize-handle.nw {
    top: 0;
    left: 0;
    width: 15px;
    height: 15px;
    cursor: nw-resize;
  }

  .resize-handle.se {
    bottom: 0;
    right: 0;
    width: 15px;
    height: 15px;
    cursor: se-resize;
  }

  .resize-handle.sw {
    bottom: 0;
    left: 0;
    width: 15px;
    height: 15px;
    cursor: sw-resize;
  }
}
.point-btn{
  position: fixed;
  right: 20px;
  bottom: 410px;
  cursor: pointer;
  display: flex;
  flex-flow: column;
  align-items: center;
  justify-content: center;
  width: 68px;
  height: 78px;
  background-image: linear-gradient(178deg, #F3F3FF 0%, #FFFFFF 100%);
  border: 1px solid #FFFFFF;
  box-shadow: 0 4px 12px 0 #a3a6cc59;
  border-radius: 4px;
  img{
    width: 24px;
    height: 24px;
    margin-bottom: 6px;
  }
  .contact_text {
    font-size: 12px;
    font-weight: 500;
    color: #5850FF;
  }
}
</style>