index.vue 12 KB
<template>
  <div class="ctnr">
    <div class="nav">
      <div style="display: inline-block;">
        <div class="configItem" :ref="'tempRef'+index"  v-for="(item,index) in rowList"  :key="item.id" @click="selectedTemp(item)" :class="{curConfigItem:curConfigId==item.id}">{{ item.name }}<i class="el-icon-close delIcon" @click.stop="delTemplate(item)"></i></div>
      </div>
      <div v-if="showInput" class="inputArea"><el-input maxLength="15" ref="addInput" @blur="inputBlur" v-model="configName" class="input"></el-input></div>
      <div v-else class="configItem addBtn" style="cursor: pointer;" @click="addTemplate"><i class="el-icon-plus border"></i></div>
      <div v-if="colList && colList.length" class="configItem pullBtn" style="cursor: pointer;">
        <i class="el-icon-arrow-down border"></i>
        <div class="pullDownArea">
          <div class="pullDownItem" @click="selectedTemp(item, 'sort')" :class="{curConfigItem:curConfigId==item.id}" v-for="item in colList" :key="item.id">{{ item.name }}<i class="el-icon-close delIcon" @click.stop="delTemplate(item)"></i></div>
        </div>
      </div>
    </div>
    <div ref="main" class="main">
      <el-button type="primary" style="margin-bottom: 16px;" size="small" @click="addCoupon" class="el-icon-plus">添加优惠券</el-button>
      <GLTable ref="tableRef" :pageConf="pageConf" :searchForm="searchForm" :tableParams="tableParams" :getListApi="getListApi" :beforeSearchFn="beforeSearchFn">
        <div class="good_name" slot="goodName" slot-scope="scope">
          <img v-if="scope.scope.appearance == 1" src="@/assets/coupon.png" alt="" />
          <img v-else src="@/assets/directCharge.png" alt="" />
          <!-- {{ scope.scope.appearance == 2 ? scope.scope.productName + (scope.scope.denomination || '') : scope.scope.productName }} -->
          {{ scope.scope.goodsName }}
        </div>
        <div class="good_status" slot="goodStatus" slot-scope="scope">
          <span class="circle" :style="{ 'background-color': scope.scope.status == 2 ? '#5850FF' : '#999999' }"></span>
          <span>
            {{ scope.scope.status == 2 ? '生效中' : '已失效' }}
          </span>
        </div>
        <div slot="auth-button" slot-scope="scope">
          <GLTableButton :buttonList="buttonList" :buttonItem="scope" :buttonNum="3"></GLTableButton>
        </div>
      </GLTable>
    </div>
    <select-coupon-dialog :templateId="curConfigId"  @confirmDialog="confirmDialog" @closeDialog="closeDialog" :selectedGoodsList="couponFormObj.selectedGoodsList" v-if="isVisible" :isVisible="isVisible"></select-coupon-dialog>
  </div>
</template>
  
