收藏
回答

微信支付与微信小程序支付如何一起接入鸿蒙app,并且为什么鸿蒙app微信小程序支付提示第三方应用信息

框架类型 问题类型 终端类型 AppID 环境ID 基础库版本
小程序 Bug 工具
import * as wxopensdk from '@tencent/wechat_open_sdk';

// WXApi 是第三方app和微信通信的openApi接口,其实例通过WXAPIFactory获取,需要传入应用申请到的AppID
export const WXApi = wxopensdk.WXAPIFactory.createWXAPI(APP_ID)

// WXApiEventHandler为微信数据的回调
class WXApiEventHandlerImpl implements wxopensdk.WXApiEventHandler {
  private onReqCallbacks: Map<OnWXReq, OnWXReq> = new Map
  private onRespCallbacks: Map<OnWXResp, OnWXResp> = new Map

  registerOnWXReqCallback(on: OnWXReq) {
    this.onReqCallbacks.set(on, on)
  }
  unregisterOnWXReqCallback(on: OnWXReq) {
    this.onReqCallbacks.delete(on)
  }

  registerOnWXRespCallback(on: OnWXResp) {
    this.onRespCallbacks.set(on, on)
  }
  unregisterOnWXRespCallback(on: OnWXResp) {
    this.onRespCallbacks.delete(on)
  }

  onReq(req: wxopensdk.BaseReq): void {
    Log.i(kTag, "onReq:%s", JSON.stringify(req))
    this.onReqCallbacks.forEach((on) => {
      on(req)
    })
  }

  onResp(resp: wxopensdk.BaseResp): void {
    Log.i(kTag, "onResp:%s", JSON.stringify(resp))
    this.onRespCallbacks.forEach((on) => {
      on(resp)
    })
  }
}
export const WXEventHandler = new WXApiEventHandlerImpl


let req = new wxopensdk.SendAuthReq
req.isOption1 = false
req.nonAutomatic = true
req.scope = 'snsapi_userinfo'
req.state = 'none'
req.transaction = 'test123'

// 向微信发送登录请求:
//   context为ohos内置类,app间跳转需依赖该类,开发者可在Component中获取
//   finished为跳转微信的结果:true表示跳转成功;false表示跳转失败,可能是因为微信未安装
let finished = await this.wxApi.sendReq(context: common.UIAbilityContext, req)


// 在EntryAbility中响应来自微信的回调
export default class EntryAbility extends UIAbility {
  onCreate(want: Want, _launchParam: AbilityConstant.LaunchParam): void {
    this.handleWeChatCallIfNeed(want)
  }
  
  onNewWant(want: Want, _launchParam: AbilityConstant.LaunchParam): void {
    this.handleWeChatCallIfNeed(want)
  }

  private handleWeChatCallIfNeed(want: Want) {
    WXApi.handleWant(want, WXEventHandler)
  }
}

上述代码是鸿蒙接入微信支付指南,但是在鸿蒙app中要实现微信支付与微信小程序支付两种支付方式,微信支付需要的的APP_ID为"wx12345678",微信小程序的APP_ID为"wx8888298",在EntryAbility.ets该文件中初始化微信支付时,WXApi引用了WXApiWrap.ets中的WXApi,代码为export const WXApi = wxopensdk.WXAPIFactory.createWXAPI(APP_ID),请问初始化微信支付需要区分微信支付的APP_ID和微信小程序的APP_ID吗,如果想要实现微信支付与微信小程序支付两种支付方式,如何初始化呢

import * as wxopensdk from '@tencent/wechat_open_sdk'; // WXApi 是第三方app和微信通信的openApi接口,其实例通过WXAPIFactory获取,需要传入应用申请到的AppID export const WXApi = wxopensdk.WXAPIFactory.createWXAPI(APP_ID) // WXApiEventHandler为微信数据的回调 class WXApiEventHandlerImpl implements wxopensdk.WXApiEventHandler { private onReqCallbacks: Map<OnWXReq, OnWXReq> = new Map private onRespCallbacks: Map<OnWXResp, OnWXResp> = new Map registerOnWXReqCallback(on: OnWXReq) { this.onReqCallbacks.set(on, on) } unregisterOnWXReqCallback(on: OnWXReq) { this.onReqCallbacks.delete(on) } registerOnWXRespCallback(on: OnWXResp) { this.onRespCallbacks.set(on, on) } unregisterOnWXRespCallback(on: OnWXResp) { this.onRespCallbacks.delete(on) } onReq(req: wxopensdk.BaseReq): void { Log.i(kTag, "onReq:%s", JSON.stringify(req)) this.onReqCallbacks.forEach((on) => { on(req) }) } onResp(resp: wxopensdk.BaseResp): void { Log.i(kTag, "onResp:%s", JSON.stringify(resp)) this.onRespCallbacks.forEach((on) => { on(resp) }) } } export const WXEventHandler = new WXApiEventHandlerImpl let req = new wxopensdk.SendAuthReq req.isOption1 = false req.nonAutomatic = true req.scope = 'snsapi_userinfo' req.state = 'none' req.transaction = 'test123' // 向微信发送登录请求: // context为ohos内置类,app间跳转需依赖该类,开发者可在Component中获取 // finished为跳转微信的结果:true表示跳转成功;false表示跳转失败,可能是因为微信未安装 let finished = await this.wxApi.sendReq(context: common.UIAbilityContext, req) // 在EntryAbility中响应来自微信的回调 export default class EntryAbility extends UIAbility { onCreate(want: Want, _launchParam: AbilityConstant.LaunchParam): void { this.handleWeChatCallIfNeed(want) } onNewWant(want: Want, _launchParam: AbilityConstant.LaunchParam): void { this.handleWeChatCallIfNeed(want) } private handleWeChatCallIfNeed(want: Want) { WXApi.handleWant(want, WXEventHandler) } }上述代码是鸿蒙接入微信支付指南,但是在鸿蒙app中要实现微信支付与微信小程序支付两种支付方式,微信支付需要的的APP_ID为"wx12345678",微信小程序的APP_ID为"wx8888298",在EntryAbility.ets该文件中初始化微信支付时,WXApi引用了WXApiWrap.ets中的WXApi,代码为export const WXApi = wxopensdk.WXAPIFactory.createWXAPI(APP_ID),请问初始化微信支付需要区分微信支付的APP_ID和微信小程序的APP_ID吗,如果想要实现微信支付与微信小程序支付两种支付方式,如何初始化呢
最后一次编辑于  03-07
回答关注问题邀请回答
收藏
登录 后发表内容