Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export default class FluwxPlugin implements FlutterPlugin, MethodCallHandler, Ab
WXAPiHandler.checkWeChatInstallation(result);
break;
case "registerApp":
WXAPiHandler.registerApp(call, result);
WXAPiHandler.registerApp(call, result, this);
break;
case "sendAuth":
this.authHandler?.sendAuth(call, result);
Expand Down Expand Up @@ -262,6 +262,7 @@ export default class FluwxPlugin implements FlutterPlugin, MethodCallHandler, Ab

attemptToResumeMsgFromWx(result: MethodResult) {
WXAPiHandler.wxApi?.handleWant(this.binding?.getAbility().launchWant, this)
result.success(null)
}

async launchMiniProgram(call: MethodCall, result: MethodResult) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as wechatOpenSDK from "@tencent/wechat_open_sdk"
import { MethodCall, MethodResult } from '@ohos/flutter_ohos'
import { bundleManager, common } from '@kit.AbilityKit'
import { bundleManager, common, Want } from '@kit.AbilityKit'

export class WXAPiHandler {
static wxApi: wechatOpenSDK.WXApi | null = null
Expand All @@ -20,22 +20,33 @@ export class WXAPiHandler {
WXAPiHandler.context = context
}

static registerApp(call: MethodCall, result: MethodResult) {
if (WXAPiHandler.wxApi != null) {
result.success(true)
return
static registerApp(call: MethodCall, result: MethodResult, eventHandler: wechatOpenSDK.WXApiEventHandler) {
if (WXAPiHandler.wxApi == null) {
const appId: string | null = call.argument("appId")
if (!appId) {
result.error("invalid app id", "are you sure your app id is correct ?", appId)
return
}
WXAPiHandler.registerWxAPIInternal(appId)
}
const appId: string | null = call.argument("appId")
if (!appId) {
result.error("invalid app id", "are you sure your app id is correct ?", appId)
return
}

WXAPiHandler.registerWxAPIInternal(appId)

WXAPiHandler.bindEventHandler(eventHandler)
result.success(WXAPiHandler.registered)
}

// The SDK dispatches every response to the handler it received in the most recent handleWant() call,
// and handleWant() is the only way to hand it one. Responses coming back through the openLink channel
// (current WeChat versions) never go through onNewWant, so until something calls handleWant() the
// handler is null and the SDK drops them silently (`this.eventHandler?.onResp(...)`). That is what
// made the first auth / pay after every cold start time out (#773).
//
// An empty Want carries no WeChat payload, so this only binds the handler: nothing is dispatched and the
// launch Want is left for attemptToResumeMsgFromWx(), which apps call once their subscribers are ready.
private static bindEventHandler(eventHandler: wechatOpenSDK.WXApiEventHandler) {
const noPayload: Want = {}
WXAPiHandler.wxApi?.handleWant(noPayload, eventHandler)
}

static checkWeChatInstallation(result: MethodResult) {
const isInstalled = bundleManager.canOpenLink("weixin://") || WXAPiHandler.wxApi?.isWXAppInstalled() === true;
result.success(isInstalled);
Expand Down