article.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. package services
  2. import (
  3. "errors"
  4. "fmt"
  5. "github.com/PuerkitoBio/goquery"
  6. "hongze/hongze_clpt/models"
  7. "hongze/hongze_clpt/utils"
  8. "html"
  9. "regexp"
  10. "sort"
  11. "strconv"
  12. "strings"
  13. "time"
  14. "unicode/utf8"
  15. )
  16. func FixArticleImgUrl(body string) (contentSub string, err error) {
  17. r := strings.NewReader(string(body))
  18. doc, err := goquery.NewDocumentFromReader(r)
  19. if err != nil {
  20. fmt.Println(err)
  21. }
  22. doc.Find("img").Each(func(i int, s *goquery.Selection) {
  23. src, _ := s.Attr("src")
  24. if i == 0 && src != "" {
  25. contentSub = src
  26. }
  27. })
  28. return
  29. }
  30. //GetReportContentTextSubByarticle 解析文章内容
  31. func GetReportContentTextSubByarticle(content, abstract string, articleId int) (contentSub string, err error) {
  32. var lenabstract int
  33. //如果不是研选就这么展示
  34. if articleId < utils.SummaryArticleId {
  35. abstract = html.UnescapeString(abstract)
  36. doc, errdoc := goquery.NewDocumentFromReader(strings.NewReader(abstract))
  37. if errdoc != nil {
  38. err = errdoc
  39. return
  40. }
  41. docabstract := doc.Text()
  42. lenabstract = utf8.RuneCountInString(docabstract)
  43. if lenabstract >= 20 {
  44. contentSub = docabstract
  45. return
  46. } else {
  47. contentSub, err = GetReportContentTextSub(content)
  48. }
  49. } else {
  50. contentSub, err = GetReportContentTextSub(content)
  51. }
  52. return
  53. }
  54. func GetReportContentTextSub(content string) (contentSub string, err error) {
  55. content = html.UnescapeString(content)
  56. doc, errdoc := goquery.NewDocumentFromReader(strings.NewReader(content))
  57. if errdoc != nil {
  58. err = errdoc
  59. return
  60. }
  61. docText := doc.Text()
  62. bodyRune := []rune(docText)
  63. bodyRuneLen := len(bodyRune)
  64. body := string(bodyRune[:bodyRuneLen])
  65. contentSub = body
  66. contentSub = strings.Replace(body, "Powered by Froala Editor", "", -1)
  67. contentSub = strings.Replace(body, "PoweredbyFroalaEditor", "", -1)
  68. contentSub = strings.Replace(body, " ", "", -1)
  69. return
  70. }
  71. func GetReportContentTextArticleBody(content string) (contentSub string) {
  72. contentSub = html.UnescapeString(content)
  73. contentSub = strings.Replace(contentSub, "<p data-f-id=\"pbf\" style=\"text-align: center; font-size: 14px; margin-top: 30px; opacity: 0.65; font-family: sans-serif;\">Powered by <a href=\"https://www.froala.com/wysiwyg-editor?pb=1\" title=\"Froala Editor\">Froala Editor</a></p>", "", -1)
  74. contentSub = strings.Replace(contentSub, "pre", "div", -1)
  75. return
  76. }
  77. //HandleArticleCategoryImg 预处理文章的封面图片
  78. func HandleArticleCategoryImg(list []*models.ArticleListResp) (items []*models.ArticleListResp, err error) {
  79. //研选的五张图片
  80. detailResearch, e := models.GetConfigByCode("category_research_img_url")
  81. if e != nil {
  82. err = errors.New("获取研选的五张图片失败" + e.Error())
  83. return
  84. }
  85. researchList := strings.Split(detailResearch.ConfigValue, "{|}")
  86. //对应分类的所图片
  87. detailCategoryUrl, err := models.GetConfigByCode("category_map_img_url")
  88. if err != nil {
  89. err = errors.New("获取对应分类的所图片失败" + err.Error())
  90. return
  91. }
  92. categoryUrlList := strings.Split(detailCategoryUrl.ConfigValue, "{|}")
  93. mapCategoryUrl := make(map[string]string)
  94. var categoryId string
  95. var imgUrlChart string
  96. for _, v := range categoryUrlList {
  97. vslice := strings.Split(v, "_")
  98. categoryId = vslice[0]
  99. imgUrlChart = vslice[len(vslice)-1]
  100. mapCategoryUrl[categoryId] = imgUrlChart
  101. }
  102. mapChartPerssion := make(map[string]string)
  103. reportMappingList, err := models.GetReportMappingStrategyAll()
  104. if err != nil {
  105. err = errors.New("GetReportMappingStrategyAll err" + err.Error())
  106. return
  107. }
  108. for _, v := range reportMappingList {
  109. mapChartPerssion[strconv.Itoa(v.CategoryId)] = v.ChartPermissionName
  110. }
  111. for k, v := range list {
  112. if list[k].Annotation == "" {
  113. imgurl, _ := FixArticleImgUrl(html.UnescapeString(list[k].Body))
  114. if imgurl != "" {
  115. list[k].BodyImg = imgurl
  116. }
  117. }
  118. item := list[k]
  119. //如果文章一开始的内容是图片,优先展示第一张图片
  120. //newBody, _ := GetReportContentTextSubByarticle(item.Body, item.Annotation, item.ArticleId)
  121. list[k].Annotation = ArticleAnnotation(item)
  122. list[k].Body = ""
  123. list[k].Abstract, _ = GetReportContentTextSub(v.Abstract)
  124. list[k].PublishDate = utils.StrTimeToTime(item.PublishDate).Format(utils.FormatDate) //时间字符串格式转时间格式
  125. list[k].ChartPermissionName = mapChartPerssion[v.CategoryId]
  126. //如果是研选系列的任意取五张图片的中的一张
  127. if v.CategoryId == "0" || v.ArticleId > utils.SummaryArticleId {
  128. knum := v.ArticleId % 5
  129. list[k].ImgUrlPc = researchList[knum]
  130. } else {
  131. list[k].ImgUrlPc = mapCategoryUrl[v.CategoryId]
  132. }
  133. if list[k].ArticleId < utils.SummaryArticleId {
  134. list[k].HttpUrl = utils.StrategyPlatform + strconv.Itoa(v.ArticleId)
  135. list[k].IsNeedJump = true
  136. }
  137. list[k].Source = 1
  138. //添加行业默认图片
  139. if v.ImgUrlPc == "" {
  140. if v.ChartPermissionName == utils.YI_YAO_NAME {
  141. list[k].ImgUrlPc = utils.YI_YAO_OTHER_IMG
  142. } else if v.ChartPermissionName == utils.XIAO_FEI_NAME {
  143. list[k].ImgUrlPc = utils.XIAO_FEI_OTHER_IMG
  144. } else if v.ChartPermissionName == utils.KE_JI_NAME {
  145. list[k].ImgUrlPc = utils.KE_JI_OTHER_IMG
  146. } else if v.ChartPermissionName == utils.ZHI_ZAO_NAME {
  147. list[k].ImgUrlPc = utils.ZHI_ZAO_OTHER_IMG
  148. }
  149. }
  150. list[k].IsResearch = utils.InArrayByInt(utils.YAN_XUAN_ARTICLE_TYPE_ID, v.ArticleTypeId)
  151. }
  152. articleIds := make([]int, 0)
  153. for i := range list {
  154. articleIds = append(articleIds, list[i].ArticleId)
  155. }
  156. // 报告关联产业信息
  157. industryMap := make(map[int][]*models.IndustrialManagementIdInt, 0)
  158. if len(articleIds) > 0 {
  159. var industryCond string
  160. var industryPars []interface{}
  161. industryCond += ` AND mg.article_id IN (` + utils.GetOrmInReplace(len(articleIds)) + `)`
  162. industryPars = append(industryPars, articleIds)
  163. industryList, e := models.GetIndustrialListByarticleId(industryPars, industryCond)
  164. if e != nil {
  165. err = errors.New("GetIndustrialListByarticleId" + e.Error())
  166. return
  167. }
  168. for i := range industryList {
  169. v := industryList[i]
  170. industryMap[v.ArticleId] = append(industryMap[v.ArticleId], &models.IndustrialManagementIdInt{
  171. ArticleId: v.ArticleId,
  172. IndustrialManagementId: v.IndustrialManagementId,
  173. IndustryName: v.IndustryName,
  174. ChartPermissionId: v.ChartPermissionId,
  175. })
  176. }
  177. }
  178. for k, v := range list {
  179. if len(industryMap[v.ArticleId]) > 0 {
  180. list[k].List = industryMap[v.ArticleId]
  181. } else {
  182. list[k].List = make([]*models.IndustrialManagementIdInt, 0)
  183. }
  184. }
  185. if len(list) == 0 {
  186. list = make([]*models.ArticleListResp, 0)
  187. }
  188. items = list
  189. return
  190. }
  191. //HandleArticleStock 处理报告关联的个股标签
  192. func HandleArticleStock(stock string) (items []*models.ComapnyNameResp) {
  193. sliceSubjects := strings.Split(stock, "/")
  194. if len(sliceSubjects) > 0 {
  195. for _, vSubject := range sliceSubjects {
  196. sliceKuohao := strings.Split(vSubject, "(") //过滤括号
  197. sliceXiahuaxian := strings.Split(sliceKuohao[0], "-") //过滤下划线
  198. subject := sliceXiahuaxian[0]
  199. items = append(items, &models.ComapnyNameResp{ComapnyName: subject})
  200. }
  201. }
  202. return
  203. }
  204. //弘则报告发布日期在三个月以内的
  205. func GetArticNewLabelWhithActivity3Month() (labelMap map[int]bool, err error) {
  206. var condition string
  207. var pars []interface{}
  208. condition += ` AND publish_date <= ? AND article_id < ? `
  209. pars = append(pars, time.Now().AddDate(0, -3, 0), utils.SummaryArticleId)
  210. articleList, e := models.GetArticleList(condition, pars)
  211. if e != nil {
  212. err = errors.New("GetArticleList, Err: " + e.Error())
  213. return
  214. }
  215. var articleIds []int
  216. for _, v := range articleList {
  217. articleIds = append(articleIds, v.ArticleId)
  218. }
  219. if len(articleIds) == 0 {
  220. return
  221. }
  222. pars = make([]interface{}, 0)
  223. condition = ` AND article_id IN (` + utils.GetOrmInReplace(len(articleIds)) + `)`
  224. pars = append(pars, articleIds)
  225. industrialList, e := models.GetIndustrialArticleGroupManagementList(condition, pars)
  226. if e != nil {
  227. err = errors.New("GetIndustrialArticleGroupManagementList, Err: " + e.Error())
  228. return
  229. }
  230. labelMap = make(map[int]bool, 0)
  231. var industrialIds []int
  232. for _, v := range industrialList {
  233. industrialIds = append(industrialIds, v.IndustrialManagementId)
  234. }
  235. // 获取活动关联的产业
  236. var groupCond string
  237. var groupPars []interface{}
  238. groupCond += ` AND b.industrial_management_id IN (` + utils.GetOrmInReplace(len(industrialIds)) + `) AND b.source = 1 `
  239. groupPars = append(groupPars, industrialIds)
  240. groups, e := models.GetActivityIndustryRelationList(groupCond, groupPars)
  241. if e != nil {
  242. err = errors.New("获取活动产业关联列表失败, Err: " + e.Error())
  243. return
  244. }
  245. for _, v := range groups {
  246. labelMap[v.ActivityId] = true
  247. }
  248. return
  249. }
  250. //GetSpecialArticleDetailUserPower 处理用户查看专项调研文章详情的权限
  251. func GetSpecialArticleDetailUserPower(user *models.WxUserItem, articleInfo *models.ArticleDetail) (havePower bool, err error) {
  252. permissionStr, e := GetCompanyPermissionUpgrade(user.CompanyId)
  253. if e != nil {
  254. err = errors.New("GetCompanyPermissionUpgrade, Err: " + e.Error())
  255. return
  256. }
  257. reportMapDetail, e := models.GetdetailByCategoryIdPush(articleInfo.CategoryId)
  258. if e != nil {
  259. err = errors.New("GetdetailByCategoryIdPush, Err: " + e.Error())
  260. return
  261. }
  262. if reportMapDetail == nil {
  263. err = errors.New("GetdetailByCategoryIdP,获取详情失败, Err: ")
  264. return
  265. }
  266. //如果没有对应的升级权限,则返回
  267. if !strings.Contains(permissionStr, reportMapDetail.ChartPermissionName) {
  268. return
  269. } else {
  270. havePower = true
  271. }
  272. return
  273. }
  274. //处理核心观点的展示规则
  275. //func ArticleAnnotation(item *models.ArticleListResp) (annotation string) {
  276. // if item.Annotation != "" {
  277. // annotation = strings.Replace(item.Annotation, "<br>", "", -1)
  278. // }
  279. // return
  280. //}
  281. func GetReportContentTextSubNew(content string) (contentSub string, err error) {
  282. content = html.UnescapeString(content)
  283. doc, errdoc := goquery.NewDocumentFromReader(strings.NewReader(content))
  284. if errdoc != nil {
  285. err = errdoc
  286. return
  287. }
  288. docText := doc.Text()
  289. bodyRune := []rune(docText)
  290. bodyRuneLen := len(bodyRune)
  291. body := string(bodyRune[:bodyRuneLen])
  292. contentSub = body
  293. contentSub = strings.Replace(contentSub, "Powered by Froala Editor", "", -1)
  294. contentSub = strings.Replace(contentSub, " ", "", -1)
  295. contentSub = strings.Replace(contentSub, "<p data-f-id=\"pbf\" style=\"text-align: center; font-size: 14px; margin-top: 30px; opacity: 0.65; font-family: sanered by <a href=\"https://www.froala.com/wysiwyg-editor?pb=1\" title=\"Froala Editor\">Froala Editor</a></p>", "", -1)
  296. return
  297. }
  298. //处理核心观点的展示规则
  299. func ArticleAnnotation(item *models.ArticleListResp) (annotation string) {
  300. if item.ArticleId >= utils.SummaryArticleId {
  301. item.Annotation = YxArticleAnnotation(item)
  302. }
  303. if item.Annotation != "" {
  304. annotation = strings.Replace(item.Annotation, "<br>", "", -1)
  305. } else {
  306. return
  307. }
  308. bodyText, _ := GetReportContentTextSubNew(annotation)
  309. if bodyText == "" {
  310. return
  311. }
  312. if annotation != "" {
  313. annotation = html.UnescapeString(annotation)
  314. doc, _ := goquery.NewDocumentFromReader(strings.NewReader(annotation))
  315. docText := doc.Text()
  316. mapDoc := make(map[int]string)
  317. var mapSort []int
  318. p := doc.Find("p")
  319. p.Each(func(tk int, pd *goquery.Selection) {
  320. pdText := pd.Text()
  321. pdText = strings.Replace(pdText, " ", "", -1)
  322. if pdText != "" {
  323. textLen := strings.Index(docText, pdText)
  324. mapDoc[(strings.Index(docText, pdText))] = pdText
  325. mapSort = append(mapSort, textLen)
  326. }
  327. })
  328. li := doc.Find("li")
  329. li.Each(func(tk int, li *goquery.Selection) {
  330. liText := li.Text()
  331. liText = strings.Replace(liText, " ", "", -1)
  332. if liText != "" {
  333. textLen := strings.Index(docText, liText)
  334. mapDoc[(strings.Index(docText, liText))] = strconv.Itoa(tk+1) + "." + liText
  335. mapSort = append(mapSort, textLen)
  336. }
  337. })
  338. ul := doc.Find("ul")
  339. ul.Each(func(tk int, ul *goquery.Selection) {
  340. ulText := ul.Text()
  341. ulText = strings.Replace(ulText, " ", "", -1)
  342. if ulText != "" {
  343. textLen := strings.Index(docText, ulText)
  344. mapDoc[(strings.Index(docText, ulText))] = ulText
  345. mapSort = append(mapSort, textLen)
  346. }
  347. })
  348. if len(mapSort) == 0 {
  349. return
  350. } else {
  351. //排序
  352. sort.Ints(mapSort)
  353. var annotationHtml string
  354. for _, vSort := range mapSort {
  355. for k, v := range mapDoc {
  356. if k == vSort && v != "" {
  357. annotationHtml += v + "<br>"
  358. }
  359. }
  360. }
  361. annotationHtml = strings.TrimRight(annotationHtml, "<br>")
  362. annotationHtml = "<p>" + annotationHtml + "</p>"
  363. annotation = annotationHtml
  364. }
  365. }
  366. return
  367. }
  368. //解析研选内容中的核心观点
  369. func YxArticleAnnotation(article *models.ArticleListResp) (annotation string) {
  370. //如果不规范,就获取内容主体
  371. if strings.Count(article.Body, "<hr") == 0 {
  372. //如果内容不规范而且,还有图片,就把核心观点置空
  373. if article.BodyImg != "" {
  374. return
  375. }
  376. annotation, _ = GetReportContentTextSub(article.Body)
  377. return
  378. }
  379. body := strings.ReplaceAll(article.Body, "<strong>", "")
  380. body = strings.ReplaceAll(body, "</strong>", "")
  381. body = strings.ReplaceAll(body, "</ol>", "</div>")
  382. body = strings.ReplaceAll(body, "<ol>", "<div>")
  383. body = strings.ReplaceAll(body, "</li>", "</p>")
  384. body = strings.ReplaceAll(body, "<li>", "<p>")
  385. re, _ := regexp.Compile("<strong.*?>")
  386. body = re.ReplaceAllString(body, "")
  387. reLi, _ := regexp.Compile("<li.*?>")
  388. body = reLi.ReplaceAllString(body, "")
  389. var plus int
  390. coreIndex := strings.Index(body, "核心观点:")
  391. plus = 15
  392. if coreIndex == -1 {
  393. coreIndex = strings.Index(body, "核心观点:")
  394. plus = 13
  395. }
  396. if coreIndex == -1 {
  397. coreIndex = strings.Index(body, "核心观点")
  398. plus = 12
  399. }
  400. if coreIndex == -1 {
  401. coreIndex = strings.Index(body, "核心结论:")
  402. plus = 15
  403. }
  404. if coreIndex == -1 {
  405. coreIndex = strings.Index(body, "核心结论:")
  406. plus = 13
  407. }
  408. if coreIndex == -1 {
  409. coreIndex = strings.Index(body, "核心结论")
  410. plus = 12
  411. }
  412. endIndex := strings.Index(body, "<hr")
  413. if coreIndex != -1 && endIndex != -1 {
  414. body = body[coreIndex+plus : endIndex]
  415. }
  416. annotation = body
  417. return
  418. }