wxQRcodeDialog.vue 9.55 KB
<template>
  <d2-dialog title="订单质保二维码设置"
             :is-visible="qRcodeVisible"
             class="qRcodeDailog"
             width="585px"
             @closeDialog="handleClose"
             @confirmDialog="handleAddConfirm">
    <!--  btnOkLabel="下载二维码" -->
    <div class="dialogContainer">
      <p class="merchantName">{{ merchantInfo.yqMerchantName }}</p>
      <p class="merchantId">{{ merchantInfo.yqMerchantId }}</p>
      <el-form :model="formData"
               :rules="rules"
               ref="elForm">
        <el-form-item prop="qrcodeType">
          <el-radio-group v-model="formData.qrcodeType">
            <el-radio class="radioItem"
                      :label="0">关闭二维码</el-radio>
            <el-radio class="radioItem"
                      :label="1">选择质保卡二维码链接</el-radio>
            <el-radio class="radioItem"
                      :label="2">加微二维码</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item prop="imgUrl"
                      v-if="formData.qrcodeType == 2">
          <el-upload action=""
                     class="elupload"
                     list-type="picture-card"
                     accept=".jpg,.png"
                     :auto-upload="false"
                     :show-file-list="false"
                     :disabled="!!imgUrl"
                     :on-change="e => handleChange(e,)"
                     :on-preview="e => handlePreview(e, )"
                     :on-remove="e => handleRemove(e, )">
            <span class="img-btn">
              <img v-if="imgUrl"
                   :src="imgUrl"
                   class="rectImg">
              <span class="img-btns"
                    v-if="imgUrl">
                <span class="btn-item">
                  <i @click.stop="handleRemove()"
                     class="el-icon-delete" />
                  <!-- <i @click.stop="handleImgLook"
                 class="el-icon-zoom-in"></i> -->
                </span>
              </span>
            </span>
            <i v-if="!imgUrl "
               class="el-icon-plus avatar-uploader-icon "
               style="color: #8c939d;">
              <span class="icon-text">上传加微二维码图片</span>
            </i>
            <!-- <img v-if="!imgUrl "
             :src='examplePicUrl'
             class="rectImg"> -->
            <!-- <div slot="tip"
             class="el-upload__tip">只能上传jpg/png文件,且不超过1M</div> -->
          </el-upload>
        </el-form-item>

      </el-form>

      <!-- <span v-if="txtDescript">{{txtDescript}}</span> -->
      <!-- <p v-show="!imgUrl"
         slot="errorTips"
         class="car-error-txt">请上传{{txtDescript}}</p> -->
    </div>

  </d2-dialog>
</template>

