@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.
package/plugin.xml ADDED
@@ -0,0 +1,54 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <!--
3
+ Copyright (c) 2025 Huawei Device, Inc. Ltd. and <马弓手>.
4
+
5
+ Licensed under the Apache License, Version 2.0 (the "License");
6
+ you may not use this file except in compliance with the License.
7
+ You may obtain a copy of the License at
8
+
9
+ http://www.apache.org/licenses/LICENSE-2.0
10
+
11
+ Unless required by applicable law or agreed to in writing, software
12
+ distributed under the License is distributed on an "AS IS" BASIS,
13
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ See the License for the specific language governing permissions and
15
+ limitations under the License.
16
+ -->
17
+ <plugin xmlns="http://apache.org/cordova/ns/plugins/1.0"
18
+ id="cordova-plugin-wechat"
19
+ version="2.2.0">
20
+ <name>Wechat</name>
21
+ <description>Cordova Wechat Plugin</description>
22
+ <license>Apache 2.0</license>
23
+ <keywords>cordova,wechat</keywords>
24
+ <repo>https://gitcode.com/OpenHarmony-Cordova/cordova-plugin-wechat.git</repo>
25
+ <issue>https://gitcode.com/OpenHarmony-Cordova/cordova-plugin-wechat/issues</issue>
26
+
27
+ <preference name="WECHATAPPID" />
28
+ <engines>
29
+ <engine name="@cordova-ohos/ohos" version=">=2.0.0" />
30
+ </engines>
31
+
32
+ <platform name="ohos">
33
+ <config-file target="src/main/resources/rawfile/config.xml" parent="/*" modules-targets-name="default">
34
+ <feature name="Wechat">
35
+ <param name="android-package" value="Wechat" />
36
+ </feature>
37
+ <preference name="WECHATAPPID" value="$WECHATAPPID"/>
38
+ </config-file>
39
+
40
+ <CMakeLists target="src/main/cpp/CMakeLists.txt" modules-name="cordova">
41
+ <param target="add_library" value="Wechat/Wechat.cpp"/>
42
+ </CMakeLists>
43
+
44
+ <source-file type="h" src="src/main/cpp/Wechat/Wechat.h" target-dir="src/main/cpp/Wechat" modules-name="cordova"/>
45
+ <source-file type="cpp" src="src/main/cpp/Wechat/Wechat.cpp" target-dir="src/main/cpp/Wechat" modules-name="cordova"/>
46
+ <source-file type="ets" src="src/main/ets/components/WeChatAction/WeChatAction.ets" target-dir="src/main/ets/components/WeChatAction" modules-name="cordova" runtimeOnly="true"/>
47
+
48
+ <js-module src="www/wechat.js" name="Wechat" modules-targets-name="default">
49
+ <clobbers target="Wechat" />
50
+ </js-module>
51
+
52
+ <dependencies type="har" name="@tencent/wechat_open_sdk" version="^1.0.0" modules-targets-name="default"/>
53
+ </platform>
54
+ </plugin>
@@ -0,0 +1,607 @@
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
+ #include "Wechat.h"
19
+ #include "ConnPool.h"
20
+ #include "CordovaViewController.h"
21
+ #include "MemPool.h"
22
+ #include "cJSON.h"
23
+ #include "HttpUrl.h"
24
+ #include "FileCache.h"
25
+ #include <filemanagement/file_uri/error_code.h>
26
+ #include <filemanagement/file_uri/oh_file_uri.h>
27
+ REGISTER_PLUGIN_CLASS(Wechat)
28
+
29
+ bool Wechat::execute(const std::string& action, cJSON* args, CallbackContext cbc)
30
+ {
31
+ if (action == "onArKTsResult") {
32
+ return onArKTsResult(args);
33
+ }
34
+
35
+ if (action == "share") {
36
+ cJSON* pJson = cJSON_GetArrayItem(args, 0);
37
+ if (pJson != nullptr) {
38
+ cJSON* pMessage = cJSON_GetObjectItem(pJson, "message");
39
+
40
+ std::string strAppId = m_preferences->getString("WECHATAPPID", "");
41
+ cJSON_AddStringToObject(pJson, "APP_ID", strAppId.c_str());
42
+
43
+ if (pMessage != nullptr) {
44
+ cJSON* pThumb = cJSON_GetObjectItem(pMessage, "thumb");
45
+ if (pThumb != nullptr && pThumb->type == cJSON_String) {
46
+ std::string strFileUri;
47
+ getFilePath(pThumb->valuestring, strFileUri);
48
+ if (!strFileUri.empty()) {
49
+ cJSON* pRespThumb = cJSON_CreateString(strFileUri.c_str());
50
+ cJSON_ReplaceItemInObject(pMessage, "thumb", pRespThumb);
51
+ }
52
+ }
53
+
54
+ cJSON* pMedia = cJSON_GetObjectItem(pMessage, "media");
55
+ if (pMedia != nullptr) {
56
+ cJSON* pImage = cJSON_GetObjectItem(pMedia, "image");
57
+ if (pImage != nullptr) {
58
+ std::string strFileUri;
59
+ getUri(pImage->valuestring, strFileUri);
60
+ if (!strFileUri.empty()) {
61
+ cJSON* pUri = cJSON_CreateString(strFileUri.c_str());
62
+ cJSON_ReplaceItemInObject(pMedia, "image", pUri);
63
+ }
64
+ }
65
+
66
+ cJSON* pFile = cJSON_GetObjectItem(pMedia, "file");
67
+ if (pFile != nullptr) {
68
+ std::string strFileUri;
69
+ getUri(pFile->valuestring, strFileUri);
70
+ if (!strFileUri.empty()) {
71
+ cJSON* pUri = cJSON_CreateString(strFileUri.c_str());
72
+ cJSON_ReplaceItemInObject(pMedia, "file", pUri);
73
+ }
74
+ }
75
+
76
+ cJSON* pVideoUrl = cJSON_GetObjectItem(pMedia, "videoUrl");
77
+ if (pVideoUrl != nullptr) {
78
+ std::string strFileUri;
79
+ getUri(pVideoUrl->valuestring, strFileUri);
80
+ if (!strFileUri.empty()) {
81
+ cJSON* pUri = cJSON_CreateString(strFileUri.c_str());
82
+ cJSON_ReplaceItemInObject(pMedia, "videoUrl", pUri);
83
+ }
84
+ }
85
+ }
86
+ }
87
+
88
+ m_cbc = cbc;
89
+ char* pPageArgs = cJSON_Print(pJson);
90
+
91
+ executeArkTs("./WeChatAction/WeChatAction/WeChatAction", 0, pPageArgs, "Wechat", cbc);
92
+ free(pPageArgs);
93
+ return true;
94
+ }
95
+ }
96
+
97
+ if (action == "sendAuthRequest") {
98
+ CordovaArgs cordovaArgs(args);
99
+ std::string strScope = cordovaArgs.getString(0);
100
+ std::string strState = cordovaArgs.getString(1);
101
+ if (strScope.empty()) {
102
+ strScope = "snsapi_userinfo";
103
+ }
104
+ if (strState.empty()) {
105
+ strState = "wechat";
106
+ }
107
+ cJSON* pJson = cJSON_CreateObject();
108
+ std::string strAppId = m_preferences->getString("WECHATAPPID", "");
109
+ cJSON_AddStringToObject(pJson, "APP_ID", strAppId.c_str());
110
+ cJSON_AddStringToObject(pJson, "scope", strScope.c_str());
111
+ cJSON_AddStringToObject(pJson, "state", strState.c_str());
112
+ m_cbc = cbc;
113
+ char* pPageArgs = cJSON_Print(pJson);
114
+ executeArkTs("./WeChatAction/WeChatAction/WeChatAction", 1, pPageArgs, "Wechat", cbc);
115
+ free(pPageArgs);
116
+ cJSON_Delete(pJson);
117
+ return true;
118
+ }
119
+
120
+ if (action == "isWXAppInstalled") {
121
+ m_cbc = cbc;
122
+ std::string strAppId = m_preferences->getString("WECHATAPPID", "");
123
+ executeArkTs("./WeChatAction/WeChatAction/WeChatAction", 2, strAppId.c_str(), "Wechat", cbc);
124
+ }
125
+
126
+ if (action == "openMiniProgram" || action == "minProgram") {
127
+ int nSize = cJSON_GetArraySize(args);
128
+ if (nSize <= 0) {
129
+ return true;
130
+ }
131
+
132
+ cJSON* pJson = cJSON_GetArrayItem(args, 0);
133
+ std::string strAppId = m_preferences->getString("WECHATAPPID", "");
134
+ cJSON_AddStringToObject(pJson, "APP_ID", strAppId.c_str());
135
+ m_cbc = cbc;
136
+ char* pPageArgs = cJSON_Print(pJson);
137
+
138
+ executeArkTs("./WeChatAction/WeChatAction/WeChatAction", 3, pPageArgs, "Wechat", cbc);
139
+ free(pPageArgs);
140
+ return true;
141
+ }
142
+
143
+ if (action == "sendPaymentRequest") {
144
+ int nSize = cJSON_GetArraySize(args);
145
+ if (nSize <= 0) {
146
+ return true;
147
+ }
148
+
149
+ cJSON* pJson = cJSON_GetArrayItem(args, 0);
150
+ std::string strAppId = m_preferences->getString("WECHATAPPID", "");
151
+ cJSON_AddStringToObject(pJson, "APP_ID", strAppId.c_str());
152
+ m_cbc = cbc;
153
+ char* pPageArgs = cJSON_Print(pJson);
154
+
155
+ executeArkTs("./WeChatAction/WeChatAction/WeChatAction", 4, pPageArgs, "Wechat", cbc);
156
+ free(pPageArgs);
157
+ return true;
158
+ }
159
+
160
+ return true;
161
+ }
162
+
163
+ bool Wechat::onArKTsResult(cJSON* args)
164
+ {
165
+ cJSON* json = cJSON_GetObjectItem(args, "content");
166
+ std::string strContent = "";
167
+ if (json != NULL) {
168
+ strContent = json->valuestring;
169
+ }
170
+ cJSON* pResult = cJSON_GetObjectItem(args, "result");
171
+ if (strContent == "WechatMinProgram" || strContent == "share") {
172
+ if (pResult != NULL && pResult->type == cJSON_Array) {
173
+ cJSON* pRet = cJSON_GetArrayItem(pResult, 0);
174
+ if (pRet != NULL) {
175
+ cJSON* pRetResult = cJSON_GetArrayItem(pResult, 1);
176
+ std::string strRet = pRet->valuestring;
177
+ if (strRet == "success") {
178
+ PluginResult pluginResult(PluginResult::OK);
179
+ pluginResult.setKeepCallback(true);
180
+ m_cbc.sendPluginResult(pluginResult);
181
+ } else if (strRet == "failed") {
182
+ m_cbc.error(strRet);
183
+ } else if (pRetResult != nullptr) {
184
+ std::string strRetResult = pRetResult->valuestring;
185
+ cJSON* pJsonRet = cJSON_Parse(strRetResult.c_str());
186
+ if (pJsonRet != nullptr) {
187
+ m_cbc.error(pJsonRet);
188
+ cJSON_Delete(pJsonRet);
189
+ } else {
190
+ m_cbc.error(strRetResult);
191
+ }
192
+ }
193
+ }
194
+ }
195
+ }
196
+
197
+ if (strContent == "isWXAppInstalled") {
198
+ if (pResult != NULL && pResult->type == cJSON_Array) {
199
+ cJSON* pRet = cJSON_GetArrayItem(pResult, 0);
200
+ if (pRet != NULL) {
201
+ std::string strRet = pRet->valuestring;
202
+ if (strRet == "true") {
203
+ m_cbc.success(1);
204
+ } else {
205
+ m_cbc.success(0);
206
+ }
207
+ }
208
+ }
209
+ }
210
+
211
+ if (strContent == "sendAuthRequest") {
212
+ if (pResult != NULL && pResult->type == cJSON_Array) {
213
+ cJSON* pRet = cJSON_GetArrayItem(pResult, 0);
214
+ if (pRet != NULL) {
215
+ cJSON* pRetResult = cJSON_GetArrayItem(pResult, 1);
216
+ std::string strRet = pRet->valuestring;
217
+ if (pRetResult != nullptr && pRetResult->type == cJSON_String) {
218
+ std::string strRetResult = pRetResult->valuestring;
219
+ cJSON* pJsonRet = cJSON_Parse(strRetResult.c_str());
220
+ if (pJsonRet != nullptr && strRet == "true") {
221
+ m_cbc.success(pJsonRet);
222
+ } else if (pJsonRet != nullptr) {
223
+ m_cbc.error(pJsonRet);
224
+ } else {
225
+ m_cbc.error(strRetResult);
226
+ }
227
+ if (pJsonRet != nullptr) {
228
+ cJSON_Delete(pJsonRet);
229
+ }
230
+ }
231
+ }
232
+ }
233
+ }
234
+
235
+ if (strContent == "sendPaymentRequest") {
236
+ if (pResult != NULL && pResult->type == cJSON_Array) {
237
+ cJSON* pRet = cJSON_GetArrayItem(pResult, 0);
238
+ if (pRet != NULL) {
239
+ cJSON* pRetResult = cJSON_GetArrayItem(pResult, 1);
240
+ std::string strRet = pRet->valuestring;
241
+ if (pRetResult != nullptr && pRetResult->type == cJSON_String) {
242
+ std::string strRetResult = pRetResult->valuestring;
243
+ cJSON* pJsonRet = cJSON_Parse(strRetResult.c_str());
244
+ if (pJsonRet != nullptr && strRet == "true") {
245
+ m_cbc.success(pJsonRet);
246
+ } else if (pJsonRet != nullptr) {
247
+ m_cbc.error(pJsonRet);
248
+ } else {
249
+ m_cbc.error(strRetResult);
250
+ }
251
+ if (pJsonRet != nullptr) {
252
+ cJSON_Delete(pJsonRet);
253
+ }
254
+ }
255
+ }
256
+ }
257
+ }
258
+
259
+ return true;
260
+ }
261
+
262
+ bool Wechat::getBase64File(const std::string& strSource, std::string& strFilePath)
263
+ {
264
+ std::string strBase64Data;
265
+ if (strSource.length() > strSource.find("base64") + 6) {
266
+ strBase64Data = strSource.substr(strSource.find("base64") + 7);
267
+ }
268
+ if (strBase64Data.empty()) {
269
+ return false;
270
+ }
271
+ std::string strFileName = HttpUrl::generateMD5(strBase64Data) + FileCache::getImageType(strBase64Data);
272
+ std::string strTarget =
273
+ Application::g_databaseDir.substr(0, Application::g_databaseDir.find("database") - 1) + "/base/" + strFileName;
274
+ std::string str = FileCache::urlDecode(strBase64Data);
275
+ int nLength = str.length();
276
+ char* pBuf = CBase64::decode(str.c_str(), nLength);
277
+ if (pBuf != nullptr) {
278
+ FileCache::writeFile(strTarget, pBuf, nLength);
279
+ }
280
+ strFilePath = strTarget;
281
+ return true;
282
+ }
283
+
284
+ bool Wechat::getFilePath(const std::string& strSource, std::string& strFileUri)
285
+ {
286
+ std::string strThumbPath;
287
+ std::string strThumb = strSource;
288
+ if (strThumb.find("http") == 0) { // 在线文件
289
+ if (!downloadFile(strThumb, strThumbPath)) {
290
+ OH_LOG_Print(
291
+ LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "Wechat", "downloadFile(strThumb, strThumbPath) == false");
292
+ return false;
293
+ }
294
+ } else if (strThumb.find("/data/storage/el2") == 0) { // 沙箱文件
295
+ strThumbPath = strThumb;
296
+ } else if (strThumb.find("data:") == 0) { // base64编码
297
+ if (!getBase64File(strThumb, strThumbPath)) {
298
+ OH_LOG_Print(
299
+ LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "Wechat", "getBase64File(strThumb, strFileUri) == false");
300
+ return false;
301
+ }
302
+ } else { // rawfile资源文件
303
+ if (!getUriFromRawfile(strThumb, strThumbPath)) {
304
+ OH_LOG_Print(
305
+ LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "Wechat", "getUriFromRawfile(strThumb, strThumbPath) == false");
306
+ return false;
307
+ }
308
+ }
309
+
310
+ if (!strThumbPath.empty()) {
311
+ strFileUri = strThumbPath;
312
+ }
313
+ return true;
314
+ }
315
+
316
+ bool Wechat::getUri(const std::string& strSource, std::string& strFileUri)
317
+ {
318
+ std::string strThumbPath;
319
+ std::string strThumb = strSource;
320
+ if (strThumb.find("http") == 0) { // 在线文件
321
+ if (!downloadFile(strThumb, strThumbPath)) {
322
+ OH_LOG_Print(
323
+ LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "Wechat", "downloadFile(strThumb, strThumbPath) == false");
324
+ return false;
325
+ }
326
+ } else if (strThumb.find("/data/storage/el2") == 0) { // 沙箱文件
327
+ strThumbPath = strThumb;
328
+ } else if (strThumb.find("data:") == 0) { // base64编码
329
+ if (!getBase64File(strThumb, strThumbPath)) {
330
+ OH_LOG_Print(
331
+ LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "Wechat", "getBase64File(strThumb, strFileUri) == false");
332
+ return false;
333
+ }
334
+ } else { // rawfile资源文件
335
+ if (!getUriFromRawfile(strThumb, strThumbPath)) {
336
+ OH_LOG_Print(
337
+ LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "Wechat", "downloadFile(strThumb, strThumbPath) == false");
338
+ return false;
339
+ }
340
+ }
341
+
342
+ if (!strThumbPath.empty()) {
343
+ char* result = nullptr;
344
+ FileManagement_ErrCode errCode =
345
+ OH_FileUri_GetUriFromPath(strThumbPath.c_str(), strThumbPath.length(), &result);
346
+ if (errCode != ERR_OK) {
347
+ OH_LOG_Print(LOG_APP,
348
+ LOG_ERROR,
349
+ LOG_PRINT_DOMAIN,
350
+ "Wechat",
351
+ "OH_FileUri_GetUriFromPath(strThumbPath.c_str(),strThumbPath.length(), &result) == false");
352
+ return false;
353
+ }
354
+ strFileUri = result;
355
+ free(result);
356
+ }
357
+ return true;
358
+ }
359
+
360
+ bool Wechat::downloadFile(const std::string& strSource, std::string& strFilePath)
361
+ {
362
+ std::vector<SMemPage> vecMemBuf;
363
+ ((CMemPool*)Application::g_memPool)->mallocPage(vecMemBuf, 1);
364
+ if (vecMemBuf.size() < 1) {
365
+ OH_LOG_Print(LOG_APP,
366
+ LOG_ERROR,
367
+ LOG_PRINT_DOMAIN,
368
+ "FileTransfer",
369
+ "((CMemPool*)Application::g_memPool)->mallocPage(vecMemBuf, 1) == NULL");
370
+ return NULL;
371
+ }
372
+ int nBufSize = ((CMemPool*)Application::g_memPool)->getPageSize();
373
+ char* pBuf = vecMemBuf[0].m_point;
374
+
375
+ std::string strExt = ".jpg";
376
+ if (strSource.find_last_of(".") != std::string::npos) {
377
+ strExt = strSource.substr(strSource.find_last_of("."));
378
+ }
379
+ std::string strFileName = HttpUrl::generateMD5(strSource) + strExt;
380
+ std::string strTarget =
381
+ Application::g_databaseDir.substr(0, Application::g_databaseDir.find("database") - 1) + "/base/" + strFileName;
382
+ if (FileCache::IsFile(strTarget, "")) {
383
+ strFilePath = strTarget;
384
+ return true;
385
+ }
386
+ bool isSuccess = download(strSource, strTarget, pBuf, nBufSize);
387
+ ((CMemPool*)Application::g_memPool)->freePage(vecMemBuf);
388
+ if (isSuccess) {
389
+ strFilePath = strTarget;
390
+ }
391
+ return isSuccess;
392
+ }
393
+
394
+ bool Wechat::download(const std::string& strSource, const std::string& strTarget, char* pBuf, const int nLength)
395
+ {
396
+ // 1,解析url
397
+ std::string urlPath = strSource;
398
+ std::string requestAddress = "";
399
+ urlPath = urlPath.erase(0, urlPath.find_first_not_of(" "));
400
+ urlPath = urlPath.erase(urlPath.find_last_not_of(" ") + 1);
401
+ if (urlPath.find("//") != std::string::npos) {
402
+ requestAddress = urlPath.substr(urlPath.find("//") + 2);
403
+ }
404
+
405
+ // 2,解析http/https协议类型
406
+ int nPort = 0;
407
+ bool isHttps = false;
408
+ if (urlPath.find("https") == 0) {
409
+ isHttps = true;
410
+ nPort = 443;
411
+ } else if (urlPath.find("http") == 0) {
412
+ isHttps = false;
413
+ nPort = 80;
414
+ } else {
415
+ return false;
416
+ }
417
+
418
+ // 3,解析域名修改请求地址
419
+ std::string strDomainAndPort;
420
+ std::string strDomain;
421
+ if (urlPath.find("//") != std::string::npos) {
422
+ strDomain = urlPath.substr(urlPath.find("//") + 2);
423
+ strDomainAndPort = strDomain;
424
+ if (strDomain.find("/") != std::string::npos) {
425
+ strDomain = strDomain.substr(0, strDomain.find("/"));
426
+ strDomainAndPort = strDomain;
427
+ }
428
+ if (strDomain.find(":") != std::string::npos) {
429
+ std::string strPort = strDomain.substr(strDomain.find(":") + 1);
430
+ strDomain = strDomain.substr(0, strDomain.find(":"));
431
+ nPort = atoi(strPort.c_str());
432
+ }
433
+ requestAddress = requestAddress.substr(strDomainAndPort.length());
434
+ }
435
+
436
+
437
+ std::string strRequest = "GET " + requestAddress + " HTTP/1.1\r\n"; // GET %s HTTP/1.1\r\n
438
+
439
+ std::vector<SMemPage> vecMemBuf;
440
+ ((CMemPool*)Application::g_memPool)->mallocPage(vecMemBuf, 1);
441
+ if (vecMemBuf.size() < 1) {
442
+ OH_LOG_Print(LOG_APP,
443
+ LOG_ERROR,
444
+ LOG_PRINT_DOMAIN,
445
+ "FileTransfer",
446
+ "((CMemPool*)Application::g_memPool)->MallocPage(vecMemBuf, 1) == NULL");
447
+ return false;
448
+ }
449
+
450
+ char* szBuf = vecMemBuf[0].m_point;
451
+ const int const_buf_size = (static_cast<CMemPool*>(Application::g_memPool))->getPageSize();
452
+ if (nPort == 80 || nPort == 443)
453
+ snprintf(szBuf, const_buf_size, "Host:%s\r\n", strDomain.c_str());
454
+ else
455
+ snprintf(szBuf, const_buf_size, "Host:%s:%d\r\n", strDomain.c_str(), nPort);
456
+ strRequest += szBuf;
457
+
458
+ snprintf(szBuf, const_buf_size, "Connection:Keep-Alive\r\n");
459
+ strRequest += szBuf;
460
+
461
+ std::string strHttpUrl = isHttps ? "https://" + strDomainAndPort : "http://" + strDomainAndPort;
462
+ if (Application::g_cookieManage != NULL && !ARKWEB_MEMBER_MISSING(Application::g_cookieManage, existCookies)) {
463
+ if (Application::g_cookieManage->existCookies(false)) {
464
+ if (!ARKWEB_MEMBER_MISSING(Application::g_cookieManage, fetchCookieSync)) {
465
+ char* pCookieValue = NULL;
466
+ Application::g_cookieManage->fetchCookieSync(strHttpUrl.c_str(), false, true, &pCookieValue);
467
+ if (pCookieValue != NULL && strlen(pCookieValue) > 0) {
468
+ std::string strCookies = pCookieValue;
469
+ std::string strCookie = "Set-Cookie:";
470
+ size_t pos;
471
+ while ((pos = strCookies.find(strCookie)) != std::string::npos) {
472
+ strCookies.erase(pos, strCookie.length());
473
+ }
474
+ snprintf(szBuf, const_buf_size, "Cookie:%s\r\n", strCookies.c_str());
475
+ strRequest += szBuf;
476
+ }
477
+ if (pCookieValue != NULL) {
478
+ OH_ArkWeb_ReleaseString(pCookieValue);
479
+ }
480
+ }
481
+ }
482
+ }
483
+
484
+ snprintf(szBuf, const_buf_size, "Keep-Alive:timeout=5, max=1000\r\n\r\n");
485
+ strRequest += szBuf;
486
+ ((CMemPool*)Application::g_memPool)->freePage(vecMemBuf);
487
+
488
+ ConnPoolManage* connPoolManage = (ConnPoolManage*)Application::g_connPoolManage;
489
+ ConnPool* pool = connPoolManage->getConnPool(strDomain);
490
+ if (pool == NULL) {
491
+ return "";
492
+ }
493
+
494
+ std::string strFileName = strTarget;
495
+ std::map<std::string, std::string> mapRespHeaders;
496
+ std::atomic<bool> isAbort{false};
497
+ if (isHttps) {
498
+ bool isClose = false;
499
+ SSL* ssl = NULL;
500
+ if (!pool->getSSL(&ssl, nPort)) {
501
+ OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "FileTransfer", "pool->getSocket(&ssl) == false");
502
+ } else if (!pool->sendWithTimeOut(ssl, strRequest, std::ref(isAbort))) {
503
+ OH_LOG_Print(LOG_APP,
504
+ LOG_ERROR,
505
+ LOG_PRINT_DOMAIN,
506
+ "FileTransfer",
507
+ "pool->sendWithTimeOut(ssl, strRequest) == false");
508
+ } else if (!pool->recvHttpWithTimeOut(ssl,
509
+ "GET",
510
+ strFileName,
511
+ mapRespHeaders,
512
+ "",
513
+ pBuf,
514
+ nLength,
515
+ isClose,
516
+ std::ref(isAbort),
517
+ nullptr)) {
518
+ OH_LOG_Print(LOG_APP,
519
+ LOG_ERROR,
520
+ LOG_PRINT_DOMAIN,
521
+ "FileTransfer",
522
+ "pool->recvHttpWithTimeOut(ssl, strFileName, pMaxBuffer, nBufSize, isReconnect) == false");
523
+ }
524
+ pool->freeSocket(ssl, isClose);
525
+ } else {
526
+ bool isClose = false;
527
+ int nSocket = -1;
528
+ if (!pool->getSocket(nSocket, nPort)) {
529
+ OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "FileTransfer", "pool->getSocket(nSocket) == false");
530
+ } else if (!pool->sendWithTimeOut(nSocket, strRequest, std::ref(isAbort))) {
531
+ OH_LOG_Print(LOG_APP,
532
+ LOG_ERROR,
533
+ LOG_PRINT_DOMAIN,
534
+ "FileTransfer",
535
+ "pool->sendWithTimeOut(nSocket, strRequest) == false");
536
+ } else if (!pool->recvHttpWithTimeOut(nSocket,
537
+ "GET",
538
+ strFileName,
539
+ mapRespHeaders,
540
+ "",
541
+ pBuf,
542
+ nLength,
543
+ isClose,
544
+ std::ref(isAbort),
545
+ nullptr)) {
546
+ OH_LOG_Print(LOG_APP,
547
+ LOG_ERROR,
548
+ LOG_PRINT_DOMAIN,
549
+ "FileTransfer",
550
+ "pool->recvHttpWithTimeOut(nSocket, strFileName, pMaxBuffer, nBufSize, isReconnect) == false");
551
+ }
552
+ pool->freeSocket(nSocket, true);
553
+ }
554
+
555
+ if (!FileCache::IsFile(strFileName, "")) {
556
+ return false;
557
+ }
558
+ return true;
559
+ }
560
+
561
+ bool Wechat::getUriFromRawfile(const std::string& strSource, std::string& strFilePath)
562
+ {
563
+ RawFile* rawfile = OH_ResourceManager_OpenRawFile(Application::g_resourceManager, strSource.c_str());
564
+ if (rawfile == nullptr) {
565
+ return false;
566
+ }
567
+ std::string strExt = ".jpg";
568
+ if (strSource.find_last_of(".") != std::string::npos) {
569
+ strExt = strSource.substr(strSource.find_last_of("."));
570
+ }
571
+ std::string strFileName = HttpUrl::generateMD5(strSource) + strExt;
572
+
573
+ std::string strTarget =
574
+ Application::g_databaseDir.substr(0, Application::g_databaseDir.find("database") - 1) + "/base/" + strFileName;
575
+ FILE* pFile = FileCache::openFile(strTarget, "");
576
+ if (pFile == nullptr) {
577
+ return false;
578
+ }
579
+ std::vector<SMemPage> vecMemPage;
580
+ const int blockSize = ((CMemPool*)Application::g_memPool)->getPageSize();
581
+ long consumed = 0;
582
+ ((CMemPool*)Application::g_memPool)->mallocPage(vecMemPage, 1);
583
+ unsigned char* buffer = (unsigned char*)vecMemPage[0].m_point;
584
+ while (true) {
585
+ OH_ResourceManager_SeekRawFile(rawfile, consumed, 0);
586
+ int ret = OH_ResourceManager_ReadRawFile(rawfile, buffer, blockSize);
587
+ if (ret == 0) {
588
+ break;
589
+ }
590
+ consumed += ret;
591
+ int nAlreadyWrite = 0;
592
+ while (true) {
593
+ int nRetSize = FileCache::writeFile(pFile, (const char*)buffer + nAlreadyWrite, ret);
594
+ nAlreadyWrite += nRetSize;
595
+ ret = ret - nRetSize;
596
+ if (ret == 0) {
597
+ break;
598
+ }
599
+ }
600
+ std::fill(buffer, buffer + blockSize, 0);
601
+ }
602
+ ((CMemPool*)Application::g_memPool)->freePage(vecMemPage);
603
+ FileCache::closeFile(pFile);
604
+ OH_ResourceManager_CloseRawFile(rawfile);
605
+ strFilePath = strTarget;
606
+ return true;
607
+ }