PayModal.vue 9.46 KB
<template>
  <div>
    <van-action-sheet
      v-model="showBox"
      @click-overlay="onCancel"
      :lock-scroll="false"
    >
      <div class="box">
        <template v-for="(i, index) in items">
          <template v-if="i.name">
            <div
              class="item flex"
              :key="index"
            >
              <div class="label text-hide">
                <span v-if="i.key != 'plateNo'">*</span>
                {{ i.name }}
              </div>
              <div class="value">
                <template v-if="i.route">
                  <van-field
                    readonly
                    :placeholder="`请选择您的${i.name}`"
                    @click="jumpCarBrands(i.route)"
                    v-model="params[i.key]"
                    class="readonly"
                    right-icon="arrow"
                  ></van-field>
                </template>
                <template v-else-if="i.type == 'plate'">
                  <van-field
                    readonly
                    :placeholder="`请填写您的${i.name}`"
                    @click="showPlate"
                    v-model="params[i.key]"
                    class="readonly"
                  ></van-field>
                </template>
                <template v-else>
                  <van-field
                    v-model="params[i.key]"
                    :placeholder="`请填写您的${i.name}`"
                    :maxlength="i.maxlength"
                    :type="i.type"
                    clearable
                  />
                </template>
              </div>
            </div>
          </template>
        </template>
        <template v-if="userSelectGoodsList.length > 1">
          <div class="goods-box">
            <goods-list
              :list="userSelectGoodsList"
              :defaultType="false"
            ></goods-list>
            <template v-if="totalPrice.decimal">
              <div class="total-price-box flex">
                <div class="total-price">
                  合计:
                  <div class="price">
                    <span class="unit">¥</span>
                    {{ totalPrice.price }}
                    <span class="decimal">.{{ totalPrice.decimal }}</span>
                  </div>
                </div>
              </div>

            </template>
          </div>
        </template>
        <!-- <div class="tips">
        本人已阅读且同意<span @click="jump('agreement')">《隐私协议》</span>
      </div> -->
        <template v-if="ISWXWORK">
          <van-button
            color="#b1b1b1"
            class="btn disabled"
          >企微暂不支持支付</van-button>
        </template>
        <template v-else>
          <van-button
            color="#f13c3d"
            class="btn"
            @click="pay"
            loading-text="支付中..."
            :loading="payLoading"
          >立即支付</van-button>
        </template>
      </div>
    </van-action-sheet>
    <license-plate
      ref="plate"
      @done="plateDone"
    ></license-plate>
  </div>
</template>

