@cordova-ohos/cordova-plugin-wechat 3.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,42 @@
1
+ /*
2
+ * Copyright (c) 2025 Huawei Device, Inc. Ltd. and <马弓手>.
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+
17
+
18
+ #ifndef MYAPPLICATION_WECHAT_H
19
+ #define MYAPPLICATION_WECHAT_H
20
+
21
+ #include "CordovaPlugin.h"
22
+
23
+ class Wechat : public CordovaPlugin {
24
+ static const int LOG_PRINT_DOMAIN = 0xff00;
25
+ CallbackContext m_cbc;
26
+ bool download(const std::string& strSource, const std::string& strTarget, char* pBuf, const int nLength);
27
+ bool downloadFile(const std::string& strSource, std::string& strFilePath);
28
+ bool getUriFromRawfile(const std::string& strSource, std::string& strFilePath);
29
+ bool getFilePath(const std::string& strSource, std::string& strFileUri);
30
+ bool getBase64File(const std::string& strSource, std::string& strFilePath);
31
+ bool getUri(const std::string& strSource, std::string& strFileUri);
32
+
33
+ public:
34
+ Wechat() {}
35
+
36
+ ~Wechat() {}
37
+
38
+ bool execute(const std::string& action, cJSON* args, CallbackContext cbc) override;
39
+ bool onArKTsResult(cJSON* args);
40
+ };
41
+
42
+ #endif // MYAPPLICATION_WECHAT_H
@@ -0,0 +1,621 @@
1
+ /*
2
+ * Copyright (c) 2025 Huawei Device, Inc. Ltd. and <马弓手>.
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+
17
+ import { ArkTsAttribute, cordovaWebTagToObjectGlobe, NativeAttribute } from "../PluginGlobal"
18
+ import { common, UIAbility, Want } from "@kit.AbilityKit";
19
+ import cordova from 'libcordova.so';
20
+ import { image } from "@kit.ImageKit";
21
+ import { MainPage } from "../MainPage";
22
+ import { NormalizeError } from "../NormalizeError";
23
+
24
+ let ERROR_WECHAT_NOT_INSTALLED:string = "未安装微信";
25
+ let ERROR_INVALID_PARAMETERS:string = "参数格式错误";
26
+ let ERROR_SEND_REQUEST_FAILED:string = "发送请求失败";
27
+ let ERROR_WECHAT_RESPONSE_COMMON:string = "普通错误";
28
+ let ERROR_WECHAT_RESPONSE_USER_CANCEL:string = "用户点击取消并返回";
29
+ let ERROR_WECHAT_RESPONSE_SENT_FAILED:string = "发送失败";
30
+ let ERROR_WECHAT_RESPONSE_AUTH_DENIED:string = "授权失败";
31
+ let ERROR_WECHAT_RESPONSE_UNSUPPORT:string = "微信不支持";
32
+ let ERROR_WECHAT_RESPONSE_UNKNOWN:string = "未知错误";
33
+
34
+
35
+ let KEY_ARG_MESSAGE:string = "message";
36
+ let KEY_ARG_SCENE:string = "scene";
37
+ let KEY_ARG_TEXT:string = "text";
38
+ let KEY_ARG_MESSAGE_TITLE:string = "title";
39
+ let KEY_ARG_MESSAGE_DESCRIPTION:string = "description";
40
+ let KEY_ARG_MESSAGE_THUMB:string = "thumb";
41
+ let KEY_ARG_MESSAGE_MEDIA:string = "media";
42
+ let KEY_ARG_MESSAGE_MEDIA_TYPE:string = "type";
43
+ let KEY_ARG_MESSAGE_MEDIA_WEBPAGEURL:string = "webpageUrl";
44
+ let KEY_ARG_MESSAGE_MEDIA_IMAGE:string = "image";
45
+ let KEY_ARG_MESSAGE_MEDIA_TEXT:string = "text";
46
+ let KEY_ARG_MESSAGE_MEDIA_MUSICURL:string = "musicUrl";
47
+ let KEY_ARG_MESSAGE_MEDIA_MUSICDATAURL:string = "musicDataUrl";
48
+ let KEY_ARG_MESSAGE_MEDIA_VIDEOURL:string = "videoUrl";
49
+ let KEY_ARG_MESSAGE_MEDIA_FILE:string = "file";
50
+ let KEY_ARG_MESSAGE_MEDIA_EMOTION:string = "emotion";
51
+ let KEY_ARG_MESSAGE_MEDIA_EXTINFO:string = "extInfo";
52
+ let KEY_ARG_MESSAGE_MEDIA_URL:string = "url";
53
+ let KEY_ARG_MESSAGE_MEDIA_USERNAME:string = "userName";
54
+ let KEY_ARG_MESSAGE_MEDIA_MINIPROGRAMTYPE:string = "miniprogramType";
55
+ let KEY_ARG_MESSAGE_MEDIA_MINIPROGRAM:string = "miniProgram";
56
+ let KEY_ARG_MESSAGE_MEDIA_PATH:string = "path";
57
+ let KEY_ARG_MESSAGE_MEDIA_WITHSHARETICKET:string = "withShareTicket";
58
+ let KEY_ARG_MESSAGE_MEDIA_HDIMAGEDATA:string = "hdImageData";
59
+
60
+ let TYPE_WECHAT_SHARING_APP:number = 1;
61
+ let TYPE_WECHAT_SHARING_EMOTION:number = 2;
62
+ let TYPE_WECHAT_SHARING_FILE:number = 3;
63
+ let TYPE_WECHAT_SHARING_IMAGE:number = 4;
64
+ let TYPE_WECHAT_SHARING_MUSIC:number = 5;
65
+ let TYPE_WECHAT_SHARING_VIDEO:number = 6;
66
+ let TYPE_WECHAT_SHARING_WEBPAGE:number = 7;
67
+ let TYPE_WECHAT_SHARING_MINI:number = 8;
68
+
69
+ let SCENE_SESSION:number = 0;//分享到对话 微信鸿蒙SDK仅支持分享到对话
70
+ let SCENE_TIMELINE:number = 1;//分享到朋友圈
71
+ let SCENE_FAVORITE:number = 2;//分享到收藏
72
+
73
+ let WXApi:ESObject = null;
74
+ let WXEventHandler:ESObject = null;
75
+ export function EntryAbility(ability:UIAbility, want: Want, appId:string) {
76
+ const module:string = "@tencent/wechat_open_sdk";
77
+ import(module).then(async (wxopensdk:ESObject) => {
78
+ type OnWXReq = (req: ESObject) => void;
79
+ type OnWXResp = (resp: ESObject) => void;
80
+ class WXApiEventHandlerImpl {
81
+ private onReqCallbacks: Map<OnWXReq, OnWXReq> = new Map
82
+ private onRespCallbacks: Map<OnWXResp, OnWXResp> = new Map
83
+
84
+ registerOnWXReqCallback(on: OnWXReq) {
85
+ this.onReqCallbacks.set(on, on)
86
+ }
87
+ unregisterOnWXReqCallback(on: OnWXReq) {
88
+ this.onReqCallbacks.delete(on)
89
+ }
90
+
91
+ registerOnWXRespCallback(on: OnWXResp) {
92
+ this.onRespCallbacks.set(on, on)
93
+ }
94
+ unregisterOnWXRespCallback(on: OnWXResp) {
95
+ this.onRespCallbacks.delete(on)
96
+ }
97
+
98
+ onReq(req: ESObject): void {
99
+ console.log("req:"+JSON.stringify(req));
100
+ this.onReqCallbacks.forEach((on) => {
101
+ on(req)
102
+ })
103
+ }
104
+
105
+ onResp(resp: ESObject): void {
106
+ console.log("resp:"+JSON.stringify(resp));
107
+ this.onRespCallbacks.forEach((on) => {
108
+ on(resp)
109
+ })
110
+ }
111
+ }
112
+
113
+ if(WXApi == null) {
114
+ WXApi = wxopensdk.WXAPIFactory.createWXAPI(appId);
115
+ WXEventHandler = new WXApiEventHandlerImpl;
116
+ WXApi.handleWant(want, WXEventHandler);
117
+ }
118
+ });
119
+ }
120
+
121
+
122
+ async function wechatMinProgram(pageIndex:NativeAttribute){
123
+ interface minProgramAttribute {
124
+ APP_ID:string;
125
+ appId: string;
126
+ path: string;
127
+ type:0;
128
+ }
129
+
130
+ let paras:object = new Object();
131
+ try {
132
+ paras = JSON.parse(pageIndex.pageArgs);
133
+ } catch (error) {
134
+ let result: ArkTsAttribute = { content: "share", result: [ERROR_INVALID_PARAMETERS] };
135
+ cordova.onArkTsResult(JSON.stringify(result), pageIndex.pageObject, pageIndex.pageWebTag);
136
+ return;
137
+ }
138
+
139
+ let wechatInfo: minProgramAttribute = {APP_ID:"",appId:"",path:"",type:0};
140
+ if(paras?.["APP_ID"] !== undefined) {
141
+ wechatInfo.APP_ID = paras?.["APP_ID"];
142
+ }
143
+ if(paras?.["userName"] !== undefined) {
144
+ wechatInfo.appId = paras?.["userName"];
145
+ }
146
+ if(paras?.["appId"] !== undefined) {
147
+ wechatInfo.appId = paras?.["appId"];
148
+ }
149
+ if(paras?.["path"] !== undefined) {
150
+ wechatInfo.path = paras?.["path"];
151
+ }
152
+ if(paras?.["miniprogramType"] !== undefined) {
153
+ wechatInfo.type = paras?.["miniprogramType"];
154
+ }
155
+ const module:string = "@tencent/wechat_open_sdk";
156
+ import(module).then(async (wxopensdk:ESObject) => {
157
+ try {
158
+ if(!cordovaWebTagToObjectGlobe.hasKey(pageIndex.pageWebTag)) {
159
+ return;
160
+ }
161
+ let mainPage = cordovaWebTagToObjectGlobe.get(pageIndex.pageWebTag) as MainPage;
162
+ let context = mainPage.getUIContext().getHostContext() as common.UIAbilityContext;
163
+ let launchMiniProgramReq:ESObject = new wxopensdk.LaunchMiniProgramReq;
164
+ launchMiniProgramReq.userName = wechatInfo.appId; //拉起的小程序的原始id
165
+ launchMiniProgramReq.path = wechatInfo.path; //拉起小程序页面的可带参路径,不填默认拉起小程序首页,对于小游戏,可以只传入 query 部分,来实现传参效果,如:传入 "?foo=bar"。
166
+ launchMiniProgramReq.miniprogramType = wechatInfo.type; //拉起小程序的类型 0-正式版 1-开发版 2-体验版
167
+ let success:ESObject = await WXApi.sendReq(context, launchMiniProgramReq);
168
+ let ret: string = success ? "success" : "failed";
169
+ let result: ArkTsAttribute = { content: "WechatMinProgram", result: [ret] };
170
+ cordova.onArkTsResult(JSON.stringify(result), pageIndex.pageObject, pageIndex.pageWebTag);
171
+ } catch (error) {
172
+ const cordovaError = NormalizeError(error);
173
+ console.error(`Failed to wechatMinProgram. Cause code: ${cordovaError.code}, message: ${cordovaError.message}`);
174
+ }
175
+ });
176
+ }
177
+
178
+
179
+ function wechatShare(pageIndex:NativeAttribute) {
180
+ let paras:object = new Object();
181
+ try {
182
+ paras = JSON.parse(pageIndex.pageArgs);
183
+ } catch (error) {
184
+ let result: ArkTsAttribute = { content: "share", result: [ERROR_INVALID_PARAMETERS] };
185
+ cordova.onArkTsResult(JSON.stringify(result), pageIndex.pageObject, pageIndex.pageWebTag);
186
+ return;
187
+ }
188
+
189
+ let appId:string = "";
190
+ if(paras?.["APP_ID"] !== undefined) {
191
+ appId = paras?.["APP_ID"];
192
+ }
193
+
194
+ if(appId == "") {
195
+ let result: ArkTsAttribute = { content: "share", result: [ERROR_INVALID_PARAMETERS] };
196
+ cordova.onArkTsResult(JSON.stringify(result), pageIndex.pageObject, pageIndex.pageWebTag);
197
+ return;
198
+ }
199
+
200
+ const module:string = "@tencent/wechat_open_sdk";
201
+ import(module).then(async (wxopensdk:ESObject) => {
202
+ let mainPage:MainPage;
203
+ try {
204
+ if(!cordovaWebTagToObjectGlobe.hasKey(pageIndex.pageWebTag)) {
205
+ return;
206
+ }
207
+ mainPage = cordovaWebTagToObjectGlobe.get(pageIndex.pageWebTag) as MainPage;
208
+ } catch (error) {
209
+ const cordovaError = NormalizeError(error);
210
+ console.error(`Failed to wechatShare. Cause code: ${cordovaError.code}, message: ${cordovaError.message}`);
211
+ return;
212
+ }
213
+ let context = mainPage!.getUIContext().getHostContext() as common.UIAbilityContext;
214
+ // 注册授权结果回调
215
+ const authCallback: ESObject = (resp: ESObject) => {
216
+ if (resp.name && resp.name == "SendMessageToWXResp") {
217
+ if (resp.errCode === wxopensdk.ErrCode.ERR_OK) {
218
+ // 分享成功 只有用户返回应用后才回调,无需处理
219
+ } else if (resp.errCode === wxopensdk.ErrCode.ERR_USER_CANCEL) {
220
+ // 用户取消分享,只有用户返回应用后才回调,不返回无法通知js侧
221
+ let result: ArkTsAttribute = { content: "share", result: ["false", JSON.stringify(resp)] };
222
+ cordova.onArkTsResult(JSON.stringify(result), pageIndex.pageObject, pageIndex.pageWebTag);
223
+ } else {
224
+ // 分享失败,只有用户返回应用后才回调,不返回无法通知js侧
225
+ let result: ArkTsAttribute = { content: "share", result: ["false", JSON.stringify(resp)] };
226
+ cordova.onArkTsResult(JSON.stringify(result), pageIndex.pageObject, pageIndex.pageWebTag);
227
+ }
228
+ // 处理完后取消注册回调(避免重复调用)
229
+ WXEventHandler.unregisterOnWXRespCallback(authCallback);
230
+ }
231
+ };
232
+
233
+ //TODO:微信SDK仅支持分享到对话,安卓支持分享到对话、分享到朋友圈、分享到收藏
234
+ if(paras?.[KEY_ARG_SCENE] !== undefined) {
235
+ let sendMessageToWXReq: ESObject = new wxopensdk.SendMessageToWXReq;
236
+ sendMessageToWXReq.scene = wxopensdk.SendMessageToWXReq.WXSceneSession;
237
+
238
+ let shareRet:string = "failed;"
239
+ if (paras?.[KEY_ARG_TEXT] !== undefined) {
240
+ WXEventHandler.registerOnWXRespCallback(authCallback);
241
+ let textObject: ESObject = new wxopensdk.WXTextObject
242
+ textObject.text = paras?.[KEY_ARG_TEXT];
243
+
244
+ let mediaMessage: ESObject = new wxopensdk.WXMediaMessage();
245
+ mediaMessage.mediaObject = textObject
246
+
247
+ let req: ESObject = new wxopensdk.SendMessageToWXReq()
248
+ req.scene = wxopensdk.SendMessageToWXReq.WXSceneSession
249
+ req.message = mediaMessage
250
+
251
+ let success: boolean = await WXApi.sendReq(context, req);
252
+ shareRet = success ? "success" : "failed";
253
+ }
254
+
255
+ if (paras?.[KEY_ARG_MESSAGE] != undefined) {
256
+ let message: object = new Object();
257
+ if (paras?.[KEY_ARG_MESSAGE] !== undefined) {
258
+ message = paras?.[KEY_ARG_MESSAGE];
259
+ }
260
+
261
+ let media: object = new Object();
262
+ if (message?.[KEY_ARG_MESSAGE_MEDIA] !== undefined) {
263
+ media = message?.[KEY_ARG_MESSAGE_MEDIA];
264
+ }
265
+
266
+ let type: number = TYPE_WECHAT_SHARING_WEBPAGE;
267
+ if (media?.[KEY_ARG_MESSAGE_MEDIA_TYPE] !== undefined) {
268
+ type = media?.[KEY_ARG_MESSAGE_MEDIA_TYPE];
269
+ }
270
+
271
+ if (type == TYPE_WECHAT_SHARING_APP) { //TODO:分享APP,微信鸿蒙SDK不支持
272
+ return;
273
+ }
274
+
275
+ if (type == TYPE_WECHAT_SHARING_EMOTION) {//TODO:分享表情,微信鸿蒙SDK不支持
276
+
277
+ }
278
+
279
+ if (type == TYPE_WECHAT_SHARING_FILE) { //TODO:分享文件,这里不注册回调,因为注册回调微信也不会回调
280
+ if(media?.[KEY_ARG_MESSAGE_MEDIA_FILE] !== undefined) {
281
+ let uri:string = media?.[KEY_ARG_MESSAGE_MEDIA_FILE];
282
+ let fileObject: ESObject = new wxopensdk.WXFileObject
283
+ fileObject.fileUri = uri;
284
+
285
+ let mediaMessage: ESObject = new wxopensdk.WXMediaMessage();
286
+ mediaMessage.mediaObject = fileObject
287
+
288
+ sendMessageToWXReq.message = mediaMessage
289
+ let success: boolean = await WXApi.sendReq(context, sendMessageToWXReq);
290
+ shareRet = success ? "success" : "failed";
291
+ }
292
+ }
293
+
294
+ if (type == TYPE_WECHAT_SHARING_IMAGE) {//TODO:分享图片,这里不注册回调,因为注册回调微信也不会回调
295
+ if (media?.[KEY_ARG_MESSAGE_MEDIA_IMAGE] !== undefined) {
296
+ let uri: string = media?.[KEY_ARG_MESSAGE_MEDIA_IMAGE];
297
+ let imageObject: ESObject = new wxopensdk.WXImageObject
298
+ imageObject.uri = uri;
299
+
300
+ let mediaMessage: ESObject = new wxopensdk.WXMediaMessage();
301
+ mediaMessage.mediaObject = imageObject
302
+
303
+ sendMessageToWXReq.message = mediaMessage
304
+ let success: boolean = await WXApi.sendReq(context, sendMessageToWXReq);
305
+ shareRet = success ? "success" : "failed";
306
+ }
307
+ }
308
+
309
+ if (type == TYPE_WECHAT_SHARING_MUSIC) {//TODO:分享音乐微信鸿蒙SDK不支持
310
+
311
+ }
312
+
313
+ if (type == TYPE_WECHAT_SHARING_VIDEO) {//TODO:分享视频微信鸿蒙SDK不支持
314
+ if(media?.[KEY_ARG_MESSAGE_MEDIA_VIDEOURL] !== undefined) {
315
+ let uri:string = media?.[KEY_ARG_MESSAGE_MEDIA_VIDEOURL];
316
+ let videoObject: ESObject = new wxopensdk.WXVideoObject
317
+ videoObject.videoUrl = uri;
318
+ let mediaMessage: ESObject = new wxopensdk.WXMediaMessage();
319
+ mediaMessage.mediaObject = videoObject
320
+
321
+ sendMessageToWXReq.message = mediaMessage
322
+ let success: boolean = await WXApi.sendReq(context, sendMessageToWXReq);
323
+ shareRet = success ? "success" : "failed";
324
+ }
325
+ }
326
+
327
+ if (type == TYPE_WECHAT_SHARING_MINI) {//分享小程序
328
+ WXEventHandler.registerOnWXRespCallback(authCallback);
329
+ let miniProgramObject: ESObject = new wxopensdk.WXMiniProgramObject();
330
+ if (media?.[KEY_ARG_MESSAGE_MEDIA_USERNAME] !== undefined) {
331
+ miniProgramObject.userName = media?.[KEY_ARG_MESSAGE_MEDIA_USERNAME];
332
+ }
333
+
334
+ if (media?.[KEY_ARG_MESSAGE_MEDIA_PATH] !== undefined) {
335
+ miniProgramObject.path = media?.[KEY_ARG_MESSAGE_MEDIA_PATH];
336
+ }
337
+
338
+ if (media?.[KEY_ARG_MESSAGE_MEDIA_MINIPROGRAMTYPE] !== undefined) {
339
+ miniProgramObject.miniprogramType = media?.[KEY_ARG_MESSAGE_MEDIA_MINIPROGRAMTYPE];
340
+ }
341
+
342
+ if (media?.[KEY_ARG_MESSAGE_MEDIA_WITHSHARETICKET] !== undefined) {
343
+ miniProgramObject.withShareTicket = media?.[KEY_ARG_MESSAGE_MEDIA_WITHSHARETICKET];
344
+ }
345
+
346
+ let mediaMessage: ESObject = new wxopensdk.WXMediaMessage();
347
+ mediaMessage.mediaObject = miniProgramObject
348
+ if (message?.[KEY_ARG_MESSAGE_TITLE] != undefined) {
349
+ mediaMessage.title = message?.[KEY_ARG_MESSAGE_TITLE];
350
+ }
351
+
352
+ if (message?.[KEY_ARG_MESSAGE_DESCRIPTION] != undefined) {
353
+ mediaMessage.description = message?.[KEY_ARG_MESSAGE_DESCRIPTION];
354
+ }
355
+
356
+ //C++侧将图片转为原生路径,避免大图片传输 缩略图不能太大,否则会报[wxopensdk::WXApi] openWeChatWithLink fail by err: Internal error 错误
357
+ if (message?.[KEY_ARG_MESSAGE_THUMB] !== undefined) {
358
+ let thumbUri: string = message?.[KEY_ARG_MESSAGE_THUMB];
359
+ const thumbPixel = image.createImageSource(thumbUri).createPixelMapSync()
360
+ const thumbBuffer = await image.createImagePacker().packToData(thumbPixel, { format: "image/png", quality: 100 })
361
+ mediaMessage.thumbData = new Uint8Array(thumbBuffer)
362
+ }
363
+
364
+ sendMessageToWXReq.message = mediaMessage
365
+ let success: boolean = await WXApi.sendReq(context, sendMessageToWXReq);
366
+ shareRet = success ? "success" : "failed";
367
+ }
368
+
369
+ if (type == TYPE_WECHAT_SHARING_WEBPAGE) {//分享网页
370
+ WXEventHandler.registerOnWXRespCallback(authCallback);
371
+ if (media?.[KEY_ARG_MESSAGE_MEDIA_WEBPAGEURL] !== undefined) {
372
+ let url: string = media?.[KEY_ARG_MESSAGE_MEDIA_WEBPAGEURL];
373
+ let webpageObject: ESObject = new wxopensdk.WXWebpageObject;
374
+ webpageObject.webpageUrl = url;
375
+
376
+ let mediaMessage: ESObject = new wxopensdk.WXMediaMessage();
377
+ mediaMessage.mediaObject = webpageObject
378
+ if (message?.[KEY_ARG_MESSAGE_TITLE] != undefined) {
379
+ mediaMessage.title = message?.[KEY_ARG_MESSAGE_TITLE];
380
+ }
381
+
382
+ if (message?.[KEY_ARG_MESSAGE_DESCRIPTION] != undefined) {
383
+ mediaMessage.description = message?.[KEY_ARG_MESSAGE_DESCRIPTION];
384
+ }
385
+
386
+ //C++侧将图片转为原生路径,避免大图片传输 缩略图不能太大,否则会报[wxopensdk::WXApi] openWeChatWithLink fail by err: Internal error 错误
387
+ if (message?.[KEY_ARG_MESSAGE_THUMB] !== undefined) {
388
+ let thumbUri: string = message?.[KEY_ARG_MESSAGE_THUMB];
389
+ const thumbPixel = image.createImageSource(thumbUri).createPixelMapSync()
390
+ const thumbBuffer = await image.createImagePacker().packToData(thumbPixel, { format: "image/png", quality: 100 })
391
+ mediaMessage.thumbData = new Uint8Array(thumbBuffer)
392
+ }
393
+
394
+ sendMessageToWXReq.message = mediaMessage
395
+ let success: boolean = await WXApi.sendReq(context, sendMessageToWXReq);
396
+ shareRet = success ? "success" : "failed";
397
+ }
398
+ }
399
+ }
400
+
401
+ let result: ArkTsAttribute = { content: "share", result: [shareRet] };
402
+ cordova.onArkTsResult(JSON.stringify(result), pageIndex.pageObject, pageIndex.pageWebTag);
403
+ }
404
+ });
405
+ }
406
+
407
+ function wechatIsInstall(pageIndex:NativeAttribute) {
408
+ let appId:string = pageIndex.pageArgs;
409
+ if(appId == "") {
410
+ let result: ArkTsAttribute = { content: "share", result: [ERROR_INVALID_PARAMETERS] };
411
+ cordova.onArkTsResult(JSON.stringify(result), pageIndex.pageObject, pageIndex.pageWebTag);
412
+ return;
413
+ }
414
+
415
+ const module:string = "@tencent/wechat_open_sdk";
416
+ import(module).then(async (wxopensdk:ESObject) => {
417
+ if(WXApi.isWXAppInstalled()) {
418
+ let result: ArkTsAttribute = { content: "isWXAppInstalled", result: ["true"] };
419
+ cordova.onArkTsResult(JSON.stringify(result), pageIndex.pageObject, pageIndex.pageWebTag);
420
+ } else {
421
+ let result: ArkTsAttribute = { content: "isWXAppInstalled", result: ["false"] };
422
+ cordova.onArkTsResult(JSON.stringify(result), pageIndex.pageObject, pageIndex.pageWebTag);
423
+ }
424
+ });
425
+ }
426
+
427
+ function wechatAuth(pageIndex:NativeAttribute) {
428
+ let paras:object = new Object();
429
+ try {
430
+ paras = JSON.parse(pageIndex.pageArgs);
431
+ } catch (error) {
432
+ let result: ArkTsAttribute = { content: "sendAuthRequest", result: [ERROR_INVALID_PARAMETERS] };
433
+ cordova.onArkTsResult(JSON.stringify(result), pageIndex.pageObject, pageIndex.pageWebTag);
434
+ return;
435
+ }
436
+
437
+ let appId:string = "";
438
+ if(paras?.["APP_ID"] !== undefined) {
439
+ appId = paras?.["APP_ID"];
440
+ }
441
+
442
+ if(appId == "") {
443
+ let result: ArkTsAttribute = { content: "sendAuthRequest", result: [ERROR_INVALID_PARAMETERS] };
444
+ cordova.onArkTsResult(JSON.stringify(result), pageIndex.pageObject, pageIndex.pageWebTag);
445
+ return;
446
+ }
447
+
448
+ let scope:string = "snsapi_userinfo";
449
+ if(paras?.["scope"] !== undefined) {
450
+ scope = paras?.["scope"];
451
+ }
452
+
453
+ let state:string = "wechat";
454
+ if(paras?.["state"] !== undefined) {
455
+ state = paras?.["state"];
456
+ }
457
+
458
+
459
+ const module:string = "@tencent/wechat_open_sdk";
460
+ import(module).then(async (wxopensdk:ESObject) => {
461
+ let mainPage:MainPage;
462
+ try {
463
+ if(!cordovaWebTagToObjectGlobe.hasKey(pageIndex.pageWebTag)) {
464
+ return;
465
+ }
466
+ mainPage = cordovaWebTagToObjectGlobe.get(pageIndex.pageWebTag) as MainPage;
467
+ } catch (error) {
468
+ const cordovaError = NormalizeError(error);
469
+ console.error(`Failed to wechatShare. Cause code: ${cordovaError.code}, message: ${cordovaError.message}`);
470
+ return;
471
+ }
472
+ let context = mainPage!.getUIContext().getHostContext() as common.UIAbilityContext;
473
+ // 注册授权结果回调
474
+ const authCallback: ESObject = (resp: ESObject) => {
475
+ if (resp.name && resp.name == "SendAuthResp") {
476
+ if (resp.errCode === wxopensdk.ErrCode.ERR_OK) {
477
+ // 授权成功
478
+ let result: ArkTsAttribute = { content: "sendAuthRequest", result: ["true", JSON.stringify(resp)] };
479
+ cordova.onArkTsResult(JSON.stringify(result), pageIndex.pageObject, pageIndex.pageWebTag);
480
+ } else if (resp.errCode === wxopensdk.ErrCode.ERR_USER_CANCEL) {
481
+ // 用户取消授权
482
+ let result: ArkTsAttribute = { content: "sendAuthRequest", result: ["false", JSON.stringify(resp)] };
483
+ cordova.onArkTsResult(JSON.stringify(result), pageIndex.pageObject, pageIndex.pageWebTag);
484
+ } else {
485
+ // 授权失败
486
+ let result: ArkTsAttribute = { content: "sendAuthRequest", result: ["false", JSON.stringify(resp)] };
487
+ cordova.onArkTsResult(JSON.stringify(result), pageIndex.pageObject, pageIndex.pageWebTag);
488
+ }
489
+ // 处理完后取消注册回调(避免重复调用)
490
+ WXEventHandler.unregisterOnWXRespCallback(authCallback);
491
+ }
492
+ };
493
+ WXEventHandler.registerOnWXRespCallback(authCallback);
494
+
495
+ let req:ESObject = new wxopensdk.SendAuthReq
496
+ req.isOption1 = false
497
+ req.nonAutomatic = true
498
+ req.scope = scope// 获取用户信息
499
+ req.state = state// 自定义状态参数
500
+ req.transaction = 'transaction_'+ new Date().getTime()// 唯一事务ID
501
+ let success: boolean = await WXApi.sendReq(context, req);
502
+ if(!success) {
503
+ let result: ArkTsAttribute = { content: "sendAuthRequest", result: ["false", ERROR_WECHAT_RESPONSE_SENT_FAILED] };
504
+ cordova.onArkTsResult(JSON.stringify(result), pageIndex.pageObject, pageIndex.pageWebTag);
505
+ }
506
+ });
507
+ }
508
+
509
+ function sendPaymentRequest(pageIndex:NativeAttribute) {
510
+ let paras:object = new Object();
511
+ try {
512
+ paras = JSON.parse(pageIndex.pageArgs);
513
+ } catch (error) {
514
+ let result: ArkTsAttribute = { content: "sendAuthRequest", result: [ERROR_INVALID_PARAMETERS] };
515
+ cordova.onArkTsResult(JSON.stringify(result), pageIndex.pageObject, pageIndex.pageWebTag);
516
+ return;
517
+ }
518
+
519
+ let appId:string = "";
520
+ if(paras?.["APP_ID"] !== undefined) {
521
+ appId = paras?.["APP_ID"];
522
+ }
523
+
524
+ let partnerid:string = "";
525
+ if(paras?.["partnerid"] !== undefined) {
526
+ partnerid = paras?.["partnerid"];
527
+ }
528
+
529
+ let prepayid:string = "";
530
+ if(paras?.["prepayid"] !== undefined) {
531
+ prepayid = paras?.["prepayid"];
532
+ }
533
+
534
+ let noncestr:string = "";
535
+ if(paras?.["noncestr"] !== undefined) {
536
+ noncestr = paras?.["noncestr"];
537
+ }
538
+
539
+ let timestamp:string = "";
540
+ if(paras?.["timestamp"] !== undefined) {
541
+ timestamp = paras?.["timestamp"];
542
+ }
543
+
544
+ let sign:string = "";
545
+ if(paras?.["sign"] !== undefined) {
546
+ sign = paras?.["sign"];
547
+ }
548
+ const module:string = "@tencent/wechat_open_sdk";
549
+ import(module).then(async (wxopensdk:ESObject) => {
550
+ let mainPage:MainPage;
551
+ try {
552
+ if(!cordovaWebTagToObjectGlobe.hasKey(pageIndex.pageWebTag)) {
553
+ return;
554
+ }
555
+ mainPage = cordovaWebTagToObjectGlobe.get(pageIndex.pageWebTag) as MainPage;
556
+ } catch (error) {
557
+ const cordovaError = NormalizeError(error);
558
+ console.error(`Failed to wechatShare. Cause code: ${cordovaError.code}, message: ${cordovaError.message}`);
559
+ return;
560
+ }
561
+ let context = mainPage!.getUIContext().getHostContext() as common.UIAbilityContext;
562
+ // 注册授权结果回调
563
+ const authCallback: ESObject = (resp: ESObject) => {
564
+ if (resp.name && resp.name == "PayResp") {
565
+ if (resp.errCode === wxopensdk.ErrCode.ERR_OK) {
566
+ // 支付成功
567
+ let result: ArkTsAttribute = { content: "sendPaymentRequest", result: ["true", JSON.stringify(resp)] };
568
+ cordova.onArkTsResult(JSON.stringify(result), pageIndex.pageObject, pageIndex.pageWebTag);
569
+ } else if (resp.errCode === wxopensdk.ErrCode.ERR_USER_CANCEL) {
570
+ // 用户取消支付
571
+ let result: ArkTsAttribute = { content: "sendPaymentRequest", result: ["false", JSON.stringify(resp)] };
572
+ cordova.onArkTsResult(JSON.stringify(result), pageIndex.pageObject, pageIndex.pageWebTag);
573
+ } else {
574
+ // 支付失败
575
+ let result: ArkTsAttribute = { content: "sendPaymentRequest", result: ["false", JSON.stringify(resp)] };
576
+ cordova.onArkTsResult(JSON.stringify(result), pageIndex.pageObject, pageIndex.pageWebTag);
577
+ }
578
+ // 处理完后取消注册回调(避免重复调用)
579
+ WXEventHandler.unregisterOnWXRespCallback(authCallback);
580
+ }
581
+ };
582
+
583
+ WXEventHandler.registerOnWXRespCallback(authCallback);
584
+
585
+ let req:ESObject = new wxopensdk.PayReq;
586
+ req.appId = appId
587
+ req.partnerId = partnerid
588
+ req.prepayId = prepayid
589
+ req.packageValue = 'Sign=WXPay'
590
+ req.nonceStr = noncestr
591
+ req.timeStamp = timestamp
592
+ req.sign = sign;
593
+ let success: boolean = await WXApi.sendReq(context, req);
594
+ if(!success) {
595
+ let result: ArkTsAttribute = { content: "sendPaymentRequest", result: ["false", ERROR_WECHAT_RESPONSE_SENT_FAILED] };
596
+ cordova.onArkTsResult(JSON.stringify(result), pageIndex.pageObject, pageIndex.pageWebTag);
597
+ }
598
+ });
599
+ }
600
+
601
+ export function WeChatAction(pageIndex:NativeAttribute) {
602
+ if(pageIndex.pageValue == 0) {
603
+ wechatShare(pageIndex);
604
+ }
605
+
606
+ if(pageIndex.pageValue == 1) {
607
+ wechatAuth(pageIndex);
608
+ }
609
+
610
+ if(pageIndex.pageValue == 2) {
611
+ wechatIsInstall(pageIndex);
612
+ }
613
+
614
+ if(pageIndex.pageValue == 3) {
615
+ wechatMinProgram(pageIndex);
616
+ }
617
+
618
+ if(pageIndex.pageValue == 4) {
619
+ sendPaymentRequest(pageIndex);
620
+ }
621
+ }