zjk
2026-09-29 54dcf0a82e7490fa58b1c056d5c0b9cb739c2931
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
// 安卓业务插件桥接,保持与设备初始化页相同的插件入口。
let HardwarePlugin = null;
if (typeof uni !== 'undefined' && typeof uni.requireNativePlugin === 'function') {
  HardwarePlugin = uni.requireNativePlugin('business-module');
}
const getPluginMethod = (methodNames = []) => {
  if (!HardwarePlugin) {
    return null;
  }
 
  return methodNames.find((methodName) => typeof HardwarePlugin[methodName] === 'function') || null;
};
 
export const HardwareAPI = {
  openWebPay(options = {}, callback) {
    return new Promise((resolve, reject) => {
      // #ifdef APP-PLUS
      console.log('[HardwareAPI] openWebPay 准备调用, options:', JSON.stringify(options));
      console.log('[HardwareAPI] business-module:', HardwarePlugin);
      const methodName = getPluginMethod(['openWebPay']);
      if (!methodName) {
        console.error('[HardwareAPI] openWebPay 方法不存在');
        reject(new Error('当前插件未提供网页支付能力'));
        return;
      }
      console.log('[HardwareAPI] openWebPay 实际调用方法:', methodName);
      try {
        HardwarePlugin[methodName](options, (result) => {
          console.log('[HardwareAPI] openWebPay 原生回调:', result);
          if (typeof callback === 'function') {
            callback(result);
          }
          resolve(result);
        });
      } catch (error) {
        console.error('[HardwareAPI] openWebPay 调用异常:', error);
        reject(error);
      }
      // #endif
      // #ifndef APP-PLUS
      reject(new Error('当前平台不支持网页支付'));
      // #endif
    });
  },
};