Browse Source

完成微信支付

chenritian 2 tuần trước cách đây
mục cha
commit
5ea7436f6e

+ 55 - 3
entry/src/main/ets/model/WechatModel.ets

@@ -1,5 +1,5 @@
 import { IBestToast } from '@ibestservices/ibest-ui';
-import { SendAuthReq } from '@tencent/wechat_open_sdk';
+import { PayReq, SendAuthReq } from '@tencent/wechat_open_sdk';
 import { ContextHelper } from '../utils/ContextHelper';
 import { WechatUtil } from '../utils/wechat/WechatUtil';
 
@@ -25,7 +25,48 @@ export interface AccessTokenResult {
 }
 
 
-export class WechatObj {
+/**
+ * 微信支付参数接口
+ */
+interface WeChatPayParams {
+  /**
+   * 应用ID(AppID)
+   */
+  appid: string;
+
+  /**
+   * 商户号(PartnerID)
+   */
+  partnerid: string;
+
+  /**
+   * 预支付交易会话ID
+   */
+  prepayid: string;
+
+  /**
+   * 固定值
+   */
+  package: string;
+
+  /**
+   * 随机字符串
+   */
+  noncestr: string;
+
+  /**
+   * 时间戳(单位:秒)
+   */
+  timestamp: string | number;
+
+  /**
+   * 签名,这里用的 MD5 签名
+   */
+  sign: string;
+}
+
+
+export class JDBObj {
   async handleWeChatLogin(): Promise<void> {
     try {
       let req = new SendAuthReq();
@@ -35,9 +76,20 @@ export class WechatObj {
       req.state = 'none';
       req.transaction = 'test123';
       let finished = await WechatUtil.getWechatApi().sendReq(ContextHelper.UIAbilityContext, req);
-      IBestToast.show({ message: '微信登录成功' });
     } catch (error) {
       IBestToast.show({ message: '微信登录失败,请重试', type: 'fail' });
     }
   }
+
+  async handleWeChatPay(param: WeChatPayParams) {
+    let req = new PayReq()
+    req.appId = param.appid
+    req.partnerId = param.partnerid
+    req.prepayId = param.prepayid
+    req.packageValue = param.package
+    req.nonceStr = param.noncestr
+    req.timeStamp = param.timestamp.toString()
+    req.sign = param.sign
+    await WechatUtil.getWechatApi().sendReq(ContextHelper.UIAbilityContext, req)
+  }
 }

+ 8 - 1
entry/src/main/ets/pages/Index.ets

@@ -70,6 +70,13 @@ struct Index {
             .domStorageAccess(true) // 关键:开启 DOM Storage 访问权限
             .databaseAccess(true)   // 建议同时开启数据库访问权限
             .javaScriptAccess(true) // 确保 JS 正常执行
+            .javaScriptProxy({
+              object: jDBViewModel.jDBItem,
+              name: "jDBItem",
+              methodList: ["handleWeChatLogin", "handleWeChatPay"],
+              asyncMethodList: ["handleWeChatLogin", "handleWeChatPay"],
+              controller: jDBViewModel.controller,
+            })
         }
         .padding({
           top: this.safeTop,
@@ -139,7 +146,7 @@ struct Index {
             .width(100)
             .height(50)
             .onClick(() => {
-              jDBViewModel.wechatItem.handleWeChatLogin()
+              jDBViewModel.jDBItem.handleWeChatLogin()
 
             })
             .position({

+ 4 - 1
entry/src/main/ets/utils/wechat/WXApiEventHandlerImpl.ets

@@ -1,5 +1,5 @@
 import { IBestToast } from '@ibestservices/ibest-ui';
-import { BaseReq, BaseResp, LaunchFromWXReq, SendAuthResp, WXApiEventHandler } from '@tencent/wechat_open_sdk';
+import { BaseReq, BaseResp, LaunchFromWXReq, PayResp, SendAuthResp, WXApiEventHandler } from '@tencent/wechat_open_sdk';
 import { WechatApi } from '../../apis/WechatApi';
 import { AccessTokenResponse, WeChatExtData } from '../../model/WechatModel';
 import { YTLog } from '../YTLog';
@@ -64,6 +64,9 @@ export class WXApiEventHandlerImpl implements WXApiEventHandler {
         // CommonUtils.showToastContent($r('app.string.wechat_login_failed'));
         IBestToast.show({ type: 'fail', message: ` ${resp.errCode}, errStr: ${resp.errStr}` })
       }
+    } else if (resp instanceof PayResp) {
+      // 支付回调处理
+      console.log('微信支付结果', JSON.stringify(resp));
     }
   }
 

+ 0 - 22
entry/src/main/ets/utils/wechat/WechatUtil.ets

@@ -19,26 +19,4 @@ export class WechatUtil {
     return WechatUtil.wechatApi;
   }
 
-
-  static async startWechatLogin(): Promise<void> {
-    try {
-      let req = new SendAuthReq();
-      req.isOption1 = false;
-      req.nonAutomatic = true;
-      req.scope = 'test_userinfo';
-      req.state = 'none';
-      req.transaction = 'test123';
-
-      let flag = await WechatUtil.getWechatApi().sendReq(ContextHelper.UIAbilityContext, req);
-      if (flag) {
-      } else {
-        IBestToast.show({ message: '微信登录失败,请重试', type: 'fail' });
-      }
-    } catch (error) {
-      console.error("WeChat login error: ", error);
-      IBestToast.show({ message: '微信登录失败,请重试', type: 'fail' });
-    }
-  }
-
-
 }

+ 4 - 4
entry/src/main/ets/viewModels/JDBViewModel.ets

@@ -3,15 +3,15 @@ import { DeviceChannelQuery } from '../model/StatusModel'
 import { SystemUtil } from '../utils/SystemUtil'
 import { webview } from '@kit.ArkWeb'
 import { DEBUG } from 'BuildProfile'
-import { WechatObj } from '../model/WechatModel'
+import { JDBObj } from '../model/WechatModel'
 
 @ObservedV2
 export class JDBViewModel {
-  @Trace flag: boolean = false
-  @Trace isExistDeviceChannel: boolean = false
+  @Trace flag: boolean = true
+  @Trace isExistDeviceChannel: boolean = true
   controller: webview.WebviewController = new webview.WebviewController()
   host: string = ''
-  wechatItem: WechatObj = new WechatObj()
+  jDBItem: JDBObj = new JDBObj()
 
   /**
    * 判断渠道是否存在