addTmpl.vue 10.3 KB
<template>
  <div class="container">
    <div class="global-title">编辑</div>
    <div class="main">
      <div class="edit-bar">
        <FoldFrame :name="item.title" v-for="item in contentList" :key="item.key">
          <div v-if="item.type == 'img'" class="bar-content">
            <div v-if="item.key == 'electronicSealUrl'" class="input-content">
              <label class="required-field" for="">服务主体:</label>
              <el-input size="small" type="text" v-model="contentObj.serviceSubject"></el-input>
            </div>
            <Upload :required="true" @uploadSuccess="uploadSuccess" @delImage="delImage" class="upload" :name="item.label + ':'" :attr="item.key" :url="contentObj[item.key]" :defaultStyle="{ width: '208px' }"></Upload>
            <span class="upload-tip">建议高度:48px</span>
          </div>
          <div v-if="item.type == 'richText'" class="bar-content">
            <div v-if="item.key == 'contractIntroduce'" class="input-content">
              <label class="required-field" for="">合同标题:</label>
              <el-input size="small" type="text" v-model="contentObj.contractTitle"></el-input>
            </div>
            <Editor :required="true" :ref="item.key + 'Ref'" :operateList="item.key == 'useConditions' ? placeholderList : []" :label="item.label" v-model="contentObj[item.key]"></Editor>
          </div>
        </FoldFrame>
      </div>
      <div class="footer-btn-wrap">
        <el-button @click="handelCancel">取消</el-button>
        <el-button type="primary" @click="commitTemplate('preview')">预览</el-button>
        <el-button type="primary" @click="commitTemplate">提交</el-button>
      </div>
    </div>
    <pdf-dialog :isVisible="isPdfVisible" title="合同" :pdfUrl="pdfUrl" @closeDialog="handlePdfClose"></pdf-dialog>
  </div>
</template>

