fix(mini-user): use Taro.request for analytics and prod API origin
Replace fetch with wx/Taro.request in client-logging for WeChat mini program; point production build to api.dukanghaoke.com. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -1,14 +1,50 @@
|
||||
const SESSION_KEY = 'dukang_session_id';
|
||||
|
||||
type WxStorage = {
|
||||
getStorageSync?: (key: string) => unknown;
|
||||
setStorageSync?: (key: string, value: unknown) => void;
|
||||
};
|
||||
|
||||
function getWxStorage(): WxStorage | undefined {
|
||||
return (globalThis as { wx?: WxStorage }).wx;
|
||||
}
|
||||
|
||||
function readStorage(key: string): string | null {
|
||||
if (typeof localStorage !== 'undefined') {
|
||||
return localStorage.getItem(key);
|
||||
}
|
||||
try {
|
||||
const v = getWxStorage()?.getStorageSync?.(key);
|
||||
return v != null && v !== '' ? String(v) : null;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
function writeStorage(key: string, value: string): void {
|
||||
if (typeof localStorage !== 'undefined') {
|
||||
localStorage.setItem(key, value);
|
||||
return;
|
||||
}
|
||||
try {
|
||||
getWxStorage()?.setStorageSync?.(key, value);
|
||||
} catch {
|
||||
/* ignore */
|
||||
}
|
||||
}
|
||||
|
||||
function newSessionId(): string {
|
||||
if (typeof crypto !== 'undefined' && 'randomUUID' in crypto) {
|
||||
return crypto.randomUUID();
|
||||
}
|
||||
return `s_${Date.now()}_${Math.random().toString(36).slice(2, 10)}`;
|
||||
}
|
||||
|
||||
export function getSessionId(): string {
|
||||
if (typeof localStorage === 'undefined') return 'ssr';
|
||||
let id = localStorage.getItem(SESSION_KEY);
|
||||
let id = readStorage(SESSION_KEY);
|
||||
if (!id) {
|
||||
id =
|
||||
typeof crypto !== 'undefined' && 'randomUUID' in crypto
|
||||
? crypto.randomUUID()
|
||||
: `s_${Date.now()}_${Math.random().toString(36).slice(2, 10)}`;
|
||||
localStorage.setItem(SESSION_KEY, id);
|
||||
id = newSessionId();
|
||||
writeStorage(SESSION_KEY, id);
|
||||
}
|
||||
return id;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user