front/src/utils/agentInviteStorage.ts
zzh 072142d7bb
All checks were successful
Build and Deploy Front / deploy (push) Successful in 6m30s
feat: 代理门户返佣预计态与客户绑定代理
运单列表/工作台/返佣四态展示,注册归因与客户表单改绑代理,侧栏与占位邮箱优化。

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-10-30 11:07:07 +08:00

109 lines
2.7 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/** 渠道代理注册归因暂存:7 天 last-touch(与 invite / advisor 并列,参数名为 agent) */
const AGENT_INVITE_STORAGE_KEY = 'oms_agent_invite_v1'
const AGENT_INVITE_TTL_MS = 7 * 24 * 60 * 60 * 1000
type AgentInvitePayload = {
code: string
/** URL 带来的锁定码,手填不应覆盖 */
locked?: boolean
ts: number
}
function safeParse(raw: string | null): AgentInvitePayload | null {
if (!raw) return null
try {
const parsed = JSON.parse(raw) as AgentInvitePayload
if (!parsed?.code || typeof parsed.ts !== 'number') return null
return {
code: String(parsed.code).trim(),
locked: Boolean(parsed.locked),
ts: parsed.ts,
}
} catch {
return null
}
}
function isExpired(ts: number, now = Date.now()): boolean {
return now - ts > AGENT_INVITE_TTL_MS
}
export function stashAgentInvite(agentCode?: string | null, locked = false): void {
const code = String(agentCode ?? '').trim()
if (!code) return
const payload: AgentInvitePayload = { code, locked, ts: Date.now() }
const raw = JSON.stringify(payload)
try {
localStorage.setItem(AGENT_INVITE_STORAGE_KEY, raw)
} catch {
/* ignore */
}
try {
sessionStorage.setItem(AGENT_INVITE_STORAGE_KEY, raw)
} catch {
/* ignore */
}
}
export function readStashedAgentInvite(): { code: string; locked: boolean } {
const now = Date.now()
const fromSession = safeParse(
(() => {
try {
return sessionStorage.getItem(AGENT_INVITE_STORAGE_KEY)
} catch {
return null
}
})(),
)
if (fromSession && !isExpired(fromSession.ts, now)) {
return { code: fromSession.code, locked: Boolean(fromSession.locked) }
}
const fromLocal = safeParse(
(() => {
try {
return localStorage.getItem(AGENT_INVITE_STORAGE_KEY)
} catch {
return null
}
})(),
)
if (fromLocal && !isExpired(fromLocal.ts, now)) {
stashAgentInvite(fromLocal.code, Boolean(fromLocal.locked))
return { code: fromLocal.code, locked: Boolean(fromLocal.locked) }
}
if (fromLocal || fromSession) {
clearStashedAgentInvite()
}
return { code: '', locked: false }
}
export function clearStashedAgentInvite(): void {
try {
localStorage.removeItem(AGENT_INVITE_STORAGE_KEY)
} catch {
/* ignore */
}
try {
sessionStorage.removeItem(AGENT_INVITE_STORAGE_KEY)
} catch {
/* ignore */
}
}
/** 解析代理归因:query.agent → 7 天暂存 */
export function resolveAgentInvite(query: Record<string, unknown>): {
code: string
locked: boolean
} {
const fromQuery = String(query.agent ?? query.agentCode ?? '').trim()
if (fromQuery) {
stashAgentInvite(fromQuery, true)
return { code: fromQuery, locked: true }
}
return readStashedAgentInvite()
}