content.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. import BaseModel from '../../../core/base'
  2. import useSummaryModel from '../transform/summary2'
  3. import useCommonTitleModel from '../transform/content'
  4. import useThirdPartyVerifyModel from '../transform/third-party-verify'
  5. import { replaceKeywordWithRichText } from '@jy/util'
  6. const thirdPartyVerify = useThirdPartyVerifyModel()
  7. class ContentModel extends BaseModel {
  8. // 引流关键词组
  9. _highlightGuideKeyMaps = {}
  10. constructor(config) {
  11. super(config)
  12. }
  13. createModel() {
  14. return {
  15. moduleShow: {
  16. // 正文模块是否显示
  17. content: false,
  18. // 摘要模块是否显示
  19. summary: false
  20. },
  21. content: useCommonTitleModel().createModel(),
  22. summary: useSummaryModel().createModel(),
  23. }
  24. }
  25. // 关键词组替换
  26. addGuideKey (desc, keys, fn) {
  27. this._highlightGuideKeyMaps[desc] = {
  28. keys,
  29. fn
  30. }
  31. }
  32. formatModel(data, isInit = false) {
  33. const result = this.createModel()
  34. const commonTitleModel = useCommonTitleModel()
  35. const summaryModel = useSummaryModel()
  36. // 基础信息
  37. if (data?.baseInfo) {
  38. result.content = commonTitleModel.transformModel(data)
  39. }
  40. // 摘要
  41. if (data?.abstract) {
  42. result.summary = summaryModel.transformModel(data)
  43. }
  44. if (result.content) {
  45. result.content.titleHighlighted = this.highlightTitleHTML(result.content.title, data, result)
  46. try {
  47. const _summary = summaryModel.getSummaryContentObject(data)
  48. result.content._summary = _summary
  49. } catch (error) {
  50. console.log(error)
  51. }
  52. try {
  53. result.content.contentHighlighted = this.highlightContentHTML(result.content.content, data, result)
  54. } catch (error) {
  55. console.error(error)
  56. }
  57. }
  58. // 根据数据情况判断模块是否展示
  59. result.moduleShow.content = !!result.content?.content
  60. result.moduleShow.summary= Array.isArray(result.summary?.list) && result.summary?.list.length > 0
  61. return result
  62. }
  63. // 高亮标题
  64. highlightTitleHTML (title, data, formatted) {
  65. if (!title) return ''
  66. const { baseInfo } = data
  67. const projectName = baseInfo?.projectName
  68. const projectCode = baseInfo?.projectCode
  69. // 下划线高亮项目名称编号
  70. if (projectName && title.toLowerCase().indexOf(projectName.toLowerCase()) > -1) {
  71. title = replaceKeywordWithRichText(title, projectName, '<span class="keyword keyword-underline project project-name hide-underline">$1</span>')
  72. }
  73. if (projectCode && title.toLowerCase().indexOf(projectCode.toLowerCase()) > -1) {
  74. title = replaceKeywordWithRichText(title, projectCode, '<span class="keyword keyword-underline project project-code">$1</span>')
  75. }
  76. // ------------------
  77. // 关键词高亮
  78. let highlightKeys = []
  79. if (formatted.content && formatted.content.highlightKeys) {
  80. highlightKeys = formatted.content.highlightKeys
  81. }
  82. highlightKeys.forEach((key) => {
  83. title = replaceKeywordWithRichText(title, key, '<span class="keyword highlight-text-orange-bd">$1</span>')
  84. })
  85. return title
  86. }
  87. // 高亮内容文本
  88. highlightContentHTML(content, data, formatted) {
  89. if (!content) return ''
  90. const { baseInfo } = data
  91. const { summary } = formatted
  92. const projectName = baseInfo?.projectName
  93. const projectCode = baseInfo?.projectCode
  94. content = content.replace(/[^\{\u4e00-\u9fa5]{1,90}{[^\}\u4e00-\u9fa5]+?}/g, '').trim()
  95. let tempNode = document.createElement('div')
  96. tempNode.innerHTML = content
  97. // 下划线高亮项目名称编号
  98. if(projectName && content.toLowerCase().indexOf(projectName.toLowerCase()) > -1){
  99. content = replaceKeywordWithRichText(content, projectName, '<span class="keyword keyword-underline my-follow project project-name hide-underline">$1</span>')
  100. }
  101. if(projectCode && content.toLowerCase().indexOf(projectCode.toLowerCase()) > -1){
  102. content = replaceKeywordWithRichText(content, projectCode, '<span class="keyword keyword-underline my-follow project project-code">$1</span>')
  103. }
  104. // 下划线高亮中标企业
  105. const winners = summary.winners
  106. if (Array.isArray(winners) && winners.length > 0) {
  107. for (let i = 0; i < winners.length; i++) {
  108. const winnerName = winners[i].name
  109. const winnerId = winners[i].id
  110. if (winnerName && content.toLowerCase().indexOf(winnerName.toLowerCase()) > -1) {
  111. content = replaceKeywordWithRichText(content, winnerName, `<span data-eid='${winnerId}' class='keyword keyword-underline winner-name my-follow-ent'>$1</span>`)
  112. }
  113. }
  114. }
  115. // 表格兼容
  116. if(content.length > 10 && content.substring(0,6).toLowerCase() === '<tbody' && content.substring(content.length-8).toLowerCase() == '</tbody>'){
  117. content = '<table>' + content + '</table>'
  118. }
  119. // 表格外面添加盒子
  120. content = content.replace(/<table/g, `<div class="content-table-container"><table`).replace(/<\/table>/g, `</table></div>`)
  121. // 高亮第三方服务
  122. if (content) {
  123. content = thirdPartyVerify.replaceKeysAndInsertMark(content)
  124. }
  125. // 插入画像引流关键词
  126. if (content && baseInfo) {
  127. for (const guideKey in this._highlightGuideKeyMaps) {
  128. const guideKeyItem = this._highlightGuideKeyMaps[guideKey]
  129. if (Array.isArray(guideKeyItem?.keys)) {
  130. for (let i = 0; i < guideKeyItem.keys.length; i++) {
  131. const rKey = guideKeyItem.keys[i]
  132. const rRich = typeof guideKeyItem.fn === 'function' ? guideKeyItem.fn({ rKey, guideKey, buyer: summary.originMap.buyer }) : '<span class="highlight-text">$1</span>'
  133. content = replaceKeywordWithRichText(content, rKey, rRich)
  134. }
  135. }
  136. }
  137. }
  138. // ------------------
  139. // 关键词高亮
  140. let highlightKeys = []
  141. if (formatted.content && formatted.content.highlightKeys) {
  142. highlightKeys = formatted.content.highlightKeys
  143. }
  144. highlightKeys.forEach((key) => {
  145. content = replaceKeywordWithRichText(content, key, '<span class="keyword highlight-text-orange-bd">$1</span>')
  146. })
  147. // 将多个连续的br替换成一个
  148. content = content.replace(/(<br\s*\/?>)+/gi, '<br>')
  149. content = content.replace(/(<br>\s*)+/gi, '<br>')
  150. content = content.replace(/^((<br\/?>)|(&nbsp;))+/g, '')
  151. content = content.replace(/((<br\/?>)|(&nbsp;))+$/g, '')
  152. // freeView点击查看高亮
  153. // <span class="freeView">点击查看</span> ==替换==> <span class="freeView free-view highlight-text">点击查看</span>
  154. content = content.replace(/freeView/g, 'freeView free-view highlight-text')
  155. return content
  156. }
  157. }
  158. /**
  159. * /publicapply/detail/baseInfo
  160. * 基础信息接口数据模型转换
  161. */
  162. function useContentModel() {
  163. return new ContentModel()
  164. }
  165. export default useContentModel