From c360c075b3df2d29ef6c3dc00759117ec9a05aa6 Mon Sep 17 00:00:00 2001 From: BrainL Date: Fri, 11 Jul 2025 10:43:04 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E6=B6=88=E9=99=A4=E5=91=8A=E8=AD=A6?= =?UTF-8?q?=EF=BC=8C=E6=8C=87=E9=92=88=E7=A9=BA=E6=B4=9E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: BrainL --- .../kits/picker/src/picker_n_exporter.cpp | 52 +++++++++++-------- 1 file changed, 31 insertions(+), 21 deletions(-) diff --git a/interfaces/kits/picker/src/picker_n_exporter.cpp b/interfaces/kits/picker/src/picker_n_exporter.cpp index 767a8f78..915ffebd 100644 --- a/interfaces/kits/picker/src/picker_n_exporter.cpp +++ b/interfaces/kits/picker/src/picker_n_exporter.cpp @@ -140,30 +140,16 @@ static void MakeResultWithBool(napi_env env, std::string key, napi_value &result } } -static void MakeResultWithUdkey(napi_env env, const std::string key, napi_value &result, - std::shared_ptr pickerCallBack) +void GetUriArray(napi_env env, napi_value &array, napi_status &status, std::vector unifiedDataSet) { - if (pickerCallBack == nullptr) { - HILOG_ERROR("[picker]: pickerCallBack is nullptr"); - return; - } - napi_value array; - napi_create_array(env, &array); - napi_status status = napi_generic_failure; - if (!pickerCallBack->want.GetParams().HasParam(key.c_str())) { - return; - } - const std::string udkey = pickerCallBack->want.GetStringParam(key.c_str()); - UDMF::QueryOption query = {.key = udkey}; - std::vector unifiedDataSet; - auto stat = UDMF::UdmfClient::GetInstance().GetBatchData(query, unifiedDataSet); - if (stat != UDMF::Status::E_OK || unifiedDataSet.empty()) { - HILOG_ERROR("[picker]: unifiedDataSet isEmpty or GetBatchData failed, stat=%{public}d", stat); - return; - } size_t len = unifiedDataSet[0].GetRecords().size(); + size_t position = 0; for (size_t i = 0; i < len; ++i) { auto readRecord = unifiedDataSet[0].GetRecordAt(i); + if (readRecord == nullptr) { + HILOG_ERROR("[picker]: readRecord is nullptr"); + return; + } auto entry = readRecord->GetEntry("general.file-uri"); if (!std::holds_alternative>(entry)) { HILOG_ERROR("[picker]: entry is not Object"); @@ -182,11 +168,35 @@ static void MakeResultWithUdkey(napi_env env, const std::string key, napi_value HILOG_ERROR("[picker]: create uri js value fail."); continue; } - status = napi_set_element(env, array, i, uriVal); + status = napi_set_element(env, array, position++, uriVal); if (status != napi_ok) { HILOG_ERROR("[picker]: napi_set_element failed, error: %{public}d", status); } } +} + +static void MakeResultWithUdkey(napi_env env, const std::string key, napi_value &result, + std::shared_ptr pickerCallBack) +{ + if (pickerCallBack == nullptr) { + HILOG_ERROR("[picker]: pickerCallBack is nullptr"); + return; + } + napi_value array; + napi_create_array(env, &array); + napi_status status = napi_generic_failure; + if (!pickerCallBack->want.GetParams().HasParam(key.c_str())) { + return; + } + const std::string udkey = pickerCallBack->want.GetStringParam(key.c_str()); + UDMF::QueryOption query = {.key = udkey}; + std::vector unifiedDataSet; + auto stat = UDMF::UdmfClient::GetInstance().GetBatchData(query, unifiedDataSet); + if (stat != UDMF::Status::E_OK || unifiedDataSet.empty()) { + HILOG_ERROR("[picker]: unifiedDataSet isEmpty or GetBatchData failed, stat=%{public}d", stat); + return; + } + GetUriArray(env, array, status,unifiedDataSet); status = napi_set_named_property(env, result, "ability_params_udkey", array); if (status != napi_ok) { HILOG_ERROR("[picker]: napi_set_named_property failed"); -- Gitee From 22f5d58d69c40aa4673376783c791f082149d1c7 Mon Sep 17 00:00:00 2001 From: BrainL Date: Fri, 11 Jul 2025 10:43:04 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E6=B6=88=E9=99=A4=E5=91=8A=E8=AD=A6?= =?UTF-8?q?=EF=BC=8C=E6=8C=87=E9=92=88=E7=A9=BA=E6=B4=9E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: BrainL --- .../kits/picker/src/picker_n_exporter.cpp | 53 +++++++++++-------- 1 file changed, 32 insertions(+), 21 deletions(-) diff --git a/interfaces/kits/picker/src/picker_n_exporter.cpp b/interfaces/kits/picker/src/picker_n_exporter.cpp index 767a8f78..2e8ec443 100644 --- a/interfaces/kits/picker/src/picker_n_exporter.cpp +++ b/interfaces/kits/picker/src/picker_n_exporter.cpp @@ -140,30 +140,16 @@ static void MakeResultWithBool(napi_env env, std::string key, napi_value &result } } -static void MakeResultWithUdkey(napi_env env, const std::string key, napi_value &result, - std::shared_ptr pickerCallBack) +void GetUriArray(napi_env env, std::vector unifiedDataSet, napi_value &array) { - if (pickerCallBack == nullptr) { - HILOG_ERROR("[picker]: pickerCallBack is nullptr"); - return; - } - napi_value array; - napi_create_array(env, &array); - napi_status status = napi_generic_failure; - if (!pickerCallBack->want.GetParams().HasParam(key.c_str())) { - return; - } - const std::string udkey = pickerCallBack->want.GetStringParam(key.c_str()); - UDMF::QueryOption query = {.key = udkey}; - std::vector unifiedDataSet; - auto stat = UDMF::UdmfClient::GetInstance().GetBatchData(query, unifiedDataSet); - if (stat != UDMF::Status::E_OK || unifiedDataSet.empty()) { - HILOG_ERROR("[picker]: unifiedDataSet isEmpty or GetBatchData failed, stat=%{public}d", stat); - return; - } size_t len = unifiedDataSet[0].GetRecords().size(); + size_t position = 0; for (size_t i = 0; i < len; ++i) { auto readRecord = unifiedDataSet[0].GetRecordAt(i); + if (readRecord == nullptr) { + HILOG_ERROR("[picker]: readRecord is nullptr"); + return; + } auto entry = readRecord->GetEntry("general.file-uri"); if (!std::holds_alternative>(entry)) { HILOG_ERROR("[picker]: entry is not Object"); @@ -182,11 +168,36 @@ static void MakeResultWithUdkey(napi_env env, const std::string key, napi_value HILOG_ERROR("[picker]: create uri js value fail."); continue; } - status = napi_set_element(env, array, i, uriVal); + auto status = napi_set_element(env, array, position, uriVal); + position++; if (status != napi_ok) { HILOG_ERROR("[picker]: napi_set_element failed, error: %{public}d", status); } } +} + +static void MakeResultWithUdkey(napi_env env, const std::string key, napi_value &result, + std::shared_ptr pickerCallBack) +{ + if (pickerCallBack == nullptr) { + HILOG_ERROR("[picker]: pickerCallBack is nullptr"); + return; + } + napi_value array; + napi_create_array(env, &array); + napi_status status = napi_generic_failure; + if (!pickerCallBack->want.GetParams().HasParam(key.c_str())) { + return; + } + const std::string udkey = pickerCallBack->want.GetStringParam(key.c_str()); + UDMF::QueryOption query = {.key = udkey}; + std::vector unifiedDataSet; + auto stat = UDMF::UdmfClient::GetInstance().GetBatchData(query, unifiedDataSet); + if (stat != UDMF::Status::E_OK || unifiedDataSet.empty()) { + HILOG_ERROR("[picker]: unifiedDataSet isEmpty or GetBatchData failed, stat=%{public}d", stat); + return; + } + GetUriArray(env, unifiedDataSet, array); status = napi_set_named_property(env, result, "ability_params_udkey", array); if (status != napi_ok) { HILOG_ERROR("[picker]: napi_set_named_property failed"); -- Gitee