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:
2026-08-03 22:53:35 +08:00
parent 89fd333702
commit c8ea5a3119
7 changed files with 157 additions and 39 deletions
+43 -7
View File
@@ -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;
}