index.vue 10.3 KB
<template>
  <el-scrollbar
    class="step-box"
    ref="stepBox"
    v-loading="activityObj.loading"
  >
    <div class="step-content">
      <glsx-combi-form
        ref="formRef"
        :isEdit="true"
        :isBlock="true"
        :formObj="configForm"
        :formData="formData"
        :formRules="rule"
      >
        <template #merchantId>
          <el-select
            v-model="configForm.merchantId"
            placeholder="请选择"
            filterable
            remote
            :remote-method="remoteMethod"
            v-reachbottom="onReachBottom"
            @focus="selectFocus"
            :loading="loading"
          >
            <el-option
              v-for="item in merchantObj.data"
              :key="item.confId"
              :label="item.confName"
              :value="item.confId"
            ></el-option>
            <template v-if="merchantObj.nothing">
              <div class="select-nothing">到底了!</div>
            </template>
          </el-select>
        </template>
        <template #activity>
          <div class="tabs flex">
            <template v-for="(i,index) in tabsObj.data">
              <template>
                <el-button
                  type="text"
                  @click="changeActivity(index)"
                  :class="['tab',{active:index == tabsObj.index}]"
                  :key="index"
                  :disabled="index == 1"
                >{{i}}</el-button>
              </template>
            </template>
          </div>
          <div
            class="activity-box"
            :key="activityObj.key"
          >
            <template v-if="activityObj.flag">
              <template v-if="activityObj.data.length">
                <template v-for="(i,index) in activityObj.data">
                  <template>
                    <component
                      :key="index"
                      :is="{
                        0:'EquityItem',
                        2:'CouponItem',
                        3:'RedEnvelopeItem'
                      }[tabsObj.index]"
                      :configForm="configForm"
                      :data="i"
                      @choose="choose"
                    ></component>
                  </template>
                </template>
                <template v-if="activityObj.nothing">
                  <div class="nothing-text">
                    到底了!
                  </div>
                </template>
              </template>
              <template v-else>
                <div class="nothing">
                  <img src="@/assets/activity-nothing.png">
                  <div>暂无内容</div>
                </div>
              </template>
            </template>
          </div>
        </template>
      </glsx-combi-form>
    </div>
  </el-scrollbar>
</template>

<script type='text/ecmascript-6'>
import { getMerchantListMixin } from '@/mixins'
import EquityItem from './EquityItem.vue'
import CouponItem from './CouponItem.vue'
import RedEnvelopeItem from './RedEnvelopeItem.vue'
// import CombiForm from '@/components/glsx-combi-form/index.vue'
export default {
  name: 'StepOne',
  mixins: [getMerchantListMixin],
  components: { RedEnvelopeItem, EquityItem, CouponItem },
  data () {
    return {
      configForm: {
        name: '',
        merchantId: '',
        cardTitle: '',
        cardCover: '',
        activityId: '',
        merchantName: ''
      },
      rule: {
        name: [{ required: true, message: '请输入问卷名称', trigger: 'change' }],
        merchantId: [{ required: true, message: '请选择所属渠道', trigger: 'change' }],
        cardTitle: [{ required: true, message: '请输入卡片标题', trigger: 'change' }],
        cardCover: [{ required: true, message: '请上传卡片封面', trigger: 'change' }],
      },
      formData: [
        {
          label: '问卷名称',
          name: 'name',
          type: 'input',
          maxlength: 50,
        },
        {
          label: '所属渠道',
          type: 'select',
          name: 'merchantId',
          addHtml: 'merchantId'
        },
        {
          label: '卡片标题',
          name: 'cardTitle',
          type: 'input',
          maxlength: 20,
        },
        {
          label: '卡片封面',
          name: 'cardCover',
          type: 'upload',
          width: '320px',
          limitSize: 10,
          maxPhotoNum: 1,
          suffixSlot: 'cardCover',
          handleUpload: async (e, file, targetItem) => {
            try {
              const imgFile = e.target.result,
                arrBase64 = imgFile.split(',')[1],
                str = file.name.split('.'),
                base64 = str.at(-1) + '@' + arrBase64,
                res = await this.$apis.common.pictureUpload({ file: base64, fileType: 'image' })
              if (res) {
                this.configForm[targetItem.name] = res
                this.formRef().validateField(targetItem.name)
              } else throw '上传失败'
            } catch (error) {
              this.$message(error)
            }
          }
        },
        {
          label: '关联活动',
          addHtml: 'activity'
        }
      ],
      tabsObj: {
        data: ['权益卡试用', '权益套餐', '商城券', '红包活动'],
        index: 0
      },
      activityObj: {
        data: [],
        key: Date.now(),
        flag: false,
        pageNum: 1,
        total: 0,
        nothing: false,
        loading: false
      },
      merchantObj: {
        nothing: false,
        data: [],
        currentPage: 1,
        name: '',
        loading: false,
        total: 0
      }
    }
  },
  watch: {
    'configForm.merchantId' (n) {
      if (n) {
        this.configForm.activityId = ''
        this.configForm.activityType = ''
        this.configForm.merchantName = this.merchantObj.data.find(e => e.confId == n).confName
        this.activityObj = this.$options.data.call(this).activityObj
        this.getActivityList()
      } else this.activityObj.data = []
    }
  },
  mounted () {
    this.onActivityReachBottom(this)
    this.activityObj.flag = true
  },
  methods: {
    formRef () {
      return this.$refs.formRef.$refs.formRef
    },
    onActivityReachBottom (_this) {
      this.$refs.stepBox.wrap.addEventListener('scroll', function () {
        const condition = this.scrollHeight - this.scrollTop <= this.clientHeight,
          { index } = _this.tabsObj,
          { total, pageNum } = _this.activityObj
        if (condition) {
          if (index == 2) {
            if (Math.ceil(total / 20) > pageNum) {
              _this.activityObj.pageNum += 1
              _this.getActivityList()
            } else {
              _this.activityObj.nothing = true
              _this.activityObj.key = Date.now()
            }
            console.log(_this.activityObj)
          }
        }
      })
    },
    changeActivity (index) {
      const { merchantId } = this.configForm
      if (this.tabsObj.index == index) return
      if (!merchantId) return this.$message.error('请选择所属渠道')
      this.tabsObj.index = index
      this.configForm.activityId = ''
      this.configForm.activityType = ''
      this.activityObj = this.$options.data.call(this).activityObj
      this.getActivityList()
    },
    async getActivityList () {
      const { index } = this.tabsObj,
        { merchantId } = this.configForm,
        { pageNum } = this.activityObj,
        targetApi = {
          0: 'getEquityActivityList',
          2: 'getMerchantCouponList',
          3: 'getActivityRedEnvelopeList'
        }[index],
        params = {
          merchantId
        }
      try {
        if (index == 2) params.pageNum = pageNum
        this.activityObj.loading = true
        const res = await this.$apis.questionnaire[targetApi](params)
        let data = res
        if (index == 2) {
          data = [...this.activityObj.data, ...res.list]
          this.activityObj.total = res.total

        }
        this.activityObj.data = data
        this.activityObj.loading = false
        this.activityObj.flag = true
      } catch (error) {
        this.activityObj.data = []
        this.activityObj.loading = false
        this.activityObj.flag = true
      }
    },
    choose (e) {
      console.log(e)
      const { index } = this.tabsObj
      this.configForm.activityId = e
      this.configForm.activityType = e ? index == 0 ? 1 : index : ''
    }
  },

}
</script>

