addCode.vue 11.2 KB
<template>
  <div class="scrm-main">
    <div class="channel-main scrm-box-f">
      <div class="chanel-form-title">基本信息</div>
      <div class="chanel-form-box">
        <el-form ref="codeForm" :model="addCodeForm" class="code-body-form" label-width="120px" :rules="codeRules">
          <el-row>
            <el-form-item label="渠道名称" prop="codeName">
              <el-input size="small" v-model.trim="addCodeForm.codeName" placeholder="请输入渠道名称" maxlength="30"></el-input>
            </el-form-item>
          </el-row>
          <el-row>
            <el-form-item label="备注">
              <el-input size="small" v-model.trim="addCodeForm.codeRemark" placeholder="请输入备注" maxlength="30"></el-input>
              <div class="form-tips">设置备注,便于后续管理查找</div>
            </el-form-item>
          </el-row>
          <el-row>
            <el-form-item label="自动通过验证">
              <el-switch v-model="addCodeForm.autoAdd" active-color="#5850FF" inactive-color="#999999"></el-switch>
              <span class="form-tips-btn">开启后,客户添加使用人员无需好友验证</span>
            </el-form-item>
          </el-row>
          <el-row>
            <el-form-item label="选择标签">
              <el-button size="small" @click="handleAddTag">
                <i class="el-icon-plus"></i>
                选择标签
              </el-button>
              <span class="form-tips-btn">
                已选择
                <span>{{ taggingLength }}</span>
                个标签
              </span>
              <div class="el-input-tag input-tag-wrapper" v-show="taggingLength">
                <template v-if="tagging && tagging.length > 0">
                  <template v-for="(item,index) in tagging">
                    <el-tag type="info" v-for="(tag, idx) in item.labelList" :key="idx" closable :disable-transitions="false" @close="removeTag(item, idx,index)">
                      {{ tag ? tag.name : '' }}
                    </el-tag>
                  </template>
                </template>
              </div>
            </el-form-item>
          </el-row>
          <el-row>
            <div class="el-form-item is-required">
              <label for="codeName" class="el-form-item__label" style="width: 120px">选择专员</label>
              <div class="el-form-item__content" style="margin-left: 120px">
                <el-button size="small" @click="handleAddEmp">
                  <i class="el-icon-plus"></i>
                  选择专员
                </el-button>
                <span class="form-tips-btn">
                  已选择
                  <span>{{ staffInfo.length }}</span>
                  人
                </span>
                <div class="el-input-tag input-tag-wrapper" v-show="staffInfo.length">
                  <el-tag effect="plain" type="info" v-for="(tag, idx) in staffInfo" :key="idx" closable :disable-transitions="false" @close="removeStaff(idx)">
                    <i class="el-icon-user-solid"></i>
                    {{ tag.staffName }}
                  </el-tag>
                </div>
                <div class="el-form-item__error" v-if="staffStatus">请选择专员</div>
              </div>
            </div>
          </el-row>
        </el-form>
      </div>
    </div>
    <div class="channel-main-welcome scrm-box-f">
      <div class="chanel-form-title">欢迎语</div>
      <div class="welcome-panel">
        <div class="welcome-switch">
          <label>自动发送欢迎语</label>
          <el-switch v-model="autoWelcome" active-color="#5850FF" inactive-color="#999999"></el-switch>
        </div>
        <div class="welcome-box" v-show="autoWelcome">
          <welcome-words :eventBus="eventBus" ref="welcome"></welcome-words>
        </div>
      </div>
    </div>
    <div
      class="footer-panel"
      :style="{
        left: footerLeft + 'px',
        width: footerWidth + 'px'
      }"
    >
      <el-button type="primary" @click="handleSave">提交</el-button>
      <el-button @click="goback">取消</el-button>
    </div>
    <select-emp @selectTable="selectTable" ref="selectEmpForm"></select-emp>
    <select-tag ref="selectTagForm" @tagSelected="tagSelected"></select-tag>
  </div>
</template>

