瀏覽代碼

Merge branch 'feature/v1.0.29' of https://jygit.jydev.jianyu360.cn/jianyu/web into feature/v1.0.29

zhangyuhan 1 年之前
父節點
當前提交
abe28a3d7f

+ 80 - 0
data/data-models/modules/article/model/content.js

@@ -1,6 +1,7 @@
 import BaseModel from '../../../core/base'
 import useSummaryModel from '../transform/summary'
 import useCommonTitleModel from '../transform/content'
+import { replaceKeyword } from '@jy/util'
 
 class ContentModel extends BaseModel {
   constructor(config) {
@@ -23,8 +24,87 @@ class ContentModel extends BaseModel {
     if (data?.abstract) {
       result.summary = useSummaryModel().transformModel(data)
     }
+
+    if (result.content) {
+      result.content.titleHighlighted = this.highlightTitleHTML(result.content.title, data, result)
+      result.content.contentHighlighted = this.highlightContentHTML(result.content.content, data, result)
+    }
     return result
   }
+
+  // 高亮标题
+  highlightTitleHTML (title, data, formatted) {
+    if (!title) return ''
+    const { baseInfo } = data
+    const projectName = baseInfo?.projectName
+    const projectCode = baseInfo?.projectCode
+    // 下划线高亮项目名称编号
+    if (projectName && title.toLowerCase().indexOf(projectName.toLowerCase()) > -1) {
+      title = replaceKeyword(title, projectName, '<span class="keyword keyword-underline project project-name hide-underline">$1</span>')
+    }
+    if (projectCode && title.toLowerCase().indexOf(projectCode.toLowerCase()) > -1) {
+      title = replaceKeyword(title, projectCode, '<span class="keyword keyword-underline project project-code">$1</span>')
+    }
+
+    // ------------------
+    // 关键词高亮
+    let highlightKeys = []
+    if (formatted.content && formatted.content.highlightKeys) {
+      highlightKeys = formatted.content.highlightKeys
+    }
+    highlightKeys.forEach((key) => {
+      title = replaceKeyword(title, key, '<span class="keyword highlight-text">$1</span>')
+    })
+    return title
+  }
+  
+  // 高亮内容文本
+  highlightContentHTML(content, data, formatted) {
+    if (!content) return ''
+    const { baseInfo } = data
+    const { summary } = formatted
+    const projectName = baseInfo?.projectName
+    const projectCode = baseInfo?.projectCode
+
+    content = content.replace(/[^\{\u4e00-\u9fa5]{1,90}{[^\}\u4e00-\u9fa5]+?}/g, '')
+    // 高亮第三方服务
+    if (content) {
+      // content = thirdPartyVerify.replaceKeys(content)
+    }
+    // 下划线高亮项目名称编号
+    if(projectName && content.toLowerCase().indexOf(projectName.toLowerCase()) > -1){
+      content = replaceKeyword(content, projectName, '<span class="keyword keyword-underline my-follow project project-name hide-underline">$1</span>')
+    }
+    if(projectCode && content.toLowerCase().indexOf(projectCode.toLowerCase()) > -1){
+      content = replaceKeyword(content, projectCode, '<span class="keyword keyword-underline my-follow project project-code">$1</span>')
+    }
+    // 下划线高亮中标企业
+    const winners = summary.winners
+    if (Array.isArray(winners) && winners.length > 0) {
+      for (let i = 0; i < winners.length; i++) {
+        const winnerName = winners[i].name
+        const winnerId = winners[i].id
+        if (winnerName && content.toLowerCase().indexOf(winnerName.toLowerCase()) > -1) {
+          content = replaceKeyword(content, winnerName, `<span data-eid='${winnerId}' class='keyword keyword-underline winner-name my-follow-ent'>$1</span>`)
+        }
+      }
+    }
+    // 表格兼容
+    if(content.length > 10 && content.substring(0,6).toLowerCase() === '<tbody' && content.substring(content.length-8).toLowerCase() == '</tbody>'){
+      content = '<table>' + content + '</table>'
+    }
+
+    // ------------------
+    // 关键词高亮
+    let highlightKeys = []
+    if (formatted.content && formatted.content.highlightKeys) {
+      highlightKeys = formatted.content.highlightKeys
+    }
+    highlightKeys.forEach((key) => {
+      content = replaceKeyword(content, key, '<span class="keyword highlight-text">$1</span>')
+    })
+    return content
+  }
 }
 
 /**

+ 36 - 3
data/data-models/modules/article/transform/content.js

@@ -1,5 +1,5 @@
 import BaseModel from '../../../core/base'
-import { dateFormatter, formatMoney } from '@jy/util'
+import { dateFormatter, formatMoney, getQueryParam } from '@jy/util'
 
 class CommonContentModel extends BaseModel {
   constructor(config) {
@@ -14,11 +14,15 @@ class CommonContentModel extends BaseModel {
       projectName: '',
       projectCode: '',
       // 高亮词组
-      keys: [],
+      highlightKeys: [],
       tags: [],
+      titleHighlighted: '',
+      contentHighlighted: '',
       time: '',
       // 计算信息
       isSelfSite: false,
+      // 正文
+      content: '',
       // TDK
       tdk: {
         title: '',
@@ -31,7 +35,7 @@ class CommonContentModel extends BaseModel {
 
   formatModel(data, isInit = false) {
     const result = this.createModel()
-    const { baseInfo } = data
+    const { baseInfo, detailInfo } = data
     result.id = baseInfo.id
     result.title = baseInfo.title
     result.projectName = baseInfo?.projectName
@@ -40,6 +44,9 @@ class CommonContentModel extends BaseModel {
     result.time = baseInfo?.publishTime
       ? dateFormatter(baseInfo.publishTime * 1000, 'yyyy-MM-dd')
       : ''
+    result.highlightKeys = this.splitUrlKeys()
+    result.content = detailInfo.detail || ''
+
     // TDK
     result.tdk.title = baseInfo.title
     result.tdk.description = baseInfo?.description
@@ -90,6 +97,32 @@ class CommonContentModel extends BaseModel {
 
     return tags
   }
+
+  // 获取url高亮字符, 并截取
+  splitUrlKeys() {
+    const kds = getQueryParam('kds')
+    const keywords = getQueryParam('keywords')
+    const s_words = kds || keywords
+    let keysList = []
+
+    // 移动端订阅预览等跳转过来传的词
+    if (/^(subkey_)/.test(s_words)) {
+      keysList = s_words.replace(/^(subkey_)/, '').split('_')
+      let keyListArr = []
+      keysList.forEach((v) => {
+        const vArr = v.split('+')
+        keyListArr = keyListArr.concat(vArr)
+      })
+    } else {
+      const reg = /[_\+\s+]/ // 匹配 _+空格
+      if (reg.test(s_words)) {
+        keysList = s_words.split(reg).filter(w => !!w)
+      } else {
+        keysList.push(s_words)
+      }
+    }
+    return keysList
+  }
 }
 
 function useCommonContentModel() {

+ 5 - 0
data/data-models/modules/article/transform/summary.js

@@ -58,6 +58,8 @@ class SummaryModel extends BaseModel {
 
   createModel() {
     return {
+      // 原始数据
+      originMap: {},
       list: [],
       // 采购单位
       buyers: [],
@@ -166,6 +168,7 @@ class SummaryModel extends BaseModel {
     )
 
     result.list = list
+    result.originMap = summary
 
     return result
   }
@@ -212,6 +215,8 @@ class SummaryModel extends BaseModel {
     })
 
     result.list = summaryItem.createList()
+    result.originMap = summary
+
     return result
   }
 }

+ 36 - 0
packages/util/modules/format/str.js

@@ -64,3 +64,39 @@ export function replaceKeyword(
     return value.replace(regExp, newChar)
   }
 }
+
+/**
+ * 从key=value&key1=value1&key2=value2...中获取key值
+ * @param {String} formString 目标字符串,必须为key=value&key1=value1的格式
+ * @param {String} targetKey 要从目标字符串获取哪个key
+ * @returns {String}
+ */
+export function getFormValue(formString, targetKey) {
+  let reg = new RegExp('(^|&)' + targetKey + '=([^&]*)(&|$)', 'i')
+  let r = formString.match(reg) //获取url中'?'符后的字符串并正则匹配
+
+  let context = ''
+  if (r != null) context = r[2]
+  // 释放变量
+  reg = null
+  r = null
+  return context == null || context == '' || context == 'undefined' ? '' : context
+}
+
+/**
+ * 从url中获取对应name的参数
+ * @param {String} name 
+ * @returns {String}
+ */
+export function getQueryParam(name) {
+  const search = window.location.search
+  let target = ''
+  if (search) {
+    const arr = search.split('?')
+    if (arr[1]) {
+      target = arr[1]
+    }
+  }
+  const value = getFormValue(target, name)
+  return decodeURIComponent(value)
+}