PayModal.vue 6.8 KB
<template>
  <van-action-sheet
    v-model="showBox"
    @click-overlay="onCancel"
  >
    <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>*</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>
                <van-field
                  v-model="params[i.key]"
                  :placeholder="`请填写您的${i.name}`"
                  :maxlength="i.maxlength"
                  :type="i.type"
                  clearable
                />
              </template>
            </div>
          </div>
        </template>
      </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>
</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
        ]
        let params = {}
        this.items.map(e => {
          params[e.key] = ''
          if (e.route) this.hasCarBrand = true
        })
        this.params = params
        if (!this.params.userPhone) this.params.userPhone = phone
        let { hasDefaultBrands, brandInfo } = this.$parent.detail
        if (hasDefaultBrands && !this.params.carBrandsText) {
          this.params.carBrandsText = `${brandInfo.vehicleBrandName} ${brandInfo.vehicleSubBrandName}`
        }
      }
    },
    payStatus () {
      this.payLoading = false
    },
    carInfo (n) {
      console.table([JSON.parse(JSON.stringify(n))])
      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
    }
  },
  mounted () {

  },
  methods: {
    onCancel () {
      this.$emit('cancel')
    },
    pay () {
      let { params, items, hasCarBrand, carInfo } = this,
        obj = {},
        phoneRex = /^[1]([3-9])[0-9]{9}$/,
        userInfo = this.getUserInfo(),
        platNumberRex = /^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}(([A-Z0-9]{4}[A-Z0-9挂学警港澳]{1}$)|([0-9]{5}[DF]$)|([DF][A-HJ-NP-Z0-9][0-9]{4}$))/

      console.log(params)
      try {
        items.map(e => {
          Object.keys(params).map(key => {
            if (!params[key] && key == e.key && e.name) throw e.name
          })
        })
      } catch (error) {
        this.showToast(`请填写${error}`)
        return
      }
      if (!phoneRex.test(params.userPhone)) {
        this.showToast('请填写正确的手机号')
        return
      }
      if (params.plateNo && !platNumberRex.test(params.plateNo)) {
        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,
        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')
    }
  }
}
</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;
        }
      }
    }
  }
  .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>