微信SDK接通

This commit is contained in:
2026-07-01 19:56:44 +08:00
parent a1cbfc7241
commit aea1513836
38 changed files with 1610 additions and 92 deletions
+40
View File
@@ -0,0 +1,40 @@
import { getRuntimePlatform } from './env';
import { ensureJssdkReady } from './jssdk';
import type { WeixinSdkConfig } from './types';
/** 调起扫码(返回二维码/条码内容,失败返回 null) */
export async function scanQrCode(config: WeixinSdkConfig): Promise<string | null> {
const platform = getRuntimePlatform();
if (platform === 'mini' && window.wx?.scanCode) {
return new Promise((resolve) => {
window.wx!.scanCode!({
onlyFromCamera: true,
scanType: ['qrCode', 'barCode'],
success: (res) => resolve(res.result || null),
fail: () => resolve(null),
});
});
}
if (platform === 'wechat-h5') {
await ensureJssdkReady({
apiBase: config.apiBase ?? '/api/v1',
clientApp: config.clientApp,
getAccessToken: config.getAccessToken,
});
if (window.wx?.scanQRCode) {
return new Promise((resolve) => {
window.wx!.scanQRCode!({
needResult: 1,
scanType: ['qrCode', 'barCode'],
success: (res) => resolve(res.resultStr || null),
fail: () => resolve(null),
});
});
}
}
const manual = typeof window !== 'undefined' ? window.prompt('当前环境无法调起微信扫码,请手动输入核销码') : null;
return manual?.trim() || null;
}