<script>
import { utils } from 'glsx-common'
import Vue from 'vue'
import selectEmp from '@/components/selectEmp'
import selectTag from '@/components/selectTag'
import welcomeWords from '@/components/welcome/welcomeWords.vue'
export default {
  name: 'addCode',
  components: { welcomeWords, selectEmp, selectTag },
  data() {
    return {
      eventBus: new Vue(),
      addCodeForm: {
        codeName: '',
        codeRemark: '',
        autoAdd: true
      },
      staffStatus: false,
      staffInfo: [],
      tagging: [],
      taggingLength: 0,
      codeId: null,
      footerLeft: 0,
      footerWidth: 0,
      autoWelcome: true,
      codeRules: {
        codeName: [{ required: true, message: '请输入渠道名称', trigger: 'blur' }]
      }
    }
  },
  watch: {
    tagging: {
      handler() {
        this.taggingLength = 0
        this.tagging.forEach(item => {
          this.taggingLength += item.labelList.length
        })
      },
      deep: true
    }
  },
  methods: {
    goback() {
      this.$router.push('/channel/index')
    },
    removeTag(item, idx,index) {
      item.labelList.splice(idx, 1)
      if(item.labelList.length == 0) {
        this.tagging.splice(index,1)
      }
    },
    removeStaff(index) {
      this.staffInfo.splice(index, 1)
    },
    handleAddEmp() {
      this.staffStatus = false
      this.$refs.selectEmpForm.init(this.staffInfo)
    },
    handleAddTag() {
      this.$refs.selectTagForm.init(this.tagging)
    },
    selectTable(item) {
      this.staffInfo = item.map(item => {
        return {
          staffId: item.relationId,
          staffName: item.staffName
        }
      })
    },
    tagSelected(item) {
      this.tagging = item
    },
    handleSave: utils.debounce(function () {
      if (this.staffInfo.length == 0) {
        this.staffStatus = true
        return
      }
      this.$refs['codeForm'].validate(valid => {
        if (valid) {
          let param = Object.assign({ id: this.codeId }, this.addCodeForm)
          let tagging = this.tagging.map(item => {
            let tagIds = item.labelList.map(tag => {
              return tag.id
            })
            let name = item.labelList.map(tag => {
              return tag.name
            })
            return {
              groupId: item.labelGroup.id,
              tagIds: tagIds.join(','),
              name: name.join(','),
              labelType: item.labelGroup.name
            }
          })
          param.tagging = JSON.stringify(tagging)
          param.autoWelcome = this.autoWelcome ? 1 : 0
          param.codeSystem = 0
          let staffId = []
          this.staffInfo.forEach(item => {
            staffId.push(item.staffId)
          })
          let channel
          if (this.codeId) {
            channel = 'updateChannelCode'
          } else {
            channel = 'saveChannelCode'
          }
          delete param.staffInfo
          param.staffId = staffId.join(',')
          param.autoAdd ? (param.autoAdd = 1) : (param.autoAdd = 0)
          const previewList = this.$common ? this.$common.utils.deepClone(this.$refs.welcome.previewList) : this.$refs.welcome.previewList
          const attachmentList = previewList.map(item => {
            if (item.attType == 1) {
              item.attContent = item.attContent.replaceAll('[@昵称]', '@昵称')
              item.attContent = item.attContent.replaceAll('@昵称', '[@昵称]')
            } else if (item.attType == 7) {
              item.attContent = JSON.stringify(item.attContent)
            }
            return item
          })
          param.welcomeInfo = attachmentList
          this.$apis.channel[channel](param).then(res => {
            if (res) this.$router.push('/channel/index')
          })
        }
      })
    }, 300),
    detailChannelCode(id, codeUuid) {
      let param = {
        id,
        codeUuid
      }
      this.$apis.channel.detailChannelCode(param).then(res => {
        this.addCodeForm = Object.assign({}, res)
        this.addCodeForm.autoAdd = this.addCodeForm.autoAdd == 1 ? true : false
        this.staffInfo = res.staffInfo
        const tagging = eval(res.tagging)
        const taggingList = tagging.map(item => {
          let name = item.name.split(',')
          let ids = item.tagIds.split(',')
          console.log(ids)
          let labelList = name.map((tag, index) => {
            return { id: ids[index], name: tag }
          })
          return {
            labelGroup: {
              id: item.groupId,
              name: item.labelType
            },
            labelList
          }
        })
        this.tagging = taggingList
        this.autoWelcome = res.autoWelcome ? true : false
        const previewList = res.welcomeInfo.map(item => {
          if (item.attType == 7) item.attContent = JSON.parse(item.attContent)
          return item
        })
        this.$refs.welcome.updateList(previewList)
      })
    }
  },
  mounted() {
    const { id, codeUuid } = this.$route.params
    if (id != -1) {
      this.codeId = id
      this.detailChannelCode(id, codeUuid)
    }
    this.footerLeft = document.querySelector('.base-container-right').offsetLeft
    this.footerWidth = document.documentElement.clientWidth - this.footerLeft
  }
}
</script> 

<style lang="scss" scoped>
.channel-main-welcome {
  margin: 20px 0px 60px;
  .welcome-panel {
    margin: 10px;
    padding: 0px 20px 20px;
    .welcome-switch {
      margin-bottom: 20px;
      label {
        margin-right: 10px;
        font-size: 14px;
        font-weight: 400;
        color: #666666;
      }
    }
    .welcome-box {
      margin-left: 48px;
    }
  }
}
.footer-panel {
  display: flex;
  align-items: center;
  justify-content: center;
  position: fixed;
  bottom: 0px;
  height: 64px;
  background: #ffffff;
  box-shadow: 0px -1px 1px 0px rgba(88, 80, 255, 0.1);
}
.chanel-form-title {
  padding: 20px;
  font-size: 16px;
  font-weight: 600;
  color: #333333;
  line-height: 16px;
  border-bottom: 1px solid #d8d8d8;
}
.code-body-form {
  padding: 20px;
  width: 60%;
  .form-tips {
    font-size: 14px;
    font-weight: 400;
    color: #999999;
    line-height: 20px;
  }
  .form-tips-btn {
    font-size: 14px;
    font-weight: 400;
    color: #999999;
    line-height: 20px;
    margin-left: 10px;
    > span {
      color: #5850ff;
    }
  }
  .input-tag-wrapper {
    position: relative;
    font-size: 14px;
    background-color: #fff;
    background-image: none;
    border-radius: 4px;
    border: 1px solid #dcdfe6;
    box-sizing: border-box;
    color: #666;
    display: inline-block;
    outline: none;
    padding: 5px 5px 0px 5px;
    max-height: 68px;
    overflow: auto;
    flex-wrap: wrap;
    transition: border-color 0.2s cubic-bezier(0.645, 0.045, 0.355, 1);
    width: 450px;
    display: flex;
    align-items: center;
    margin-top: 6px;
    font-size: 14px;
    i {
      color: #75acff;
      margin-right: 3px;
      font-size: 14px;
    }
  }
  .el-tag {
    margin-right: 4px;
    height: 26px;
    line-height: 24px;
    margin-bottom: 5px;
  }
}
</style>