Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
C
clx-fluwx
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
openSourceLibrary
clx-fluwx
Commits
0ecc3810
Unverified
提交
0ecc3810
authored
10月 25, 2024
作者:
yeliulee
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add impl for payment & share for wechat on ohos
Signed-off-by:
yeliulee
<
yeliuleet@gmail.com
>
(cherry picked from commit 480e8f770167ca3905e9c0b7b09d94d25fb788c1)
上级
ee981f7d
显示空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
245 行增加
和
26 行删除
+245
-26
FluwxPlugin.ets
ohos/src/main/ets/components/plugin/FluwxPlugin.ets
+159
-26
FluwxShareHandler.ets
...main/ets/components/plugin/handlers/FluwxShareHandler.ets
+86
-0
没有找到文件。
ohos/src/main/ets/components/plugin/FluwxPlugin.ets
浏览文件 @
0ecc3810
...
...
@@ -9,10 +9,11 @@ import {
MethodResult,
NewWantListener,
} from '@ohos/flutter_ohos';
import * as wechat
Open
SDK from "@tencent/wechat_open_sdk"
import * as wechatSDK from "@tencent/wechat_open_sdk"
import { FluwxAuthHandler } from './handlers/FluwxAuthHandler';
import { AbilityConstant, common, Want } from '@kit.AbilityKit';
import { WXAPiHandler } from './handlers/WXAPiHandler';
import { FluwxShareHandler } from './handlers/FluwxShareHandler';
const MESSAGE_CHANNEL_NAME = "com.jarvanmo/fluwx"
const KEY_ERR_STR = "errStr"
...
...
@@ -21,11 +22,14 @@ const KEY_OPEN_ID = "openId"
const KEY_TYPE = "type"
/** FluwxPlugin **/
export default class FluwxPlugin implements FlutterPlugin, MethodCallHandler, AbilityAware, NewWantListener, wechat
Open
SDK.WXApiEventHandler {
export default class FluwxPlugin implements FlutterPlugin, MethodCallHandler, AbilityAware, NewWantListener, wechatSDK.WXApiEventHandler {
private channel: MethodChannel | null = null;
private appContext: common.Context | null = null;
private binding: AbilityPluginBinding | null = null;
private uiContext: common.UIAbilityContext | null = null;
private binding: AbilityPluginBinding | null = null
private authHandler: FluwxAuthHandler | null = null;
private shareHandler: FluwxShareHandler | null = null;
private extMsg: string | null = null;
getUniqueClassName(): string {
return "FluwxPlugin"
...
...
@@ -36,44 +40,39 @@ export default class FluwxPlugin implements FlutterPlugin, MethodCallHandler, Ab
this.channel.setMethodCallHandler(this)
this.appContext = binding.getApplicationContext();
this.authHandler = new FluwxAuthHandler(this.channel);
this.shareHandler = new FluwxShareHandler();
}
onDetachedFromEngine(binding: FlutterPluginBinding): void {
this.channel = null;
this.appContext = null;
this.authHandler = null;
this.shareHandler = null;
}
onAttachedToAbility(binding: AbilityPluginBinding): void {
WXAPiHandler.setContext(binding.getAbility().context);
this.binding = binding;
this.binding = binding
this.uiContext = binding.getAbility().context;
WXAPiHandler.setContext(this.uiContext);
binding.addOnNewWantListener(this)
}
onDetachedFromAbility(): void {
this.binding = null;
this.binding?.removeOnNewWantListener(this)
this.binding = null
this.uiContext = null;
}
// 微信回调 start
onReq = (req: wechatOpenSDK.BaseReq): void => {
}
onResp = (resp: wechatOpenSDK.BaseResp): void => {
if (resp instanceof wechatOpenSDK.SendAuthResp) {
this.handleAuthResponse(resp);
onMethodCall(call: MethodCall, result: MethodResult): void {
if (call.method.startsWith("share")) {
this.shareHandler?.share(call, result);
return;
}
}
// 微信回调 end
onNewWant(want: Want, launchParams: AbilityConstant.LaunchParam): void {
WXAPiHandler.wxApi?.handleWant(want, this)
}
onMethodCall(call: MethodCall, result: MethodResult): void {
switch (call.method) {
case "isWeChatInstalled":
WXAPiHandler.checkWeChatInstallation(result);
break;
case "registerApp":
WXAPiHandler.registerApp(call, result);
break;
...
...
@@ -86,15 +85,100 @@ export default class FluwxPlugin implements FlutterPlugin, MethodCallHandler, Ab
case "stopAuthByQRCode":
this.authHandler?.stopAuthByQRCode(result);
break;
case "isWeChatInstalled":
WXAPiHandler.checkWeChatInstallation(result);
case "payWithFluwx":
this.handlePay(call, result);
break;
case "payWithHongKongWallet":
// TODO
result.notImplemented();
break;
case "launchMiniProgram":
// TODO
result.notImplemented();
break;
case "subscribeMsg":
// TODO
result.notImplemented();
break;
case "autoDeduct":
// TODO
result.notImplemented();
break;
case "autoDeductV2":
// TODO
result.notImplemented();
break;
case "openWXApp":
result.success(WXAPiHandler.wxApi?.openWechat(this.uiContext));
break;
case "getExtMsg":
result.success(this.extMsg);
this.extMsg = null;
break;
case "openWeChatCustomerServiceChat":
// TODO
result.notImplemented();
break;
case "checkSupportOpenBusinessView":
// TODO
result.notImplemented();
break;
case "openBusinessView":
// TODO
result.notImplemented();
break;
case "openWeChatInvoice":
// TODO
result.notImplemented();
break;
case "openUrl":
// TODO
result.notImplemented();
break;
case "openRankList":
// TODO
result.notImplemented();
break;
case "attemptToResumeMsgFromWx":
this.attemptToResumeMsgFromWx(result);
break;
case "selfCheck":
result.success(null)
break;
default:
result.notImplemented()
result.notImplemented()
;
}
}
handleAuthResponse(resp: wechatOpenSDK.SendAuthResp) {
onNewWant(want: Want, launchParams: AbilityConstant.LaunchParam): void {
WXAPiHandler.wxApi?.handleWant(want, this);
}
// 微信回调 start
onReq = (req: wechatSDK.BaseReq): void => {
// TODO
}
onResp = (resp: wechatSDK.BaseResp): void => {
if (resp instanceof wechatSDK.SendAuthResp) {
this.onAuthResponse(resp);
return;
}
if (resp instanceof wechatSDK.SendMessageToWXResp) {
this.onSendMessageToWXResp(resp);
return;
}
if (resp instanceof wechatSDK.PayResp) {
this.onPayResp(resp);
return;
}
}
// 微信回调 end
onAuthResponse(resp: wechatSDK.SendAuthResp) {
const result: Map<string, ESObject> = new Map();
result.set(KEY_ERR_CODE, resp.errCode);
result.set(KEY_ERR_STR, resp.errStr);
...
...
@@ -108,4 +192,52 @@ export default class FluwxPlugin implements FlutterPlugin, MethodCallHandler, Ab
this.channel?.invokeMethod("onAuthResponse", result);
}
onSendMessageToWXResp(resp: wechatSDK.SendMessageToWXResp) {
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);
this.channel?.invokeMethod("onShareResponse", _result);
}
onPayResp(resp: wechatSDK.PayResp) {
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("prepayId", resp.prepayId);
_result.set("returnKey", resp.returnKey);
_result.set("extData", resp.extData);
this.channel?.invokeMethod("onPayResponse", _result);
}
async handlePay(call: MethodCall, result: MethodResult) {
if (!WXAPiHandler.wxApi) {
result.error("Unassigned WxApi", "please config wxapi first", null);
return;
}
const request = new wechatSDK.PayReq();
request.appId = call.argument("appId");
request.partnerId = call.argument("partnerId");
request.prepayId = call.argument("prepayId");
request.packageValue = call.argument("packageValue");
request.nonceStr = call.argument("nonceStr");
request.timeStamp = call.argument("timeStamp").toString();
request.sign = call.argument("sign");
request.signType = call.argument("signType");
request.extData = call.argument("extData");
const done = await WXAPiHandler.wxApi?.sendReq(this.uiContext, request);
result.success(done);
}
attemptToResumeMsgFromWx(result: MethodResult) {
WXAPiHandler.wxApi?.handleWant(this.binding?.getAbility().launchWant, this)
}
}
\ No newline at end of file
ohos/src/main/ets/components/plugin/handlers/FluwxShareHandler.ets
0 → 100644
浏览文件 @
0ecc3810
import { Any, MethodCall, MethodResult } from "@ohos/flutter_ohos"
import * as wechatSDK from "@tencent/wechat_open_sdk"
import { WXAPiHandler } from "./WXAPiHandler"
import { buffer } from "@kit.ArkTS"
import { fileUri } from "@kit.CoreFileKit"
export class FluwxShareHandler {
share(call: MethodCall, result: MethodResult) {
if (!WXAPiHandler.wxApi) {
result.error("Unassigned WxApi", "please config wxapi first", null);
return;
}
switch (call.method) {
case "shareText":
this.shareText(call, result);
break;
case "shareMiniProgram":
// TODO
result.notImplemented();
break;
case "shareImage":
this.shareImage(call, result);
break;
case "shareMusic":
// TODO
result.notImplemented();
break;
case "shareVideo":
// TODO
result.notImplemented();
break;
case "shareWebPage":
// TODO
result.notImplemented();
break;
case "shareFile":
// TODO
result.notImplemented();
break;
default:
result.notImplemented();
break;
}
}
async shareText(call: MethodCall, result: MethodResult) {
const textObj = new wechatSDK.WXTextObject();
textObj.text = call.argument("source");
const mediaMsg = new wechatSDK.WXMediaMessage();
mediaMsg.mediaObject = textObj;
const req = new wechatSDK.SendMessageToWXReq();
req.message = mediaMsg;
result.success(WXAPiHandler.wxApi?.sendReq(WXAPiHandler.uiContext, req))
}
async shareImage(call: MethodCall, result: MethodResult) {
const map: Map<string, Any> = call.argument("source") ?? new Map();
// const imageHash: string | null = call.argument("imgDataHash");
const bytes: Uint8Array | null = map.get("uint8List");
const imageObj = new wechatSDK.WXImageObject();
if (bytes) {
const buff: buffer.Buffer = buffer.from(bytes.buffer);
imageObj.imageData = buff.toString("base64", 0, buff.length);
} else {
const localImagePath: string | null = map.get("localImagePath");
if (localImagePath) {
imageObj.uri = localImagePath.startsWith("file://") ? localImagePath : fileUri.getUriFromPath(localImagePath)
}
}
const mediaMsg = new wechatSDK.WXMediaMessage();
mediaMsg.mediaObject = imageObj;
const req = new wechatSDK.SendMessageToWXReq();
req.message = mediaMsg;
result.success(WXAPiHandler.wxApi?.sendReq(WXAPiHandler.uiContext, req));
}
}
\ No newline at end of file
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论