withdrawal.js 7.45 KB
// packageIndex/pages/integral/toCash/toCash.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    navStyle: "background-color: transparent;",
    showBank: false,
    agree: false,
    btnDisabled: true,
    isConfig: true,
    showAuthDialog: false,
    showConfirmDialog: false,
    sumbitScore: '',
    disclaimerAgreementUrl: '',
    buttons: [{
      text: '取消'
    }, {
      text: '同意',
      extClass: 'btn_primary'
    }],
    confirmButtons: [{
      text: '取消'
    }, {
      text: '确认',
      extClass: 'btn_primary'
    }],
    score: '0',
    bankInfo: {},
    bankList: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    wx.p.router.getRouterParams().then(params => {
      this.setData(params)
    })
    this.getBankList()
  },
  back() {
    wx.p.router.back()
  },
  jump() {

  },
  async getBankList() {
    const {
      phone,
      channelSource,
      merchantId,
      merchantName
    } = wx.p.storage.get('userInfo'),
      res = await wx.p.api.withdrawalApi.getStaffBankList({
        staffPhone: phone,
        channelSource,
        merchantId,
        merchantName
      })
    if (res.code == 0) {
      res && res.data?.map(e => {
        e.bankNum = e.bankNumber.replace(/^(?:\d+)(.{4})$/, '**** **** **** $1')
      })
      this.setData({
        bankList: res.data || [],
        bankInfo: res.data[0]
      }, () => {
        this.checkbankConfig()
        //获取免责协议地址获取
        this.getAgreementUrl()
      })
    }

  },
  checkbankConfig() {
    let {
      bankInfo
    } = this.data, {
      merchantId,
      merchantName,
      channelSource
    } = wx.p.storage.get('userInfo');
    wx.p.api.withdrawalApi.checkbankConfig({
      id: bankInfo.id,
      merchantId,
      merchantName,
      channelSource
    }).then(res => {
      if (res.code == 0) {
        this.setData({
          isConfig: res.data
        })
      }
    })
  },
  getAgreementUrl() {
    let {
      bankInfo
    } = this.data;
    wx.p.api.withdrawalApi.getAgreementUrl({
      bankUserName: bankInfo.bankUserName
    }).then(res => {
      if (res.code == 0) {
        this.setData({
          disclaimerAgreementUrl: res.data
        })
      }
    }).catch((err) => {
      wx.p.wxApi.showToast(err)
    })
  },
  showBankModel() {
    this.setData({
      showBank: true
    })
    wx.setPageStyle({
      style: {
        overflow: 'hidden'
      }
    })
  },
  bindPickerChange(e) {
    let bankInfo = e.detail
    this.setData({
      bankInfo,
      showBank: false
    }, () => {
      wx.setPageStyle({
        style: {
          overflow: 'unset'
        }
      })
      this.checkbankConfig()
      this.getAgreementUrl()
    })
  },
  updateBankConfig() {
    let {
      bankInfo,
      score
    } = this.data;
    wx.showModal({
      title: '提示',
      content: '请上传您持卡人对应的身份证正反面照片!',
      confirmText: '确认上传',
      cancelText: '取消',
      confirmColor: '#F13C3D'
    }).then(res => {
      if (res.confirm) {
        wx.p.router.go('/packageIndex/pages/bankAccount/add/add', {
          item: bankInfo,
          score
        })
      }
    })
  },
  goBank() {
    let {
      score
    } = this.data;
    wx.p.router.go('/packageIndex/pages/bankAccount/add/add', {
      score
    })
  },
  toSumbitData() {
    let {
      sumbitScore,
      disclaimerAgreementUrl,
      bankInfo: {
        id
      },
      isConfig
    } = this.data;
    if (!sumbitScore) return wx.p.wxApi.showToast('请输入提现积分')
    const checkAgree = this.checkAgree()
    if (checkAgree) return false
    if (isConfig) {
      this.updateBankConfig()
      return false
    }
    const money = this.accSub(Number(sumbitScore), 0)
    if (money <= 0) return wx.p.wxApi.showToast(`您的积分不足,请确认后重新申请兑换`)
    wx.p.router.go('/packageIndex/pages/integral/withdrawalRules/withdrawalRules', {
      sumbitScore,
      disclaimerAgreementUrl,
      bankId: id
    })
  },
  accSub(arg1, arg2) {
    let r1, r2, m, n;
    try {
      r1 = arg1.toString().split(".")[1].length;
    } catch (e) {
      r1 = 0;
    }
    try {
      r2 = arg2.toString().split(".")[1].length;
    } catch (e) {
      r2 = 0;
    }
    m = Math.pow(10, Math.max(r1, r2)); //last modify by deeka //动态控制精度长度
    n = (r1 >= r2) ? r1 : r2;
    return ((arg1 * m - arg2 * m) / m).toFixed(n);
  },
  toTixianAll() {
    let {
      score
    } = this.data;
    this.setData({
      sumbitScore: score
    },()=>{
      this.checkBtn(score)
    })
  },
  handleInput(e) {
    let {
      value
    } = e.detail
    let {
      sumbitScore,
      score
    } = this.data;
    sumbitScore = this.priceVerify(value, score)
    this.setData({
      sumbitScore: sumbitScore || ''
    }, () => {
      this.checkBtn(sumbitScore)
    })
  },
  checkBtn(score) {
    this.setData({
      btnDisabled: Number(score) ? false : true
    })
  },
  // 价格输入校验规则(提现积分值:最小值需大于0)
  priceVerify(price, max, min = 0) {
    console.log(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.'
      }
    }
    // 4. 处理多个小数点的情况,只保留第一个小数点
    const dotIndex = price.indexOf('.')
    if (dotIndex !== -1) {
      const beforeDot = price.substring(0, dotIndex)
      const afterDot = price.substring(dotIndex + 1).replace(/\./g, '')
      price = beforeDot + '.' + afterDot
    }

    result = price
    // 4.判断最大值和最小值
    if (price) {
      price = Number(price)
      max = Number(max)
      if ((max || max === 0) && price > max) {
        result = max
      }
      if ((min || min === 0) && price < min) {
        result = min
      }
    }
    return result
  },
  navbarReady(e) {
    let {
      height
    } = e.detail
    this.setData({
      navHeight: height
    })
  },
  checkAgree() {
    let {
      agree
    } = this.data
    if (!agree) {
      this.setData({
        showAuthDialog: true
      })
      return true
    }
    return false
  },
  authChoose(e) {
    this.setData({
      agree: e.detail
    })
  },
  agreeEvent(e) {
    console.log(e)
    let {
      value
    } = e.detail
    if (value == 1) {
      this.setData({
        showAuthDialog: false,
        agree: true
      })
    } else {
      this.setData({
        showAuthDialog: false
      })
    }

  },
  onClose(e) {
    console.log(e)
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    if (this.refresh) {
      this.getBankList()
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {
    this.refresh = true;
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})