Procházet zdrojové kódy

feat: 需求调整为最快匹配词展示一次引流

zhangyuhan před 1 měsícem
rodič
revize
c04f8552af
1 změnil soubory, kde provedl 41 přidání a 4 odebrání
  1. 41 4
      data/data-models/modules/article/model/content.js

+ 41 - 4
data/data-models/modules/article/model/content.js

@@ -146,13 +146,50 @@ class ContentModel extends BaseModel {
     // 插入画像引流关键词
     const canType = ['招标', '采购意向', '预告']
     if (content && canType.includes(baseInfo.topType)) {
+
+      function findFirstMatchedKeyword(guideKeyItemKeys, content) {
+        // 检查guideKeyItemKeys和content是否有效
+        if (!Array.isArray(guideKeyItemKeys) || typeof content !== 'string') {
+          return null;
+        }
+
+        // 按照关键词长度排序
+        const sortedKeys = [...guideKeyItemKeys].sort((a, b) => a.length - b.length);
+
+        for (const key of sortedKeys) {
+          const index = content.indexOf(key);
+          if (index !== -1) {
+            return {
+              keyword: key,
+              position: index
+            };
+          }
+        }
+
+        return null; // 没有找到匹配的关键词
+      }
+
+      function insertAtPosition(content, keyword, position, newContent) {
+        // 检查 position 是否在有效范围内
+        if (position < 0 || position > content.length) {
+          console.log('位置无效');
+          return content;
+        }
+
+        // 使用 slice 方法在指定位置插入新内容
+        return content.slice(0, position) + newContent.replace(/\$1/g, keyword) + content.slice(position + keyword.length);
+      }
+
+
+
       for (const guideKey in this._highlightGuideKeyMaps) {
         const guideKeyItem = this._highlightGuideKeyMaps[guideKey]
         if (Array.isArray(guideKeyItem?.keys)) {
-          for (let i = 0; i < guideKeyItem.keys.length; i++) {
-            const rKey = guideKeyItem.keys[i]
-            const rRich = typeof guideKeyItem.fn === 'function' ? guideKeyItem.fn({ rKey, guideKey, buyer: summary.originMap.buyer }) : '<span class="highlight-text">$1</span>'
-            content = replaceKeywordWithRichText(content, rKey, rRich)
+          // 找到最快匹配的词
+          const result = findFirstMatchedKeyword(guideKeyItem.keys, content)
+          const rRich = typeof guideKeyItem.fn === 'function' ? guideKeyItem.fn({ buyer: summary.originMap.buyer }) : '<span class="highlight-text">$1</span>'
+          if (result?.position) {
+            content = insertAtPosition(content, result.keyword, result.position, rRich)
           }
         }
       }