瀏覽代碼

完成支付宝授权

chenritian 2 周之前
父節點
當前提交
b126688f1a

+ 6 - 0
entry/src/main/ets/entryability/EntryAbility.ets

@@ -7,6 +7,7 @@ import { AppStorageKeyCollect } from '../constants';
 import { WechatUtil } from '../utils/wechat/WechatUtil';
 import { YTLog } from '../utils/YTLog';
 import { wechatEventHandler } from '../utils/wechat/WXApiEventHandlerImpl';
+import { ALiPayUtil } from '../utils/alipay/ALiPayUtil';
 
 const TAG: string = '[EntryAbility]';
 const DOMAIN = 0x0000;
@@ -70,6 +71,7 @@ export default class EntryAbility extends UIAbility {
     YTLog.info(TAG, `onNewWant parameters: ${JSON.stringify(want.parameters)}`);
     YTLog.info(TAG, `onNewWant full Want: ${JSON.stringify(want)}`);
     this.handleWeChatCallIfNeed(want);
+    this.handleAliPayResponse(want);
   }
 
   private handleWeChatCallIfNeed(want: Want) {
@@ -77,4 +79,8 @@ export default class EntryAbility extends UIAbility {
     const handled = WechatUtil.getWechatApi().handleWant(want, wechatEventHandler);
     YTLog.info(TAG, `WXApi.handleWant result: ${handled}, eventHandler: ${wechatEventHandler ? 'exists' : 'null'}`);
   }
+
+  private handleAliPayResponse(want: Want) {
+    ALiPayUtil.handleResponse(want)
+  }
 }

+ 6 - 0
entry/src/main/ets/model/JDBModel.ets

@@ -0,0 +1,6 @@
+export interface RunH5Param {
+  // type: 'wx_login'-微信登录 | 'wx_pay'-微信支付 | 'ali_login' 支付宝登录授权 | 'ali_pay' 支付宝支付
+  // 'wx_share' - 微信分享 | 'wx_pyq_share' -微信朋友圈分享 | 'qq_share'QQ分享 | 'wx_code' - 微信授权码获取
+  type: 'wx_login' | 'wx_pay' | 'ali_login' | 'ali_pay' | 'wx_share' | 'wx_pyq_share' | 'qq_share' | 'wx_code'
+  data: ESObject
+}

+ 6 - 0
entry/src/main/ets/model/WechatModel.ets

@@ -1,5 +1,6 @@
 import { IBestToast } from '@ibestservices/ibest-ui';
 import { PayReq, SendAuthReq } from '@tencent/wechat_open_sdk';
+import { ALiPayUtil } from '../utils/alipay/ALiPayUtil';
 import { ContextHelper } from '../utils/ContextHelper';
 import { WechatUtil } from '../utils/wechat/WechatUtil';
 
@@ -92,4 +93,9 @@ export class JDBObj {
     req.sign = param.sign
     await WechatUtil.getWechatApi().sendReq(ContextHelper.UIAbilityContext, req)
   }
+
+
+  handleAlipayAuthorization(){
+    ALiPayUtil.startAuthorization()
+  }
 }

+ 22 - 12
entry/src/main/ets/pages/Index.ets

@@ -73,8 +73,8 @@ struct Index {
             .javaScriptProxy({
               object: jDBViewModel.jDBItem,
               name: "jDBItem",
-              methodList: ["handleWeChatLogin", "handleWeChatPay"],
-              asyncMethodList: ["handleWeChatLogin", "handleWeChatPay"],
+              methodList: ["handleWeChatLogin", "handleWeChatPay", "handleAlipayAuthorization"],
+              asyncMethodList: ["handleWeChatLogin", "handleWeChatPay", "handleAlipayAuthorization"],
               controller: jDBViewModel.controller,
             })
         }
@@ -142,17 +142,27 @@ struct Index {
             bottom: YTAvoid.getBottom()
           })
 
-          Button('微信登录')
-            .width(100)
-            .height(50)
-            .onClick(() => {
-              jDBViewModel.jDBItem.handleWeChatLogin()
+          Row() {
+            Button('微信登录')
+              .width(100)
+              .height(50)
+              .onClick(() => {
+                jDBViewModel.jDBItem.handleWeChatLogin()
 
-            })
-            .position({
-              left: '50%',
-              bottom: 200
-            })
+              })
+
+            Button('支付宝授权')
+              .width(100)
+              .height(50)
+              .onClick(() => {
+                jDBViewModel.jDBItem.handleAlipayAuthorization()
+
+              })
+          }
+          .position({
+            left: '50%',
+            bottom: 200
+          })
 
         }.width('100%').height('100%')
       }