<script>
import { utils } from 'glsx-common'
import selectCouponDialog from './components/selectCouponDialog.vue'
  export default {
    name: 'index',
    components: {
      selectCouponDialog
    },
    data() {
      return {
        curConfigId: '',
        showInput: false,
        isVisible: false,
        configName: '',
        curIdx: -1,
        couponFormObj: {
          selectedGoodsList: []
        },
        templateList: [
        ],
        rowList: [],
        colList: [],
        getListApi: this.$apis.goodsCouponApi.getCouponById,
        pageConf: { pageSize: 'pageSize', pageNum: 'pageNum', totalCount: 'total' },
        searchForm: {
          templateId: ''
        },
        buttonList: [
          {
            type: 'el-button',
            name: '删除',
            attrs: {
              type: 'text',
              fn: '删除'
            },
            listeners: {
              click: row => this.delCoupon(row.scope)
            }
          }
        ],
        tableParams: {
          listName: 'list',
          tableOption: true,
          tableColumns: [
            {
              label: '品牌',
              prop: 'brandName',
              width: 150,
            },
            {
              label: '商品名称',
              minWidth: 220,
              addHtml: 'goodName'
            },
            {
              label: '数量',
              prop: 'couponAmount',
              width: 150
            },
            {
              label: '状态',
              width: 150,
              addHtml: 'goodStatus'
            },
            {
              label: '券面额/元',
              width: 150,
              formatter: cell => {
                return cell.denomination ? Number((Number(cell.denomination) / 100).toFixed(2)) : 0
              }
            },
            {
              label: '有效期/天',
              width: 150,
              prop: 'effectiveDays'
            }
          ]
        },
      }
    },
    created() {
      
    },
    mounted(){
      const loading = this.$loading({
        lock: true,
        text: 'Loading...',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.5)',
        customClass:'loadingClass'
      })
      this.getTemplateList().then(res=>{
        loading.close()
      }).catch(err=>{
        loading.close()
      })
      window.addEventListener('resize', this.resizeDom);
    },
    beforeDestroy(){
      window.removeEventListener('resize', this.resizeDom)
    },  
    methods: {
      resizeDom(){
        this.calcWidth(true)
      },
      delTemplate(row){
        this.$confirm(`确认删除${row.name}吗?`, '提示', {
        type: 'warning',
        callback: action => {
          if(action=='confirm'){
            let params = {
              id: row.id
            }
            this.$apis.goodsCouponApi.delTemplateById(params).then(res => {
              if(res){
                this.$message.success('删除成功')
                this.getTemplateList()
              }
            })
           }
          }
        })
      },
      delCoupon(row){
        this.$confirm('确认删除吗?', '提示', {
        type: 'warning',
        callback: action => {
          if(action=='confirm'){
            let params = {
              templateId: this.curConfigId,
              couponId: row.couponId
            }
            this.$apis.goodsCouponApi.delCouponById(params).then(res => {
              if(res){
                this.$message.success('删除成功')
                this.handleOk()
              }
            })
           }
          }
        })
      },
      getTemplateList(){
        return new Promise((resolve, reject) => {
          this.$apis.goodsCouponApi.getTemplateList({pageSize: 10000, pageNum: 1}).then(res => {
            resolve(res)
            if(res?.list?.length){
              this.curConfigId = res.list[0]?.id
              this.handleOk()
              this.templateList = res.list
              this.rowList = res.list
              this.calcWidth()
            }
          }).catch(err=>{
            reject(err)
          })
        })
      },
      addTemplate(){
        this.showInput = true
        this.$nextTick(()=>{
          this.$refs.addInput.focus()
        })
      },
      beforeSearchFn(){
        return new Promise((resolve, reject) => {
          this.searchForm.templateId = this.curConfigId   
          // if(!this.curConfigId) return reject(); 
          resolve()
        })
      },
      handleOk() {
        this.$refs.tableRef.refreshTable()
      },
      selectedTemp(item, isSort){
        this.curConfigId = item.id
        // if(isSort){
        //   let idx = this.templateList.findIndex(row => row.id == item.id)
        //   let arr = this.templateList.splice(idx, 1)
        //   this.templateList.unshift(arr[0])
        //   this.calcWidth()
        // }
        this.handleOk()
      },
      addCoupon(){
        if(!this.curConfigId) return this.$message.error('请先选择模板');
        this.isVisible = true
      },
      calcWidth(resize){
        if(resize){
          this.rowList =  this.templateList
        }
        let calculated = false
        let mainW = this.$refs?.main?.offsetWidth || 0
        this.$nextTick(()=>{
          this.rowList.reduce((pre, cur,index) => {
            let curW = this.$refs?.['tempRef'+index]?.[0]?.offsetWidth
            if((pre + curW) > mainW-500 && !calculated) {
              this.curIdx = index
              calculated = true
            }
            return pre + curW
          },0);
          this.colList = this.curIdx >-1 ? this.templateList.slice(this.curIdx) : []
          this.rowList = this.curIdx >-1 ? this.templateList.slice(0, this.curIdx) : this.templateList
        })
      },
      inputBlur(e){
        this.showInput = false
        if(this.configName) {
          this.$apis.goodsCouponApi.saveTemplate({name: this.configName}).then(res =>{
            if(res){
              this.configName = ''
              this.$message.success('添加成功')
              this.getTemplateList()
            }
          })
        }
      },
      closeDialog() {
        this.isVisible = false
      },
      confirmDialog(e) {
        // this.couponFormObj.selectedGoodsList = e || []
        if(e?.length){
          let params = {
            templateId: this.curConfigId,
            coupons: e.map(item => {return {couponId: item.id, couponAmount: item.couponAmount}})
          }
          this.$apis.goodsCouponApi.saveCouponInfo(params).then(res => {
            if(res){
              this.$message.success('添加成功')
              this.handleOk()
            }
          })
        }
      },
    }
  }
  </script>
  <style lang="scss" scoped>
  .ctnr {
    .nav{
        .inputArea{
          display: inline-block;
          height: 40px;
          line-height: 40px;
          border-radius: 4px 4px 0 0;
          margin-right: 1px;
          .input{
            width: 245px;
            /deep/ .el-input__inner{
              vertical-align: baseline;
              box-sizing: border-box;
              border: none;
            }
          }
        }
        .configItem{
          display: inline-block;
          padding: 0 20px;
          height: 40px;
          line-height: 40px;
          background-color: #F8F8F8;
          border-radius: 4px 4px 0 0;
          margin-right: 1px;
          color: #666666;
          letter-spacing: 0;
          text-align: center;
          cursor: pointer;
          .delIcon{
            font-size: 12px;
            color: #666;
            margin-left: 12px;
          }
        }
        .curConfigItem{
          background-color: #fff;
          color: #5850FF;
          font-weight: 500;
        }
        .addBtn, .pullBtn{
          width: 50px;
          box-sizing: border-box;
          padding: 0;
          position: relative;
          .border{
            border: 1px dashed #999;
            color: #999;
          }
        }
        .pullBtn{
          &:hover{
            .pullDownArea{
              display: block;
            }
          }
          .pullDownArea{
            display: none;
            position: absolute;
            width: 200px;
            max-height: 200px;
            overflow-y: auto;
            right: 0;
            background-color: #fff;
            z-index: 2;
            box-shadow: 0 3px 6px -4px #0000001f, 0 6px 16px 0 #00000014, 0 9px 28px 8px #0000000d;
            border-radius: 4px;
            .pullDownItem{
              height: 36px;
              line-height: 36px;
              font-weight: 400;
              padding: 0 15px;
              font-size: 14px;
              overflow: hidden;
              white-space: nowrap;
              text-overflow: ellipsis;
              color: #666666;
              background-color: #F8F8F8;
              &:hover{
                background-color: #f5f7fa;
              }
            }
            .curConfigItem{
              background-color: #fff;
              color: #5850FF;
              font-weight: 500;
            }
          }
        }
      }
    .main{
      background-color: #fff;
      padding: 16px 20px;
      .good_name {
        img {
          width: 40px;
          height: 20px;
          vertical-align: text-bottom;
        }
      }
      .good_status {
        .circle {
          display: inline-block;
          width: 6px;
          height: 6px;
          border-radius: 50%;
          vertical-align: middle;
          margin-right: 4px;
        }
      }
    }
  }
  </style>