Files
sgxt_web/src/utils/auth.js
2025-04-12 14:54:02 +08:00

78 lines
1.8 KiB
JavaScript

import {
getItem,
setItem
} from '@/utils/storage';
import {
TOKEN_TIMEOUT_VSLUE,
TIME_STAMP
} from '@/constant';
/*
* 1.设置时间戳
*/
export function setTimeStamp() {
setItem(TIME_STAMP, Date.now());
}
/*
* 2.获取时间戳
*/
export function getTimeStamp() {
return getItem(TIME_STAMP);
}
/*
* 1.是否超时 对比当前时间 和登录时间
*/
export function isCheckTimeout() {
const currentTime = Date.now();
const timeStamp = getTimeStamp();
return currentTime - timeStamp > TOKEN_TIMEOUT_VSLUE;
}
//格式化时间
export function dateFormat(date, fmt = "YY-MM-DD hh:mm:ss") {
let ret
const opt = {
"Y+": date.getFullYear().toString(), // 年
"M+": (date.getMonth() + 1).toString(), // 月
"D+": date.getDate().toString(), // 日
"h+": date.getHours().toString(), // 时
"m+": date.getMinutes().toString(), // 分
"s+": date.getSeconds().toString() // 秒
// 有其他格式化字符需求可以继续添加,必须转化成字符串
}
for (let k in opt) {
ret = new RegExp("(" + k + ")").exec(fmt)
if (ret) {
fmt = fmt.replace(ret[1], (ret[1].length == 1) ? (opt[k]) : (opt[k].padStart(ret[1].length, "0")))
}
}
return fmt
}
// 字符串拼数组
export function spliceArray(targetArr) {
let currentArr = []
if(targetArr.length > 0) {
targetArr.forEach((item, index) => {
if(index % 2 == 0 && index != targetArr.length){
currentArr.push([targetArr[index],targetArr[index+1]])
}
})
}
return currentArr
}
// 数组转字符串
export function spliceString(targetArr){
let tempArr = []
let currentStr = ''
if(targetArr.length > 0) {
targetArr.forEach(item => {
tempArr.push(...item)
})
currentStr = tempArr.toString()
}
return currentStr
}