关闭职位页空个人资料时强制上传一份经历,和添加时间处理工具

This commit is contained in:
2026-05-22 09:59:34 +08:00
parent 1637ee5bbc
commit dd103f0794
6 changed files with 269 additions and 17 deletions
+168
View File
@@ -0,0 +1,168 @@
/**
* 时间处理工具
* 提供毫秒时间戳转换、时间间隔计算方法、浏览器本地时间事件缓存记录工具
*/
/**
* 时间精度级别
* returnYear — 只返回年
* returnMonth — 返回到月
* returnDay — 返回到天
* returnHour — 返回到小时
* returnMinute — 返回到分钟
* returnSecond — 返回到秒
*/
export type TimePrecision = 'returnYear' | 'returnMonth' | 'returnDay' | 'returnHour' | 'returnMinute' | 'returnSecond'
/**
* 毫秒时间戳转 LocalDateTime 字符串
* @param timestamp 毫秒时间戳
* @param precision 返回精度,默认 returnSecond
* @returns 格式化的本地时间字符串
*/
export function timestampToLocalDateTime(timestamp: number | null | undefined, precision: TimePrecision = 'returnSecond'): string {
if (timestamp == null) return '--'
const date = new Date(timestamp)
const y = date.getFullYear()
if (precision === 'returnYear') return `${y}`
const m = String(date.getMonth() + 1).padStart(2, '0')
if (precision === 'returnMonth') return `${y}-${m}`
const d = String(date.getDate()).padStart(2, '0')
if (precision === 'returnDay') return `${y}-${m}-${d}`
const h = String(date.getHours()).padStart(2, '0')
if (precision === 'returnHour') return `${y}-${m}-${d} ${h}`
const min = String(date.getMinutes()).padStart(2, '0')
if (precision === 'returnMinute') return `${y}-${m}-${d} ${h}:${min}`
const s = String(date.getSeconds()).padStart(2, '0')
return `${y}-${m}-${d} ${h}:${min}:${s}`
}
/**
* 计算毫秒时间戳与当前时间的间隔天数
* 如果目标时间在当前时间之前,返回负数
* @param timestamp 毫秒时间戳
* @returns 间隔天数(正数表示未来,负数表示过去)
*/
export function timestampDiffDays(timestamp: number | null | undefined): number {
if (timestamp == null) return 0
const diffMs = timestamp - Date.now()
return Math.floor(diffMs / (1000 * 60 * 60 * 24))
}
/**
* 计算毫秒时间戳与当前时间的详细间隔
* 返回如 "200天1小时5分30秒" 或 "-3天2小时10分15秒" 的格式
* @param timestamp 毫秒时间戳
* @returns 格式化的时间间隔字符串
*/
export function timestampDiffDetailed(timestamp: number | null | undefined): string {
if (timestamp == null) return '--'
let diffMs = timestamp - Date.now()
// 判断是否为过去时间
const prefix = diffMs < 0 ? '-' : ''
diffMs = Math.abs(diffMs)
const days = Math.floor(diffMs / (1000 * 60 * 60 * 24))
diffMs %= 1000 * 60 * 60 * 24
const hours = Math.floor(diffMs / (1000 * 60 * 60))
diffMs %= 1000 * 60 * 60
const minutes = Math.floor(diffMs / (1000 * 60))
diffMs %= 1000 * 60
const seconds = Math.floor(diffMs / 1000)
// 拼接结果,只显示有值的部分
let result = ''
if (days > 0) result += `${days}`
if (hours > 0) result += `${hours}小时`
if (minutes > 0) result += `${minutes}`
if (seconds > 0 || result === '') result += `${seconds}`
return `${prefix}${result}`
}
// ==================== 浏览器本地时间事件缓存记录工具 ====================
/** localStorage 存储的 key */
const TIME_EVENT_CACHE_KEY = 'local_time_event_cache'
/**
* 自定义事件名称 ID 注册表
* 每次新增事件 ID 必须在此处登记,格式:事件ID — 事件描述说明
*
* ┌──────────────────────────────────────────────────────────────┐
* │ 事件名称ID │ 事件描述说明 │
* ├──────────────────────────────────────────────────────────────┤
* │ member_status_query │ 会员状态查询时间记录 │
* │ │ │
* │ │ │
* │ │ │
* └──────────────────────────────────────────────────────────────┘
*/
/** 本地时间事件缓存数据项 */
export interface TimeEventRecord {
/** 用户ID(对应 vuex store 中 userInfo.id */
userId: number | string
/** 事件名称ID(需在上方注册表中登记) */
eventId: string
/** 时间点(LocalDateTime 格式字符串,如 "2026-05-21 14:30:00" */
time: string
}
/**
* 从 localStorage 读取时间事件缓存数据
* @returns 缓存的时间事件数组
*/
function getTimeEventCache(): TimeEventRecord[] {
try {
const raw = localStorage.getItem(TIME_EVENT_CACHE_KEY)
if (!raw) return []
return JSON.parse(raw) as TimeEventRecord[]
} catch {
return []
}
}
/**
* 将时间事件缓存数据保存到 localStorage
* @param data 时间事件数组
*/
function setTimeEventCache(data: TimeEventRecord[]): void {
localStorage.setItem(TIME_EVENT_CACHE_KEY, JSON.stringify(data))
}
/**
* 查询本地时间事件缓存
* 根据用户ID和事件名称ID查找对应的时间记录
* @param eventId 事件名称ID
* @param userId 用户ID(从 vuex store.state.userInfo.id 获取)
* @returns 匹配的时间点字符串,未找到返回 null
*/
export function getTimeEvent(eventId: string, userId: number | string): string | null {
const cache = getTimeEventCache()
const record = cache.find(item => item.userId == userId && item.eventId === eventId)
return record ? record.time : null
}
/**
* 保存/更新本地时间事件缓存
* 同一个用户ID + 事件名称ID 只允许存在唯一一条记录
* @param eventId 事件名称ID
* @param userId 用户ID(从 vuex store.state.userInfo.id 获取)
* @param time 要保存的时间点(LocalDateTime 格式字符串)
*/
export function setTimeEvent(eventId: string, userId: number | string, time: string): void {
const cache = getTimeEventCache()
const index = cache.findIndex(item => item.userId == userId && item.eventId === eventId)
if (index >= 0) {
// 已存在则更新时间点
cache[index].time = time
} else {
// 不存在则新增一条记录
cache.push({ userId, eventId, time })
}
setTimeEventCache(cache)
}