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.
59 lines
1.6 KiB
59 lines
1.6 KiB
/*
|
|
全局共享实用方法 shared.js
|
|
*/
|
|
let shareMD5 = require("./shareMD5.js"); // 签名加密js文件
|
|
|
|
// 判断对错/是否显示,万能校验
|
|
export function isRight(obj) {
|
|
if (isValueType(obj) === 'string') {
|
|
obj = obj.trim();
|
|
if (obj === 'null' || obj === 'undefined') {
|
|
return false;
|
|
}
|
|
} else if (isValueType(obj) === 'number' && (isValueType(obj) === "number" && !isNaN(obj)) && obj !== 0) {
|
|
return true;
|
|
} else if (isValueType(obj) === 'boolean') {
|
|
return obj
|
|
}
|
|
for (var key in obj) {
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
// 判断一个值所属的类型,返回一个字符串
|
|
export function isValueType(value) {
|
|
let str = Object.prototype.toString.call(value);
|
|
return str.match(/\[object (.*?)\]/)[1].toLowerCase();
|
|
}
|
|
|
|
// 生成随机唯一token
|
|
export function createToken(par = {}){
|
|
const date = new Date(); // 获取时间对象
|
|
let fullYear = date.getFullYear(); // 年
|
|
let month = String(date.getMonth() + 1); // 月
|
|
month = fillZero(month);
|
|
let getdate = String(date.getDate()); // 日
|
|
getdate = fillZero(getdate);
|
|
let hours = String(date.getHours()); // 时
|
|
hours = fillZero(hours);
|
|
let minutes = String(date.getMinutes()); // 分
|
|
minutes = fillZero(minutes);
|
|
let seconds = String(date.getSeconds()); // 秒
|
|
seconds = fillZero(seconds);
|
|
let randomStr = Math.random().toString(32).substr(2); // 随机字符串
|
|
let tmp = `${fullYear}/${month}/${getdate} ${hours}:${minutes}:${seconds} ${randomStr}`;
|
|
for(let i in par){
|
|
tmp += par[i];
|
|
}
|
|
let token = shareMD5.md5(tmp, 16);
|
|
return token;
|
|
}
|
|
|
|
//不足2位补0
|
|
export function fillZero(time) {
|
|
if (time <= 9) {
|
|
time = '0' + time;
|
|
}
|
|
return time;
|
|
}
|