<script type="text/ecmascript-6">
import { ssoApi } from '@/api'
import { Notify } from 'vant'
import store from '@/store'
export default {
  name: 'PayModal',
  props: {
    value: Boolean,
    list: {
      type: Array,
      default: () => []
    },
    payStatus: Number,
    carInfo: {
      type: Object,
      default: () => { }
    }
  },
  watch: {
    value (n) {
      let { phone } = this.getUserInfo()
      this.showBox = n
      if (n && !this.params) {
        this.items = [
          { name: '姓名', key: 'userName', maxlength: 40 },
          // { name: '电话', key: 'userPhone', maxlength: 11, type: 'tel' },
          ...this.list
        ]
        console.log(this.items)
        let params = {}
        this.items.map(e => {
          params[e.key] = ''
          if (e.route) this.hasCarBrand = true
          if (e.key == 'userPhone') {
            this.hasPhoneSelect = true
            if (phone) params[e.key] = phone
          }
        })
        this.params = params
        let { hasDefaultBrands, brandInfo, userSelectGoodsList } = this.$parent.detail
        if (hasDefaultBrands && !this.params.carBrandsText) {
          this.params.carBrandsText = `${brandInfo.vehicleBrandName} ${brandInfo.vehicleSubBrandName}`
        }
        if (userSelectGoodsList) {
          this.userSelectGoodsList = userSelectGoodsList
          this.bus.$on('changeUserSelectGoodsList', () => {
            this.setTotalPrice()
          })
          this.setTotalPrice()
        }
      }
    },
    payStatus () {
      this.payLoading = false
    },
    carInfo (n) {
      console.table([JSON.parse(JSON.stringify(n))])
      if (this.params) {
        this.params.carBrandsText = `${n.vehicleBrandName} ${n.vehicleSubBrandName}`
        this.params.carTypeText = `${n.vehicleSeriesName} ${n.vehicleShapeName}`
      }
    },
  },
  data () {
    return {
      showBox: false,
      payLoading: false,
      items: [],
      params: null,
      hasCarBrand: false,
      hasPhoneSelect: false,
      userSelectGoodsList: [],
      totalPrice: {}
    }
  },
  mounted () {

  },
  methods: {
    setTotalPrice () {
      const {
        userSelectGoodsList
      } = this
      let totalPrice = 0,
        totalGoodsNumber = 0
      userSelectGoodsList && userSelectGoodsList.map(e => {
        totalPrice += (e.activityPrice * e.goodsNumber)
        totalGoodsNumber += e.goodsNumber
      })
      let [price, dec] = this.decimal(totalPrice).toString().split('.')
      if (dec) dec = dec.length == 1 ? `${dec}0` : dec
      this.totalPrice = {
        price,
        decimal: dec || '00'
      }
      if (totalGoodsNumber == 0) this.onCancel()
    },
    onCancel () {
      this.$emit('cancel')
    },
    pay () {
      let { params, items, hasCarBrand, carInfo, hasPhoneSelect } = this,
        obj = {},
        phoneRex = /^[1]([3-9])[0-9]{9}$/,
        userInfo = this.getUserInfo()
      console.log(params)
      const onInput = ["plateNo"]
      try {
        items.map(e => {
          Object.keys(params).map(key => {
            if (!params[key] && key == e.key && e.name && !onInput.includes(key)) throw e.name
          })
        })
      } catch (error) {
        this.showToast(`请填写${error}`)
        return
      }
      if (params.userPhone && !phoneRex.test(params.userPhone)) {
        this.showToast('请填写正确的手机号')
        return
      }
      Object.keys(params).map(key => {
        if (params[key]) obj[key] = params[key]
      })
      if (hasCarBrand) {
        delete obj.carBrandsText
        delete obj.carTypeText
        obj = {
          ...obj,
          ...carInfo
        }
      }
      if (userInfo.phone) this.emit(obj)
      else {
        ssoApi.captchaLogin({
          mobile: params.userPhone || '88888888888',
          captcha: 8888,
          source: 'CROWDFUNDING_H5'
        }).then(({ code, data, message }) => {
          if (code == '0') {
            this.setUserInfo(data)
            this.setSessionId(data.sessionId)
            store.dispatch('registerHttp', data.sessionId)
            this.emit(obj)
          }
          else Notify(message)
        })
      }
    },
    emit (obj) {
      console.log(obj)
      this.payLoading = true
      this.$emit('pay', { obj, that: this.$parent })
    },
    jumpCarBrands (name) {
      let { hasDefaultBrands, brandInfo } = this.$parent.detail,
        { carInfo } = this
      let chooseItem = Object.keys(carInfo).length
      if (name == 'carSeries') {
        if (chooseItem || hasDefaultBrands) {
          let id = chooseItem ? carInfo.vehicleSubBrandId : brandInfo.vehicleSubBrandId
          let name = chooseItem ? carInfo.vehicleSubBrandName : brandInfo.vehicleSubBrandName
          let list = [{
            id,
            name,
            childs: null
          }]
          this.to('crowdfunding.carSeries', {
            brandInfo: chooseItem ? carInfo : brandInfo,
            list
          })
          return
        }
      }
      this.to('crowdfunding.carBrands')
    },
    showPlate () {
      this.$refs.plate.toggle()
    },
    plateDone ({ value }) {
      this.params.plateNo = value
    }
  }
}
</script>

<style scoped lang="scss">
.box {
  padding: 8px 16px 16px;
  .item {
    border-bottom: 1px solid #f1f1f1;
    height: 49px;
    color: #282829;
    .label {
      font-size: 12px;
      width: 86px;
      span {
        color: #f13c3d;
      }
    }
    .value {
      width: calc(100% - 86px);
      /deep/.van-field {
        padding: 0;
        font-size: 12px;
      }
      .readonly {
        /deep/.van-field__control {
          white-space: nowrap;
          overflow: hidden;
          text-overflow: ellipsis;
        }
      }
    }
  }
  .total-price-box {
    height: 54px;
    justify-content: flex-end;
    padding: 0 16px;
    .total-price {
      display: flex;
      align-items: baseline;
      font-size: 14px;
      color: #333333;

      .price {
        font-weight: 600;
        font-size: 24px;
        color: #f13c3d;
        display: flex;
        align-items: baseline;

        .decimal,
        .unit {
          font-size: 12px;
        }
      }
    }
  }
  .btn {
    display: block;
    font-size: 16px;
    font-weight: 600;
    color: #ffffff;
    width: 100%;
    height: 44px;
    box-shadow: 0px 2px 6px 2px rgba(241, 60, 61, 0.1);
    border-radius: 4px;
    border: 1px solid #f13c3d;
    margin: 16px auto 0;
  }
  .disabled {
    box-shadow: 0px 2px 6px 2px rgba(177, 177, 177, 0.1);
    border: 1px solid #b1b1b1;
  }
}
</style>