151 lines
3.3 KiB
JavaScript
151 lines
3.3 KiB
JavaScript
/*
|
|
* @Author: ddmt
|
|
* @Date: 2024-9-29 20:50:12
|
|
* @LastEditTime: 2024-9-30 0:13:12
|
|
* @LastEditors: ddmt
|
|
* @Description: ddmt-index file
|
|
* @FilePath: /Tool/number.js
|
|
*/
|
|
|
|
//生成从minNum到maxNum的随机数
|
|
export function randomNum (minNum, maxNum) {
|
|
switch (arguments.length) {
|
|
case 1:
|
|
return parseInt(Math.random() * minNum + 1, 10);
|
|
case 2:
|
|
return parseInt(Math.random() * (maxNum - minNum + 1) + minNum, 10);
|
|
default:
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
|
|
/*
|
|
* 数组循环函数
|
|
* @param {Array} arr
|
|
* @param {number} index
|
|
*/
|
|
export function nextArray (arr, index) {
|
|
index--;
|
|
return arr.slice(index + 1, arr.length).concat(arr.slice(0, index + 1));
|
|
}
|
|
|
|
/*
|
|
* 数组去重函数
|
|
* @param {Array} arr
|
|
*/
|
|
export function ArrayDeHeavy (arr) {
|
|
let newArr = new Set();
|
|
arr.forEach(item => {
|
|
newArr.add(item);
|
|
});
|
|
return Array.from(newArr);
|
|
}
|
|
|
|
|
|
/*
|
|
* 获取相对时间(中文)
|
|
* @param {Date} date
|
|
*/
|
|
export function getRelativeTime (date) {
|
|
const now = new Date();
|
|
const diffInSeconds = Math.floor((now.getTime() - date.getTime()) / 1000);
|
|
|
|
if (diffInSeconds < 60) {
|
|
return '刚刚';
|
|
}
|
|
|
|
const diffInMinutes = Math.floor(diffInSeconds / 60);
|
|
if (diffInMinutes < 60) {
|
|
return `${diffInMinutes}分钟前`;
|
|
}
|
|
|
|
const diffInHours = Math.floor(diffInMinutes / 60);
|
|
if (diffInHours < 24) {
|
|
return `${diffInHours}小时前`;
|
|
}
|
|
|
|
const diffInDays = Math.floor(diffInHours / 24);
|
|
if (diffInDays < 30) {
|
|
return `${diffInDays}天前`;
|
|
}
|
|
|
|
const diffInMonths = Math.floor(diffInDays / 30);
|
|
if (diffInMonths < 12) {
|
|
return `${diffInMonths}个月前`;
|
|
}
|
|
|
|
const diffInYears = Math.floor(diffInMonths / 12);
|
|
if (diffInYears < 5) {
|
|
return `${diffInYears}年前`;
|
|
}
|
|
|
|
return '很久以前';
|
|
}
|
|
|
|
/*
|
|
* 解析nginx日志(单行)
|
|
* @param {string} log
|
|
*/
|
|
function parseNginxLog (log) {
|
|
const logPattern = /^(\S+) - - \[([^\]]+)\] "(\S+) (\S+) HTTP\/\d\.\d" (\d+) (\d+) "([^"]*)" "([^"]*)"/;
|
|
|
|
try {
|
|
const match = log.match(logPattern);
|
|
if (match) {
|
|
return {
|
|
ip: match[1],
|
|
timestamp: match[2],
|
|
method: match[3],
|
|
url: match[4],
|
|
status: parseInt(match[5]),
|
|
responseSize: parseInt(match[6]),
|
|
referrer: match[7],
|
|
userAgent: match[8],
|
|
};
|
|
}
|
|
} catch (error) {
|
|
return error;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
/*
|
|
* cookies 解析函数
|
|
* @param {string} cookiesStr
|
|
* @return {Array} CookieArray
|
|
*/
|
|
function toCookiesArray(cookies) {
|
|
if (!cookies || cookies.length === 0) return;
|
|
|
|
// 解析旧的 cookies
|
|
const cookieMap = {};
|
|
cookies.split("; ").forEach(cookie => {
|
|
const [key, value] = cookie.split("=");
|
|
if (key) cookieMap[key] = value;
|
|
});
|
|
|
|
return cookieMap;
|
|
}
|
|
|
|
|
|
/*
|
|
* cookies 更新函数
|
|
* @param {Array} setCookieArray 新的 Set-Cookie 数组
|
|
*/
|
|
function updateCookies(cookies, setCookieArray) {
|
|
if (!setCookieArray || setCookieArray.length === 0) return;
|
|
|
|
// 解析旧的 cookies
|
|
const cookieMap = toCookiesArray(cookies);
|
|
|
|
// 解析新的 Set-Cookie 数组并更新
|
|
setCookieArray.forEach(cookieStr => {
|
|
const cookiePair = cookieStr.split(";")[0]; // 只取 key=value
|
|
const [key, value] = cookiePair.split("=");
|
|
if (key) cookieMap[key] = value;
|
|
});
|
|
|
|
// 重新拼接成字符串存回全局 cookies
|
|
return Object.entries(cookieMap).map(([key, value]) => `${key}=${value}`).join("; ");
|
|
} |