import BaseModel from '../../../core/base'
import useSummaryModel from '../transform/summary2'
import useCommonTitleModel from '../transform/content'
import useThirdPartyVerifyModel from '../transform/third-party-verify'
import { replaceKeyword } from '@jy/util'
const thirdPartyVerify = useThirdPartyVerifyModel()
class ContentModel extends BaseModel {
constructor(config) {
super(config)
}
createModel() {
return {
moduleShow: {
// 正文模块是否显示
content: false,
// 摘要模块是否显示
summary: false
},
content: useCommonTitleModel().createModel(),
summary: useSummaryModel().createModel()
}
}
formatModel(data, isInit = false) {
const result = this.createModel()
const commonTitleModel = useCommonTitleModel()
const summaryModel = useSummaryModel()
// 基础信息
if (data?.baseInfo) {
result.content = commonTitleModel.transformModel(data)
}
// 摘要
if (data?.abstract) {
result.summary = summaryModel.transformModel(data)
}
if (result.content) {
result.content.titleHighlighted = this.highlightTitleHTML(result.content.title, data, result)
try {
result.content.contentHighlighted = this.highlightContentHTML(result.content.content, data, result)
} catch (error) {
console.error(error)
}
}
// 根据数据情况判断模块是否展示
result.moduleShow.content = !!result.content?.content
result.moduleShow.summary= Array.isArray(result.summary?.list) && result.summary?.list.length > 0
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, '$1')
}
// ------------------
// 关键词高亮
let highlightKeys = []
if (formatted.content && formatted.content.highlightKeys) {
highlightKeys = formatted.content.highlightKeys
}
highlightKeys.forEach((key) => {
title = replaceKeyword(title, key, '$1')
})
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, '')
// 将多个连续的br替换成一个
content = content.replace(/(
)+/gi, '
')
content = content.replace(/^((
)|( ))+/g, '')
content = content.replace(/((
)|( ))+$/g, '')
// 下划线高亮项目名称编号
if(projectName && content.toLowerCase().indexOf(projectName.toLowerCase()) > -1){
content = replaceKeyword(content, projectName, ' ')
}
if(projectCode && content.toLowerCase().indexOf(projectCode.toLowerCase()) > -1){
content = replaceKeyword(content, projectCode, '$1')
}
// 下划线高亮中标企业
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, `$1`)
}
}
}
// 表格兼容
if(content.length > 10 && content.substring(0,6).toLowerCase() === '