纸通宝小程序
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 

236 lines
7.0 KiB

// pages/bank/edit/index.js
import { getBankcardInfo, ocrBankcard, verifyBankcard } from "../../api/payment"
import { postCaptcha } from "../../api/user"
import { getBaseInfo } from "../../api/ztb"
const util = require('../../../utils/util')
import { $wuxCountDown } from '../../../components/index'
const app = getApp()
Page({
/**
* 页面的初始数据
*/
data: {
userInfo: null,
form: {
phone: null, //银行卡预留手机号
cardName: null, //银行名称
cardNo: null, //卡号
openingBank: null,
cardType: null, //银行卡类型
idno: null, //身份证号
name: null, //持卡人姓名
verificationCode: null, //验证码
isExist: 0, //查询银行卡是否重复1重复0不重复
type: 2
},
bankName: null,
cardType: null,
second: null,
codeEnable: true
},
/**
* 生命周期函数--监听页面加载
*/
onLoad: function (options) {
if(util.isEmpty(app.globalData.userInfo.realName)){
wx.showLoading({ title: '加载中', mask: true })
getBaseInfo().then(result => {
app.globalData.userInfo = result.data
this.setData({ userInfo: app.globalData.userInfo, ['form.name']: app.globalData.userInfo.realName })
wx.hideLoading()
}).catch(err => {
wx.hideLoading()
})
} else {
this.setData({ userInfo: app.globalData.userInfo, ['form.name']: app.globalData.userInfo.realName })
}
},
/**
* 生命周期函数--监听页面初次渲染完成
*/
bindForm: function (e) {
// var form_data = 'form.' + e.target.id
this.data.form[e.target.id] = e.detail.value
if (e.target.id == 'cardNo') {
this.setData({
['form.cardName']: null,
bankName: null,
['form.cardType']: null,
cardType: null
})
}
},
inputBlur: function (e) {
if (e.target.id == 'idno' && !util.isEmpty(e.detail.value)) {
var result = util.checkId(e.detail.value)
if (result.status == 0) {
util.showToast(result.msg)
}
} else if (e.target.id == 'cardNo' && !util.isEmpty(e.detail.value)) {
if (!util.isEmpty(e.detail.value)) {
this.data.form.cardNo = e.detail.value
}
}
},
checkCardNo: function(cardNo){
cardNo = cardNo.replace(/\s+/g, '')
getBankcardInfo(cardNo).then(result => {
this.setData({
['form.cardName']: result.data.bank,
bankName: result.data.bankName,
['form.cardType']: result.data.cardType,
cardType: this.getCardType(result.data.cardType)
})
}).catch(error => {
util.showToast(error)
})
},
getCardType: function(type){
if('DC' == type){
return '储蓄卡'
}
if('CC' == type){
return '信用卡'
}
if('SCC' == type){
return '准贷记卡'
}
if('PC' == type){
return '预付费卡'
}
return ''
},
fetchCode: function () {
if (util.isEmpty(this.data.form.name)) {
util.showToast('请输入持卡人姓名')
return
}
if (util.isEmpty(this.data.form.cardNo)) {
util.showToast('请输入银行卡卡号')
return
}
if (util.isEmpty(this.data.form.phone)) {
util.showToast('请输入手机号码')
return
}
if (!(/^1\d{10}$/.test(this.data.form.phone)) || this.data.form.phone.length < 11) {
util.showToast('请输入11位手机号码')
return
}
this.data.form.cardNo = this.data.form.cardNo.replace(/\s+/g, '')
wx.showLoading({ title: '处理中', mask: true })
postCaptcha({ verifiableAccount: this.data.form.phone, verifiableAccountType: 1, purpose: 4 }).then(result => {
wx.hideLoading()
this.setData({ codeEnable: false })
util.showToast('验证码已经发送')
this.second = new $wuxCountDown({
date: +(new Date) + 60000,
onEnd() {
this.setData({
second: '重新获取验证码',
codeEnable: true
})
},
render(date) {
const sec = this.leadingZeros(date.sec, 2) + ' 秒 '
date.sec !== 0 && this.setData({
second: sec,
})
}
})
}).catch(error => {
wx.hideLoading()
util.showToast(error)
})
},
submitForm: function(){
if (util.isEmpty(this.data.bankName) && !util.isEmpty(this.data.form.cardNo)) {
this.checkCardNo(this.data.form.cardNo)
}
if (util.isEmpty(this.data.form.name)) {
util.showToast('请输入持卡人姓名')
return
}
if (util.isEmpty(this.data.form.cardNo)) {
util.showToast('请输入银行卡卡号')
return
}
// if (util.isEmpty(this.data.form.cardName)) {
// util.showToast('输入银行卡卡号暂无信息,请重新输入银行卡卡号')
// return
// }
// if (util.isEmpty(this.data.form.idno) && this.data.form.type == 4) {
// util.showToast('请输入身份证号码')
// return
// }
// if (util.checkId(this.data.form.idno).status == 0 && this.data.form.type == 4) {
// util.showToast('身份证号码有误')
// return
// }
if (util.isEmpty(this.data.form.phone) && this.data.form.type == 4) {
util.showToast('请输入手机号码')
return
}
if (this.data.form.type == 4 && (!(/^1\d{10}$/.test(this.data.form.phone)) || this.data.form.phone.length < 11)) {
util.showToast('请输入11位手机号码')
return
}
if (util.isEmpty(this.data.form.verificationCode) && this.data.form.type == 4) {
util.showToast('请输入验证码')
return
}
wx.showLoading({ title: '处理中', mask: true })
verifyBankcard(this.data.form).then(res => {
wx.hideLoading()
if(res.data.trxstatus == 1){
if(this.data.form.type == 2){
this.setData({ ['form.type']: 4 })
} else {
util.showBackToast('添加成功')
}
} else{
util.showToast('银行卡添加失败')
}
}).catch(error => {
wx.hideLoading()
util.showToast(error)
})
},
blankSuccess: function(e){
this.setData({ ['form.cardNo']: e.detail.number.text })
this.checkCardNo(this.data.form.cardNo)
},
scanCard: function(){
var that = this
wx.chooseImage({
count: 1, //默认9
sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图,默认二者都有
sourceType: ['album', 'camera'], //从相册选择
success: (res) => {
const fileSystemManager = wx.getFileSystemManager()
fileSystemManager.readFile({
filePath: res.tempFilePaths[0], // 例如图片临时路径
encoding: 'base64',
success(res) {
that.ocrBankcard(res.data)
}
})
}
})
},
ocrBankcard: function(base64Image){
wx.showLoading({ title: '处理中', mask: true })
ocrBankcard({base64Image}).then(res => {
wx.hideLoading()
this.setData({ ['form.cardNo']: res.data.card_num })
this.checkCardNo(res.data.card_num)
}).catch(error => {
wx.hideLoading()
util.showToast(error)
})
}
})