11import { Any, MethodCall, MethodResult } from "@ohos/flutter_ohos"
22import { buffer } from "@kit.ArkTS"
3- import { fileUri } from "@kit.CoreFileKit"
3+ import { fileUri, fileIo as fs } from "@kit.CoreFileKit"
44import * as wxopensdk from '@tencent/wechat_open_sdk';
55import { WXAPiHandler } from "./WXAPiHandler"
66
@@ -32,8 +32,7 @@ export class FluwxShareHandler {
3232 this.shareWebPage(call, result);
3333 break;
3434 case "shareFile":
35- // TODO
36- result.notImplemented();
35+ this.shareFile(call, result);
3736 break;
3837 default:
3938 result.notImplemented();
@@ -108,6 +107,54 @@ export class FluwxShareHandler {
108107 result.success(done)
109108 }
110109
110+ async shareFile(call: MethodCall, result: MethodResult) {
111+ const source: Map<string, Any> = call.argument("source") ?? new Map();
112+ const schemaIndex: number = source.get("schema")
113+ let filePath: string = ""
114+
115+ // check schema is file or binary
116+ if (schemaIndex < 2) {
117+ result.error("ARG", "currently only file or binary schema is supported on ohos", null);
118+ return;
119+ }
120+
121+ // case schema is file
122+ if(schemaIndex == 2) {
123+ filePath = source.get("source")
124+ if (filePath.startsWith("file://")) {
125+ filePath = filePath;
126+ } else {
127+ filePath = fileUri.getUriFromPath(filePath);
128+ }
129+ } else {
130+ // case schema is binary, write to temp file first
131+ const bytes: Uint8Array = source.get("source");
132+ const suffix: string = source.get("suffix") ?? ".tmp";
133+ const fileName = `share_temp_${new Date().getTime()}${suffix}`;
134+ const tempFilePath = `${WXAPiHandler.uiContext?.tempDir}/${fileName}`;
135+ const tempFile = fs.openSync(tempFilePath, fs.OpenMode.READ_WRITE || fs.OpenMode.CREATE);
136+ await fs.write(tempFile.fd, bytes.buffer);
137+ await fs.close(tempFile.fd);
138+ filePath = fileUri.getUriFromPath(tempFilePath);
139+ }
140+
141+ const fileObject = new wxopensdk.WXFileObject()
142+ fileObject.fileUri = filePath
143+
144+ const mediaMessage = new wxopensdk.WXMediaMessage()
145+ mediaMessage.mediaObject = fileObject
146+ mediaMessage.title = call.argument("title") || ""
147+ mediaMessage.description = call.argument("description") || ""
148+
149+ const req = new wxopensdk.SendMessageToWXReq()
150+ this.setCommonArgs(call, req, mediaMessage)
151+ req.message = mediaMessage
152+
153+ const done = await WXAPiHandler.wxApi?.sendReq(WXAPiHandler.uiContext, req);
154+
155+ result.success(done)
156+ }
157+
111158 async shareMiniProgram(call: MethodCall, result: MethodResult) {
112159 const miniProgramObject = new wxopensdk.WXMiniProgramObject()
113160 miniProgramObject.userName = call.argument("userName")
@@ -118,7 +165,7 @@ export class FluwxShareHandler {
118165 let miniProgramTypeInt: number = call.argument("miniProgramType")
119166 if (miniProgramTypeInt === 1) {
120167 miniProgramType = wxopensdk.WXMiniProgramType.TEST
121- } else if (miniProgramType === 2) {
168+ } else if (miniProgramTypeInt === 2) {
122169 miniProgramType = wxopensdk.WXMiniProgramType.PREVIEW
123170 }
124171
0 commit comments