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:
@@ -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: () => {},
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user