139 lines
4.4 KiB
TypeScript
139 lines
4.4 KiB
TypeScript
import { isSamePhone, normalizePhoneDigits } from '@/utils/validate'
|
||
|
||
const LEGACY_STORAGE_KEY = 'czzj_password_modified_at'
|
||
export const PASSWORD_MODIFIED_RECORD_KEY = 'czzj_password_modified_record'
|
||
const PENDING_BY_PHONE_KEY = 'czzj_password_modified_pending_phone'
|
||
|
||
const STORAGE_KEY = PASSWORD_MODIFIED_RECORD_KEY
|
||
|
||
interface PasswordModifiedRecord {
|
||
userId: string
|
||
at: number
|
||
}
|
||
|
||
interface PendingPasswordModifiedRecord {
|
||
phone: string
|
||
at: number
|
||
}
|
||
|
||
function readRecord(): PasswordModifiedRecord | null {
|
||
try {
|
||
const raw = uni.getStorageSync(STORAGE_KEY) as PasswordModifiedRecord | string | number
|
||
if (raw && typeof raw === 'object' && 'userId' in raw && 'at' in raw) {
|
||
const userId = String((raw as PasswordModifiedRecord).userId).trim()
|
||
const at = Number((raw as PasswordModifiedRecord).at)
|
||
if (userId && at > 0) return { userId, at }
|
||
}
|
||
} catch {
|
||
// ignore
|
||
}
|
||
return null
|
||
}
|
||
|
||
/** 移除旧版全局时间戳(不区分账号,会导致注销后新账号误读) */
|
||
export function purgeLegacyPasswordModifiedStorage(): void {
|
||
try {
|
||
uni.removeStorageSync(LEGACY_STORAGE_KEY)
|
||
} catch {
|
||
// ignore
|
||
}
|
||
}
|
||
|
||
/** 密码修改成功后记录时间戳(必须绑定当前 userId) */
|
||
export function markPasswordModified(userId: string | number, at = Date.now()): void {
|
||
const id = String(userId).trim()
|
||
if (!id) return
|
||
purgeLegacyPasswordModifiedStorage()
|
||
uni.setStorageSync(STORAGE_KEY, { userId: id, at })
|
||
}
|
||
|
||
export function clearPasswordModifiedAt(): void {
|
||
purgeLegacyPasswordModifiedStorage()
|
||
try {
|
||
uni.removeStorageSync(STORAGE_KEY)
|
||
uni.removeStorageSync(PENDING_BY_PHONE_KEY)
|
||
} catch {
|
||
// ignore
|
||
}
|
||
}
|
||
|
||
export function exportPasswordModifiedRecord(): PasswordModifiedRecord | null {
|
||
return readRecord()
|
||
}
|
||
|
||
export function restorePasswordModifiedRecord(record: PasswordModifiedRecord | null): void {
|
||
if (!record?.userId || !record.at) return
|
||
uni.setStorageSync(STORAGE_KEY, record)
|
||
}
|
||
|
||
function readPendingByPhone(): PendingPasswordModifiedRecord | null {
|
||
try {
|
||
const raw = uni.getStorageSync(PENDING_BY_PHONE_KEY) as PendingPasswordModifiedRecord
|
||
if (raw && typeof raw === 'object' && raw.phone && raw.at > 0) {
|
||
return { phone: normalizePhoneDigits(String(raw.phone)), at: Number(raw.at) }
|
||
}
|
||
} catch {
|
||
// ignore
|
||
}
|
||
return null
|
||
}
|
||
|
||
/** 未登录改密时先按手机号暂存,登录后由 adoptPendingPasswordModifiedForUser 写入 userId */
|
||
export function markPasswordModifiedPendingForPhone(phone: string, at = Date.now()): void {
|
||
const normalized = normalizePhoneDigits(phone)
|
||
if (!normalized) return
|
||
purgeLegacyPasswordModifiedStorage()
|
||
uni.setStorageSync(PENDING_BY_PHONE_KEY, { phone: normalized, at })
|
||
}
|
||
|
||
export function adoptPendingPasswordModifiedForUser(
|
||
userId: string | number,
|
||
phone?: string,
|
||
): number | null {
|
||
const pending = readPendingByPhone()
|
||
const boundPhone = phone?.trim() ? normalizePhoneDigits(phone) : ''
|
||
if (!pending || !boundPhone || !isSamePhone(pending.phone, boundPhone)) return null
|
||
markPasswordModified(userId, pending.at)
|
||
try {
|
||
uni.removeStorageSync(PENDING_BY_PHONE_KEY)
|
||
} catch {
|
||
// ignore
|
||
}
|
||
return pending.at
|
||
}
|
||
|
||
/** 仅返回与当前 userId 匹配的记录;不匹配则视为残留并清除 */
|
||
export function getPasswordModifiedAt(userId?: string | number | null): number | null {
|
||
purgeLegacyPasswordModifiedStorage()
|
||
|
||
const currentUserId = userId != null ? String(userId).trim() : ''
|
||
if (!currentUserId) return null
|
||
|
||
const record = readRecord()
|
||
if (!record) return null
|
||
if (record.userId !== currentUserId) {
|
||
clearPasswordModifiedAt()
|
||
return null
|
||
}
|
||
return record.at
|
||
}
|
||
|
||
/** 展示「上次修改:刚刚 / 3天前」等文案 */
|
||
export function formatPasswordModifiedDesc(at: number | null): string {
|
||
if (!at) return '上次修改:暂无记录'
|
||
|
||
const diff = Date.now() - at
|
||
if (diff < 0) return '上次修改:刚刚'
|
||
|
||
const minute = 60 * 1000
|
||
const hour = 60 * minute
|
||
const day = 24 * hour
|
||
|
||
if (diff < minute) return '上次修改:刚刚'
|
||
if (diff < hour) return `上次修改:${Math.floor(diff / minute)}分钟前`
|
||
if (diff < day) return `上次修改:${Math.floor(diff / hour)}小时前`
|
||
if (diff < 30 * day) return `上次修改:${Math.floor(diff / day)}天前`
|
||
if (diff < 365 * day) return `上次修改:${Math.floor(diff / (30 * day))}个月前`
|
||
return `上次修改:${Math.floor(diff / (365 * day))}年前`
|
||
}
|