本章记录vue开发过程中随机生成手机号、身份证号、银行卡号、不重复订单号四种方法函数
1.随机生成手机号:
getMoble(callback) {
var prefixArray = new Array("130", "131", "132", "133", "135", "137", "138", "170", "187", "189");
var i = parseInt(10 * Math.random());
var prefix = prefixArray[i];
for (var j = 0; j < 8; j++) {
prefix = prefix + Math.floor(Math.random() * 10);
}
callback(prefix)
}
//调用方法为:
this.getMoble(res=> {
console.log('手机号:',res)
})
2.随机生成身份证号:
getId_no(callback){
var coefficientArray = [ "7","9","10","5","8","4","2","1","6","3","7","9","10","5","8","4","2"];// 加权因子
var lastNumberArray = [ "1","0","X","9","8","7","6","5","4","3","2"];// 校验码
var address = "420101"; // 住址
var birthday = "19810101"; // 生日
var s = Math.floor(Math.random()*10).toString() + Math.floor(Math.random()*10).toString() + Math.floor(Math.random()*10).toString();
var array = (address + birthday + s).split("");
var total = 0;
for (const i of array) {
total = total + parseInt(array[i])*parseInt(coefficientArray[i]);
}
var lastNumber = lastNumberArray[parseInt(total%11)];
var id_no_String = address + birthday + s + lastNumber;
callback(id_no_String)
}
//调用方法为:
this.getId_no(res=> {
console.log('身份证号:',res)
})
3.随机生成银行卡号:
getBank_account(callback) {
var arrStr = ['0102', '0103', '0105', '0301', '104', '0303', '305', '0306', '0308', '0410', '302', '304', '309', '310', '315', '316']
var bank_no = arrStr[Math.floor(Math.random() * arrStr.length)]
var prefix = ''
switch (bank_no) {
case '0102':
prefix = '622202'
break
case '0103':
prefix = '622848'
break
case '0105':
prefix = '622700'
break
case '0301':
prefix = '622262'
break
case '104':
prefix = '621661'
break
case '0303':
prefix = '622666'
break
case '305':
prefix = '622622'
break
case '0306':
prefix = '622556'
break
case '0308':
prefix = '622588'
break
case '0410':
prefix = '622155'
break
case '302':
prefix = '622689'
break
case '304':
prefix = '622630'
break
case '309':
prefix = '622908'
break
case '310':
prefix = '621717'
break
case '315':
prefix = '622323'
break
case '316':
prefix = '622309'
break
default:
}
for (var j = 0; j < 12; j++) {
prefix = prefix + Math.floor(Math.random() * 10)
}
callback(prefix)
}
//调用方法为:
this.getBank_account(res=> {
console.log('银行卡号:',res)
})
4.随机生成不重复订单号:
/**
* 随机生成18位不重复的订单编号
* length 随机后缀字符长度,默认为4
*/
generateOrderNumber(length = 4,callback) {
const now = new Date()
let year = now.getFullYear().toString()
let month = (now.getMonth() + 1)
let day = now.getDate()
let hour = now.getHours()
let minutes = now.getMinutes()
let seconds = now.getSeconds()
// 个位数补齐十位数
month = month < 10 ? '0' + month : month
day = day < 10 ? '0' + day : day
hour = hour < 10 ? '0' + hour : hour
minutes = minutes < 10 ? '0' + minutes : minutes
seconds = seconds < 10 ? '0' + seconds : seconds
month = month.toString()
day = day.toString()
hour = hour.toString()
minutes = minutes.toString()
seconds = seconds.toString()
// 存放订单号
let num = ''
// N位随机数(加在时间戳后面)
for (var i = 0; i < length; i++) {
num += Math.floor(Math.random() * 10)
}
callback('Sj' + year + month + day + hour + minutes + seconds + num)
}
//调用方法为:
this.generateOrderNumber(4,res =>{
console.log('订单号:',res)
})