add.vue 13.2 KB
<!--
 * @Author: 前端 qianduan01@didihu.com.cn
 * @Date: 2023-05-10 17:17:31
 * @LastEditors: yangming yangming@didihu.com.cn
 * @LastEditTime: 2023-07-19 15:47:50
 * @FilePath: \outbound\src\views\calendar\add.vue
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
-->
<template>
  <div>
    <div class="main-box">
      <glsx-combi-form class="edit-form-container" :isBlock="true" ref="editFormRef" formRef="editChildRef" :formObj="editForm" :formData="editFormData" :formRules="editRule">
        <template slot="activityTimeHtml">
          <div class="activity-time-container" :style="{ 'margin-left': defaultLabelWidth }">
            <el-date-picker v-model="editForm.activityTime" type="daterange" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" value-format="yyyy/MM/dd"></el-date-picker>
          </div>
        </template>
        <div slot="selectGoodsBtn">
          <div class="play-panel">
            <div class="card-panel">
              <el-tabs type="card" v-model="editForm.couponTemplateId" @tab-click="changeCouponTemplate">
                <el-tab-pane :label="item.name" :name="item.id" v-for="item in couponTemplateList" :key="item.id"></el-tab-pane>
              </el-tabs>
              <div class="chose-panel" v-loading="goodsLoading">
                <el-button type="default" size="small" class="el-icon-plus select-btn" @click="handleGoodsAdd">选择福利券</el-button>
                <div style="width: 772px" v-if="selectedGoodsList && selectedGoodsList.length">
                  <div class="goods-panel">
                    <el-table :data="selectedGoodsList" border style="width: 100%">
                      <el-table-column type="index" label="序号" width="70"></el-table-column>
                      <el-table-column prop="brandName" label="品牌" width="130"></el-table-column>
                      <el-table-column prop="goodsName" label="商户名称" width="150"></el-table-column>
                      <el-table-column prop="couponAmount" label="数量" width="70"></el-table-column>
                      <el-table-column prop="denomination" label="劵面额" width="90">
                        <template slot-scope="scope">{{ (scope.row.denomination / 100).toFixed(2) }}元</template>
                      </el-table-column>
                      <el-table-column prop="effectiveDays" label="有效期" width="80">
                        <template slot-scope="scope">{{ scope.row.effectiveDays }}天</template>
                      </el-table-column>
                      <el-table-column prop="status" label="状态" width="80">
                        <template slot-scope="scope">{{ scope.row.status == 1 ? '未生效' : scope.row.status == 2 ? '生效中' : scope.row.status == 3 ? '已失效' : '' }}</template>
                      </el-table-column>
                      <el-table-column label="操作" width="100">
                        <template slot-scope="scope">
                          <el-button @click="handleDeleteGoods(scope.row)" type="text" size="small">删除</el-button>
                        </template>
                      </el-table-column>
                    </el-table>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>
      </glsx-combi-form>
      <div class="preview-panel">
        <PhoneView :editForm="editForm" ref="preview" :selectedGoodsList="selectedGoodsList"></PhoneView>
      </div>
    </div>
    <GLFooter>
      <el-button @click="handleCancel">取消</el-button>
      <el-button type="primary" @click="handleSave">提交</el-button>
    </GLFooter>
    <select-goods-dialog v-if="isSelectGoodsVisible" :isSelectGoodsVisible="isSelectGoodsVisible" :selectedGoodsLists="selectedGoodsList" @handleGoodsConfirm="handleGoodsConfirm" @handleGoodsClose="handleGoodsClose"></select-goods-dialog>
  </div>