<script>
// import baseDialog from '@/components/baseDialog/baseDialog'
import { uploadImg } from '@/api/sys.base'
import { yqMerchantConfigDetail, serviceMerchantRelateQrcode } from '@/api/serviceManage'
export default {
  name: 'WxQRcodeDialog',
  components: {
    // baseDialog
  },
  props: {
    qRcodeVisible: {
      type: Boolean,
      default: false
    },
    merchantInfo: {
      type: Object
      // default: function () {
      //   return {
      //     merchantId: '44209883',
      //     merchantName: 'test'
      //   }
      // }
    }
  },
  data() {
    return {
      imgUrl: '',
      defaultImgUrl: '',
      qrcodeType: '',
      formData: {
        qrcodeType: '',
        imgUrl: ''
      },
      rules: {
        qrcodeType: [{ required: true, message: '请选择配置方式', trigger: 'change' }],
        imgUrl: [
          {
            required: true,
            trigger: 'change',
            validator: (rule, value, callback) => {
              console.log(value, 'value')
              this.formData.qrcodeType === 2 ? (value ? callback() : callback(new Error('请上传加微二维码'))) : callback()
            }
          }]
      }
      // oldImgUrl: ''
      // dialogConfig: {
      //   setting: {
      //     showClose: true,
      //     appendToBody: false,
      //     // width: '720px',
      //     btnList: [
      //     ]
      //   },
      //   config: {
      //     title: '上传加微二维码',
      //     width: '720px'
      //   }

      // }
    }
  },
  watch: {
    imgUrl(val) {
      this.formData.imgUrl = val
      // if (val) {
      //   // this.yqMerchantConfigDetail()
      // }
    }
  },
  mounted() {
    this.yqMerchantConfigDetail()
  },
  methods: {
    yqMerchantConfigDetail() {
      // let parmas = this.merchantInfo.merchantId
      yqMerchantConfigDetail({ yqMerchantId: this.merchantInfo.yqMerchantId }).then((result) => {
        console.log(result)
        const { workWechatQrcode, qrcodeType } = result
        this.imgUrl = workWechatQrcode
        this.defaultImgUrl = workWechatQrcode
        this.formData.qrcodeType = qrcodeType
      }).catch((err) => {
        console.log(err)
      })
    },
    handleClose() {
      this.$emit('update:qRcodeVisible', false)
    },
    async handleAddConfirm() {
      this.$refs.elForm.validate(async (valid) => {
        console.log(valid)
        // return
        if (valid) {
          const params = {
            yqMerchantId: this.merchantInfo.yqMerchantId,
            ...this.formData
          }
          const result = await serviceMerchantRelateQrcode(params)
          console.log(result)
          this.notice.okTips()
          this.handleClose()
        }
      })
    },
    handleChange(file, attrName, targetItem) {
      if (!(file.size / 1024 / 1024 < 1)) {
        this.$message.error('请上传小于1M的图片')
        return false
      }
      this.handleImgChange({ file })
      // this.$emit('handleImgChange', { file: file, attrName: attrName, targetItem: targetItem })
    },
    handlePreview() { },
    handleRemove(attrName, targetItem) {
      // this.$emit('handleImgRemove', { attrName: attrName, targetItem: targetItem })
      this.imgUrl = ''
    },
    handleImgChange({ file, attrName, targetItem }) {
      // var hasFnList = ['idCard', 'idCardBack', 'bankCard', 'businessLicense', 'authorizerIdCard']
      console.log(file)
      var reader = new FileReader()
      var fileInfo = file.raw
      reader.readAsDataURL(fileInfo)
      reader.onload = e => {
        const imgFile = e.target.result
        // const base64 = fileInfo.name.split('.')[1] + '@' + imgFile.split(',')[1]
        const base64 = fileInfo.name.split('.')[fileInfo.name.split('.').length - 1] + '@' + imgFile.split(',')[1]
        uploadImg({ file: base64 }).then(res => {
          console.log(res)
          // http://qcimg01.glsx.com.cn:7060/group1/M00/02/25/wKgD3mMMeE2AIylRAADPUBbxKwQ414.png
          this.imgUrl = res
          // this.picObj[attrName].url = res
          // this.editForm.papers[this.picObj[attrName].attrName] = this.picObj[attrName].url
          // this.writeToEditForm(attrName)
          // this.$refs.editFormRef.$refs.editForm.clearValidate(targetItem.name)
          // if (hasFnList.includes(attrName)) {
          //   targetItem.autoRecognitionFn(res, attrName)
          // }
        })
      }
    },
    handleImgRemove({ attrName, targetItem }) {
      this.picObj[attrName].url = ''
      delete this.editForm.papers[this.picObj[attrName].attrName]
      // this.writeToEditForm(attrName, false)
      // this.$refs.editFormRef.$refs.editForm.validateField(targetItem.name)
    }
  }
}
</script>

<style lang="scss" scoped>
::v-deep.qRcodeDailog {
  // color: rebeccapurple;
  // .el-dialog__header {
  //   border: none;
  // }
  .el-icon-plus {
    // position: relative;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
  }
  .icon-text {
    position: absolute;
    white-space: nowrap;
    top: 28px;
    left: -48px;
    margin-top: 8px;
    font-size: 14px;
  }
}
.dialogContainer {
  box-sizing: border-box;
  width: 300px;
  // height: 290px;
  margin: auto;
  padding: 16px 24px;
  display: flex;
  flex-direction: column;
  // align-items: center;
  // justify-content: center;
  text-align: left;

  border: 1px solid #e8e8e8;
  p {
    margin: 0;
  }
  .radioItem {
    margin-top: 10px;
  }
  .merchantName {
    font-weight: 600;
    margin-bottom: 10px;
  }
  .elupload {
    margin: 10px auto 0;
    position: relative;
    display: flex;
    justify-content: center;
  }

  .disabled .el-upload--picture-card {
    display: none;
  }

  .rectImg {
    width: 100%;
    height: 100%;
    object-fit: contain;
    // overflow: ;
    // width: 137px;
    // height: 90px;
  }

  .img-btn {
    display: inline-block;
    position: relative;
    width: 100%;
    height: 100%;
    overflow: hidden;
  }
  .img-btn .img-btns {
    display: none;
    position: absolute;
    width: 146px;
    height: 146px;
    left: 0;
    top: 0;
    z-index: 2;
    color: #fff;
    background: rgba(0, 0, 0, 0.5);
  }
  .img-btn .img-btns .btn-item {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
  }

  .img-btn .img-btns .btn-item {
    display: flex;
    width: 70%;
    justify-content: space-around;
  }

  .img-btn:hover .img-btns {
    display: block;
  }
  .el-upload--picture-card i {
    color: #fff;
  }
}

// .gl-upload-img .car-error-txt {
//   font-size: 12px;
//   line-height: 6px;
//   margin: 0 0;
// }
</style>