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,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,
|
||||
});
|
||||
}
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
import { postJson } from './http';
|
||||
|
||||
export type ClientErrorLevel = 'fatal' | 'error' | 'warn' | 'info';
|
||||
export type ClientErrorCategory =
|
||||
| 'js_error'
|
||||
@@ -31,10 +33,10 @@ export function installClientErrorReporting(opts: ClientErrorReporterOptions) {
|
||||
};
|
||||
if (token) headers.Authorization = `Bearer ${token}`;
|
||||
|
||||
void fetch(`${opts.apiBase}/common/client-errors`, {
|
||||
method: 'POST',
|
||||
postJson({
|
||||
url: `${opts.apiBase}/common/client-errors`,
|
||||
headers,
|
||||
body: JSON.stringify({
|
||||
body: {
|
||||
level: payload.level,
|
||||
category: payload.category,
|
||||
message: payload.message,
|
||||
@@ -42,8 +44,8 @@ export function installClientErrorReporting(opts: ClientErrorReporterOptions) {
|
||||
pagePath: window.location.pathname,
|
||||
clientApp: opts.clientApp,
|
||||
extra: payload.extra,
|
||||
}),
|
||||
}).catch(() => {});
|
||||
},
|
||||
});
|
||||
};
|
||||
|
||||
window.addEventListener('error', (ev) => {
|
||||
@@ -78,16 +80,16 @@ export function reportApiError(
|
||||
};
|
||||
if (token) headers.Authorization = `Bearer ${token}`;
|
||||
|
||||
void fetch(`${opts.apiBase}/common/client-errors`, {
|
||||
method: 'POST',
|
||||
postJson({
|
||||
url: `${opts.apiBase}/common/client-errors`,
|
||||
headers,
|
||||
body: JSON.stringify({
|
||||
body: {
|
||||
level: 'warn',
|
||||
category: input.category ?? 'api_error',
|
||||
message: input.message.slice(0, 1000),
|
||||
pagePath: window.location.pathname,
|
||||
clientApp: opts.clientApp,
|
||||
extra: { status: input.status, url: input.url },
|
||||
}),
|
||||
}).catch(() => {});
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
export type PostJsonInput = {
|
||||
url: string;
|
||||
headers?: Record<string, string>;
|
||||
body: unknown;
|
||||
};
|
||||
|
||||
export type PostJson = (input: PostJsonInput) => void;
|
||||
|
||||
type WxLike = {
|
||||
request?: (opts: {
|
||||
url: string;
|
||||
method?: string;
|
||||
header?: Record<string, string>;
|
||||
data?: unknown;
|
||||
fail?: () => void;
|
||||
}) => void;
|
||||
};
|
||||
|
||||
function getWx(): WxLike | undefined {
|
||||
return (globalThis as { wx?: WxLike }).wx;
|
||||
}
|
||||
|
||||
/** fire-and-forget POST JSON;优先 fetch,小程序回退 wx.request */
|
||||
export function postJson(input: PostJsonInput): void {
|
||||
const headers = input.headers ?? { 'Content-Type': 'application/json' };
|
||||
|
||||
if (typeof fetch === 'function') {
|
||||
void fetch(input.url, {
|
||||
method: 'POST',
|
||||
headers,
|
||||
body: JSON.stringify(input.body),
|
||||
}).catch(() => {});
|
||||
return;
|
||||
}
|
||||
|
||||
const wxApi = getWx();
|
||||
if (wxApi?.request) {
|
||||
wxApi.request({
|
||||
url: input.url,
|
||||
method: 'POST',
|
||||
header: headers,
|
||||
data: input.body,
|
||||
fail: () => {},
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -8,6 +8,7 @@ export {
|
||||
type StoreTrackerOptions,
|
||||
type PartnerTrackerOptions,
|
||||
} from './analytics';
|
||||
export { postJson, type PostJson, type PostJsonInput } from './http';
|
||||
export {
|
||||
installClientErrorReporting,
|
||||
reportApiError,
|
||||
|
||||
@@ -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