article.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  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, user *models.WxUserItem) (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. // 处理文章收藏字段
  179. mapCollect, e := GetUserAticleCollectMap(user)
  180. if e != nil {
  181. err = errors.New("GetUserAticleCollectMap" + e.Error())
  182. return
  183. }
  184. for k, v := range list {
  185. if len(industryMap[v.ArticleId]) > 0 {
  186. list[k].List = industryMap[v.ArticleId]
  187. } else {
  188. list[k].List = make([]*models.IndustrialManagementIdInt, 0)
  189. }
  190. if _, ok := mapCollect[v.ArticleId]; ok {
  191. list[k].IsCollect = true
  192. }
  193. }
  194. if len(list) == 0 {
  195. list = make([]*models.ArticleListResp, 0)
  196. }
  197. items = list
  198. return
  199. }
  200. // HandleArticleStock 处理报告关联的个股标签
  201. func HandleArticleStock(stock string) (items []*models.ComapnyNameResp) {
  202. sliceSubjects := strings.Split(stock, "/")
  203. if len(sliceSubjects) > 0 {
  204. for _, vSubject := range sliceSubjects {
  205. sliceKuohao := strings.Split(vSubject, "(") //过滤括号
  206. sliceXiahuaxian := strings.Split(sliceKuohao[0], "-") //过滤下划线
  207. subject := sliceXiahuaxian[0]
  208. items = append(items, &models.ComapnyNameResp{ComapnyName: subject})
  209. }
  210. }
  211. return
  212. }
  213. // 弘则报告发布日期在三个月以内的
  214. func GetArticNewLabelWhithActivity3Month() (labelMap map[int]bool, err error) {
  215. var condition string
  216. var pars []interface{}
  217. condition += ` AND publish_date <= ? AND article_id < ? `
  218. pars = append(pars, time.Now().AddDate(0, -3, 0), utils.SummaryArticleId)
  219. articleList, e := models.GetArticleList(condition, pars)
  220. if e != nil {
  221. err = errors.New("GetArticleList, Err: " + e.Error())
  222. return
  223. }
  224. var articleIds []int
  225. for _, v := range articleList {
  226. articleIds = append(articleIds, v.ArticleId)
  227. }
  228. if len(articleIds) == 0 {
  229. return
  230. }
  231. pars = make([]interface{}, 0)
  232. condition = ` AND article_id IN (` + utils.GetOrmInReplace(len(articleIds)) + `)`
  233. pars = append(pars, articleIds)
  234. industrialList, e := models.GetIndustrialArticleGroupManagementList(condition, pars)
  235. if e != nil {
  236. err = errors.New("GetIndustrialArticleGroupManagementList, Err: " + e.Error())
  237. return
  238. }
  239. labelMap = make(map[int]bool, 0)
  240. var industrialIds []int
  241. for _, v := range industrialList {
  242. industrialIds = append(industrialIds, v.IndustrialManagementId)
  243. }
  244. // 获取活动关联的产业
  245. var groupCond string
  246. var groupPars []interface{}
  247. groupCond += ` AND b.industrial_management_id IN (` + utils.GetOrmInReplace(len(industrialIds)) + `) AND b.source = 1 `
  248. groupPars = append(groupPars, industrialIds)
  249. groups, e := models.GetActivityIndustryRelationList(groupCond, groupPars)
  250. if e != nil {
  251. err = errors.New("获取活动产业关联列表失败, Err: " + e.Error())
  252. return
  253. }
  254. for _, v := range groups {
  255. labelMap[v.ActivityId] = true
  256. }
  257. return
  258. }
  259. // GetSpecialArticleDetailUserPower 处理用户查看专项调研文章详情的权限
  260. func GetSpecialArticleDetailUserPower(user *models.WxUserItem, articleInfo *models.ArticleDetail) (havePower bool, err error) {
  261. permissionStr, e := GetCompanyPermission(user.CompanyId)
  262. if e != nil {
  263. err = errors.New("GetCompanyPermissionUpgrade, Err: " + e.Error())
  264. return
  265. }
  266. reportMapDetail, e := models.GetdetailByCategoryIdPush(articleInfo.CategoryId)
  267. if e != nil {
  268. err = errors.New("GetdetailByCategoryIdPush, Err: " + e.Error())
  269. return
  270. }
  271. if reportMapDetail == nil {
  272. err = errors.New("GetdetailByCategoryIdP,获取详情失败, Err: ")
  273. return
  274. }
  275. //如果没有对应的升级权限,则返回
  276. if !strings.Contains(permissionStr, reportMapDetail.ChartPermissionName) {
  277. return
  278. } else {
  279. havePower = true
  280. }
  281. return
  282. }
  283. //处理核心观点的展示规则
  284. //func ArticleAnnotation(item *models.ArticleListResp) (annotation string) {
  285. // if item.Annotation != "" {
  286. // annotation = strings.Replace(item.Annotation, "<br>", "", -1)
  287. // }
  288. // return
  289. //}
  290. func GetReportContentTextSubNew(content string) (contentSub string, err error) {
  291. content = html.UnescapeString(content)
  292. doc, errdoc := goquery.NewDocumentFromReader(strings.NewReader(content))
  293. if errdoc != nil {
  294. err = errdoc
  295. return
  296. }
  297. docText := doc.Text()
  298. bodyRune := []rune(docText)
  299. bodyRuneLen := len(bodyRune)
  300. body := string(bodyRune[:bodyRuneLen])
  301. contentSub = body
  302. contentSub = strings.Replace(contentSub, "Powered by Froala Editor", "", -1)
  303. contentSub = strings.Replace(contentSub, " ", "", -1)
  304. 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)
  305. return
  306. }
  307. // 处理核心观点的展示规则
  308. func ArticleAnnotation(item *models.ArticleListResp) (annotation string) {
  309. if item.ArticleId >= utils.SummaryArticleId {
  310. item.Annotation = YxArticleAnnotation(item)
  311. }
  312. if item.Annotation != "" {
  313. annotation = strings.Replace(item.Annotation, "<br>", "", -1)
  314. } else {
  315. return
  316. }
  317. bodyText, _ := GetReportContentTextSubNew(annotation)
  318. if bodyText == "" {
  319. return
  320. }
  321. if annotation != "" {
  322. annotation = html.UnescapeString(annotation)
  323. doc, _ := goquery.NewDocumentFromReader(strings.NewReader(annotation))
  324. docText := doc.Text()
  325. mapDoc := make(map[int]string)
  326. mapSortRepeat := make(map[string]string)
  327. var mapSort []int
  328. p := doc.Find("p")
  329. p.Each(func(tk int, pd *goquery.Selection) {
  330. pdText := pd.Text()
  331. pdText = strings.Replace(pdText, " ", "", -1)
  332. if pdText != "" {
  333. textLen := strings.Index(docText, pdText)
  334. if mapSortRepeat[strconv.Itoa(textLen)] == "" {
  335. mapDoc[(strings.Index(docText, pdText))] = pdText
  336. mapSort = append(mapSort, textLen)
  337. mapSortRepeat[strconv.Itoa(textLen)] = strconv.Itoa(textLen)
  338. }
  339. }
  340. })
  341. li := doc.Find("li")
  342. li.Each(func(tk int, li *goquery.Selection) {
  343. liText := li.Text()
  344. liText = strings.Replace(liText, " ", "", -1)
  345. if liText != "" {
  346. textLen := strings.Index(docText, liText)
  347. if mapSortRepeat[strconv.Itoa(textLen)] == "" {
  348. mapDoc[(strings.Index(docText, liText))] = strconv.Itoa(tk+1) + "." + liText
  349. mapSort = append(mapSort, textLen)
  350. mapSortRepeat[strconv.Itoa(textLen)] = strconv.Itoa(textLen)
  351. }
  352. }
  353. })
  354. ul := doc.Find("ul")
  355. ul.Each(func(tk int, ul *goquery.Selection) {
  356. ulText := ul.Text()
  357. ulText = strings.Replace(ulText, " ", "", -1)
  358. if ulText != "" {
  359. textLen := strings.Index(docText, ulText)
  360. if mapSortRepeat[strconv.Itoa(textLen)] == "" {
  361. mapDoc[(strings.Index(docText, ulText))] = ulText
  362. mapSort = append(mapSort, textLen)
  363. mapSortRepeat[strconv.Itoa(textLen)] = strconv.Itoa(textLen)
  364. }
  365. }
  366. })
  367. if len(mapSort) == 0 {
  368. return
  369. } else {
  370. //排序
  371. sort.Ints(mapSort)
  372. var annotationHtml string
  373. for _, vSort := range mapSort {
  374. for k, v := range mapDoc {
  375. if k == vSort && v != "" {
  376. annotationHtml += v + "<br>"
  377. }
  378. }
  379. }
  380. annotationHtml = strings.TrimRight(annotationHtml, "<br>")
  381. annotationHtml = "<p>" + annotationHtml + "</p>"
  382. annotation = annotationHtml
  383. }
  384. }
  385. return
  386. }
  387. // 解析研选内容中的核心观点
  388. func YxArticleAnnotation(article *models.ArticleListResp) (annotation string) {
  389. //如果不规范,就获取内容主体
  390. if strings.Count(article.Body, "<hr") == 0 {
  391. //如果内容不规范而且,还有图片,就把核心观点置空
  392. if article.BodyImg != "" {
  393. return
  394. }
  395. annotation, _ = GetReportContentTextSub(article.Body)
  396. return
  397. }
  398. body := strings.ReplaceAll(article.Body, "<strong>", "")
  399. body = strings.ReplaceAll(body, "</strong>", "")
  400. body = strings.ReplaceAll(body, "</ol>", "</div>")
  401. body = strings.ReplaceAll(body, "<ol>", "<div>")
  402. body = strings.ReplaceAll(body, "</li>", "</p>")
  403. body = strings.ReplaceAll(body, "<li>", "<p>")
  404. re, _ := regexp.Compile("<strong.*?>")
  405. body = re.ReplaceAllString(body, "")
  406. reLi, _ := regexp.Compile("<li.*?>")
  407. body = reLi.ReplaceAllString(body, "")
  408. var plus int
  409. coreIndex := strings.Index(body, "核心观点:")
  410. plus = 15
  411. if coreIndex == -1 {
  412. coreIndex = strings.Index(body, "核心观点:")
  413. plus = 13
  414. }
  415. if coreIndex == -1 {
  416. coreIndex = strings.Index(body, "核心观点")
  417. plus = 12
  418. }
  419. if coreIndex == -1 {
  420. coreIndex = strings.Index(body, "核心结论:")
  421. plus = 15
  422. }
  423. if coreIndex == -1 {
  424. coreIndex = strings.Index(body, "核心结论:")
  425. plus = 13
  426. }
  427. if coreIndex == -1 {
  428. coreIndex = strings.Index(body, "核心结论")
  429. plus = 12
  430. }
  431. endIndex := strings.Index(body, "<hr")
  432. if coreIndex != -1 && endIndex != -1 {
  433. body = body[coreIndex+plus : endIndex]
  434. }
  435. annotation = body
  436. return
  437. }
  438. // 获取研选类型的文章分类Id
  439. func GetYanXuanArticleTypeIds() (articleTypeIds string, err error) {
  440. var condition string
  441. condition = " AND is_show_yanx = 1 "
  442. listType, e := models.GetCygxArticleTypeListCondition(condition)
  443. if e != nil {
  444. err = errors.New("GetCygxArticleTypeListCondition, Err: " + e.Error())
  445. return
  446. }
  447. for _, v := range listType {
  448. articleTypeIds += strconv.Itoa(v.ArticleTypeId) + ","
  449. }
  450. articleTypeIds = strings.TrimRight(articleTypeIds, ",")
  451. if articleTypeIds == "" {
  452. err = errors.New("研选分类ID不能为空")
  453. return
  454. }
  455. return
  456. }
  457. // GetUserAticleCollectMap 获取用户收藏的文章ID
  458. func GetUserAticleCollectMap(user *models.WxUserItem) (respMap map[int]int, err error) {
  459. list, e := models.GetCygxArticleCollectListByUser(user.UserId)
  460. if e != nil {
  461. err = errors.New("GetCygxArticleCollectListByUser, Err: " + e.Error())
  462. return
  463. }
  464. articleMap := make(map[int]int)
  465. for _, v := range list {
  466. articleMap[v.ArticleId] = v.ArticleId
  467. }
  468. respMap = articleMap
  469. return
  470. }