content.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 { replaceKeyword } 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. result.content.contentHighlighted = this.highlightContentHTML(result.content.content, data, result)
  38. }
  39. // 根据数据情况判断模块是否展示
  40. result.moduleShow.content = !!result.content?.content
  41. result.moduleShow.summary= Array.isArray(result.summary?.list) && result.summary?.list.length > 0
  42. return result
  43. }
  44. // 高亮标题
  45. highlightTitleHTML (title, data, formatted) {
  46. if (!title) return ''
  47. const { baseInfo } = data
  48. const projectName = baseInfo?.projectName
  49. const projectCode = baseInfo?.projectCode
  50. // 下划线高亮项目名称编号
  51. if (projectName && title.toLowerCase().indexOf(projectName.toLowerCase()) > -1) {
  52. title = replaceKeyword(title, projectName, '<span class="keyword keyword-underline project project-name hide-underline">$1</span>')
  53. }
  54. if (projectCode && title.toLowerCase().indexOf(projectCode.toLowerCase()) > -1) {
  55. title = replaceKeyword(title, projectCode, '<span class="keyword keyword-underline project project-code">$1</span>')
  56. }
  57. // ------------------
  58. // 关键词高亮
  59. let highlightKeys = []
  60. if (formatted.content && formatted.content.highlightKeys) {
  61. highlightKeys = formatted.content.highlightKeys
  62. }
  63. highlightKeys.forEach((key) => {
  64. title = replaceKeyword(title, key, '<span class="keyword highlight-text">$1</span>')
  65. })
  66. return title
  67. }
  68. // 高亮内容文本
  69. highlightContentHTML(content, data, formatted) {
  70. if (!content) return ''
  71. const { baseInfo } = data
  72. const { summary } = formatted
  73. const projectName = baseInfo?.projectName
  74. const projectCode = baseInfo?.projectCode
  75. content = content.replace(/[^\{\u4e00-\u9fa5]{1,90}{[^\}\u4e00-\u9fa5]+?}/g, '')
  76. // 将多个连续的br替换成一个
  77. content = content.replace(/(<br\s*\/?>)+/gi, '<br>')
  78. content = content.replace(/^((<br\/?>)|(&nbsp;))+/g, '')
  79. content = content.replace(/((<br\/?>)|(&nbsp;))+$/g, '')
  80. // 下划线高亮项目名称编号
  81. if(projectName && content.toLowerCase().indexOf(projectName.toLowerCase()) > -1){
  82. content = replaceKeyword(content, projectName, '<span class="keyword keyword-underline my-follow project project-name hide-underline">$1</span>')
  83. }
  84. if(projectCode && content.toLowerCase().indexOf(projectCode.toLowerCase()) > -1){
  85. content = replaceKeyword(content, projectCode, '<span class="keyword keyword-underline my-follow project project-code">$1</span>')
  86. }
  87. // 下划线高亮中标企业
  88. const winners = summary.winners
  89. if (Array.isArray(winners) && winners.length > 0) {
  90. for (let i = 0; i < winners.length; i++) {
  91. const winnerName = winners[i].name
  92. const winnerId = winners[i].id
  93. if (winnerName && content.toLowerCase().indexOf(winnerName.toLowerCase()) > -1) {
  94. content = replaceKeyword(content, winnerName, `<span data-eid='${winnerId}' class='keyword keyword-underline winner-name my-follow-ent'>$1</span>`)
  95. }
  96. }
  97. }
  98. // 表格兼容
  99. if(content.length > 10 && content.substring(0,6).toLowerCase() === '<tbody' && content.substring(content.length-8).toLowerCase() == '</tbody>'){
  100. content = '<table>' + content + '</table>'
  101. }
  102. // 表格外面添加盒子
  103. content = content.replace(/<table/g, `<div class="content-table-container"><table`).replace(/<\/table>/g, `</table></div>`)
  104. // ------------------
  105. // 关键词高亮
  106. let highlightKeys = []
  107. if (formatted.content && formatted.content.highlightKeys) {
  108. highlightKeys = formatted.content.highlightKeys
  109. }
  110. highlightKeys.forEach((key) => {
  111. content = replaceKeyword(content, key, '<span class="keyword highlight-text">$1</span>')
  112. })
  113. // 高亮第三方服务
  114. if (content) {
  115. content = thirdPartyVerify.replaceKeysAndInsertMark(content)
  116. }
  117. // freeView点击查看高亮
  118. // <span class="freeView">点击查看</span> ==替换==> <span class="freeView free-view highlight-text">点击查看</span>
  119. content = content.replace(/freeView/g, 'freeView free-view highlight-text')
  120. return content
  121. }
  122. }
  123. /**
  124. * /publicapply/detail/baseInfo
  125. * 基础信息接口数据模型转换
  126. */
  127. function useContentModel() {
  128. return new ContentModel()
  129. }
  130. export default useContentModel