+ 47 - 0
entry/src/main/ets/utils/alipay/ALiPayUtil.ets

@@ -0,0 +1,47 @@
+import { AFAuthServiceResponse, AFService, AFServiceCenter, AFServiceParams, AFWantParams } from '@alipay/afservicesdk';
+import Want from '@ohos.app.ability.Want';
+import BuildProfile from 'BuildProfile';
+import { jDBViewModel } from '../../viewModels/JDBViewModel';
+
+export class ALiPayUtil {
+  static ALIPAY_APP_ID = '2021006101646784'
+  static scheme = 'jdb'
+  static scope = 'auth_user'
+  static state = 'init'
+
+  static handleResponse(want: Want) {
+    return AFServiceCenter.handleResponse(want);
+  }
+
+  static startAuthorization() {
+    /**
+     * 构建参数
+     */
+    let bizParams = new Map<string, string>()
+    let url =
+      encodeURIComponent(`https://authweb.alipay.com/auth?auth_type=PURE_OAUTH_SDK&app_id=${ALiPayUtil.ALIPAY_APP_ID}&scope=${ALiPayUtil.scope}&state=${ALiPayUtil.state}`)
+    bizParams.set("url", url);
+
+    let backWant: AFWantParams = {
+      bundleName: BuildProfile.BUNDLE_NAME, //接入方应用包名
+      moduleName: "entry",
+      abilityName: "EntryAbility"
+    }
+
+    let params =
+      new AFServiceParams(bizParams, false, true, ALiPayUtil.scheme, backWant, (response: AFAuthServiceResponse) => {
+        //授权返回值
+        console.info('=========ALiPay response=' + JSON.stringify(response));
+        //TODO 调用业务方接口处理授权结果
+        jDBViewModel.runJavaScript({
+          type: 'ali_login',
+          data: response
+        })
+      })
+
+    /**
+     * 调用授权
+     */
+    AFServiceCenter.call(AFService.AFServiceAuth, params)
+  }
+}

+ 14 - 0
entry/src/main/ets/viewModels/JDBViewModel.ets

@@ -4,6 +4,7 @@ import { SystemUtil } from '../utils/SystemUtil'
 import { webview } from '@kit.ArkWeb'
 import { DEBUG } from 'BuildProfile'
 import { JDBObj } from '../model/WechatModel'
+import { RunH5Param } from '../model/JDBModel'
 
 @ObservedV2
 export class JDBViewModel {
@@ -52,6 +53,19 @@ export class JDBViewModel {
     }
     return `${host}/?v=${Date.now()}#/`
   }
+
+  runJavaScript(param: RunH5Param) {
+
+    this.controller.runJavaScript(
+      `window.handleArkTSCall(${JSON.stringify(param)})`,
+      (error, result) => {
+        if (!error) {
+          console.info('调用成功,H5 返回值: ' + JSON.stringify(result));
+          // IBestToast.show('调用成功,H5 返回值: ' + JSON.stringify(result))
+        }
+      }
+    );
+  }
 }
 
 export const jDBViewModel = new JDBViewModel()

+ 2 - 1
entry/src/main/module.json5

@@ -55,7 +55,8 @@
       "https",
       "weixin",
       "wxopensdk",
-      "alipays"
+      "alipays",
+      "apmqpdispatch"
     ]
   }
 }

二進制
libs/afservicesdk-1.0.241118203225.har


+ 7 - 0
oh-package-lock.json5

@@ -6,10 +6,17 @@
   "lockfileVersion": 3,
   "ATTENTION": "THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.",
   "specifiers": {
+    "@alipay/afservicesdk@libs/afservicesdk-1.0.241118203225.har": "@alipay/afservicesdk@libs/afservicesdk-1.0.241118203225.har",
     "@ohos/hamock@1.0.0": "@ohos/hamock@1.0.0",
     "@ohos/hypium@1.0.24": "@ohos/hypium@1.0.24"
   },
   "packages": {
+    "@alipay/afservicesdk@libs/afservicesdk-1.0.241118203225.har": {
+      "name": "@alipay/afservicesdk",
+      "version": "1.0.241118203225",
+      "resolved": "libs/afservicesdk-1.0.241118203225.har",
+      "registryType": "local"
+    },
     "@ohos/hamock@1.0.0": {
       "name": "",
       "version": "1.0.0",

+ 1 - 0
oh-package.json5

@@ -2,6 +2,7 @@
   "modelVersion": "6.0.0",
   "description": "Please describe the basic information.",
   "dependencies": {
+    "@alipay/afservicesdk": "file:./libs/afservicesdk-1.0.241118203225.har"
   },
   "devDependencies": {
     "@ohos/hypium": "1.0.24",