OffAdd.vue 8.85 KB
<template>
  <div class="add-wrap">
    <div class="container gl-form-page">
      <el-form ref="configRef" label-width="154px" :rules="configRules" :model="configForm">
        <el-form-item label="活动名称:" prop="activityName" class="m-t-20">
          <el-input :size="size" v-model="configForm.activityName" placeholder="请输入" maxlength="25" :disabled="disabled"></el-input>
        </el-form-item>
        <el-form-item label="所属渠道:" prop="merchantId" class="m-t-20">
          <el-select :size="size" v-model="configForm.merchantId" placeholder="请选择" :disabled="disabled" 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="nothing">到底了!</div>
            </template>
          </el-select>
        </el-form-item>
        <el-form-item label="活动时间:" prop="activityTime" class="m-t-20">
          <el-date-picker :size="size" v-model="configForm.activityTime" type="daterange" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" :picker-options="afterRangePickerOptions"></el-date-picker>
        </el-form-item>
        <el-form-item label="活动形式:" prop="activityType" class="m-t-20">
          <el-radio-group :size="size" v-model="configForm.activityType" style="width: 400px" :disabled="disabled">
            <el-radio :label="0">随机红包</el-radio>
            <el-radio :label="1">九宫格</el-radio>
            <el-radio :label="2">翻牌</el-radio>
            <el-radio :label="3">大转盘</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="导入链接:" prop="exportUrl" class="m-t-20">
          <div class="activity-link">
            <div class="upload-btn">
              <el-button size="small">
                <i class="el-icon-plus"></i>
                上传文件
              </el-button>
              <input type="file" style="opacity: 0" ref="file" accept="*" @change="uploadFile" />
            </div>
            <a href="https://img01.glsx.com.cn/weapp/resource/saas/scrm/xlsx/红包链接.xlsx" target="_blank">下载导入模板</a>
            <template v-if="configForm.exportUrl">
              <div class="file-info flex">
                {{ fileName }}
                <div class="el-icon-success"></div>
              </div>
            </template>
            <template v-if="fileError">
              <div class="file-info flex error">
                <a :href="fileError" target="_blank">下载失败列表</a>
                <div class="el-icon-error"></div>
              </div>
            </template>
          </div>
        </el-form-item>
      </el-form>
    </div>
    <GLFooter>
      <el-button @click="handleClose">取消</el-button>
      <el-button type="primary" @click="handleConfirm">提交</el-button>
    </GLFooter>
  </div>
</template>
  
  <script type='text/ecmascript-6'>
export const afterRangePickerOptions = {
  disabledDate(date) {
    const yesterday = new Date(new Date().setDate(new Date().getDate() - 1))
    if (date < yesterday) {
      return true
    }
    return false
  }
}
import commonFun from '../common/fun.js'
import * as dayjs from 'dayjs'
import { getMerchantListMixin } from '@/mixins'

