content.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. import BaseModel from '../../../core/base'
  2. import useSummaryModel from '../transform/summary'
  3. import useCommonTitleModel from '../transform/content'
  4. import { replaceKeyword } from '@jy/util'
  5. class ContentModel extends BaseModel {
  6. constructor(config) {
  7. super(config)
  8. }
  9. createModel() {
  10. return {
  11. content: useCommonTitleModel().createModel(),
  12. summary: useSummaryModel().createModel()
  13. }
  14. }
  15. formatModel(data, isInit = false) {
  16. const result = this.createModel()
  17. // 基础信息
  18. if (data?.baseInfo) {
  19. result.content = useCommonTitleModel().transformModel(data)
  20. }
  21. // 摘要
  22. if (data?.abstract) {
  23. result.summary = useSummaryModel().transformModel(data)
  24. }
  25. if (result.content) {
  26. result.content.titleHighlighted = this.highlightTitleHTML(result.content.title, data, result)
  27. result.content.contentHighlighted = this.highlightContentHTML(result.content.content, data, result)
  28. }
  29. return result
  30. }
  31. // 高亮标题
  32. highlightTitleHTML (title, data, formatted) {
  33. if (!title) return ''
  34. const { baseInfo } = data
  35. const projectName = baseInfo?.projectName
  36. const projectCode = baseInfo?.projectCode
  37. // 下划线高亮项目名称编号
  38. if (projectName && title.toLowerCase().indexOf(projectName.toLowerCase()) > -1) {
  39. title = replaceKeyword(title, projectName, '<span class="keyword keyword-underline project project-name hide-underline">$1</span>')
  40. }
  41. if (projectCode && title.toLowerCase().indexOf(projectCode.toLowerCase()) > -1) {
  42. title = replaceKeyword(title, projectCode, '<span class="keyword keyword-underline project project-code">$1</span>')
  43. }
  44. // ------------------
  45. // 关键词高亮
  46. let highlightKeys = []
  47. if (formatted.content && formatted.content.highlightKeys) {
  48. highlightKeys = formatted.content.highlightKeys
  49. }
  50. highlightKeys.forEach((key) => {
  51. title = replaceKeyword(title, key, '<span class="keyword highlight-text">$1</span>')
  52. })
  53. return title
  54. }
  55. // 高亮内容文本
  56. highlightContentHTML(content, data, formatted) {
  57. if (!content) return ''
  58. const { baseInfo } = data
  59. const { summary } = formatted
  60. const projectName = baseInfo?.projectName
  61. const projectCode = baseInfo?.projectCode
  62. content = content.replace(/[^\{\u4e00-\u9fa5]{1,90}{[^\}\u4e00-\u9fa5]+?}/g, '')
  63. // 高亮第三方服务
  64. if (content) {
  65. // content = thirdPartyVerify.replaceKeys(content)
  66. }
  67. // 下划线高亮项目名称编号
  68. if(projectName && content.toLowerCase().indexOf(projectName.toLowerCase()) > -1){
  69. content = replaceKeyword(content, projectName, '<span class="keyword keyword-underline my-follow project project-name hide-underline">$1</span>')
  70. }
  71. if(projectCode && content.toLowerCase().indexOf(projectCode.toLowerCase()) > -1){
  72. content = replaceKeyword(content, projectCode, '<span class="keyword keyword-underline my-follow project project-code">$1</span>')
  73. }
  74. // 下划线高亮中标企业
  75. const winners = summary.winners
  76. if (Array.isArray(winners) && winners.length > 0) {
  77. for (let i = 0; i < winners.length; i++) {
  78. const winnerName = winners[i].name
  79. const winnerId = winners[i].id
  80. if (winnerName && content.toLowerCase().indexOf(winnerName.toLowerCase()) > -1) {
  81. content = replaceKeyword(content, winnerName, `<span data-eid='${winnerId}' class='keyword keyword-underline winner-name my-follow-ent'>$1</span>`)
  82. }
  83. }
  84. }
  85. // 表格兼容
  86. if(content.length > 10 && content.substring(0,6).toLowerCase() === '<tbody' && content.substring(content.length-8).toLowerCase() == '</tbody>'){
  87. content = '<table>' + content + '</table>'
  88. }
  89. // ------------------
  90. // 关键词高亮
  91. let highlightKeys = []
  92. if (formatted.content && formatted.content.highlightKeys) {
  93. highlightKeys = formatted.content.highlightKeys
  94. }
  95. highlightKeys.forEach((key) => {
  96. content = replaceKeyword(content, key, '<span class="keyword highlight-text">$1</span>')
  97. })
  98. return content
  99. }
  100. }
  101. /**
  102. * /publicapply/detail/baseInfo
  103. * 基础信息接口数据模型转换
  104. */
  105. function useContentModel() {
  106. return new ContentModel()
  107. }
  108. export default useContentModel