poster.vue 9.96 KB
<template>
  <div class="channel-main scrm-box-f" v-loading="loading">
    <div class="poster-body">
      <div class="poster-item" @click="setPoster(item)" @mouseenter="item.show = true" @mouseleave="item.show = false" v-for="(item, index) in posterList" :key="index">
        <div class="poster-del" v-show="item.show" @click.stop="handleDelete(item.id)">
          <img src="@/assets/img/del.png" />
        </div>
        <div class="poster-item-box" v-loading="item.loadingImg">
          <img
            :src="item.thumbnailUrl ? item.thumbnailUrl : item.autoUrl"
            class="poster-image"
            v-if="item.imgWidth"
            :style="{
              width: item.imgWidth + 'px',
              height: item.imgHeight + 'px'
            }"
          />
        </div>
        <div class="poster-item-link" @click.stop="downloadIamge(item.autoUrl)">下载海报</div>
      </div>
      <div class="poster-item">
        <div class="poster-item-box">
          <div>
            <div class="poster-upload-image">
              <img src="@/assets/img/channel/upload.png" />
              <div class="poster-tips">上传海报图片,将自动配上渠道活码</div>
            </div>
            <div class="image-uploader">
              <div class="poster-item-link">上传海报</div>
              <input type="file" class="image-uploader" style="opacity: 0" ref="file" accept="*" @change="upLoadImg" />
            </div>
          </div>
        </div>
      </div>
    </div>
    <el-dialog title="创建活码海报" :visible.sync="dialogVisible">
      <div class="poster-draggable-title">拖动活码可调整活码位置</div>
      <div class="poster-draggable" v-loading="loadingDrag">
        <div
          class="poster-image"
          v-if="dragHeight"
          :style="{
            'background-image': 'url(' + backUrl + ')',
            height: dragHeight + 'px'
          }"
        >
          <vue-draggable-resizable
            :w="width"
            :h="height"
            :x="x"
            :y="y"
            :min-width="50"
            :min-height="50"
            class="img_cl"
            :style="{
              'background-image': 'url(' + codeUrl + ')'
            }"
            parent=".poster-image"
            @dragging="onDrag"
            @resizing="onResize"
            :customId="index"
            @activated="onActivated"
          ></vue-draggable-resizable>
        </div>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button size="small" @click="dialogVisible = false">取 消</el-button>
        <el-button size="small" type="primary" @click="savePoster">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import util from '@/utils/util'
import VueDraggableResizable from '@/components/draggable/vue-draggable-resizable'
import '@/components/draggable/vue-draggable-resizable.css'
export default {
  name: 'poster',
  components: {
    VueDraggableResizable
  },

  data() {
    return {
      loading: false,
      loadingDrag: false,
      dialogVisible: false,
      posterList: [],
      img_url: '',
      dragHeight: 0,
      imgWidth: 0,
      imgHeight: 0,
      x: 0,
      y: 0,
      width: 50,
      height: 50,
      backHeight: 0,
      backWidth: 0,
      backUrl: '',
      codeUrl: ''
    }
  },

  methods: {
    downloadIamge(imgsrc) {
      util.downloadIamge(imgsrc)
    },
    goBack() {
      this.$router.push('/channel/index')
    },
    handleDelete(id) {
      this.$confirm('是否删除?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$apis.channel.deleteChannelCodeBill({ id }).then(res => {
          if (res) this.getPoster()
        })
      })
    },
    setPoster(item) {
      this.dragHeight = 0
      this.id = item.id
      this.x = item.x
      this.y = item.y
      this.width = item.width
      this.height = item.height
      this.backUrl = item.backUrl
      this.getDraggable(this.backUrl)
    },
    enterPoster() {},
    leavePoster() {},
    upLoadImg(e) {
      const file = e.srcElement.files[0]
      let fileName = file.name
      if (file.name.lastIndexOf('.') === -1) {
        this.$message.info('路径不正确')
        return false
      }
      let AllImgExt = '.jpg|.jpeg|.gif|.bmp|.png|.swf'
      let extName = file.name.substring(file.name.lastIndexOf('.')).toLowerCase()
      if (AllImgExt.indexOf(extName + '|') === -1) {
        this.$message.info('图片格式不正确')
        return false
      }
      if (file.size / 1024 / 1000 > 10) {
        this.$message.info('请上传小于10M的图片')
        return
      }
      this.loading = true
      if (window.FileReader) {
        let reader = new FileReader()
        let self = this
        reader.readAsDataURL(file)
        reader.onload = img => {
          console.log(file)
          let type = fileName.split('.')[1]
          let imgUrl = img.target.result.split(',')[1]
          // self.loadingImg = true;
          self.$apis.common
            .pictureUpload({
              file: `${type}@${imgUrl}`
            })
            .then(res => {
              if (res) {
                self.id = null
                self.dragHeight = 0
                self.x = 0
                self.y = 0
                self.width = 100
                self.height = 100
                self.backUrl = res
                self.getDraggable(res)
              } else {
                self.loading = false
              }
              self.$refs.file.value = ''
            })
        }
      }
    },
    onDrag(x, y) {
      this.x = x
      this.y = y
    },
    onActivated(index) {
      console.log(index)
    },
    onResize(x, y, width, height) {
      this.x = x
      this.y = y
      this.width = width
      this.height = height
    },
    savePoster() {
      this.loadingDrag = true
      let { x, y, width, height, backUrl, codeUrl, id, dragHeight } = this
      let params = {
        x,
        y,
        id,
        width,
        height,
        backUrl,
        codeUrl,
        backWidth: 375,
        backHeight: parseInt(dragHeight)
      }
      let code
      if (id) {
        code = 'updateChannelCodeBill'
      } else {
        code = 'saveChannelCodeBill'
      }
      this.$apis.channel[code](params).then(res => {
        if (res) {
          this.dialogVisible = false
          this.getPoster()
        }
      })
    },
    getPoster() {
      let { id } = this.$route.params
      this.$apis.channel.getSubCodeChannelCodeBill({ codeId: id }).then(res => {
        let listChannelCodeBill = res.channelCodeBillInfo
        this.codeUrl = res.qrCode
        listChannelCodeBill.forEach(item => {
          item.show = false
          item.loadingImg = true
          let backUrl = item.thumbnailUrl ? item.thumbnailUrl : item.autoUrl
          this.backUrl = backUrl
          let img = new Image()
          img.src = backUrl
          this.loadingImg = true
          this.loadingDrag = false
          img.onload = function() {
            if (img.width > img.height) {
              item.imgWidth = 280
              item.imgHeight = (img.height * 280) / img.width
            } else {
              item.imgHeight = 280
              item.imgWidth = (img.width * 280) / img.height
            }
            item.loadingImg = false
          }
        })
        this.posterList = listChannelCodeBill
      })
    },
    getDraggable(img_url) {
      let img = new Image()
      img.src = img_url
      img.onload = () => {
        this.dragHeight = (img.height * 375) / img.width
        this.dialogVisible = true
        this.loading = false
      }
    }
  },

  mounted() {
    this.getPoster()
  }
}
</script>

