add.vue 7.02 KB
<template>
  <div
    class="layout"
    v-loading="loading"
  >
    <div class="tabs flex">
      <template v-for="(i,index) in tabs">
        <template>
          <div
            :key="index"
            :class="['tab flex',{'active':step == index + 1}]"
          >
            <template v-if="index != 0">
              <div class="line"></div>
            </template>
            <template v-if="step == 2 && index == 0">
              <div class="icon finish">
                <span class="el-icon-check"></span>
              </div>
            </template>
            <template v-else>
              <div class="icon">{{index + 1}}</div>
            </template>
            {{i}}
          </div>
        </template>
      </template>
    </div>
    <div class="content">
      <step-one
        v-show="step == 1"
        ref="stepOne"
        :disabled="disabled"
      />
      <step-two
        v-show="step == 2"
        ref="stepTwo"
        :disabled="disabled"
        :userInfoFieldList="userInfoFieldList"
      />
    </div>
    <GLFooter class="footer">
      <el-button
        @click="handleCancel"
        size="medium"
      >{{step == 1?'取消':'上一步'}}</el-button>
      <el-button
        type="primary"
        @click="handleConfirm"
        size="medium"
      >{{step == 1?'下一步': disabled ? '返回' :'提交'}}</el-button>
    </GLFooter>
  </div>
</template>

<script type='text/ecmascript-6'>
import StepOne from './components/StepOne/index.vue'
import StepTwo from './components/StepTwo/index.vue'
import { transformData } from '@/common/constant'
export default {
  components: { StepOne, StepTwo },
  name: '',
  data () {
    return {
      parsedData: [],
      step: 1,
      tabs: ['基本信息', '配置内容'],
      loading: false,
      disabled: false,
      fromId: '',
      isEdit: false,
      userInfoFieldList: []
    }
  },
  mounted () {
    const { id, type } = this.$route.query
    if (id) {
      this.loading = true
      this.fromId = id
      this.disabled = !type
      this.getQuestionnaireDetail()
      this.isEdit = type
    }
    this.getUserInfoFieldList()
  },
  methods: {
    async getQuestionnaireDetail () {
      try {
        const data = await this.$apis.questionnaire.getQuestionnaireDetail({
          id: this.fromId
        }), {
          $refs: {
            stepOne,
            stepTwo
          }
        } = this
        this.setStepOneData(stepOne, data)
        this.setStepTwoData(stepTwo, data)
        this.loading = false
      } catch (error) {
        setTimeout(() => this.$router.go(-1), 1.5e3)
      }
    },
    async getUserInfoFieldList () {
      if (this.disabled) return
      const res = await this.$apis.questionnaire.getUserInfoFieldList()
      this.userInfoFieldList = res || []
    },
    setStepOneData (stepOne, data) {
      stepOne.merchantObj.data = [{
        confId: data.merchantId,
        confName: data.merchantName
      }]
      stepOne.configForm = {
        name: data.name,
        merchantId: data.merchantId,
        cardTitle: data.cardTitle,
        cardCover: data.cardCover,
        activityId: data.activityId,
        merchantName: data.merchantName,
        activityType: data.activityType
      }
      if (data.activityType) {
        stepOne.tabsObj.index = {
          1: 0,
          2: 2,
          3: 3
        }[data.activityType]
      }
      if (!this.disabled || data.activityId) {
        stepOne.activityObj.flag = false
        stepOne.getActivityList()
      }
    },
    setStepTwoData (stepTwo, res) {
      stepTwo.configForm = {
        title: res.title,
        desc: res.desc,
        questions: res.questions,
      }
      res.questions.map(e => {
        let { type, option, maxLength } = e,
          data = void 0
        if (type != 3) {
          data = option.split('\n')
          if (type == 5) {
            const [, two, three] = data[0].split('/')
            data.splice(0, 1)
            data = transformData(data, three ? 3 : two ? 2 : 1)
          }
        } else {
          data = maxLength
          option = maxLength
        }
        stepTwo.questionsList.push({
          type,
          required: e.required,
          text: e.text,
          userInfoField: e.userInfoField,
          option,
          data,
          itemValue: type == 2 || type == 5 ? [] : ''
        })
      })
    },
    handleCancel () {
      const { step } = this
      step == 1 ? this.$router.go(-1) : this.step = 1
    },
    handleConfirm (autoJump = true) {
      const { step, isEdit, fromId, disabled, $refs: {
        stepOne,
        stepTwo
      } } = this,
        apiName = fromId ? 'updateQuestionnaire' : 'addQuestionnaire'
      if (step == 1) {
        stepOne.formRef().validate(valid => {
          if (valid) this.step = 2
        })
      } else {
        if (disabled) return this.$router.go(-1)
        stepTwo.formRef().validate(async valid => {
          if (valid) {
            this.loading = true
            try {
              const params = {
                ...stepOne.configForm,
                ...stepTwo.configForm
              }
              if (fromId) params.id = fromId
              const res = await this.$apis.questionnaire[apiName](params)
              if (res) {
                if (autoJump) {
                  this.$message({
                    message: isEdit ? '编辑成功' : '创建成功',
                    type: 'success'
                  })
                  this.$router.go(-1)
                } else {
                  this.fromId = res.id
                  this.loading = false
                }
              }
              else throw isEdit ? '编辑失败' : '创建失败'
            } catch (error) {
              this.loading = false
              if (error) this.$message.error(error)
            }
          }
        })
      }
    },


  }
}
</script>

<style scoped lang='scss'>
.layout {
  position: absolute;
  width: 100%;
  height: calc(100vh - 48px);
  top: 0;
  left: 0;
  .tabs {
    height: 64px;
    justify-content: center;
    -webkit-user-select: none;
    .tab {
      font-size: 16px;
      color: #000000a6;
      line-height: 24px;
      .icon {
        width: 32px;
        height: 32px;
        border-radius: 100%;
        border: 1px solid #c6c6c6;
        box-sizing: border-box;
        display: flex;
        align-items: center;
        justify-content: center;
        margin-right: 8px;
        font-size: 14px;
        color: #c6c6c6;
      }
      .finish {
        border-color: #5850ff !important;
        border-width: 2px;
      }
      .line {
        width: 160px;
        height: 1px;
        background: #e5e5e5;
        margin: 0 8px;
      }
    }
    .active {
      font-weight: 500;
      color: #000000d9;
      .icon {
        background: #5850ff;
        border-color: #5850ff;
        color: #fff;
      }
    }
    .el-icon-check {
      color: #5850ff;
      font-size: 18px;
      font-weight: 700;
    }
  }
  .content {
    height: calc(100% - 136px);
    background-color: #fff;
    margin: 0 24px;
    border-radius: 0 4px 4px 4px;
  }
  .footer {
    padding: 16px 0;
  }
}
</style>