<script>
const errorTipObj = {
  contractTitle: '合同标题',
  contractIntroduce: '合同介绍',
  relevantDefinition: '相关定义',
  useConditions: '使用条件',
  baseContext: '使用条件',
  serviceScope: '服务范围',
  exclusionLiability: '除外责任',
  serviceData: '服务所需资料',
  specialStatement: '特别声明',
  serviceSubject: '服务主体'
}
import FoldFrame from './components/FoldFrame'
import Upload from '@/components/Upload'
import Editor from '@/components/Editor'
import pdfDialog from '@/views/beforeSale/pdfDialog'
export default {
  name: 'AddTmpl',
  components: {
    Editor,
    Upload,
    FoldFrame,
    pdfDialog
  },
  data() {
    return {
      isPdfVisible: false,
      pdfUrl: '',
      routerInfo: {},
      placeholderList: [],
      pdfObj: {
        merchantId: '',
        packageCode: ''
      },
      contentObj: {
        id: '',
        logoUrl: '',
        contractTitle: '',
        contractIntroduce: '',
        relevantDefinition: '',
        useConditions: '',
        serviceScope: '',
        exclusionLiability: '',
        serviceData: '',
        specialStatement: '',
        serviceSubject: '',
        electronicSealUrl: ''
      },
      contentList: [
        {
          title: 'Logo区域',
          label: 'Logo图',
          key: 'logoUrl',
          type: 'img'
        },
        // {
        //   label: '合同标题区域',
        //   label: '合同标题',
        //   key: 'contractTitle',
        //   type: 'input'
        // },
        {
          title: '合同介绍区域',
          label: '合同介绍:',
          key: 'contractIntroduce',
          type: 'richText'
        },
        {
          title: '相关定义区域',
          label: '相关定义:',
          key: 'relevantDefinition',
          type: 'richText'
        },
        {
          title: '使用条件区域',
          label: '使用条件:',
          key: 'useConditions',
          type: 'richText'
        },
        {
          title: '服务范围区域',
          label: '服务范围:',
          key: 'serviceScope',
          type: 'richText'
        },
        {
          title: '除外责任区域',
          label: '除外责任:',
          key: 'exclusionLiability',
          type: 'richText'
        },
        {
          title: '服务所需资料区域',
          label: '服务所需资料:',
          key: 'serviceData',
          type: 'richText'
        },
        {
          title: '特别声明区域',
          label: '特别声明:',
          key: 'specialStatement',
          type: 'richText'
        },
        {
          title: '电子章图片区域',
          label: '电子章图片',
          key: 'electronicSealUrl',
          type: 'img'
        }
      ]
    }
  },

  created() {
    this.routerInfo = JSON.parse(decodeURIComponent(this.$route.params.id))
  },

  mounted() {
    this.getContractDetail()
  },

  methods: {
    getContractDetail() {
      this.$apis.contractManager.getContractDetailApi({ id: this.routerInfo.id }).then(res => {
        if (res && res.templateConfig) {
          this.placeholderList = res.placeholderList
          this.templateType = res.templateConfig.templateType
          if (res.templateConfig.templateType == 1) {
            this.contentList = [
              {
                title: 'Logo区域',
                label: 'Logo图',
                key: 'logoUrl',
                type: 'img'
              },
              {
                title: '合同介绍区域',
                label: '合同介绍:',
                key: 'contractIntroduce',
                type: 'richText'
              },
              {
                title: '使用条件区域',
                label: '使用条件:',
                key: 'useConditions',
                type: 'richText'
              }
            ]
          }
          for (const key in this.contentObj) {
            if (key != 'useConditions') {
              this.contentObj[key] = res.templateConfig[key]
            } else {
              // 如果是自定义模板类型,使用条件的值取的是baseContext,自动化取useConditions,传参也是
              let useConditions = res.templateConfig.templateType == 1 ? res.templateConfig.baseContext : res.templateConfig.useConditions
              for (var i = 0; i < res.placeholderList.length; i++) {
                var placeholder = res.placeholderList[i]
                let targetStr = `<span data-w-e-type="text" data-key="${placeholder.key}" data-name="${placeholder.name}">${placeholder.name}</span>`
                useConditions = useConditions.replace(new RegExp(placeholder.key, 'g'), targetStr)
              }
              this.contentObj.useConditions = useConditions
            }
          }
          this.pdfObj = {
            merchantId: res.templateConfig.merchantId,
            packageCode: this.routerInfo.isParent ? res.templateConfig.parentId : res.templateConfig.packageCode
          }
        }
      })
    },
    handelCancel() {
      this.$router.push('/ContractManager/index')
    },
    getParams() {
      let params = this.$common.utils.deepClone(this.contentObj)
      let errorTip = ''
      params.useConditions = params.useConditions?.replace(/<span.*?data-w-e-type.*?data-key="(.*?)".*?<\/span>/g, '$1')
      if (this.templateType == 1) {
        params = {
          id: params.id,
          baseContext: params.useConditions,
          contractTitle: params.contractTitle,
          contractIntroduce: params.contractIntroduce,
          logoUrl: params.logoUrl
        }
      }
      for (const key in params) {
        if (key != 'id' && !params[key]) {
          if (key == 'electronicSealUrl' || key == 'logoUrl') {
            errorTip = `请上传${key == 'logoUrl' ? 'Logo图' : '电子章图片'}`
          } else {
            errorTip = `${errorTipObj[key]}不能为空`
          }
        }
      }
      if (errorTip) return { status: -1, errorTip }
      return params
    },
    commitTemplate(type) {
      const params = this.getParams()
      if (params.status == -1) return this.$message.error(params.errorTip)
      const _targetParam = {
        ...params
      }
      if (this.routerInfo.skuId) _targetParam.skuId = this.routerInfo.skuId
      this.$apis.contractManager.saveContractTemplateApi(_targetParam).then(res => {
        if (res) {
          if (type == 'preview') {
            this.handlePdf()
          } else {
            this.$message.success('编辑成功')
            this.$router.push('/ContractManager/index')
          }
        }
      })
    },
    handlePdfClose() {
      this.isPdfVisible = !this.isPdfVisible
    },
    handlePdf() {
      const loading = this.$loading({
        lock: true,
        text: 'Loading',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.3)'
      })
      const params = {
        packageCode: this.pdfObj.packageCode,
        baseFlag: this.routerInfo.isParent ? 2 : (this.routerInfo.skuId ? 1 : 0)
      }
      if (!this.routerInfo.skuId) params.merchantId = this.pdfObj.merchantId
      this.$apis.contractManager.printContractApi(params).then(res => {
        this.pdfUrl = res
        this.handlePdfClose()
        loading.close()
      })
    },
    uploadSuccess(e) {
      this.contentObj[e.attr] = e.url
    },
    delImage(e) {
      this.contentObj[e.attr] = ''
    }
  }
}
</script>

<style lang="scss" scoped>
.container {
  background-color: #fff;
  margin-bottom: 60px;
  .footer-btn-wrap {
    width: 100%;
    height: 60px;
    line-height: 60px;
    position: fixed;
    bottom: 0;
    background-color: #fff;
    text-align: center;
    left: 0;
    right: 0;
    box-shadow: 0 -2px 10px 0 #7a7a7a33;
  }
  .footer-btn {
    text-align: center;
    margin-top: 16px;
  }
  .input-content {
    display: flex;
    align-items: center;
    margin-bottom: 24px;
    label {
      flex-shrink: 0;
      width: 154px;
      white-space: nowrap;
      text-align: right;
      font-weight: 400;
      font-size: 14px;
      color: #333333;
    }
    /deep/ .el-input__inner {
      width: 342px;
    }
  }
  .main {
    padding: 10px 20px 32px;
    .edit-bar {
      .bar-content {
        background: #f9f9f9;
        padding: 20px;
        margin-bottom: 10px;
        /deep/ .editor {
          width: 935px !important;
        }
        .upload {
          display: inline-block;
        }
        .upload-tip {
          height: 20px;
          font-weight: 400;
          font-size: 14px;
          color: #999999;
          vertical-align: bottom;
          margin-left: 10px;
        }
      }
    }
  }
}
</style>