util.js 积累的一些基础函数代码
export function checkMobile(mobile) {
var index1 = mobile.substring(0, 1);
var index2 = mobile.substring(0, 2);
if (index2 == '11' || index2 == '12' || index1 !== '1' || mobile.length !== 11) {
return false;
} else {
return true;
}
}
export function checkPwd(pwd) {
let pwdreg = /^(?! [^a - zA - Z] + $)(?! \D + $)/;
return pwdreg.test(pwd);
}
export function checkIdcard(idcard) {
var cardreg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
console.log(idcard)
return cardreg.test(idcard);
}
export function checkHz(val) {
var hzreg = /^[\u4e00 - \u9fff] + $/;
return hzreg.test(val);
}
function add0(m) {
return m < 10 ? '0' + m : m
}
export function format(shijianchuo) {
//shijianchuo是整数,否则要parseInt转换
var time = new Date(shijianchuo);
var y = time.getFullYear();
var m = time.getMonth() + 1;
var d = time.getDate();
var h = time.getHours();
var mm = time.getMinutes();
var s = time.getSeconds();
return y + '-' + add0(m) + '-' + add0(d) + ' ' + add0(h) + ':' + add0(mm) + ':' + add0(s);
}
export function luhnCheck(bankno) {
var lastNum = bankno.substr(bankno.length - 1, 1);
var first15Num = bankno.substr(0, bankno.length - 1);
var newArr = new Array();
for (var i = first15Num.length - 1; i > -1; i--) {
newArr.push(first15Num.substr(i, 1));
}
var arrJiShu = new Array();
var arrJiShu2 = new Array();
var arrOuShu = new Array();
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 = new Array();
var jishu_child2 = new Array();
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;
var sumOuShu = 0;
var sumJiShuChild1 = 0;
var sumJiShuChild2 = 0;
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);
//计算luhn值
var k = parseInt(sumTotal) % 10 == 0 ? 10 : parseInt(sumTotal) % 10;
var luhn = 10 - k;
if (lastNum == luhn) {
console.log("luhn验证通过");
return true;
} else {
console.log("银行卡号必须符合luhn校验");
return false;
}
}
import Cookies from 'js-cookie' // const TokenKey = 'Mobile-Token' export function getToken(key) { return Cookies.get(key) } export function setToken(key, token) { return Cookies.set(key, token) } export function removeToken(key) { return Cookies.remove(key) }
formatDateTime(inputTime) { var date = new Date(inputTime); var y = date.getFullYear(); var m = date.getMonth() + 1; m = m < 10 ? ('0' + m) : m; var d = date.getDate(); d = d < 10 ? ('0' + d) : d; var h = date.getHours(); h = h < 10 ? ('0' + h) : h; var minute = date.getMinutes(); var second = date.getSeconds(); minute = minute < 10 ? ('0' + minute) : minute; second = second < 10 ? ('0' + second) : second; return y + '-' + m + '-' + d + ' ' + h + ':' + minute; }
export function getDecrypt(token,batchNo,cardCode) {
var secretKey = token + batchNo
var shaSecretKey = sha256(secretKey).toString()
console.log(shaSecretKey)
var aesKey = shaSecretKey.substring(0, 32)
aesKey = Crypt.enc.Utf8.parse(aesKey)
var aesIv = shaSecretKey.substring(32, 32 + 16)
aesIv = Crypt.enc.Utf8.parse(aesIv)
var decoded = Crypt.AES.decrypt(cardCode, aesKey, {
iv: aesIv,
mode: Crypt.mode.CBC,
padding: Crypt.pad.ZeroPadding
})
console.log("解密卡密:")
console.log(Crypt.enc.Utf8.stringify(decoded))
return Crypt.enc.Utf8.stringify(decoded)
}
//获取文件真实类型
export function getImageFileType(file) {
var returnObj = {
type: '',
size: ''
}
return new Promise(function (reslove, reject) {
const typeFile = new TypeFile(file);
typeFile.onParseEnd = function () {
returnObj.type = typeFile.ext;
returnObj.size = typeFile.input.size / 1024 / 1024;
reslove(returnObj)
};
typeFile.onParseError = function () {
reslove(returnObj)
}
typeFile.start();
})
}
/**
*
* @param {dataset} dataset 数组对象
*/
export function jsonFormat(dataset) {
//dataset是一个对象,dataset.source是一个二维数组
const data = dataset.source
const tableColumns = dataset.source[0]
let datajson = {}
var jsonresult = []
for (let i = 0; i < data.length; i++) {
for (let j = 0; j < tableColumns.length; j++) {
datajson[tableColumns[j]] = data[i][j]
}
jsonresult.push(datajson)
datajson = {}
}
return jsonresult
}
/****
* 图片file转 Blob显示
*/
export function dataURItoBlob(file) {
let reader = new FileReader();
let rs = reader.readAsArrayBuffer(file);
let blob = null;
reader.onload = (e) => {
if (typeof e.target.result === 'object') {
blob = new Blob([e.target.result])
} else {
blob = e.target.result
}
return blob
}
}
/**
* 获取当前时间
* @param {timeout} timeout 时间戳
*/
export const getDateVal = () => {
var date = new Date();
var y = date.getFullYear();
var m = date.getMonth() + 1;
m = m < 10 ? ('0' + m) : m;
var d = date.getDate();
d = d < 10 ? ('0' + d) : d;
var h = date.getHours();
h = h < 10 ? ('0' + h) : h;
var minute = date.getMinutes();
var second = date.getSeconds();
minute = minute < 10 ? ('0' + minute) : minute;
second = second < 10 ? ('0' + second) : second;
return{
'ymd':y + '-' + m + '-' + d,
'ymdhm':y + '-' + m + '-' + d + ' ' + h + ':' + minute,
}
}
export function checkMobile(mobile) {
// let mobilereg = /^(13[0-9]|14[5-9]|15[012356789]|166|17[0-8]|18[0-9]|19[8-9])[0-9]{8}$/;
// return mobilereg.test(mobile);
var index1 = mobile.substring(0, 1);
var index2 = mobile.substring(0, 2);
if (index2 == '11' || index2 == '12' || index1 !== '1' || mobile.length !== 11) {
return false;
} else {
return true;
}
}
export function checkPwd(pwd) {
let pwdreg = /^(?![^a-zA-Z]+$)(?!\D+$)/;
return pwdreg.test(pwd);
}
export function checkIdcard(idcard) {
var cardreg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
return cardreg.test(idcard);
}
//检测汉字export function checkHz(val) {
var hzreg = /^[\u4e00-\u9fff]+$/;
return hzreg.test(val);
}
/**
* URL地址
* @param {*} s
*/
export function isURL (s) {
return /^http[s]?:\/\/.*/.test(s)
}
/**
* 邮箱
* @param {*} s
*/
export function isEmail (s) {
return /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+((.[a-zA-Z0-9_-]{2,3}){1,2})$/.test(s)
}
/**
* 手机号码
* @param {*} s
*/
export function isMobile (s) {
return /^1[0-9]{10}$/.test(s)
}