<style scoped lang='scss'>
.step-box {
  width: 100%;
  height: 100%;
  box-sizing: border-box;
  /deep/.el-scrollbar__wrap {
    &::-webkit-scrollbar {
      width: 0;
      height: 0;
      color: transparent;
    }
  }
  .step-content {
    padding-top: 32px;
  }
  /deep/.el-form-item__content {
    width: fit-content;
    max-width: calc(100% - 174px);
  }
  /deep/.activity-img,
  /deep/.muti-imgs {
    width: 104px;
    height: 104px;
    background: #0000000a;
    border: 1px dashed #00000026;
    border-radius: 4px;
    input {
      margin-top: 0;
    }
    .img-w-h,
    .img-btns {
      width: 100%;
      height: 100%;
    }
  }
  /deep/.activity-img,
  /deep/.muti-imgs {
    margin-bottom: 30px;
    &::before {
      // content: '图片大小10M以内';
      content: '\56fe\7247\5927\5c0f\0031\0030M\4ee5\5185';
      display: block;
      font-size: 14px;
      color: #999999;
      position: absolute;
      left: 0;
      line-height: 20px;
      bottom: -30px;
      width: 200px;
      cursor: auto;
    }
  }
  .tabs {
    height: 36px;
    background: #f8f8f8;
    border-radius: 4px;
    padding: 0 2px;
    width: fit-content;
    .tab {
      height: 32px;
      font-size: 14px;
      color: #666666;
      padding: 0 16px;
      margin: 0;
    }
    .active {
      background: #ffffff;
      border-radius: 3px;
      font-weight: 500;
      color: #5850ff;
    }
    .is-disabled {
      color: #cfcfcf;
    }
  }
  .activity-box {
    display: flex;
    gap: 16px;
    flex-wrap: wrap;
    margin-top: 16px;

    .nothing {
      margin-top: 24px;
      font-weight: 500;
      font-size: 16px;
      color: #666666;
      text-align: center;
      img {
        width: 323px;
        height: 92px;
        margin-bottom: 16px;
      }
    }
    .nothing-text {
      color: #666666;
      text-align: center;
      font-weight: 500;
      font-size: 16px;
      width: 100%;
    }
  }
}
.select-nothing {
  font-size: 14px;
  padding: 0 20px;
  white-space: nowrap;
  color: #606266;
  height: 34px;
  line-height: 34px;
  text-align: center;
}
</style>