Ship customer portal and marketing pages, user edit drawer with employee archive, IDE employee numbers, channel config refactor, and related nginx/route updates. Co-authored-by: Cursor <cursoragent@cursor.com>
95 lines
2.2 KiB
TypeScript
95 lines
2.2 KiB
TypeScript
/** 邀请码归因暂存:7 天 last-touch(与裂变方案一致) */
|
||
|
||
const INVITE_REF_STORAGE_KEY = 'oms_invite_ref_v1';
|
||
const INVITE_REF_TTL_MS = 7 * 24 * 60 * 60 * 1000;
|
||
|
||
type InviteRefPayload = {
|
||
code: string;
|
||
/** 写入时间戳,last-touch */
|
||
ts: number;
|
||
};
|
||
|
||
function safeParse(raw: string | null): InviteRefPayload | null {
|
||
if (!raw) return null;
|
||
try {
|
||
const parsed = JSON.parse(raw) as InviteRefPayload;
|
||
if (!parsed?.code || typeof parsed.ts !== 'number') return null;
|
||
return { code: String(parsed.code).trim(), ts: parsed.ts };
|
||
} catch {
|
||
return null;
|
||
}
|
||
}
|
||
|
||
function isExpired(ts: number, now = Date.now()): boolean {
|
||
return now - ts > INVITE_REF_TTL_MS;
|
||
}
|
||
|
||
/** 写入 / 刷新 last-touch */
|
||
export function stashInviteRef(inviteCode?: string | null): void {
|
||
const code = String(inviteCode ?? '').trim();
|
||
if (!code) return;
|
||
const payload: InviteRefPayload = { code, ts: Date.now() };
|
||
const raw = JSON.stringify(payload);
|
||
try {
|
||
localStorage.setItem(INVITE_REF_STORAGE_KEY, raw);
|
||
} catch {
|
||
/* ignore */
|
||
}
|
||
try {
|
||
sessionStorage.setItem(INVITE_REF_STORAGE_KEY, raw);
|
||
} catch {
|
||
/* ignore */
|
||
}
|
||
}
|
||
|
||
export function readStashedInviteRef(): string {
|
||
const now = Date.now();
|
||
const fromSession = safeParse(
|
||
(() => {
|
||
try {
|
||
return sessionStorage.getItem(INVITE_REF_STORAGE_KEY);
|
||
} catch {
|
||
return null;
|
||
}
|
||
})(),
|
||
);
|
||
if (fromSession && !isExpired(fromSession.ts, now)) {
|
||
return fromSession.code;
|
||
}
|
||
|
||
const fromLocal = safeParse(
|
||
(() => {
|
||
try {
|
||
return localStorage.getItem(INVITE_REF_STORAGE_KEY);
|
||
} catch {
|
||
return null;
|
||
}
|
||
})(),
|
||
);
|
||
if (fromLocal && !isExpired(fromLocal.ts, now)) {
|
||
// 同步回 session,方便同会话 OAuth
|
||
stashInviteRef(fromLocal.code);
|
||
return fromLocal.code;
|
||
}
|
||
|
||
if (fromLocal || fromSession) {
|
||
clearStashedInviteRef();
|
||
}
|
||
return '';
|
||
}
|
||
|
||
export function clearStashedInviteRef(): void {
|
||
try {
|
||
localStorage.removeItem(INVITE_REF_STORAGE_KEY);
|
||
} catch {
|
||
/* ignore */
|
||
}
|
||
try {
|
||
sessionStorage.removeItem(INVITE_REF_STORAGE_KEY);
|
||
} catch {
|
||
/* ignore */
|
||
}
|
||
}
|
||
|
||
export const INVITE_REF_TTL_DAYS = 7;
|