content.js 5.9 KB

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