Skip to content

Commit

Permalink
migrate to "@tencent/wechat_open_sdk" v1.0.3 and add impl for plugin.…
Browse files Browse the repository at this point in the history
…launchMiniProgram (#671)
  • Loading branch information
yeliulee authored Nov 11, 2024
1 parent 157e996 commit d7f847f
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 11 deletions.
2 changes: 1 addition & 1 deletion ohos/oh-package.json5
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
"license": "Apache-2.0",
"dependencies": {
"@ohos/flutter_ohos": "file:./har/flutter.har",
"@tencent/wechat_open_sdk": "^1.0.0"
"@tencent/wechat_open_sdk": "1.0.3"
}
}
34 changes: 32 additions & 2 deletions ohos/src/main/ets/components/plugin/FluwxPlugin.ets
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,7 @@ export default class FluwxPlugin implements FlutterPlugin, MethodCallHandler, Ab
result.notImplemented();
break;
case "launchMiniProgram":
// TODO
result.notImplemented();
this.launchMiniProgram(call, result);
break;
case "subscribeMsg":
// TODO
Expand Down Expand Up @@ -174,6 +173,11 @@ export default class FluwxPlugin implements FlutterPlugin, MethodCallHandler, Ab
this.onPayResp(resp);
return;
}

if (resp instanceof wechatSDK.LaunchMiniProgramResp) {
this.onLaunchMiniProgramResp(resp);
return;
}
}

// 微信回调 end
Expand Down Expand Up @@ -215,6 +219,20 @@ export default class FluwxPlugin implements FlutterPlugin, MethodCallHandler, Ab
this.channel?.invokeMethod("onPayResponse", _result);
}

onLaunchMiniProgramResp(resp: wechatSDK.LaunchMiniProgramResp) {
const _result: Map<string, ESObject> = new Map();
_result.set(KEY_ERR_CODE, resp.errCode);
_result.set(KEY_ERR_STR, resp.errStr);
_result.set(KEY_TYPE, resp.type);
_result.set(KEY_OPEN_ID, resp.openId);

if (resp.extMsg) {
_result.set("extMsg", resp.extMsg);
}

this.channel?.invokeMethod("onLaunchMiniProgramResponse", _result);
}

async handlePay(call: MethodCall, result: MethodResult) {
if (!WXAPiHandler.wxApi) {
result.error("Unassigned WxApi", "please config wxapi first", null);
Expand All @@ -240,4 +258,16 @@ export default class FluwxPlugin implements FlutterPlugin, MethodCallHandler, Ab
attemptToResumeMsgFromWx(result: MethodResult) {
WXAPiHandler.wxApi?.handleWant(this.binding?.getAbility().launchWant, this)
}

async launchMiniProgram(call: MethodCall, result: MethodResult) {
const request = new wechatSDK.LaunchMiniProgramReq();
request.userName = call.argument("userName");
request.path = call.argument("path");
// sdk 内当前没 type 常量, 直接使用传入的参数
request.miniprogramType = call.argument("miniprogramType") ?? 0;

const done = await WXAPiHandler.wxApi?.sendReq(this.uiContext, request);

result.success(done);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export class FluwxAuthHandler implements OAuthCallback {
return this.diffDevOauth;
}

sendAuth(call: MethodCall, result: MethodResult) {
async sendAuth(call: MethodCall, result: MethodResult) {
const req = new SendAuthReq();
req.isOption1 = false;
req.scope = call.argument("scope");
Expand All @@ -57,10 +57,10 @@ export class FluwxAuthHandler implements OAuthCallback {
req.openId = openid;
}
req.nonAutomatic = call.argument("nonAutomatic") ?? false;
result.success(WXAPiHandler.wxApi?.sendReq(
WXAPiHandler.uiContext,
req
))

const done = await WXAPiHandler.wxApi?.sendReq(WXAPiHandler.uiContext, req);

result.success(done);
}

authByQRCode(call: MethodCall, result: MethodResult) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ export class FluwxShareHandler {
const req = new wechatSDK.SendMessageToWXReq();
req.message = mediaMsg;

result.success(WXAPiHandler.wxApi?.sendReq(WXAPiHandler.uiContext, req))
const done = await WXAPiHandler.wxApi?.sendReq(WXAPiHandler.uiContext, req);

result.success(done)
}

async shareImage(call: MethodCall, result: MethodResult) {
Expand All @@ -80,6 +82,7 @@ export class FluwxShareHandler {
const req = new wechatSDK.SendMessageToWXReq();
req.message = mediaMsg;

result.success(WXAPiHandler.wxApi?.sendReq(WXAPiHandler.uiContext, req));
const done = await WXAPiHandler.wxApi?.sendReq(WXAPiHandler.uiContext, req);
result.success(done);
}
}
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 { bundle, bundleManager, common } from '@kit.AbilityKit'
import { bundleManager, common } from '@kit.AbilityKit'

export class WXAPiHandler {
static wxApi: wechatOpenSDK.WXApi | null = null
Expand Down

0 comments on commit d7f847f

Please sign in to comment.