c8ea5a3119
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>
47 lines
1005 B
TypeScript
47 lines
1005 B
TypeScript
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: () => {},
|
|
});
|
|
}
|
|
}
|