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
+39 -20
View File
@@ -1,3 +1,4 @@
import { postJson, type PostJson } from './http';
import { getSessionId } from './session';
export type TrackParams = Record<string, unknown>;
@@ -6,10 +7,20 @@ export type UserTrackerOptions = {
apiBase: string;
clientApp: string;
getToken?: () => string | null;
postJson?: PostJson;
getPagePath?: () => string | undefined;
};
function resolvePagePath(getPagePath?: () => string | undefined): string | undefined {
if (getPagePath) return getPagePath();
if (typeof window !== 'undefined') return window.location.pathname;
return undefined;
}
export function createUserTracker(opts: UserTrackerOptions) {
const getToken = opts.getToken ?? (() => localStorage.getItem('accessToken'));
const send = opts.postJson ?? postJson;
const getToken = opts.getToken ?? (() =>
typeof localStorage !== 'undefined' ? localStorage.getItem('accessToken') : null);
function track(eventName: string, params?: TrackParams) {
const sessionId = getSessionId();
@@ -20,27 +31,27 @@ export function createUserTracker(opts: UserTrackerOptions) {
};
if (token) headers.Authorization = `Bearer ${token}`;
void fetch(`${opts.apiBase}/analytics/events`, {
method: 'POST',
send({
url: `${opts.apiBase}/analytics/events`,
headers,
body: JSON.stringify({
body: {
sessionId,
clientApp: opts.clientApp,
events: [{ eventName, params: { sessionId, ...params } }],
}),
}).catch(() => {});
},
});
}
function trackPageView(eventName: string, params?: TrackParams) {
track(eventName, {
pagePath: typeof window !== 'undefined' ? window.location.pathname : undefined,
pagePath: resolvePagePath(opts.getPagePath),
...params,
});
}
function trackSessionStart() {
track('session_start', {
pagePath: typeof window !== 'undefined' ? window.location.pathname : undefined,
pagePath: resolvePagePath(opts.getPagePath),
});
}
@@ -52,32 +63,36 @@ export type StoreTrackerOptions = {
clientApp: string;
getToken: () => string | null;
getStoreId: () => string | null;
postJson?: PostJson;
getPagePath?: () => string | undefined;
};
export function createStoreTracker(opts: StoreTrackerOptions) {
const send = opts.postJson ?? postJson;
function track(eventName: string, params?: TrackParams) {
const token = opts.getToken();
const storeId = opts.getStoreId();
if (!token || !storeId) return;
void fetch(`${opts.apiBase}/analytics/store-events`, {
method: 'POST',
send({
url: `${opts.apiBase}/analytics/store-events`,
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${token}`,
'X-Client-App': opts.clientApp,
},
body: JSON.stringify({
body: {
sessionId: getSessionId(),
storeId,
events: [{ eventName, params: { sessionId: getSessionId(), storeId, ...params } }],
}),
}).catch(() => {});
},
});
}
function trackPageView(eventName: string, params?: TrackParams) {
track(eventName, {
pagePath: typeof window !== 'undefined' ? window.location.pathname : undefined,
pagePath: resolvePagePath(opts.getPagePath),
...params,
});
}
@@ -89,30 +104,34 @@ export type PartnerTrackerOptions = {
apiBase: string;
clientApp: string;
getToken: () => string | null;
postJson?: PostJson;
getPagePath?: () => string | undefined;
};
export function createPartnerTracker(opts: PartnerTrackerOptions) {
const send = opts.postJson ?? postJson;
function track(eventName: string, params?: TrackParams) {
const token = opts.getToken();
if (!token) return;
void fetch(`${opts.apiBase}/analytics/partner-events`, {
method: 'POST',
send({
url: `${opts.apiBase}/analytics/partner-events`,
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${token}`,
'X-Client-App': opts.clientApp,
},
body: JSON.stringify({
body: {
sessionId: getSessionId(),
events: [{ eventName, params: { sessionId: getSessionId(), ...params } }],
}),
}).catch(() => {});
},
});
}
function trackPageView(eventName: string, params?: TrackParams) {
track(eventName, {
pagePath: typeof window !== 'undefined' ? window.location.pathname : undefined,
pagePath: resolvePagePath(opts.getPagePath),
...params,
});
}