</template>
<script>
import selectGoodsDialog from './components/SelectGoodsDialog'
import PhoneView from './components/PhoneView'
import getUploadImgColor from '@/utils/color'
const RENEWAL = [1, 2, 3, 4, 5, 6, 7, 8]
const WIDTH = 280
const RATIO = 375 / WIDTH
export default {
  name: 'addCalendar',
  data() {
    return {
      isSelectGoodsVisible: false,
      goodsLoading: false,
      editForm: {},
      couponTemplateList: [],
      selectedGoodsList: [],
      editFormData: [
        {
          label: '营销标题',
          name: 'title',
          type: 'input',
          maxlength: 10
        },
        {
          label: '营销副标题',
          name: 'subTitle',
          type: 'input',
          maxlength: 20
        },
        {
          label: '活动时间',
          name: 'activityTime',
          addHtml: 'activityTimeHtml',
          isHide: false
        },
        {
          label: '关键字',
          name: 'keywords',
          type: 'input',
          maxlength: 20
        },
        {
          label: '场景选择',
          name: 'marketingType',
          type: 'radio',
          optionList: [
            {
              optionId: 1,
              optionLabel: '道路救援'
            },
            {
              optionId: 2,
              optionLabel: '定保招揽'
            },
            {
              optionId: 5,
              optionLabel: '续保招揽'
            },
            {
              optionId: 6,
              optionLabel: '节假日关怀'
            }
          ],
          optionId: 'optionId',
          optionLabel: 'optionLabel'
        },
        {
          label: '平台福利',
          name: 'goodsList',
          addHtml: 'selectGoodsBtn',
          tips: '平台提供了种类丰富的权益商品,可进一步提升产品套餐的受众,强烈推荐!'
        },
        {
          label: '销售价格/元',
          name: 'platformBenefits',
          type: 'input',
          maxlength: 20,
          isDisabled: true
        }
      ],
      editRule: {
        title: [{ required: true, message: '请输入营销主题', trigger: 'input' }],
        subTitle: [{ required: true, message: '请输入营销副标题', trigger: 'input' }],
        activityTime: [{ required: true, message: '请选择活动时间', trigger: 'change' }],
        keywords: [{ required: true, message: '请输入关键词', trigger: 'input' }],
        marketingType: [{ required: true, message: '请选择场景', trigger: 'change' }]
      }
    }
  },
  components: {
    selectGoodsDialog,
    PhoneView
  },
  mounted() {
    this.couponTemplate()
    const { id } = this.$route.query
    this.calendarDetail(id)
  },
  watch: {
    //金额根据已选择状态为“生效中”福利券:sum(券面额*数量计算)
    selectedGoodsList(val) {
      this.editForm.goodsList = val.length ? 1 : null
      const platformBenefits = val.reduce((prev, cur) => {
        console.log(prev, cur.denomination * cur.couponAmount)
        if (cur.status == 2) prev = prev + cur.denomination * cur.couponAmount
        return prev
      }, 0)
      this.editForm.platformBenefits = (platformBenefits / 100).toFixed(2)
    }
  },
  methods: {
    calendarDetail(id) {
      this.$apis.calendar.calendarDetail({ id }).then(res => {
        this.editForm = this.$common.utils.deepClone(res) || {}
        if (res.startTime) {
          this.$set(this.editForm, 'activityTime', [res.startTime, res.endTime])
        }
        getUploadImgColor(res.backgroundImg, color => {
          this.editForm.mainColor = color
        })
        this.selectedGoodsList = this.editForm.couponItemList || []
        this.selectedGoodsList.forEach(item => {
          item.id = item.couponId
        })
      })
    },
    handleCancel() {
      this.$router.push('/calendar/index')
    },
    handleSave() {
      this.$refs.editFormRef.$refs.editChildRef.validate(valid => {
        if (valid) {
          this.addOrUpdate()
        }
      })
    },
    getCouponInfoParams() {
      var param = this.$common.utils.deepClone(this.editForm)
      const d = param.activityTime
      if (d && d.length > 0) {
        param.startTime = d[0]
        param.endTime = d[1]
      }
      param.platformBenefits = (param.platformBenefits * 100).toFixed(0) //存的分,需乘100
      param.couponItemList = this.selectedGoodsList.map(item => {
        return {
          couponId: item.id || item.couponId,
          couponAmount: item.couponAmount
        }
      })
      if (param.titleStyle && RENEWAL.includes(param.codeId)) {
        const style1 = this.$refs.preview.draggableList[0]
        const style2 = this.$refs.preview.draggableList[1]
        const titleStyle = JSON.parse(param.titleStyle)
        const subTitleStyle = JSON.parse(param.subTitleStyle)
        const styles = [style1, style2]
        for (const style of styles) {
          style.x = parseInt(style.x * RATIO)
          style.y = parseInt(style.y * RATIO)
          style.size = parseInt(style.size * RATIO)
          style.width = parseInt(style.width * RATIO)
        }
        param.titleStyle = JSON.stringify(Object.assign(titleStyle, { x: style1.x, y: style1.y, size: style1.size, width: style1.width }))
        param.subTitleStyle = JSON.stringify(Object.assign(subTitleStyle, { x: style2.x, y: style2.y, size: style2.size, width: style2.width }))
      }
      console.log(param.titleStyle)
      delete param.goodsList
      delete param.couponTemplateName
      delete param.couponTemplateId
      delete param.activityTime
      delete param.status
      delete param.platformBenefits
      return param
    },
    addOrUpdate() {
      this.loading = true
      var params = this.getCouponInfoParams()
      this.$apis.calendar
        .calendarUpdate(params)
        .then(res => {
          if (res) {
            this.$message.success('操作成功')
            this.handleCancel()
          }
        })
        .finally(() => {
          this.loading = false
        })
    },
    //选择商品优惠券
    handleGoodsAdd() {
      this.handleGoodsClose()
    },
    handleGoodsClose() {
      this.isSelectGoodsVisible = !this.isSelectGoodsVisible
    },
    handleGoodsConfirm(selectedData) {
      this.$refs.editFormRef.$refs.editChildRef.clearValidate('goodsList')
      this.selectedGoodsList = selectedData || []
      // this.setNameHide('goodsListBlock')
      this.handleGoodsClose()
    },
    handleDeleteGoods(row) {
      const index = this.selectedGoodsList.findIndex(k => k.id == row.id)
      this.selectedGoodsList.splice(index, 1)
      // this.setNameHide('goodsListBlock')
    },
    //获取优惠券模板
    couponTemplate() {
      this.$apis.calendar.couponTemplate({ pageSize: 10000, pageNum: 1 }).then(res => {
        this.couponTemplateList = res.list || []
      })
    },
    changeCouponTemplate(tab, event) {
      //   this.$refs.editFormRef.$refs[this.editChildRef].clearValidate('goodsList')
      const templateId = tab.name
      this.editForm.couponTemplateId = templateId
      this.editForm.couponTemplateName = tab.label
      this.couponTemplateDetails(templateId)
    },
    //获取优惠券模板详情
    couponTemplateDetails(templateId) {
      this.goodsLoading = true
      this.$apis.calendar.couponTemplateDetails({ templateId, pageSize: 10000, pageNum: 1 }).then(res => {
        this.selectedGoodsList = res.list || []
        this.selectedGoodsList.forEach(item => (item.id = item.couponId))
        this.goodsLoading = false
      })
    }
  }
}
</script>
<style lang="scss">
.main-box {
  background: #fff;
  padding: 20px;
  min-height: calc(100vh - 180px);
  display: flex;
  margin-bottom: 16px;
  .el-form-item__content {
    width: auto;
  }
  .preview-panel {
    width: 400px;
    margin-left: auto;
    .preview-bg {
      width: 280px;
      height: 556px;
    }
  }
  .play-panel {
    .card-panel {
      width: 710px;
      .el-tabs__nav {
        border: none;
      }
      .el-tabs__header {
        margin: 0;
      }
      .el-tabs__item {
        background: #fafafa;
        border: 1px solid #e5e5e5;
        border-radius: 4px 4px 0 0;
        border-right: 1px solid #e5e5e5;
        border-top: 1px solid #e5e5e5;
        border-left: 1px solid #e5e5e5 !important;
        border-bottom: none;
        margin-right: 1px;
        width: auto;
        &.is-active {
          color: #5850ff;
          background: #fff;
          font-weight: 600;
        }
      }
    }
    .choseRow {
      display: flex;
      height: auto;
      .choseItem {
        display: inline-block;
        cursor: pointer;
        line-height: 40px;
        height: 40px;
        background: #fafafa;
        border-radius: 4px 4px 0 0;
        border: 1px solid #e5e5e5;
        border-bottom: none;
        margin-right: 1px;
        padding: 0 20px;
        &.active {
          color: #5850ff;
          background: #fff;
          font-weight: 600;
        }
      }
    }
    .chose-panel {
      .select-btn {
        margin-bottom: 10px;
      }
      .waiting-box {
        width: 278px;
        height: 278px;
        margin: 15px auto 0;
      }
      padding: 20px;
      width: 784px;
      min-height: 296px;
      background: #ffffff;
      border: 1px solid #e5e5e5;
      border-radius: 0 4px 4px 4px;
    }
  }
  .activity-time-container {
    width: 400px;
  }
}
</style>