export default {
  mixins: [getMerchantListMixin],
  name: '',
  props: {
    editObj: {
      type: Object,
      default: () => {}
    }
  },
  data() {
    return {
      id: this.$route.query.id,
      afterRangePickerOptions,
      disabled: false,
      configForm: {
        activityName: '',
        merchantId: '',
        activityTime: '',
        activityType: 0,
        exportUrl: ''
      },
      configRules: {
        activityName: [
          {
            required: true,
            message: '请输入活动名称',
            trigger: 'blur'
          }
        ],
        merchantId: [
          {
            required: true,
            message: '请选择所属渠道',
            trigger: 'blur'
          }
        ],
        activityTime: [
          {
            required: true,
            message: '请输入活动时间',
            trigger: 'blur'
          }
        ],
        activityType: [
          {
            required: true
          }
        ],
        exportUrl: [
          {
            required: true,
            message: '请导入链接'
          }
        ]
      },
      fileName: '',
      fileError: ''
    }
  },
  created() {
    if (this.id) this.getRedEnvelopeActivityDetail()
  },
  methods: {
    async getRedEnvelopeActivityDetail() {
      let { id } = this
      try {
        const { configForm } = this,
          res = await this.$apis.redenvelope.getRedEnvelopeActivityDetail({ id }),
          merchantId = String(res.merchantId)
        this.configForm = {
          ...configForm,
          activityName: res.activityName,
          activityType: res.activityType,
          merchantId,
          activityTime: [dayjs(res.activityStartTime).toDate(), dayjs(res.activityEndTime).toDate()],
          id
        }
        this.merchantObj.data = [
          {
            confId: merchantId,
            confName: res.merchantName
          }
        ]
        this.disabled = true
        this.configRules.exportUrl[0].required = false
      } catch (error) {
        this.$message.error(error)
      }
    },

    handleClose() {
      //   this.configForm = this.$options.data.call(this).configForm
      //   this.disabled = false
      //   this.fileError = ''
      //   this.$refs.configRef.resetFields()
      //   this.merchantObj.data = []
      //   this.$emit('update:editObj', {})
      this.$router.push('/redEnvelope/index')
    },
    handleConfirm() {
      this.$refs.configRef.validate(async valid => {
        if (valid) {
          try {
            const { configForm, merchantObj } = this
            let params = this.$common.utils.deepClone(configForm),
              targetName = params.id ? 'updateActivityRedEnvelope' : 'createActivityRedEnvelope'
            params.sourceType = 1
            params.activityStartTime = dayjs(params.activityTime[0]).format('YYYY-MM-DD')
            params.activityEndTime = dayjs(params.activityTime[1]).format('YYYY-MM-DD')
            params.merchantName = merchantObj.data.find(e => e.confId == params.merchantId).confName
            delete params.activityTime
            console.log(JSON.parse(JSON.stringify(params)))
            const res = await this.$apis.redenvelope[targetName](params)
            if (res) {
              this.$message({
                message: params.id ? '编辑成功' : '创建成功',
                type: 'success'
              })
              this.handleClose()
            } else throw params.id ? '编辑失败' : '创建失败'
          } catch (error) {
            this.$message.error(error)
          }
        }
      })
    },
    async uploadFile(e) {
      const { id } = this.configForm,
        target = id ? 'updateCheckFile' : 'createCheckFile'
      try {
        const { url, name } = await commonFun.uploadFile(e, this),
          params = { exportUrl: url }
        if (id) params.id = id
        this.$refs.file.value = ''
        const res = await this.$apis.redenvelope[target](params)
        if (res) {
          if (res.totalCount == 0 || res.failCount > 0) {
            this.fileError = res.errorUrl
            this.$message.error('红包链接导入失败')
            this.configForm.exportUrl = ''
            return
          }
          this.$message({
            message: '导入成功',
            type: 'success'
          })
          this.fileError = ''
          this.configForm.exportUrl = url
          this.fileName = name
        }
      } catch (error) {
        console.log(error)
        if (error) this.$message.error(error)
        this.$refs.file.value = ''
      }
    }
  }
}
</script>
  
  <style scoped lang='scss'>
.container {
  padding: 32px 20px;
  background: #ffffff;
  border-radius: 0 4px 4px 4px;
  margin: 16px;
}
.activity-link {
  display: flex;
  width: 500px;
  .upload-btn {
    position: relative;
    width: 110px;
    height: 32px;
    .el-button--small {
      width: 100%;
      height: 100%;
    }
    input {
      cursor: pointer;
      position: absolute;
      height: 100%;
      width: 100%;
      top: 0px;
      left: 0px;
      font-size: 0px;
    }
  }
  a,
  .error {
    margin-left: 8px;
    color: #5850ff;
    font-size: 14px;
    cursor: pointer;
  }

  .file-info {
    margin-left: 8px;
    .el-icon-success {
      margin-left: 8px;
      color: #67c23a;
    }
    .el-icon-error {
      margin-left: 8px;
      color: red;
    }
  }
}
.nothing {
  font-size: 14px;
  padding: 0 20px;
  white-space: nowrap;
  color: #606266;
  height: 34px;
  line-height: 34px;
  text-align: center;
}
</style>