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.
348 lines
10 KiB
348 lines
10 KiB
|
|
export function isNull(o) {
|
|
if(o === 0 || o === "0"){
|
|
return false;
|
|
}
|
|
if(o == "undefined" || o == null || o == ""){
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
export function isPhone(o) {
|
|
if(/^1[23456789]\d{9}$/.test(o)){
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
export function isMail(o) {
|
|
if(/^([a-z0-9A-Z]+[-|\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\.)+[a-zA-Z]{2,}$/.test(o)){
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
export function isNumber(value) {
|
|
return !isNaN(parseFloat(value)) && isFinite(value)
|
|
}
|
|
export function isLetter(o) {
|
|
if(/^[A-Za-z]+$/.test(o)){
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
export function isNumberOrLetter(o) {
|
|
if(/^[A-Za-z0-9]+$/.test(o)){
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
// 日期格式化
|
|
export function parseTime(time, pattern) {
|
|
if (arguments.length === 0 || !time) {
|
|
return null
|
|
}
|
|
const format = pattern || '{y}-{m}-{d} {h}:{i}:{s}'
|
|
let date
|
|
if (typeof time === 'object') {
|
|
date = time
|
|
} else {
|
|
if ((typeof time === 'string') && (/^[0-9]+$/.test(time))) {
|
|
time = parseInt(time)
|
|
} else if (typeof time === 'string') {
|
|
time = time.replace(new RegExp(/-/gm), '/').replace('T', ' ').replace(new RegExp(/\.[\d]{3}/gm), '');
|
|
}
|
|
if ((typeof time === 'number') && (time.toString().length === 10)) {
|
|
time = time * 1000
|
|
}
|
|
date = new Date(time)
|
|
}
|
|
const formatObj = {
|
|
y: date.getFullYear(),
|
|
m: date.getMonth() + 1,
|
|
d: date.getDate(),
|
|
h: date.getHours(),
|
|
i: date.getMinutes(),
|
|
s: date.getSeconds(),
|
|
a: date.getDay()
|
|
}
|
|
const time_str = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => {
|
|
let value = formatObj[key]
|
|
// Note: getDay() returns 0 on Sunday
|
|
if (key === 'a') { return ['日', '一', '二', '三', '四', '五', '六'][value] }
|
|
if (result.length > 0 && value < 10) {
|
|
value = '0' + value
|
|
}
|
|
return value || 0
|
|
})
|
|
return time_str
|
|
}
|
|
|
|
export function formatDate(time, option) {
|
|
if (('' + time).length === 10) {
|
|
time = parseInt(time) * 1000
|
|
} else {
|
|
time = +time
|
|
}
|
|
|
|
if (option) {
|
|
return parseTime(time, option)
|
|
}
|
|
|
|
const d = new Date(time)
|
|
const now = Date.now()
|
|
|
|
const diff = (now - d) / 1000
|
|
|
|
if (diff < 30) {
|
|
return '刚刚'
|
|
} else if (diff < 3600) {
|
|
// less 1 hour
|
|
return Math.ceil(diff / 60) + '分钟前'
|
|
} else if (diff < 3600 * 24) {
|
|
return Math.ceil(diff / 3600) + '小时前'
|
|
} else if (diff < 3600 * 24 * 2) {
|
|
return '1天前'
|
|
} else if (diff < 3600 * 24 * 3) {
|
|
return '2天前'
|
|
} else if (diff < 3600 * 24 * 4) {
|
|
return '3天前'
|
|
} else if (diff < 3600 * 24 * 5) {
|
|
return '4天前'
|
|
} else if (diff < 3600 * 24 * 6) {
|
|
return '5天前'
|
|
} else if (diff < 3600 * 24 * 7) {
|
|
return '6天前'
|
|
} else if (diff < 3600 * 24 * 8) {
|
|
return '7天前'
|
|
}
|
|
if(diff < 3600 * 24 * 365) {
|
|
return (
|
|
(d.getMonth()*1 + 1) +
|
|
'月' +
|
|
d.getDate() +
|
|
'日'
|
|
)
|
|
} else {
|
|
return (
|
|
d.getFullYear() +
|
|
'年' +
|
|
(d.getMonth()*1 + 1) +
|
|
'月'
|
|
)
|
|
}
|
|
}
|
|
|
|
export function formatTime(time, option) {
|
|
if (('' + time).length === 10) {
|
|
time = parseInt(time) * 1000
|
|
} else {
|
|
time = +time
|
|
}
|
|
|
|
if (option) {
|
|
return parseTime(time, option)
|
|
}
|
|
|
|
const d = new Date(time)
|
|
const now = Date.now()
|
|
|
|
const diff = (now - d) / 1000
|
|
|
|
return (
|
|
d.getFullYear() +
|
|
'-' +
|
|
((d.getMonth()*1 + 1) + "").padStart(2,0) +
|
|
'-' +
|
|
(d.getDate() + '').padStart(2,0) +
|
|
' ' +
|
|
(d.getHours() + '').padStart(2,0) +
|
|
':' +
|
|
(d.getMinutes() + '').padStart(2,0) +
|
|
':' +
|
|
(d.getSeconds() + '').padStart(2,0)
|
|
)
|
|
}
|
|
|
|
// number:要格式化的数字,
|
|
// decimals:保留几位小数, 默认0位
|
|
// dec_point:小数点符号, 默认 .
|
|
// thousands_sep:千分位符号 默认 ,
|
|
// tail_add: 小数点后面数据是否添加0补足位数, 默认空字符
|
|
// null_default: 如果为空的值, 默认空字符
|
|
function numberFormat(number, decimals, tail_add="", thousands_sep, dec_point, null_default="") {
|
|
number = (number + '').replace(/[^0-9+-Ee.]/g, '');
|
|
var n = !isFinite(+number) ? 0 : +number,
|
|
prec = !isFinite(+decimals) ? 0 : Math.abs(decimals),
|
|
sep = (typeof thousands_sep === 'undefined') ? ',' : thousands_sep,
|
|
dec = (typeof dec_point === 'undefined') ? '.' : dec_point,
|
|
s = '',
|
|
toFixedFix = function (n, prec) {
|
|
var k = Math.pow(10, prec);
|
|
return '' + Math.round(n * k) / k;
|
|
};
|
|
|
|
s = (prec ? toFixedFix(n, prec) : '' + Math.round(n)).split('.');
|
|
var re = /(-?\d+)(\d{3})/;
|
|
if(sep.length > 0){
|
|
while (re.test(s[0])) {
|
|
s[0] = s[0].replace(re, "$1" + sep + "$2");
|
|
}
|
|
}
|
|
if ((s[1] || '').length < prec) {
|
|
s[1] = s[1] || '';
|
|
s[1] += new Array(prec - s[1].length + 1).join(tail_add);
|
|
}
|
|
if((s[1] || '').length == 0){
|
|
return s[0]
|
|
}
|
|
return s.join(dec);
|
|
}
|
|
// number:要格式化的数字,
|
|
// decimals:保留几位小数, 默认0位
|
|
// dec_point:小数点符号, 默认 .
|
|
// thousands_sep:千分位符号 默认 ,
|
|
// tail_add: 小数点后面数据是否添加0补足位数, 默认空字符
|
|
// null_default: 如果为空的值, 默认空字符
|
|
export function formatNumber(number, decimals, tail_add, thousands_sep, dec_point, null_default) {
|
|
if(isNull(number) || isNull(String(number).trim())){return null_default;}
|
|
if(!isNumber(String(number).trim())){return number;}
|
|
number = String(number).trim();
|
|
return numberFormat(number, decimals, tail_add, thousands_sep, dec_point, null_default);
|
|
}
|
|
// thousands_sep:千分位符号 默认空字符
|
|
export function formatPercent(number, decimals, tail_add, thousands_sep="", dec_point, null_default) {
|
|
if(isNull(number) || isNull(String(number).trim().replaceAll(",", ""))){return null_default;}
|
|
if(!isNumber(String(number).trim().replaceAll(",", ""))){return number;}
|
|
number = String(number).trim().replaceAll(",", "");
|
|
let value = numberFormat(number*100, decimals, tail_add, thousands_sep, dec_point, null_default);
|
|
if(value){
|
|
return value + "%";
|
|
}
|
|
return "";
|
|
}
|
|
|
|
// get brower
|
|
export function GetCurrentBrowser () {
|
|
let ua = navigator.userAgent.toLocaleLowerCase()
|
|
let browserType = null
|
|
if (ua.match(/msie/) != null || ua.match(/trident/) != null) {
|
|
browserType = 'IE'
|
|
} else if (ua.match(/firefox/) != null) {
|
|
browserType = 'firefox'
|
|
} else if (ua.match(/ucbrowser/) != null) {
|
|
browserType = 'UC'
|
|
} else if (ua.match(/opera/) != null || ua.match(/opr/) != null) {
|
|
browserType = 'opera'
|
|
} else if (ua.match(/bidubrowser/) != null) {
|
|
browserType = 'baidu'
|
|
} else if (ua.match(/metasr/) != null) {
|
|
browserType = 'sougou'
|
|
} else if (ua.match(/tencenttraveler/) != null || ua.match(/qqbrowse/) != null) {
|
|
browserType = 'QQ'
|
|
} else if (ua.match(/maxthon/) != null) {
|
|
browserType = 'maxthon'
|
|
} else if (ua.match(/chrome/) != null) {
|
|
var is360 = _mime('type', 'application/vnd.chromium.remoting-viewer')
|
|
if (is360) {
|
|
browserType = '360'
|
|
} else {
|
|
browserType = 'chrome'
|
|
}
|
|
} else if (ua.match(/safari/) != null) {
|
|
browserType = 'Safari'
|
|
} else {
|
|
browserType = 'others'
|
|
}
|
|
return browserType
|
|
}
|
|
|
|
function _mime (option, value) {
|
|
var mimeTypes = navigator.mimeTypes
|
|
for (var mt in mimeTypes) {
|
|
if (mimeTypes[mt][option] === value) {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
// get os
|
|
export function GetOs () {
|
|
let sUserAgent = navigator.userAgent.toLocaleLowerCase()
|
|
let isWin = (navigator.platform.toLocaleLowerCase() == 'win32') || (navigator.platform.toLocaleLowerCase() === 'windows')
|
|
let isMac = (navigator.platform.toLocaleLowerCase() === 'mac68k') || (navigator.platform.toLocaleLowerCase() === 'macppc')
|
|
|| (navigator.platform === 'macintosh') || (navigator.platform.toLocaleLowerCase() === 'macintel')
|
|
if (isMac) return 'Mac'
|
|
var isUnix = (navigator.platform === 'x11') && !isWin && !isMac
|
|
if (isUnix) return 'Unix'
|
|
var isLinux = (String(navigator.platform.toLocaleLowerCase()).indexOf('linux') > -1)
|
|
if (isLinux) return 'Linux'
|
|
if (isWin) {
|
|
var isWin2K = sUserAgent.indexOf('windows nt 5.0') > -1 || sUserAgent.indexOf('windows 2000') > -1
|
|
if (isWin2K) return 'Win2000'
|
|
var isWinXP = sUserAgent.indexOf('windows nt 5.1') > -1 || sUserAgent.indexOf('windows xp') > -1
|
|
if (isWinXP) return 'WinXP'
|
|
var isWin2003 = sUserAgent.indexOf('windows nt 5.2') > -1 || sUserAgent.indexOf('windows 2003') > -1
|
|
if (isWin2003) return 'Win2003'
|
|
var isWinVista = sUserAgent.indexOf('windows nt 6.0') > -1 || sUserAgent.indexOf('windows vista') > -1
|
|
if (isWinVista) return 'WinVista'
|
|
var isWin7 = sUserAgent.indexOf('windows nt 6.1') > -1 || sUserAgent.indexOf('windows 7') > -1
|
|
if (isWin7) return 'Win7'
|
|
var isWin8 = sUserAgent.indexOf('windows nt 6.2') > -1 || sUserAgent.indexOf('windows 8') > -1
|
|
if (isWin8) return 'Win8'
|
|
var isWin10 = sUserAgent.indexOf('windows nt 10.0') > -1 || sUserAgent.indexOf('windows nt 6.4') > -1 || sUserAgent.indexOf('windows 10') > -1
|
|
if (isWin10) return 'Win10'
|
|
var isSimulator = sUserAgent.indexOf('linux') > -1 && sUserAgent.indexOf('android') > -1
|
|
if (isSimulator) return 'Win手机模拟器'
|
|
return navigator.platform
|
|
}
|
|
if (sUserAgent.indexOf('android') > -1) return 'Android'
|
|
if (sUserAgent.indexOf('iphone') > -1) return 'iPhone'
|
|
if (sUserAgent.indexOf('symbianos') > -1) return 'SymbianOS'
|
|
if (sUserAgent.indexOf('windows phone') > -1) return 'Windows Phone'
|
|
if (sUserAgent.indexOf('ipad') > -1) return 'iPad'
|
|
if (sUserAgent.indexOf('ipod') > -1) return 'iPod'
|
|
return navigator.platform
|
|
}
|
|
|
|
export function getRandom(len){
|
|
let data = Math.random() + "";
|
|
let index = 2;
|
|
while(data[index] == 0){
|
|
index++;
|
|
}
|
|
return data.substr(index, len);
|
|
}
|
|
|
|
export function getLongRandom(len){
|
|
if(len <= 16){
|
|
return getRandom(len)
|
|
}
|
|
let data = Math.random() + "";
|
|
let index = 2;
|
|
while(data[index] == 0){
|
|
index++;
|
|
}
|
|
return parseTime(Date.now(), '{y}{m}{d}{h}{i}{s}') + "" + data.substr(index, len-16);
|
|
}
|
|
|
|
export function moveInArray(array, fromIndex, toIndex) {
|
|
const element = array.splice(fromIndex, 1)[0];
|
|
array.splice(toIndex, 0, element);
|
|
}
|
|
|
|
export function jsonCharBigInt(s){
|
|
if(typeof s !== 'string'){
|
|
return s
|
|
}
|
|
let re = /([\\]?['|"]{1})(\w+[\\]?['|"]{1}[ |\n|r|\t]*:[ |\n|r|\t]*)([-]?\d{15,})/g
|
|
return s.replaceAll(re, "$1$2$1$3$1")
|
|
}
|
|
|
|
export function jsonParse(s){
|
|
if(typeof s !== 'string'){
|
|
return s
|
|
}
|
|
let re = /([\\]?['|"]{1})(\w+[\\]?['|"]{1}[ |\n|r|\t]*:[ |\n|r|\t]*)([-]?\d{15,})/g
|
|
return JSON.parse(s.replaceAll(re, "$1$2$1$3$1"))
|
|
}
|