/* console打印开关 */
let conType = true;
let common = (function() {
/* ******************************************************************************************** */
/* ********************************************console打印************************************* */
/* ******************************************************************************************** */
/* log 打印*/
function log(index) {
conFun(index, 'log');
}
/* 错误打印 */
function error(index) {
conFun(index, 'error');
}
/* 提示打印 */
function inof(index) {
conFun(index, 'inof');
}
/* 警告打印 */
function warn(index) {
conFun(index, 'warn');
}
/* 打印嵌套 */
function group(index) {
conFun(index, 'group');
}
/* 表格打印 */
function table(index) {
conFun(index, 'table');
}
/* 追踪函数过程 */
function tracer(index) {
conFun(index, 'tracer');
}
/* 动态获取方法和输出类型 */
function conFun(index, type) {
if (conType) {
eval('console.' + type + "(" + JSON.stringify(index) + ")")
}
}
/* ******************************************************************************************** */
/* ********************************************类型判断**************************************** */
/* ******************************************************************************************** */
// 是否是字符串
function isString(value) {
return Object.prototype.toString.call(value) == "[object String]";
}
// 是否是数字
function isNumber(value) {
return Object.prototype.toString.call(value) == "[object Number]";
}
// 是否是布尔值
function isBoolean(value) {
return Object.prototype.toString.call(value) == "[object Boolean]";
}
// 是否undefined
function isUndefined(value) {
return Object.prototype.toString.call(value) == "[object Undefined]";
}
// 是否是null
function isNull(value) {
return Object.prototype.toString.call(value) == "[object Null]";
}
// 是否数组
function isArray(value) {
return Object.prototype.toString.call(value) == "[object Array]";
}
// 是否是函数
function isFunction(value) {
return Object.prototype.toString.call(value) == "[object Function]";
}
// 是否是对象
function isObject(value) {
return Object.prototype.toString.call(value) == "[object Object]";
}
// 是否是正则表达式
function isRegExp(value) {
return Object.prototype.toString.call(value) == "[object RegExp]";
}
// 是否是日期对象
function isDate(value) {
return Object.prototype.toString.call(value) == "[object Date]";
}
/* ----------------------------------------------------------------- */
/* ------------------------密码强度--------------------------------- */
/* ----------------------------------------------------------------- */
function getPasswordLv(stringText) {
if (stringText) {
let passLv = 0;
if (stringText.length >= 6) {
passLv++;
}
if (/[0-9]/.test(stringText)) {
passLv++;
}
if (/[a-z]/.test(stringText)) {
passLv++;
}
if (/[A-Z]/.test(stringText)) {
passLv++;
}
if (/[\.|-|_]/.test(stringText)) {
passLv++;
}
return passLv;
}
}
/* ******************************************************************************************** */
/* ********************************************set操作**************************************** */
/* ******************************************************************************************** */
/* set并集 [合并数组] */
function setMerge(...value) {
let arr = []
for (let i = 0; i < value.length; i++) {
arr = new Set(arr, value[i]);
}
return arr;
}
/* set交集 [相同部分集合] */
function setOwn(value1, value2) {
return new Set([...value1].filter((x) => {
value2.has(x)
}));
}
/* set差集 [不同部分] */
function setUnlike(value1, value2) {
return new Set([...value1].filter((x) => {
!value2.has(x)
}))
}
/* ******************************************************************************************** */
/* **************************************localStroage缓存操作********************************** */
/* ******************************************************************************************** */
/* 添加缓存 */
function local_set(key, value) {
if (key && value) {
window.localStorage.setItem(key, value);
}
}
/* 获取指定缓存 */
function local_get(key) {
key ? key : null;
return window.localStorage.getItem(key) || null;
}
/* 获取所有缓存 */
function local_getAll() {
return localStorage.valueOf();
}
/* 删除某个key缓存 */
function local_remove(key) {
localStorage.removeItem(key);
}
/* 清空缓存 */
function local_clear() {
localStorage.clear();
}
/* 判断key缓存是否存在 */
function local_has(key) {
return localStorage.hasOwnProperty(key)
}
/* ********************************************************************************************************* */
/* ************************************************Date时间************************************************* */
/* ********************************************************************************************************* */
/* 获取当前时间 */
/*
isTimer 布尔值 true 年月日 + 时分秒 false 只有年月日
separatorFront : 年月日的分割线 默认 "-"
separatorDehind : 时分秒的分割线 默认 ":"
*/
function getNewDate(isTimer, separatorFront, separatorDehind) {
let t = new Date();
let time = "";
let n = t.getFullYear();
let y = bianhuan(t.getMonth() + 1);
let r = bianhuan(t.getDate());
let s = bianhuan(t.getHours());
let f = bianhuan(t.getMinutes());
let m = bianhuan(t.getSeconds());
let front = separatorFront ?? "-";
let behind = separatorDehind ?? ':';
let timer = isTimer ?? false;
if (!timer) {
time = `${n}${front}${y}${front}${r}`
} else {
time = `${n}${front}${y}${front}${r} ${s}${behind}${f}${behind}${m}`
}
return time;
}
// 加“0”判断
function bianhuan(s) {
s = s < 10 ? "0" + s : s;
return s;
}
/* 时间戳转字符串 */
function switchDate(timestamp, isTimer, separatorFront, separatorDehind) {
//1651222547924
let time = "";
let t = new Date(timestamp);
let n = t.getFullYear();
let y = bianhuan(t.getMonth() + 1);
let r = bianhuan(t.getDate());
let s = bianhuan(t.getHours());
let f = bianhuan(t.getMinutes());
let m = bianhuan(t.getSeconds());
let front = separatorFront ?? "-";
let behind = separatorDehind ?? ':';
let timer = isTimer ?? false;
if (!timer) {
time = `${n}${front}${y}${front}${r}`
} else {
time = `${n}${front}${y}${front}${r} ${s}${behind}${f}${behind}${m}`
}
return time;
}
/*
通过时间返回星期几
getDate 可以是时间戳 , 也可以是时间格式的字符串
*/
function getWeek(getDate) {
let t = new Date(getDate);
let r = bianhuan(t.getDate());
let week = t.getDay()
let str = '星期'
let strNumber = ['一', '二', '三', '四', '五', '六', '日']
if (week == 0) {
week = strNumber.length - 1
} else {
week -= 1;
}
return str + strNumber[week]
}
// 对年份是闰年的判断
function isLeapYear(getDate) {
let t = new Date(getDate);
let n = t.getFullYear();
if (n % 4 == 0 && n % 100 != 0 || n % 400 == 0) {
return true;
}else{
return false;
}
}
/* ********************************************************************************************************* */
/* ************************************************获取状态************************************************** */
/* ********************************************************************************************************* */
/*
判断登录的是PC还是手机端
false PC
true 手机
*/
function isMobile() {
var userAgentInfo = navigator.userAgent;
var mobileAgents = ["Android", "iPhone", "SymbianOS", "Windows Phone", "iPad", "iPod"];
var mobile_flag = false;
//根据userAgent判断是否是手机
for (var v = 0; v < mobileAgents.length; v++) {
if (userAgentInfo.indexOf(mobileAgents[v]) > 0) {
mobile_flag = true;
break;
}
}
var screen_width = window.screen.width;
var screen_height = window.screen.height;
//根据屏幕分辨率判断是否是手机
if (screen_width < 500 && screen_height < 800) {
mobile_flag = true;
}
return mobile_flag;
}
/* ********************************************************************************************************* */
/* ************************************************输出***************************************************** */
/* ********************************************************************************************************* */
return {
/*console打印集合 */
log: log,
error: error,
inof: inof,
warn: warn,
group: group,
table: table,
tracer: tracer,
/* 类型判断集合 */
isString: isString,
isNumber: isNumber,
isBoolean: isBoolean,
isUndefined: isUndefined,
isNull: isNull,
isArray: isArray,
isFunction: isFunction,
isObject: isObject,
isRegExp: isRegExp,
isDate: isDate,
/* set操作 */
setMerge: setMerge,
setOwn: setOwn,
setUnlike: setUnlike,
/* 缓存操作 */
local_set: local_set,
local_get: local_get,
local_getAll: local_getAll,
local_remove: local_remove,
local_clear: local_clear,
local_has: local_has,
getPasswordLv: getPasswordLv,
/* data操作 */
getNewDate: getNewDate,
switchDate: switchDate,
getWeek: getWeek,
isLeapYear: isLeapYear,
/* 其他 */
isMobile: isMobile,
}
})()