create.vue 11.3 KB
<template>
  <div class="gift-layout">
    <template v-if="flag">
      <div class="box">
        <div class="item">
          <div class="label">礼包名称</div>
          <div class="value">
            <van-field
              v-model="obj.name"
              :disabled="disabledList.includes('name')"
              input-align="right"
              maxlength="15"
              placeholder="请输入礼包名称"
              @input="input"
            />
          </div>
        </div>
        <div class="item">
          <div class="label">
            备注名称
            <span>(仅内部可见)</span>
          </div>
          <div class="value">
            <van-field
              v-model="obj.remarkName"
              input-align="right"
              :disabled="disabledList.includes('remark')"
              maxlength="30"
              placeholder="可按车主姓名+手机号形式备注"
              @input="input"
            />
          </div>
        </div>
      </div>
      <div class="box">
        <div class="item">
          <div class="label">礼包明细</div>
          <div class="value">预估成本:<span>¥{{centsToyuan(total)}}</span></div>
        </div>
        <template v-if="obj.couponList && obj.couponList.length">
          <div class="detail-box">
            <template v-for="(i,index) in obj.couponListView">
              <div
                class="detail"
                :key="index"
              >
                <!-- status 优惠券状态(1:未生效,2:生效中,3:已失效) -->
                <!-- 优惠券类型 1:满减券,2:折扣券,3:代金券,4:兑换券 5:购车代金券 6:商品折扣券,7:门店服务 -->
                <span class="detail-title text-center">{{discountEnum[i.discountType]}}</span>
                <template v-for="(e,ind) in i.list">
                  <div
                    :class="['detail-item',{'coupon-disabled':e.status != 2 && obj.receivedFlag != 2}]"
                    :key="ind"
                  >
                    <div class="name flex">
                      {{e.couponName}}
                      <template v-if="e.status != 2 && obj.receivedFlag != 2">
                        <div class="name-tips text-center">{{{1:'未生效',3:'已失效'}[e.status]}}</div>
                    </template>
                  </div>
                  <div class="num flex">
                    数量:x{{e.couponAmount}}
                    <div class="price">¥{{centsToyuan(e.couponAnticipatedCost *  e.couponAmount)}}</div>
                  </div>
                   <div class="del sprite-icon icon-gift-del" v-if="!disabledList.includes('add')"  @click="delCoupon(e)"></div>
                </div>
                </template> 
              </div>
            </template>
          </div>
        </template>
        <div v-if="!disabledList.includes('add')" :class="['add-box text-center',{'add-line':obj.couponList.length}]">
          <div
            class="add-btn text-center"
            @click="showGiftModal"
          >
            <div class="sprite-icon icon-gift-add"></div>
          </div>
        </div>
      </div>
      <div v-if="obj.receiveRule!=3" class="box readonly-box radio-box">
        <div class="title">领取条件</div>
        <template v-for="(i,index) in receiveWayObj">
          <div
            class="radio-item"
            :key="index"
          >
            <div
              class="radio-name flex"
              @click="changeRadio(index)"
            >
              <div :class="['radio',{'radio-active':obj.receiveWay == i.value}]"></div>
              {{i.name}}
            </div>
            <template v-if="obj.receiveWay == i.value">
              <div class="radio-input">
                <van-field
                  v-model="obj[i.key]"
                  :type="i.type || 'text'"
                  :maxlength="i.maxlength"
                  :placeholder="i.placeholder"
                  @input="input(i,$event)"
                />
              </div>
            </template>
          </div>
        </template>
      </div>
      <div class="box readonly-box textarea">
        <div class="title">赠送原因</div>
        <div class="textarea-box">
          <van-field
            v-model="obj.giveReason"
            type="textarea"
            placeholder="请说明赠送礼包的原因(非必填)"
            maxlength="50"
            @input="input"
          />
        </div>
      </div>
      <div
        :class="['btn text-center x-center create',{'disabled-btn':disabled}]"
        @click="submit"
      >{{id?'确定':'创建'}}</div>
    </template>
    <gift-modal
      ref="GiftModal"
      :selectedList="obj.couponList"
      @confirm="couponConfirm"
    />
  </div>
</template>

