export const debounce = (handler, delay) => {
var timer = null;
return function () {
var _self = this, _arg = arguments;
clearTimeout(timer);
timer = setTimeout(function () {
handler.apply(_self, _arg);
}, delay);
}
}
export const commonSubStr = (value) => {
if (value != '' && value.substr(0, 1) == '.') {
return value = "";
}
value = value.replace(/^0*(0\.|[1-9])/, '$1');
value = value.replace(/[^\d.]/g, "");
value = value.replace(/\.{2,}/g, ".");
value = value.replace(".", "$#$").replace(/\./g, "").replace("$#$", ".");
value = value.replace(/^(\-)*(\d+)\.(\d\d).*$/, '$1$2.$3');
if (value.indexOf(".") < 0 && value != "") {
if (value.substr(0, 1) == '0' && value.length == 2) {
value = value.substr(1, value.length);
}
}
return value
}
export const handleParams = params => {
let commonHeader = {};
commonHeader['app'] = {
token: '',
deviceId: '',
version: '',
wifi: '',
address: ''
}
commonHeader['themeId'] = '';
params['commonHeader'] = commonHeader;
return params;
}
export const setStore = (name, content) => {
if (!name) return;
if (typeof content !== 'string') {
content = JSON.stringify(content);
}
window.localStorage.setItem(name, content);
}
export const getStore = name => {
if (!name) return;
return window.localStorage.getItem(name);
}
export const removeStore = name => {
if (!name) return;
window.localStorage.removeItem(name);
}
export const seesionStore = (name, content) => {
if (!name) return;
if (typeof content !== 'string') {
content = JSON.stringify(content);
}
sessionStorage.setItem(name, content);
}
export const getSeesionStore = name => {
if (!name) return;
return sessionStorage.getItem(name);
}
export const removeSeesionStore = name => {
if (!name) return;
sessionStorage.removeItem(name);
}
export const transDate = (value, format) => {
if (!value) {
return ''
}
let time = new Date(value);
let year = time.getFullYear();
let month = time.getMonth() + 1 + '';
let data = time.getDate() + '';
if (month.length < 2) {
month = '0' + month;
}
if (data.length < 2) {
data = "0" + data
}
let newtime = year + `${format}` + month + `${format}` + data;
return newtime;
}
export const getNowDate = () => {
let time = new Date();
let year = time.getFullYear();
let month = time.getMonth() + 1 + '';
let data = time.getDate() + '';
if (month.length < 2) {
month = '0' + month;
}
if (data.length < 2) {
data = "0" + data
}
let newtime = year + "年" + month + "月" + data + "日";
return newtime
}
export const _getNowDate = () => {
let time = new Date();
let year = time.getFullYear();
let month = time.getMonth() + 1 + '';
let data = time.getDate() + '';
if (month.length < 2) {
month = '0' + month;
}
if (data.length < 2) {
data = "0" + data
}
let newtime = year + "-" + month + "-" + data;
return newtime
}
export const getYearMonth = (value) => {
let year = value.getFullYear();
let month = value.getMonth() + 1 + '';
if (month.length < 2) {
month = '0' + month;
}
let newtime = year + "年" + month + "月";
return newtime
}
export const cloneObj = obj => {
let newObj = JSON.parse(JSON.stringify(obj));
return newObj
}
export const postCodeCheck = value => {
let reg = /^[0-9]{6}$/;
let result = reg.test(value);
return result
}
export const mobileCheck = value => {
let reg = /^1[0-9][0-9]{9}$/;
let result = reg.test(value);
return result
}
export const telephoneCheck = value => {
let reg = /^0\d{2,3}-?\d{7,8}$/;
let result = reg.test(value);
return result
}
export const urlCheck = value => {
let str = `^((https|http|ftp|rtsp|mms)?://)`
+ `?(([0-9a-z_!~*'().&=+$%-]+:)?[0-9a-z_!~*'().&=+$%-]+@)?`
+ `?(([0-9]{1,3}\.){3}[0-9]{1,3}`
+ `|`
+ `([0-9a-z_!~*'()-]+\.)*`
+ `([0-9a-z][0-9a-z]{0,61})?[0-9a-z]\.`
+ `[a-z]{2,6})`
+ `(:[0-9]{1,4})?`
+ `((/?)|`
+ `(/[0-9a-z_!~*'().;?:@&=+$,%#-]+)+/?)$`;
let reg = new RegExp(str);
let result = reg.test(value);
return result
}
export const faxCheck = value => {
let reg = /^(\d{3,4})?[-]?\d{7,8}$/;
let result = reg.test(value);
return result
}
export const emailCheck = value => {
let reg = /^[a-z0-9]+([._\\-]*[a-z0-9])*@([a-z0-9]+[-a-z0-9]*[a-z0-9]+.){1,63}[a-z0-9]+$/;
let result = reg.test(value);
return result
}
export const twoPoinTwo = value => {
var reg = /^[0-9]{0,2}(\.[0-9]{0,2})?$/;
var reg1 = /^[0-9]{0,16}$/
let result = !((!reg.test(value)) && (!reg1.test(value)));
return result
}
export const decimalNumFour = value => {
let reg = /^[+|-]{0,1}[0-9]{0,20}(\.[0-9]{0,4})?$/;
let result = reg.test(value);
return result
}
export const decimalNumTwo = value => {
let reg = /^[+|-]{0,1}[0-9]{0,20}(\.[0-9]{0,2})?$/;
let result = reg.test(value);
return result
}
export const specialSymbol = value => {
let reg = /^[A-Za-z0-9|\u4e00-\u9fa5|\.|\:|\:]+$/;
let result = reg.test(value || null);
return result
}
export const checkCard = value => {
let reg = /^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/;
let result = reg.test(value);
return result
}
export const checkName = value => {
var reg = /^[\u4E00-\u9FA5]{1,6}$/;
let result = reg.test(value);
return result
}
export const checkNumber = value => {
var reg = /^[0-9]*$/;
let result = reg.test(value);
return result
}
export const checkDotNumber = value => {
var reg = /([1-9]\d*\.?\d*)|(0\.\d*[1-9])/;
let result = reg.test(value);
return result
}
export const checkChinese = value => {
var reg = /.*[\u4e00-\u9fa5]+.*$/;
let result = reg.test(value);
return result
}
export const checkEnAndNum = value => {
var reg = /^[A-Za-z0-9]+$/;
let result = reg.test(value || null);
return result
}
export const checkZnum = value => {
var reg = /^[+]{0,1}(\d+)$|^[+]{0,1}(\d+\.\d+)$/;
let result = reg.test(value);
return result
}
export const checkZzNum = value => {
var reg = /^[1-9]\d*$/;
let result = reg.test(value);
return result
}
export const checkEn = value => {
var reg = /^[A-Za-z0-9\_]+$/;
let result = reg.test(value || null);
return result
}
export const checkChineseLenMax = value => {
var reg = /^[\u4e00-\u9fa5]{0,20}$/;
let result = reg.test(value);
return result
}
export const checkChineseLenMin = value => {
var reg = /^[\u4e00-\u9fa5]{20,}$/;
let result = reg.test(value);
return result
}
export const deepclone = (obj) => {
var c = obj instanceof Array ? [] : {};
for (var i in obj) if (obj.hasOwnProperty(i)) {
var prop = obj[i];
if (typeof prop == 'object') {
if (prop instanceof Array) {
c[i] = [];
for (var j = 0; j < prop.length; j++) {
if (typeof prop[j] != 'object') {
c[i].push(prop[j]);
} else {
c[i].push(deepclone(prop[j]));
}
}
} else {
c[i] = deepclone(prop);
}
} else {
c[i] = prop;
}
}
return c;
}
export const setBtnActive = (btnJudge) => {
var status;
if (btnJudge) {
status = false;
} else {
status = true;
}
return status;
}
export const compareClock = (time1, time2) => {
var start = time1.split(":")[0] + time1.split(":")[1]
var end = time2.split(":")[0] + time2.split(":")[1]
return parseInt(start) > parseInt(end)
}
export const compareDate = (time1, time2) => {
var start = time1.split("-")[0] + time1.split("-")[1] + time1.split("-")[2];
var end = time2.split("-")[0] + time2.split("-")[1] + time2.split("-")[2];
return parseInt(start) > parseInt(end)
}
export const timestamp = (time) => {
var date = new Date(time);
var timestamp = date.getTime();
return timestamp;
}
export const chineseCapitals = (n) => {
var fraction = ['角', '分'];
var digit = [
'零', '壹', '贰', '叁', '肆',
'伍', '陆', '柒', '捌', '玖'
];
var unit = [
['元', '万', '亿'],
['', '拾', '佰', '仟']
];
var head = n < 0 ? '欠' : '';
n = Math.abs(n);
var s = '';
for (var i = 0; i < fraction.length; i++) {
s += (digit[Math.floor(n * 10 * Math.pow(10, i)) % 10] + fraction[i]).replace(/零./, '');
}
s = s || '整';
n = Math.floor(n);
for (var i = 0; i < unit[0].length && n > 0; i++) {
var p = '';
for (var j = 0; j < unit[1].length && n > 0; j++) {
p = digit[n % 10] + unit[1][j] + p;
n = Math.floor(n / 10);
}
s = p.replace(/(零.)*零$/, '').replace(/^$/, '零') + unit[0][i] + s;
}
n = head + s.replace(/(零.)*零元/, '元')
.replace(/(零.)+/g, '零')
.replace(/^整$/, '零元整');
return head + s.replace(/(零.)*零元/, '元')
.replace(/(零.)+/g, '零')
.replace(/^整$/, '零元整');
}
export const formatCurrency = (num) => {
var reg = /(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/;
if (num === 0 || num == "0") {
return num = '0.00';
}
if (num) {
num = num.toString();
var sign, unit;
if (num.indexOf("+") > -1) {
sign = '+';
num = num.substring(1, num.length);
} else if (num.indexOf("-") > -1) {
sign = '-';
num = num.substring(1, num.length);
} else {
sign = '';
}
num = num.replace(/\$|\,/g, '');
num = parseFloat(num).toFixed(2).toString();
if (num.indexOf("元") === num.length - 1) {
unit = '元';
num = num.substring(1, num.length - 1);
} else {
unit = '';
}
if (reg.test(num)) {
var cents = num.indexOf(".") > 0 ? num.substr(num.indexOf(".")) : '';
cents = cents.length > 1 ? cents : '';
num = num.indexOf(".") > 0 ? num.substring(0, (num.indexOf("."))) : num;
for (var i = 0; i < Math.floor((num.length - (1 + i)) / 3); i++) {
num = num.substring(0, num.length - (4 * i + 3)) + ',' + num.substring(num.length - (4 * i + 3));
}
return (sign + num + cents + unit);
} else {
Toast('请输入正确的金额');
return '';
}
}
}
export const stringToArray = (val) => {
if (val && (Object.prototype.toString.call(val) == '[object String]')) {
val = val.replace(/\"|\'|\s/g, '').replace(/spersigns/g, '%')
.replace(/\{/g, '{"').replace(/\}/g, '"}').replace(/\:/g, '":"').replace(/\,/g, '","')
.replace(/\"{/g, '{').replace(/\}"/g, '}');
val = JSON.parse(val)
}
return val;
}
export const acctHidden = (value) => {
let txt = '';
txt = value.substring(0, 4) + ' **** **** ' + value.substring(value.length - 4, value.length)
return txt
}
export const bankStyle = (bankNo) => {
let styleBank;
if (bankNo) {
let acctBankNo = bankNo.split('-')[0];
let acctNo = bankNo.split('-')[1] ? `-${bankNo.split('-')[1]}` : '';
let styleGroup = acctBankNo.length < 4 ? '' : acctBankNo.match(/\d{4}/g).join(' ');
let remainderLen = acctBankNo.length % 4;
styleBank = remainderLen === 0 ?
`${styleGroup}${acctNo}` :
`${styleGroup}${styleGroup ? ' ' : ''}${acctBankNo.substr(-remainderLen)}${acctNo}`;
} else {
styleBank = '';
}
return styleBank;
}
export const datedifference = (sDate1, sDate2) => {
var dateSpan, iDays;
sDate1 = Date.parse(sDate1);
sDate2 = Date.parse(sDate2);
dateSpan = sDate2 - sDate1;
dateSpan = Math.abs(dateSpan);
iDays = Math.floor(dateSpan / (24 * 3600 * 1000));
return iDays
};
export const timeFormat = (value) => {
if (!value) {
return '';
}
let dateString = value.toString();
let pattern = /(\d{4})(\d{2})(\d{2})/;
let formatedDate = dateString.replace(pattern, "$1-$2-$3");
return formatedDate;
}
export const transTimeFormat = (value, format) => {
if (!value) {
return '';
}
if (!format) {
format = ''
}
let dateString = value.toString();
let pattern = /(\d{4})(\d{2})/;
let formatedDate = dateString.replace(pattern, '$1' + `${format}` + '$2');
return formatedDate;
}
export const positiveInterger = (value) => {
let positiveInt = /^[1-9]{1}[0-9]*$/
if (positiveInt.test(value)) {
return true;
}
return false;
}
export const clearSpace = (val) => {
let bankNo = val.replace(/\s+/g, "");
return bankNo;
}
export const transDateMonth = (value, format) => {
if (!value) {
return ''
}
if (!format) {
format = ''
}
let time = new Date(value);
let year = time.getFullYear();
let month = time.getMonth() + 1 + '';
if (month.length < 2) {
month = '0' + month;
}
let newtime = year + `${format}` + month;
return newtime;
}
export const transFormatDate = (value) => {
if (value) {
let str = value;
let date = new Date(value);
let y = date.getFullYear();
let m = String(date.getMonth() + 1);
m = Number(m) < 10 ? ('0' + m) : m;
let d = String(date.getDate());
d = Number(d) < 10 ? ('0' + d) : d;
return y + '-' + m + '-' + d;
} else {
return '';
}
}
export const isNotObject = (obj) => {
var name;
for (name in obj) {
return false;
}
return true;
}
export const baiduMap = (bd_lat, bd_lon) => {
var X_PI = Math.PI * 3000.0 / 180.0;
var x = bd_lon, y = bd_lat;
var z = Math.sqrt(x * x + y * y) + 0.00002 * Math.sin(y * X_PI);
var theta = Math.atan2(y, x) + 0.000003 * Math.cos(x * X_PI);
var gg_lng = z * Math.cos(theta) + 0.0055;
var gg_lat = z * Math.sin(theta) + 0.0063;
return { gg_lng, gg_lat };
}
export const idCard = (value) => {
let idRule = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/
if (idRule.test(value)) {
return true;
}
return false;
}
export const moneyCard = (value) => {
let idRule = /(^(\d{9,20}[-])\d{3}$)|(^\d{9,20}$)/;
if (idRule.test(value)) {
return true;
}
return false;
}
export const ImageCompress = (imgFile, imageSrc) => {
let image = new Image();
image.src = imageSrc;
image.onload = function () {
let canvas = document.createElement("canvas");
let ctx = canvas.getContext("2d");
let tCanvas = document.createElement("canvas");
let tctx = tCanvas.getContext("2d");
let initSize = image.src.length;
let width = image.width;
let height = image.height;
let ratio;
if ((ratio = width * height / 4000000) > 1) {
ratio = Math.sqrt(ratio);
width /= ratio;
height /= ratio;
} else {
ratio = 1;
}
canvas.width = width;
canvas.height = height;
ctx.fillRect(0, 0, canvas.width, canvas.height);
let count;
if ((count = width * height / 1000000) > 1) {
count = ~~(Math.sqrt(count) + 1);
let nw = ~~(width / count);
let nh = ~~(height / count);
tCanvas.width = nw;
tCanvas.height = nh;
let twidthArr = new Array();
let theightArr = new Array();
for (let i = 0; i < count; i++) {
twidthArr[i] = function (num1) {
for (let j = 0; j < count; j++) {
theightArr[j] = function (num2) {
tctx.drawImage(
image,
num1 * nw * ratio,
num2 * nh * ratio,
nw * ratio,
nh * ratio,
0,
0,
nw,
nh
);
ctx.drawImage(tCanvas, num1 * nw, num2 * nh, nw, nh);
}(j);
}
}(i);
}
} else {
ctx.drawImage(image, 0, 0, width, height);
}
let saveImageUrl = canvas.toDataURL('image/jpeg', 0.7);
let saveImage = dataURLtoFile(saveImageUrl);
function dataURLtoFile(dataurl) {
let arr = dataurl.split(',');
let mime = arr[0].match(/:(.*?);/)[1];
let suffix = mime.split('/')[1];
let bstr = atob(arr[1]);
let n = bstr.length;
let u8arr = new Uint8Array(n);
while (n--) {
u8arr[n] = bstr.charCodeAt(n);
}
return new Blob([u8arr], { type: mime })
}
console.info(saveImage, "11111");
return saveImage;
}
}
export const handleEnterEvent = (isFromEvPram, msgtext) => {
let param = {
eventMessageId: isFromEvPram.eventMessageId,
messageStatus: '1'
}
if (isFromEvPram.feedBack) {
param['feedBack'] = isFromEvPram.feedBack
}
rpcHttp(param, 'msgFeedBack', 'false', 'false').then(
result => {
if (result["code"] == 200) {
if (result.data.isDeleted) {
let params = {
eventMessageId: isFromEvPram.eventMessageId
};
nativeHttp("eventRefresh", params).then(
res => {
}, err => {
Toast(err);
}
);
}
if (msgtext) {
Toast(msgtext);
setTimeout(() => {
AlipayJSBridge.call("prePage", {})
}, 1000);
}
} else {
AlipayJSBridge.call('closeLoading', {});
Toast(result.msg);
}
}, err => {
AlipayJSBridge.call('closeLoading', {});
Toast("系统异常,请重试");
}
);
}