修复接收乱码Bug
authorxue <2144439972@qq.com>
Mon, 8 Dec 2025 08:56:54 +0000 (16:56 +0800)
committerxue <2144439972@qq.com>
Mon, 8 Dec 2025 08:56:54 +0000 (16:56 +0800)
code/sdk/application/wearable/ln/nativeapp/nativeui/thunderbird/src/ThunderBirdModel.cpp
code/sdk/application/wearable/ln/nativeapp/nativeui/uiservice/src/ui_service.cpp

index 297f655..3b28e37 100644 (file)
@@ -3056,11 +3056,13 @@ extern "C" {
     // NPC表情变化处理函数
     void ThunderBirdModel::HandleNpcEmotionChange(const char* emotion)
     {
+        //用string来接收表情
+        std::string emotionStr(emotion);
         //return;//确保稳定性该版本不使用NPC表情变化
-        WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "ThunderBirdModel::HandleNpcEmotionChange: %s", emotion ? emotion : "NULL");
+        WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "ThunderBirdModel::HandleNpcEmotionChange: %s", emotionStr.c_str());
         
-        if (emotion == nullptr || strlen(emotion) == 0) {
-            WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "ThunderBirdModel::HandleNpcEmotionChange: emotion is null or empty");
+        if (emotionStr.empty()) {
+            WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "ThunderBirdModel::HandleNpcEmotionChange: emotion is empty");
             return;
         }
         
@@ -3072,33 +3074,33 @@ extern "C" {
         if(ThunderBirdDialogueView::GetInstance() && currentSubView == ThunderBirdDialogueView::THUNDERBIRD_DIALOGUEVIEW_SPEAKING)
         {
             // 检查传入的表情是否与当前对话界面表情不同
-            if (m_dialogueExpression != emotion) {
+            if (m_dialogueExpression != emotionStr) {
                 // 记录新的对话界面表情
-                WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "ThunderBirdModel::HandleNpcEmotionChange: 记录新的对话界面表情: %s", emotion);
+                WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "ThunderBirdModel::HandleNpcEmotionChange: 记录新的对话界面表情: %s", emotionStr.c_str());
                 WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "ThunderBirdModel::HandleNpcEmotionChange: 当前对话界面表情: %s", m_dialogueExpression.c_str()); 
-                m_dialogueExpression = emotion;
+                m_dialogueExpression = emotionStr;
                 
                 // 切换表情 - 根据眼睛颜色和形状判断是否为默认表情
                 std::string expressionPath;
                 if(eyesColor_.empty() == true || eyesShape_.empty() == true) {
                     // 默认表情
-                    std::string emotionFileName = MapEmotionToFileName(emotion);
+                    std::string emotionFileName = MapEmotionToFileName(emotionStr.c_str());
                     expressionPath = "/user/res/video/Default/" + emotionFileName + ".mp4";
                 } else {
                     // 非默认表情
-                    if(strcmp(emotion, "winking") == 0) {
+                    if(emotionStr == "winking") {
                         // 如果为眨眼表情,则在默认文件目录里面
                         // expressionPath = "/user/res/video/CloseEyes/" + eyesShape_ + "/" + eyesColor_ + ".mp4";
                         return;
                     } else {
                         // 非眨眼表情,则在下载文件目录里面
-                        if(strcmp(emotion, "sleeping") == 0)
+                        if(emotionStr == "sleeping")
                         {
                             expressionPath = "/user/res/video/Default/sleeping.mp4";
                         }
                         else
                         {
-                            expressionPath = "/user/userdata/leiniao/emoji/" + eyesShape_ + "/" + eyesColor_ + "/" + emotion + ".mp4";
+                            expressionPath = "/user/userdata/leiniao/emoji/" + eyesShape_ + "/" + eyesColor_ + "/" + emotionStr + ".mp4";
                         }
                     }
                 }
@@ -3117,7 +3119,7 @@ extern "C" {
             } 
             else
             {
-             WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "NPC表情未变化,跳过更新: %s", emotion);
+             WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "NPC表情未变化,跳过更新: %s", emotionStr.c_str());
             }
         }
         else//其他界面,则为时间线的状态表情变化
index 3de60b6..e179aa7 100644 (file)
@@ -747,11 +747,41 @@ void HandAudioRecordingEndNotify(Request *request)
 void HandNpcEmotionChangeNotify(Request *request)
 {
     WEARABLE_LOGE(WEARABLE_LOG_MODULE_UI_SERVICE, "HandNpcEmotionChangeNotify msg_id = %d", request->msgId);
-    std::string emotion(static_cast<char*>(request->data), request->len);
-    WEARABLE_LOGE(WEARABLE_LOG_MODULE_UI_SERVICE, "HandNpcEmotionChangeNotify emotion = %s", emotion.c_str());
-    GraphicService::GetInstance()->PostGraphicEvent(
-        std::bind(&OHOS::ThunderBirdModel::HandleNpcEmotionChange,OHOS::ThunderBirdModel::GetInstance(),const_cast<char*>(emotion.c_str())));
+    WEARABLE_LOGE(WEARABLE_LOG_MODULE_UI_SERVICE, "HandNpcEmotionChangeNotify len = %d", request->len);
+    
+        // 检查数据有效性
+        if (request->data == nullptr || request->len <= 0) {
+            WEARABLE_LOGE(WEARABLE_LOG_MODULE_UI_SERVICE, "HandNpcEmotionChangeNotify: invalid data or length");
+            return;
+        }
+        
+        // 确保字符串以null结尾,避免乱码
+        std::string value;
+        if (request->len > 0) {
+            const char* data = static_cast<const char*>(request->data);
+            if (data[request->len - 1] == '\0') {
+                value = std::string(data, request->len - 1);
+            } else {
+                value = std::string(data, request->len);
+            }
+        }
+        
+        // 验证字符串是否包含有效字符
+        if (value.empty() || value.find_first_not_of(" \t\n\r") == std::string::npos) {
+            WEARABLE_LOGE(WEARABLE_LOG_MODULE_UI_SERVICE, "HandNpcEmotionChangeNotify: text string is empty or contains only whitespace");
+            return;
+        }
+        
+        WEARABLE_LOGE(WEARABLE_LOG_MODULE_UI_SERVICE, "HandNpcEmotionChangeNotify emotion = %s", value.c_str());
+        
+        // 创建字符串副本,避免const_cast问题
+        std::string emotionCopy = value;    
+        GraphicService::GetInstance()->PostGraphicEvent(
+            [emotionCopy]() {
+                OHOS::ThunderBirdModel::GetInstance()->HandleNpcEmotionChange(const_cast<char*>(emotionCopy.c_str()));
+            });
 }
+
 // 通知时间线状态变化
 void HandTimelineStatusChangeNotify(Request *request)
 {