<script type='text/ecmascript-6'>
import GiftModal from './GiftModal.vue'
import { giftApi, wxConfig} from '@/api'
import { discountTypeMixin } from '@/mixins'
import { setApproveParam } from '@/mixins/giftMixin'
export default {
  name: '',
  components: { GiftModal },
  mixins: [discountTypeMixin],
  data () {
    return {
      flag: false,
      disabled: true,
      disabledList:[],
      obj: {
        receiveWay: 1,
        couponList: []
      },
      receiveWayObj: [
        { name: '指定手机号', type: 'tel', placeholder: '客户需输入指定的手机号,才能成功领取礼包', maxlength: 11, key: 'phone', value: 1 },
        { name: '指定车架号后6位', placeholder: '客户需输入指定的车架号,才能成功领取礼包', maxlength: 6, key: 'vin', value: 2 },
      ],
      total: 0,
      id: '',
      test: ''
    }
  },
  mounted () {
    let { id,perfectFlag } = this.getQuery()
    if (id) {
      this.getDetail(id)
      this.id = id
    }
    else this.flag = true
    wxConfig.init(['thirdPartyOpenPage','onHistoryBack'])
  },
  methods: {
    input (i, e) {
      if (i && i.key == 'vin') this.obj[i.key] = e.replace(/[^0-9a-zA-Z]/g, '')
      this.checkData()
    },
    setCouponData (res) {
      let total = 0
      let discountTypeList = Array.from(new Set([...res.couponList.map(e => e.discountType)]))
      res.couponList.map(e => {
        if (e.status == 2) total += Number(e.couponAnticipatedCost) * e.couponAmount
      })
      res.couponListView = discountTypeList.map(i => {
        let list = []
        res.couponList.map(e => {
          if (i == e.discountType) list.push(e)
        })
        return {
          discountType: i,
          list
        }
      })
      this.total = total
      return res
    },
    getDetail () {
      let { id,pageIdx, childId,perfectFlag } = this.getQuery()
      let API = pageIdx==1? giftApi.getUserGiftDetail : pageIdx==2 ? giftApi.getApproveGiftDetail : giftApi.getGiftDetail
      let pramas = {id,childId}
      API(pramas).then(res => {
        this.hideLoading()
        if(res.receiveRule==2 || (res.receiveRule==3 && res.stock>1)){
          this.disabledList=['name','remark','add']
        }
        res = this.setCouponData(res)
        this.flag = true
        if (res.receiveWay == 1) res.phone = res.receiveDetails
        else res.vin = res.receiveDetails
        this.obj = res
        this.checkData()
      })
    },
    changeRadio (index) {
      this.obj.receiveWay = index == 0 ? 1 : 2
      this.checkData()
    },
    showGiftModal () {
      this.$refs.GiftModal.toggle()
    },
    couponConfirm ({ chooseItems }) {
      this.obj.couponList = JSON.parse(JSON.stringify(chooseItems))
      this.obj = this.setCouponData(this.obj)
      this.checkData()
    },
    checkData () {
      let { obj } = this
      let target = ['name', 'remarkName', 'couponList', 'phone', 'vin']
      try {
        target.map(key => {
          if (key == 'phone' || key == 'vin') {
            if(obj.receiveRule!=3){
              if (obj.receiveWay == 1) {
                if (!obj.phone) throw key
              } else {
                if (!obj.vin) throw key
              }
            }
          } else {
            if (!obj[key]) throw key
            if (Array.isArray(obj[key])) {
              if (!obj[key].length) throw key
            }
          }
        })
      } catch (error) {
        this.disabled = true
        return
      }
      this.disabled = false
    },
    delCoupon (e) {
      let { obj: { couponList } } = this
      let index = null
      couponList.map((i, ind) => {
        if (i.discountType == e.discountType && i.couponId == e.couponId) index = ind
      })
      couponList.splice(index, 1)
      this.obj.couponList = couponList
      this.obj = this.setCouponData(this.obj)
      this.checkData()
    },
    submit () {
      const phoneRex = /^[1]([3-9])[0-9]{9}$/
      let { obj, disabled, id } = this
      if (disabled) return
      if(obj.receiveRule!=3){
        if (obj.receiveWay == 1) {
          if (!phoneRex.test(obj.phone)) {
            this.showToast('请输入正确的手机号')
            return
          }
        } else {
          if (obj.vin.length < 6) {
            this.showToast('请输入车架号的后6位')
            return
          }
        }
      }
      try {
        obj.couponList.map(e => {
          if (e.status != 2) throw false
        })
      } catch (error) {
        this.showToast('礼包有失效券')
        return
      }
      let couponCreateRequests = []
      obj.couponList.map(e => {
        couponCreateRequests.push({
          couponId: e.couponId,
          couponAmount: e.couponAmount,
          couponAnticipatedCost: e.couponAnticipatedCost
        })
      })
      let params = {
        name: obj.name,
        remarkName: obj.remarkName,
        receiveWay: obj.receiveWay,
        giveReason: obj.giveReason,
        couponCreateRequests,
        stock:1, 
        receiveDetailList: obj.receiveWay == 1 ? obj.phone : obj.vin
      }
      if (id){
        params.id = id
        params.approveFlag = obj.approveFlag
        params.receiveRule = obj.receiveRule
        params.stock = obj.stock
        params.staffCreateRequests = obj.staffList
        params.childId = obj.childId
      }
      params.requestSource = 2
      let { perfectFlag,pageIdx } = this.getQuery()
      if(id && perfectFlag==2){
        params = {
          id,
          receiveWay:obj.receiveWay,
          receiveDetail: obj.receiveWay == 1 ? obj.phone : obj.vin,
          giveReason: obj.giveReason,
        }
      }
      this.showLoading('提交中...')
      const API =(id && perfectFlag==2) ? giftApi.createPerfectGift :  giftApi.createOrupdataGift
      API(params).then(res => {
        this.hideLoading()
        if(res.approveFlag == 2){
          wxConfig.thirdPartyOpenPage(setApproveParam(res))
          .then((resp) => {
            setTimeout(()=>{
              let { pageIdx} = this.getQuery()
              let childId = pageIdx ==3 ? res.directionalGiftChildId : this.obj.childId ? this.obj.childId: ''
              this.$router.replace({ name: 'gift.detail', query: {id:res.directionalGiftId,approveStatus:999,childId}})
            },500)
          })
        }else{
          // if(pageIdx==2){this.$route.query.approveStatus = 999}
          this.$route.params.giftToast = `${id ? '修改' : '创建'}成功`
          this.$router.back()
        }
      })
    }
  }
}
</script>

<style scoped lang='scss'>
@import '@/assets/css/gift-detail.scss';
.item {
  .van-field {
    padding: 0;
  }
}
.add-line {
  border-top: 1px solid rgba(195, 195, 195, 0.3);
}
</style>