js 銀行卡正則校驗

// pages/wapManageFinance/bankManage.js
const {
  getProvinceList,
  getCityList,
  getCountyList,
  addMemberBank,
  memberBankList,
  delBank
} = require('../../../common/http.js')
Page({
  /**
   * 頁面的初始數據
   */
  data: {
    isaddBank: false,
    bankName: '',
    regionName: '',
    bankList: ['工商銀行', '農業銀行', '建設銀行', '招商銀行', '北京銀行', '平安銀行', '交通銀行', '中國銀行', '興業銀行', '民生銀行', '光大銀行','中信銀行','郵政儲蓄銀行', '農村商業銀行', '廣大銀行', '阿拉善左旗方大村鎮銀行', '安徽肥西石銀村鎮銀行'],
    provinceArray: [], //省列表數據
    provinceName: '',
    provinceId: null,
    cityArray: [], //城市列表數據
    cityName: '',
    cityId: null,
    countyArray: [], //地區列表數據
    dataList: []
  },
  //查詢銀行卡
  getMemberBankList() {
    let data = { sdMemberId: wx.getStorageSync('sdMemberId') }
    if(+this.data.type === 0){
      data.sdMemberId = wx.getStorageSync('formerMemberId')
    }
    memberBankList(data).then(res => {
      this.setData({
        dataList: res.data.memberBankList
      })
    })
  },
  //省列表請求
  getProvince() {
    getProvinceList().then(res => {
      if (res.data.code === 0) {
        this.setData({
          provinceArray: res.data.list
        })
      }
    })
  },
  //城市列表請求
  getCity(id) {
    getCityList({
      id
    }).then(res => {
      if (res.data.code === 0) {
        this.setData({
          cityArray: res.data.list
        })
      }
    })
  },
  //區列表請求
  // getCounty(id) {
  //   getCountyList({
  //     id
  //   }).then(res => {
  //     if (res.data.code === 0) {
  //       this.setData({
  //         countyArray: res.data.list
  //       })
  //     }
  //   })
  // },
  //省份 pirck
  provinceChange(e) {
    let index = e.detail.value
    let id = this.data.provinceArray[index].id
    //請求城市
    this.getCity(id)
    this.setData({
      provinceName: this.data.provinceArray[index].name,
      provinceId: id
    })
  },
  //城市 pirck
  cityChange(e) {
    let index = e.detail.value
    let id = this.data.cityArray[index].id
    //請求地區
    // this.getCounty(id)
    this.setData({
      cityName: this.data.cityArray[index].name,
      cityId: id
    })
  },
  //區縣 pirck
  // countyChange(e) {
  //   let index = e.detail.value
  //   let id = this.data.countyArray[index].id
  //   this.setData({
  //     countyName: this.data.countyArray[index].name,
  //     countyId: id
  //   })
  // },
  blackUp() {
    wx.navigateBack({
      delta: 1
    })
  },
  //打開填寫窗
  openShade() {
    this.setData({
      isaddBank: true
    })
    this.getProvince()
  },
  //關閉填寫窗
  closeShade() {
    this.setData({
      isaddBank: false,
      bankName: '',
      regionName: '',
      provinceArray: [], //省列表數據
      provinceName: '',
      cityArray: [], //城市列表數據
      cityName: ''
    })
  },
  //開戶行
  bankChange(e) {
    this.setData({
      bankName: this.data.bankList[e.detail.value]
    })
  },
  //添加銀行卡信息
  formSubmit(e) {
    let {
      bankName,
      bankAccount,
      bankNo,
      branchName
    } = e.detail.value
    let data = {
      bankName,
      bankAccount,
      bankNo,
      branchName,
      provinceId: this.data.provinceId,
      cityId: this.data.cityId,
      // countyId: this.data.countyId,
      isDefault: 0,
      sdMemberId: wx.getStorageSync('sdMemberId')
    }
    if(+this.data.type === 0){
      data.sdMemberId = wx.getStorageSync('formerMemberId')
    }
    if (!bankName || !bankAccount || !bankNo || !branchName || !data.provinceId || !data.cityId) {
      wx.showToast({
        title: '請填寫完整的信息',
        icon: 'none',
        duration: 3000
      })
      return
    }
    if (this.checkBankno(bankNo) === false){
      wx.showToast({
        title: '銀行卡卡號不正確',
        icon: 'none',
        duration: 3000
      })
      return
    }
    wx.showLoading({
      title: '加載中',
      mask: true
    })
    addMemberBank(data).then(res => {
      wx.hideLoading()
      if (res.data.code === 0) {
        wx.showToast({
          title: '添加成功',
          icon: 'none',
          duration: 1500
        })
        this.setData({
          isaddBank: false
        })
        this.getMemberBankList()
      }
    })
  },
  //刪除銀行卡
  deleteBank(e) {
    let id = e.currentTarget.dataset.id
    wx.showLoading({
      title: '加載中',
      mask: true
    })
    delBank({
      id
    }).then(res => {
      wx.hideLoading()
      if (res.data.code === 0) {
        wx.showToast({
          title: '刪除成功',
          icon: 'none',
          duration: 1500
        })
        this.getMemberBankList()
      }
    })
  },
  
  //銀行卡正則校驗
  checkBankno(bankno) {
    var lastNum = bankno.substr(bankno.length - 1, 1); //取出最後一位(與luhm進行比較)
    var first15Num = bankno.substr(0, bankno.length - 1); //前15或18位
    var newArr = [];

    for (var i = first15Num.length - 1; i > -1; i--) { //前15或18位倒序存進數組
      newArr.push(first15Num.substr(i, 1));
    }

    var arrJiShu = []; //奇數位*2的積 <9
    var arrJiShu2 = []; //奇數位*2的積 >9
    var arrOuShu = []; //偶數位數組
    for (var j = 0; j < newArr.length; j++) {
      if ((j + 1) % 2 == 1) { //奇數位
        if (parseInt(newArr[j]) * 2 < 9)
          arrJiShu.push(parseInt(newArr[j]) * 2);
        else
          arrJiShu2.push(parseInt(newArr[j]) * 2);
      } else //偶數位
        arrOuShu.push(newArr[j]);
    }

    var jishu_child1 = []; //奇數位*2 >9 的分割之後的數組個位數
    var jishu_child2 = []; //奇數位*2 >9 的分割之後的數組十位數
    for (var h = 0; h < arrJiShu2.length; h++) {
      jishu_child1.push(parseInt(arrJiShu2[h]) % 10);
      jishu_child2.push(parseInt(arrJiShu2[h]) / 10);
    }

    var sumJiShu = 0; //奇數位*2 < 9 的數組之和
    var sumOuShu = 0; //偶數位數組之和
    var sumJiShuChild1 = 0; //奇數位*2 >9 的分割之後的數組個位數之和
    var sumJiShuChild2 = 0; //奇數位*2 >9 的分割之後的數組十位數之和
    var sumTotal = 0;
    for (var m = 0; m < arrJiShu.length; m++) {
      sumJiShu = sumJiShu + parseInt(arrJiShu[m]);
    }
    for (var n = 0; n < arrOuShu.length; n++) {
      sumOuShu = sumOuShu + parseInt(arrOuShu[n]);
    }
    for (var p = 0; p < jishu_child1.length; p++) {
      sumJiShuChild1 = sumJiShuChild1 + parseInt(jishu_child1[p]);
      sumJiShuChild2 = sumJiShuChild2 + parseInt(jishu_child2[p]);
    }
    //計算總和
    sumTotal = parseInt(sumJiShu) + parseInt(sumOuShu) + parseInt(sumJiShuChild1) + parseInt(sumJiShuChild2);
    //計算Luhm值
    var k = parseInt(sumTotal) % 10 == 0 ? 10 : parseInt(sumTotal) % 10;
    var luhm = 10 - k;
    if (lastNum == luhm) {
      return true;
    } else {
      return false;
    }
  },
  /**
   * 生命週期函數--監聽頁面加載
   */
  onLoad: function(options) {
    if (options.type){
      this.setData({ type: options.type})
    }
    this.getMemberBankList()
  },

  /**
   * 生命週期函數--監聽頁面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命週期函數--監聽頁面顯示
   */
  onShow: function() {

  },

  /**
   * 生命週期函數--監聽頁面隱藏
   */
  onHide: function() {

  },

  /**
   * 生命週期函數--監聽頁面卸載
   */
  onUnload: function() {

  },

  /**
   * 頁面相關事件處理函數--監聽用戶下拉動作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 頁面上拉觸底事件的處理函數
   */
  onReachBottom: function() {

  }
})

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章