index.js 3 KB
import { IMG_URL } from '~/utils/constantData'
import MineApi from '~/api/mineApi/index'
import Dialog from '@vant/weapp/dialog/dialog';
Page({
  data: {
    IMGURL: IMG_URL,
    withdrawalScore: 0,
    sumbitScore: ''
  },
  toSumbitData() {
    let { sumbitScore,withdrawalScore } = this.data
    let isString = typeof sumbitScore == 'string'
    if (!sumbitScore) return wx.$api.showToast('请输入有效的积分值')
    if (sumbitScore == 0) return wx.$api.showToast('请输入有效的积分值')
    if(isString&&sumbitScore.indexOf('.')==(sumbitScore.length-1)) return wx.$api.showToast('请输入有效的积分值')
    if(sumbitScore<0.3) return wx.$api.showToast('单次最低提现积分不得低于0.3,请确认后重新申请提现')
    if(Number(sumbitScore)>Number(withdrawalScore)) return wx.$api.showToast(`您的积分不足,请确认后重新申请提现`)
    Dialog.confirm({
      title: '提示',
      message: `请确认是否将${sumbitScore}积分进行提现`,
    }).then(() => {
      wx.$api.showLoading('提交中')
      MineApi.WithdrawalApi({ withdrawalScore: sumbitScore }).then(res => {
        wx.hideLoading();
        if (res.code == 0) {
          wx.$eventbus.emit('REFRESH_WITHDRALWAL')
          wx.$router.redirectTo('packageMine.withdrawalSuccess.index')
        } else {
          wx.$api.showToast(res.message)
        }
      })
    }).catch(()=>{

    });
  },
  onRoute(options) {
    this.setData({
      withdrawalScore: options.withdrawalScore,
      user: options.user,
    })
  },
  toFillCount(e) {
    let { value } = e.detail
    let { withdrawalScore, sumbitScore } = this.data
    sumbitScore = this.priceVerify(value, withdrawalScore)
    this.setData({
      sumbitScore: sumbitScore || ''
    })
  },
  // 失焦校验
  checkNum(e) {
    let { value } = e.detail
    if (value[value.length - 1] == ".") {
      this.setData({
        sumbitScore: ''
      })
    }
  },
  toTixianAll() {
    let { withdrawalScore } = this.data
    this.setData({
      sumbitScore: withdrawalScore
    })
  },
  // 价格输入校验规则(提现积分值:最小值需大于0)
  priceVerify(price, max, min = 0) {
    let result = 0
    // 1.限制小数点为2位
    price = price.replace(/[^\d.]/g, "")
    price = price.replace(/^(\-)*(\d+)\.(\d\d).*$/, '$1$2.$3')
    // 2.禁止第一位为小数点
    if (price.indexOf('.') == 0) {
      price = ''
    }

    // 3.如果第一位是0,那第二位开始禁止为数值,只可以为小数点
    if (price.indexOf('0') == 0 && price.length > 1) {
      if (price.indexOf('.') != 1) {
        price = '0'
      }
      if (price[2] == '.') {
        price = '0.'
      }
    }
    // if (price.indexOf('.') != -1) {
    //   price = price.slice(0, price.indexOf('.'))
    // }
    result = price
    // 4.判断最大值和最小值
    if (price) {
      price = Number(price)
      max = Number(max)
      if (max && price > max) {
        result = max
      }
      if (min && price < min) {
        result = min
      }
    }
    return result
  }
})