fix:bug解决
This commit is contained in:
@@ -1,17 +1,123 @@
|
||||
const STORAGE_KEY = 'czzj_password_modified_at'
|
||||
import { isSamePhone, normalizePhoneDigits } from '@/utils/validate'
|
||||
|
||||
/** 密码修改成功后记录时间戳 */
|
||||
export function markPasswordModified(at = Date.now()): void {
|
||||
uni.setStorageSync(STORAGE_KEY, at)
|
||||
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
|
||||
}
|
||||
|
||||
export function getPasswordModifiedAt(): number | null {
|
||||
const value = uni.getStorageSync(STORAGE_KEY)
|
||||
if (typeof value === 'number' && value > 0) return value
|
||||
if (typeof value === 'string' && /^\d+$/.test(value)) return Number(value)
|
||||
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 '上次修改:暂无记录'
|
||||
|
||||
Reference in New Issue
Block a user