sunshibo 4 дней назад
Родитель
Сommit
faa9d1394f
1 измененных файлов с 189 добавлено и 1 удалено
  1. 189 1
      HBuilderProjects/shareAiConsult.html

+ 189 - 1
HBuilderProjects/shareAiConsult.html

@@ -377,6 +377,8 @@
       var APP_ANDROID_PACKAGE = "com.alien.Udianzaizhe";
       var APP_IOS_URL_SCHEME = "shopro://";
       var APP_UNI_AI_PATH = "pages/aiSearchResult/index";
+      var AI_CONVERSATION_API =
+        "http://183.252.196.135:9100/ai/life-manager/api/v1/legal-consultation/history/conversation";
 
       var sharePayloadCache = null;
       var weChatJssdkConfigured = false;
@@ -497,6 +499,192 @@
         }
       }
 
+      function getConversationQueryParams() {
+        var sessionId = String(q("session_id") || q("sessionId") || "").trim();
+        var userId = String(q("user_id") || q("userId") || "").trim();
+        if (!sessionId || !userId) return null;
+        return { sessionId: sessionId, userId: userId };
+      }
+
+      function buildConversationApiUrl(sessionId, userId) {
+        var params = new URLSearchParams();
+        params.set("session_id", sessionId);
+        params.set("user_id", userId);
+        return AI_CONVERSATION_API + "?" + params.toString();
+      }
+
+      function normalizeConversationRole(raw) {
+        var role = String(raw || "").trim().toLowerCase();
+        if (
+          role === "user" ||
+          role === "human" ||
+          role === "customer" ||
+          role === "question" ||
+          role === "1" ||
+          role === "我"
+        ) {
+          return "user";
+        }
+        if (
+          role === "ai" ||
+          role === "assistant" ||
+          role === "bot" ||
+          role === "ubao" ||
+          role === "answer" ||
+          role === "2" ||
+          role === "system"
+        ) {
+          return "ai";
+        }
+        return "";
+      }
+
+      function normalizeConversationApiPayload(res) {
+        if (!res || typeof res !== "object") return null;
+        if (res.code != null) {
+          var code = Number(res.code);
+          if (code !== 200 && code !== 0 && res.success !== true) {
+            throw new Error(res.msg || res.message || "对话接口返回 code " + code);
+          }
+        }
+
+        var root = res.data != null && typeof res.data === "object" ? res.data : res;
+        var rawList =
+          (Array.isArray(root) && root) ||
+          (Array.isArray(root.messages) && root.messages) ||
+          (Array.isArray(root.messageList) && root.messageList) ||
+          (Array.isArray(root.conversation) && root.conversation) ||
+          (Array.isArray(root.conversations) && root.conversations) ||
+          (Array.isArray(root.records) && root.records) ||
+          (Array.isArray(root.list) && root.list) ||
+          null;
+
+        if (!rawList) return null;
+
+        var messages = [];
+        for (var i = 0; i < rawList.length; i++) {
+          var item = rawList[i];
+          if (!item || typeof item !== "object") continue;
+
+          var role = normalizeConversationRole(
+            item.role || item.sender || item.messageRole || item.type || item.messageType || item.senderType
+          );
+          var content = String(
+            item.content != null
+              ? item.content
+              : item.message != null
+                ? item.message
+                : item.text != null
+                  ? item.text
+                  : ""
+          ).trim();
+
+          if (!role && item.question) {
+            role = "user";
+            content = String(item.question).trim();
+          } else if (!role && item.answer) {
+            role = "ai";
+            content = String(item.answer).trim();
+          } else if (!role && item.userMessage) {
+            role = "user";
+            content = String(item.userMessage).trim();
+          } else if (!role && (item.aiMessage || item.assistantMessage)) {
+            role = "ai";
+            content = String(item.aiMessage || item.assistantMessage).trim();
+          }
+
+          if (!content) continue;
+
+          var imageUrl = item.imageUrl || item.image || item.imgUrl || item.url;
+          if (imageUrl && /^(https?:)?\/\//i.test(String(imageUrl))) {
+            messages.push({
+              role: role || "user",
+              isImage: true,
+              imageUrl: String(imageUrl),
+            });
+            continue;
+          }
+
+          messages.push({
+            role: role || (messages.length % 2 === 0 ? "user" : "ai"),
+            content: content,
+          });
+        }
+
+        if (!messages.length) return null;
+
+        return {
+          messages: messages,
+          shareTime:
+            root.shareTime ||
+            root.createdAt ||
+            root.createTime ||
+            root.updateTime ||
+            root.timestamp ||
+            res.timestamp,
+          firstQuestion: root.firstQuestion || root.title || root.topic || "",
+          sessionId: root.sessionId || root.session_id || "",
+          userId: root.userId || root.user_id || "",
+        };
+      }
+
+      function fetchConversationHistory(sessionId, userId) {
+        var requestUrl = buildConversationApiUrl(sessionId, userId);
+        console.log("[conversation-api] GET", requestUrl);
+        return fetch(requestUrl, {
+          method: "GET",
+          mode: "cors",
+          credentials: "omit",
+          headers: { Accept: "application/json" },
+        })
+          .then(function (r) {
+            if (r.ok) return r.json();
+            return r
+              .text()
+              .catch(function () {
+                return "";
+              })
+              .then(function (text) {
+                var hint = "";
+                try {
+                  var j = JSON.parse(text);
+                  hint = j.msg || j.message || "";
+                } catch (eP) {
+                  if (text) hint = text.slice(0, 120);
+                }
+                throw new Error("对话接口 HTTP " + r.status + (hint ? ":" + hint : ""));
+              });
+          })
+          .then(function (res) {
+            var data = normalizeConversationApiPayload(res);
+            if (!data) {
+              throw new Error("对话接口返回数据为空或格式无法解析");
+            }
+            return data;
+          });
+      }
+
+      function loadPageData() {
+        var main = document.getElementById("main");
+        var params = getConversationQueryParams();
+        if (!params) {
+          render(parsePayload());
+          return;
+        }
+
+        main.innerHTML = '<div class="empty">对话内容加载中…</div>';
+        fetchConversationHistory(params.sessionId, params.userId)
+          .then(function (data) {
+            render(data);
+          })
+          .catch(function (e) {
+            var tip = e && e.message ? e.message : "对话加载失败,请稍后重试";
+            console.warn("[conversation-api]", tip, e);
+            main.innerHTML = '<div class="empty">' + escHtml(tip) + "</div>";
+            refreshWxLaunchTagAttrs();
+          });
+      }
+
       function getFirstUserQuestion(messages) {
         var list = Array.isArray(messages) ? messages : [];
         for (var i = 0; i < list.length; i++) {
@@ -1328,7 +1516,7 @@
           }
         }
 
-        render(parsePayload());
+        loadPageData();
       }
 
       if (document.readyState === "complete") {