<style lang="scss" scoped>
.channel-main {
  padding: 10px 20px;
  min-height: calc(100% - 30px);
}
.image-uploader {
  position: relative;
  cursor: pointer;
  .poster-item-link {
    position: absolute;
    top: 0px;
    left: 0px;
    width: 100%;
    margin: 0 auto !important;
    text-align: center;
  }
  /deep/ {
    .el-upload-list {
      display: none;
    }
  }
}
.poster-draggable-title {
  font-size: 14px;
  font-weight: 400;
  color: #5850ff;
  line-height: 20px;
  text-align: center;
  margin-bottom: 10px;
}
.poster-draggable {
  display: flex;
  justify-content: center;
  // height: 500px;
  // overflow: auto;
  .poster-image {
    background-size: 100% 100%;
    width: 375px;
    .img_cl {
      background-size: 100% 100%;
    }
  }
}
.poster-body {
  display: flex;
  flex-wrap: wrap;
  margin-top: 20px;
  height: calc(100% -200px);
  .poster-item {
    margin: 0px 15px 20px 0px;
    position: relative;
    overflow: hidden;
    .poster-del {
      transition: all 1s ease;
      position: absolute;
      right: -52px;
      top: -52px;
      width: 104px;
      height: 104px;
      border-radius: 50%;
      background: rgba($color: #5850ff, $alpha: 0.15);
      text-align: center;
      line-height: 104px;
      img {
        width: 20px;
        height: auto;
        margin-top: 60px;
        margin-right: 40px;
        cursor: pointer;
      }
    }
    &-box {
      width: 300px;
      height: 300px;
      background: #f7f7f7;
      border-radius: 8px;
      display: flex;
      justify-content: center;
      align-items: center;
      text-align: center;
      .poster-upload-image {
        text-align: center;
        font-size: 14px;
        font-weight: 400;
        color: #999999;
        line-height: 20px;
        margin-bottom: 20px;
        img {
          width: 90px;
          height: auto;
        }
        .poster-tips {
          margin: 10px 0px 20px;
        }
      }
      .poster-image {
        background-size: 100% 100%;
        width: 300px;
        height: 300px;
        .img_cl {
          background-image: url('../../assets/img/qrcode.png');
          background-size: 100% 100%;
        }
      }
    }
    &-link {
      cursor: pointer;
      margin-top: 10px;
      text-align: center;
      font-size: 14px;
      font-weight: 500;
      color: #5850ff;
      line-height: 20px;
    }
  }
}
</style>