123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623 |
- package services
- import (
- "errors"
- "fmt"
- "github.com/PuerkitoBio/goquery"
- "hongze/hongze_mfyx/models"
- "hongze/hongze_mfyx/utils"
- "html"
- "regexp"
- "sort"
- "strconv"
- "strings"
- )
- func GetReportContentTextSub(content string) (contentSub string, err error) {
- content = html.UnescapeString(content)
- doc, err := goquery.NewDocumentFromReader(strings.NewReader(content))
- docText := doc.Text()
- bodyRune := []rune(docText)
- bodyRuneLen := len(bodyRune)
- if bodyRuneLen > 200 {
- bodyRuneLen = 200
- }
- body := string(bodyRune[:bodyRuneLen])
- contentSub = body
- contentSub = strings.Replace(body, "Powered by Froala Editor", "", -1)
- return
- }
- func GetReportContentTextSubNew(content string) (contentSub string, err error) {
- content = html.UnescapeString(content)
- doc, errdoc := goquery.NewDocumentFromReader(strings.NewReader(content))
- if errdoc != nil {
- err = errdoc
- return
- }
- docText := doc.Text()
- bodyRune := []rune(docText)
- bodyRuneLen := len(bodyRune)
- body := string(bodyRune[:bodyRuneLen])
- contentSub = body
- contentSub = strings.Replace(contentSub, "Powered by Froala Editor", "", -1)
- contentSub = strings.Replace(contentSub, " ", "", -1)
- 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)
- return
- }
- func FixArticleImgUrl(body string) (contentSub string, err error) {
- r := strings.NewReader(string(body))
- doc, err := goquery.NewDocumentFromReader(r)
- if err != nil {
- fmt.Println(err)
- }
- doc.Find("img").Each(func(i int, s *goquery.Selection) {
- src, _ := s.Attr("src")
- if i == 0 && src != "" {
- contentSub = src
- }
- })
- return
- }
- // UserViewRedisData 阅读数据
- type UserViewRedisData struct {
- Mobile string `json:"mobile"`
- Email string `json:"email"`
- RealName string `json:"real_name"`
- CompanyName string `json:"company_name"`
- ViewTime string `json:"view_time" description:"阅读时间,格式:2022-02-17 13:06:13"`
- ProductId int `json:"product_id" description:"报告所属产品,ficc:1,权益:2"`
- CompanyId int `json:"company_id" description:"客户id"`
- UserId int `json:"user_id" description:"用户id"`
- ReportId int `json:"report_id" description:"报告id"`
- StopTime int `json:"stop_time" description:"停留时间"`
- ReportChapterId int `json:"report_chapter_id" description:"章节ID"`
- OutId int `json:"out_id" description:"记录ID"`
- }
- type ReportViewRecord struct {
- Id int `orm:"column(id);pk"`
- //UserId int `json:"user_id" description:"用户ID"`
- //ReportId int `description:"报告id"`
- //Mobile string `description:"手机号"`
- //Email string `description:"邮箱"`
- //RealName string `description:"用户实际姓名"`
- //CompanyName string `description:"公司名称"`
- //CreateTime time.Time `description:"创建时间"`
- //StopTime int `json:"stop_time" description:"停留时间"`
- //ReportChapterId int `json:"report_chapter_id" description:"章节ID"`
- //OutId int `json:"out_id" description:"记录ID"`
- Mobile string `json:"mobile"`
- Email string `json:"email"`
- RealName string `json:"real_name"`
- CompanyName string `json:"company_name"`
- ViewTime string `json:"view_time" description:"阅读时间,格式:2022-02-17 13:06:13"`
- ProductId int `json:"product_id" description:"报告所属产品,ficc:1,权益:2"`
- CompanyId int `json:"company_id" description:"客户id"`
- UserId int `json:"user_id" description:"用户id"`
- ReportId int `json:"report_id" description:"报告id"`
- StopTime int `json:"stop_time" description:"停留时间"`
- ReportChapterId int `json:"report_chapter_id" description:"章节ID"`
- OutId int `json:"out_id" description:"章节ID"`
- }
- // PushViewRecordNewRedisData 阅读数据加入到redis
- func PushViewRecordNewRedisData(reportViewRecord *ReportViewRecord, companyId int) bool {
- data := &UserViewRedisData{
- Mobile: reportViewRecord.Mobile,
- UserId: reportViewRecord.UserId,
- Email: reportViewRecord.Email,
- RealName: reportViewRecord.RealName,
- CompanyName: reportViewRecord.CompanyName,
- ViewTime: reportViewRecord.ViewTime,
- ProductId: 2,
- CompanyId: companyId,
- ReportId: reportViewRecord.ReportId,
- StopTime: reportViewRecord.StopTime,
- ReportChapterId: reportViewRecord.ReportChapterId,
- OutId: reportViewRecord.OutId,
- }
- if utils.Re == nil {
- err := utils.Rc.LPush(utils.CACHE_KEY_USER_VIEW, data)
- if err != nil {
- fmt.Println("PushViewRecordNewRedisData LPush Err:" + err.Error())
- }
- return true
- }
- return false
- }
- // GetSpecialArticleDetailUserPower 处理用户查看专项调研文章详情的权限
- func GetSpecialArticleDetailUserPower(user *models.WxUserItem, articleInfo *models.ArticleDetail) (havePower bool, err error) {
- userType, _, e := GetUserType(user.CompanyId)
- if e != nil {
- err = errors.New("GetSpecialUserType, Err: " + e.Error())
- return
- }
- // 永续客户、大套餐客户可以查看行业升级套餐客户 权限
- if userType == 1 || userType == 2 {
- havePower = true
- return
- }
- permissionStr, e := GetCompanyPermissionUpgrade(user.CompanyId)
- if e != nil {
- err = errors.New("GetCompanyPermissionUpgrade, Err: " + e.Error())
- return
- }
- reportMapDetail, e := models.GetdetailByCategoryIdPush(articleInfo.CategoryId)
- if e != nil {
- err = errors.New("GetdetailByCategoryIdPush, Err: " + e.Error())
- return
- }
- if reportMapDetail == nil {
- err = errors.New("GetdetailByCategoryIdP,获取详情失败, ")
- return
- }
- fmt.Println(permissionStr)
- //如果没有对应的升级权限,则返回
- if !strings.Contains(permissionStr, reportMapDetail.ChartPermissionName) {
- return
- } else {
- havePower = true
- }
- return
- }
- // HandleArticleCategoryImg 预处理文章的封面图片
- func HandleArticleCategoryImg(list []*models.HomeArticle) (items []*models.HomeArticle, err error) {
- //研选的五张图片
- detailResearch, e := models.GetConfigByCode("category_research_img_url")
- if e != nil {
- err = errors.New("获取研选的五张图片失败" + e.Error())
- return
- }
- researchList := strings.Split(detailResearch.ConfigValue, "{|}")
- //对应分类的所图片
- detailCategoryUrl, err := models.GetConfigByCode("category_map_img_url")
- if err != nil {
- err = errors.New("获取对应分类的所图片失败" + err.Error())
- return
- }
- categoryUrlList := strings.Split(detailCategoryUrl.ConfigValue, "{|}")
- mapCategoryUrl := make(map[string]string)
- var categoryId string
- var imgUrlChart string
- for _, v := range categoryUrlList {
- vslice := strings.Split(v, "_")
- categoryId = vslice[0]
- imgUrlChart = vslice[len(vslice)-1]
- mapCategoryUrl[categoryId] = imgUrlChart
- }
- mapChartPerssion := make(map[string]string)
- reportMappingList, err := models.GetReportMappingStrategyAll()
- if err != nil {
- err = errors.New("GetReportMappingStrategyAll err" + err.Error())
- return
- }
- for _, v := range reportMappingList {
- mapChartPerssion[strconv.Itoa(v.CategoryId)] = v.ChartPermissionName
- }
- for k, v := range list {
- list[k].Abstract, _ = GetReportContentTextSub(v.Abstract)
- item := list[k]
- //如果文章一开始的内容是图片,优先展示第一张图片
- if list[k].Annotation == "" {
- imgurl, _ := FixArticleImgUrl(html.UnescapeString(list[k].Body))
- if imgurl != "" {
- list[k].BodyHtml = imgurl
- }
- }
- //newBody, _ := GetReportContentTextSubByarticle(item.Body, item.Annotation, item.ArticleId)
- list[k].Annotation = ArticleAnnotation(item)
- list[k].Body = ""
- list[k].PublishDate = utils.StrTimeToTime(item.PublishDate).Format(utils.FormatDate) //时间字符串格式转时间格式
- list[k].ChartPermissionName = mapChartPerssion[v.CategoryId]
- //如果是研选系列的任意取五张图片的中的一张
- if v.CategoryId == "0" || v.ArticleId > utils.SummaryArticleId {
- knum := v.ArticleId % 5
- list[k].ImgUrlPc = researchList[knum]
- } else {
- list[k].ImgUrlPc = mapCategoryUrl[v.CategoryId]
- }
- list[k].ArticleResponse = 4 // 默认展示核心观点
- //ArticleResponse int `description:"报告类型 0:啥也不是,1研选报告,2:研选纪要,3:研选沙龙,4;核心观点"`
- if list[k].ArticleId >= utils.SummaryArticleId {
- list[k].HttpUrl = utils.StrategyPlatform + strconv.Itoa(v.ArticleId)
- list[k].IsNeedJump = true
- list[k].ArticleResponse = 1
- }
- list[k].Source = 1
- //添加行业默认图片
- if v.ImgUrlPc == "" {
- if v.ChartPermissionName == utils.YI_YAO_NAME {
- list[k].ImgUrlPc = utils.YI_YAO_OTHER_IMG
- } else if v.ChartPermissionName == utils.XIAO_FEI_NAME {
- list[k].ImgUrlPc = utils.XIAO_FEI_OTHER_IMG
- } else if v.ChartPermissionName == utils.KE_JI_NAME {
- list[k].ImgUrlPc = utils.KE_JI_OTHER_IMG
- } else if v.ChartPermissionName == utils.ZHI_ZAO_NAME {
- list[k].ImgUrlPc = utils.ZHI_ZAO_OTHER_IMG
- }
- }
- //是不是研选报告
- if v.ArticleTypeId > 0 {
- list[k].IsResearch = true
- if v.ArticleTypeId == 12 {
- list[k].LabelKeyword = "研选沙龙"
- list[k].LabelKeywordImgLink = utils.LABEL_ICO_6
- } else {
- list[k].LabelKeyword = "纪要"
- list[k].LabelKeywordImgLink = utils.LABEL_ICO_5
- }
- }
- }
- articleIds := make([]int, 0)
- for i := range list {
- articleIds = append(articleIds, list[i].ArticleId)
- }
- articleMapPv := GetArticleHistoryByArticleId(articleIds) //文章Pv
- // 报告关联产业信息
- industryMap := make(map[int][]*models.IndustrialManagementIdInt, 0)
- if len(articleIds) > 0 {
- var industryCond string
- var industryPars []interface{}
- industryCond += ` AND mg.article_id IN (` + utils.GetOrmInReplace(len(articleIds)) + `)`
- industryPars = append(industryPars, articleIds)
- industryList, e := models.GetIndustrialListByarticleId(industryPars, industryCond)
- if e != nil {
- err = errors.New("GetIndustrialListByarticleId" + e.Error())
- return
- }
- for i := range industryList {
- v := industryList[i]
- industryMap[v.ArticleId] = append(industryMap[v.ArticleId], &models.IndustrialManagementIdInt{
- ArticleId: v.ArticleId,
- IndustrialManagementId: v.IndustrialManagementId,
- IndustryName: v.IndustryName,
- ChartPermissionId: v.ChartPermissionId,
- })
- }
- }
- for k, v := range list {
- if len(industryMap[v.ArticleId]) > 0 {
- list[k].List = industryMap[v.ArticleId]
- } else {
- list[k].List = make([]*models.IndustrialManagementIdInt, 0)
- }
- v.Pv = articleMapPv[v.ArticleId]
- if v.Pv > 999 {
- list[k].Pv = 999
- }
- }
- if len(list) == 0 {
- list = make([]*models.HomeArticle, 0)
- }
- items = list
- return
- }
- // 处理核心观点的展示规则
- func ArticleAnnotation(item *models.HomeArticle) (annotation string) {
- if item.ArticleId >= utils.SummaryArticleId {
- item.Annotation = YxArticleAnnotation(item)
- }
- if item.Annotation != "" {
- annotation = strings.Replace(item.Annotation, "<br>", "", -1)
- annotation = strings.Replace(item.Annotation, " ", "", -1)
- } else {
- return
- }
- bodyText, _ := GetReportContentTextSubNew(annotation)
- if bodyText == "" {
- return
- }
- if annotation != "" {
- annotation = html.UnescapeString(annotation)
- doc, _ := goquery.NewDocumentFromReader(strings.NewReader(annotation))
- docText := doc.Text()
- mapDoc := make(map[int]string)
- var mapSort []int
- p := doc.Find("p")
- p.Each(func(tk int, pd *goquery.Selection) {
- pdText := pd.Text()
- pdText = strings.Replace(pdText, " ", "", -1)
- if pdText != "" {
- textLen := strings.Index(docText, pdText)
- mapDoc[(strings.Index(docText, pdText))] = pdText
- mapSort = append(mapSort, textLen)
- }
- })
- li := doc.Find("li")
- li.Each(func(tk int, li *goquery.Selection) {
- liText := li.Text()
- liText = strings.Replace(liText, " ", "", -1)
- if liText != "" {
- textLen := strings.Index(docText, liText)
- mapDoc[(strings.Index(docText, liText))] = strconv.Itoa(tk+1) + "." + liText
- mapSort = append(mapSort, textLen)
- }
- })
- ul := doc.Find("ul")
- ul.Each(func(tk int, ul *goquery.Selection) {
- ulText := ul.Text()
- ulText = strings.Replace(ulText, " ", "", -1)
- if ulText != "" {
- textLen := strings.Index(docText, ulText)
- mapDoc[(strings.Index(docText, ulText))] = ulText
- mapSort = append(mapSort, textLen)
- }
- })
- if len(mapSort) == 0 {
- return
- } else {
- //排序
- sort.Ints(mapSort)
- var annotationHtml string
- for _, vSort := range mapSort {
- for k, v := range mapDoc {
- if k == vSort && v != "" {
- annotationHtml += v + "<br>"
- }
- }
- }
- annotationHtml = strings.TrimRight(annotationHtml, "<br>")
- annotationHtml = "<p>" + annotationHtml + "</p>"
- annotation = annotationHtml
- }
- }
- return
- }
- // 处理核心观点的展示规则
- func AnnotationHtml(bodyText string) (annotation string) {
- if bodyText == "" {
- return
- }
- annotation = bodyText
- annotation = html.UnescapeString(annotation)
- doc, _ := goquery.NewDocumentFromReader(strings.NewReader(annotation))
- docText := doc.Text()
- mapDoc := make(map[int]string)
- var mapSort []int
- p := doc.Find("p")
- p.Each(func(tk int, pd *goquery.Selection) {
- pdText := pd.Text()
- //pdText = strings.Replace(pdText, " ", "", -1)
- if pdText != "" {
- textLen := strings.Index(docText, pdText)
- mapDoc[(strings.Index(docText, pdText))] = pdText
- mapSort = append(mapSort, textLen)
- }
- })
- li := doc.Find("li")
- li.Each(func(tk int, li *goquery.Selection) {
- liText := li.Text()
- liText = strings.Replace(liText, " ", "", -1)
- if liText != "" {
- textLen := strings.Index(docText, liText)
- mapDoc[(strings.Index(docText, liText))] = strconv.Itoa(tk+1) + "." + liText
- mapSort = append(mapSort, textLen)
- }
- })
- ul := doc.Find("ul")
- ul.Each(func(tk int, ul *goquery.Selection) {
- ulText := ul.Text()
- ulText = strings.Replace(ulText, " ", "", -1)
- if ulText != "" {
- textLen := strings.Index(docText, ulText)
- mapDoc[(strings.Index(docText, ulText))] = ulText
- mapSort = append(mapSort, textLen)
- }
- })
- section := doc.Find("section")
- section.Each(func(tk int, section *goquery.Selection) {
- sectionText := section.Text()
- sectionText = strings.Replace(sectionText, " ", "", -1)
- if sectionText != "" {
- textLen := strings.Index(docText, sectionText)
- mapDoc[(strings.Index(docText, sectionText))] = sectionText
- mapSort = append(mapSort, textLen)
- }
- })
- if len(mapSort) == 0 {
- return
- } else {
- //排序
- sort.Ints(mapSort)
- var annotationHtml string
- for _, vSort := range mapSort {
- for k, v := range mapDoc {
- if k == vSort && v != "" {
- annotationHtml += v + "<br>"
- }
- }
- }
- annotationHtml = strings.TrimRight(annotationHtml, "<br>")
- annotationHtml = "<p>" + annotationHtml + "</p>"
- annotation = annotationHtml
- }
- return
- }
- // 处理产品内测展示规则
- func ProductInteriorHtml(bodyText string) (annotation string) {
- if bodyText == "" {
- return
- }
- sliceBody := strings.Split(bodyText, "</p>")
- annotation, _ = GetReportContentTextSub(sliceBody[0])
- return
- }
- // 解析研选内容中的核心观点
- func YxArticleAnnotation(article *models.HomeArticle) (annotation string) {
- //如果不规范,就获取内容主体
- if strings.Count(article.Body, "<hr") == 0 {
- //如果内容不规范而且,还有图片,就把核心观点置空
- if article.BodyHtml != "" {
- return
- }
- annotation, _ = GetReportContentTextSub(article.Body)
- return
- }
- body := strings.ReplaceAll(article.Body, "<strong>", "")
- body = strings.ReplaceAll(body, "</strong>", "")
- body = strings.ReplaceAll(body, "</ol>", "</div>")
- body = strings.ReplaceAll(body, "<ol>", "<div>")
- body = strings.ReplaceAll(body, "</li>", "</p>")
- body = strings.ReplaceAll(body, "<li>", "<p>")
- re, _ := regexp.Compile("<strong.*?>")
- body = re.ReplaceAllString(body, "")
- reLi, _ := regexp.Compile("<li.*?>")
- body = reLi.ReplaceAllString(body, "")
- var plus int
- coreIndex := strings.Index(body, "核心观点:")
- plus = 15
- if coreIndex == -1 {
- coreIndex = strings.Index(body, "核心观点:")
- plus = 13
- }
- if coreIndex == -1 {
- coreIndex = strings.Index(body, "核心观点")
- plus = 12
- }
- if coreIndex == -1 {
- coreIndex = strings.Index(body, "核心结论:")
- plus = 15
- }
- if coreIndex == -1 {
- coreIndex = strings.Index(body, "核心结论:")
- plus = 13
- }
- if coreIndex == -1 {
- coreIndex = strings.Index(body, "核心结论")
- plus = 12
- }
- endIndex := strings.Index(body, "<hr")
- //如果有下划线跟核心观点就获取 核心观点~下划线之间的内容,如果没有就获取整个下划线的内容
- if endIndex != -1 {
- if coreIndex != -1 {
- body = body[coreIndex+plus : endIndex]
- } else {
- body = body[0:endIndex]
- }
- }
- annotation, _ = GetReportContentTextSub(body)
- return
- }
- // GetYxArticleIdMap 获取研选文章ID
- func GetYxArticleIdMap(articleIds []int) (mapResp map[int]bool) {
- var err error
- defer func() {
- if err != nil {
- go utils.SendAlarmMsg("获取研选文章ID失败,GetYxArticleIdMap"+err.Error(), 2)
- }
- }()
- var condition string
- var pars []interface{}
- condition = ` AND article_type_id > 0 `
- if len(articleIds) > 0 {
- condition += ` AND article_id IN (` + utils.GetOrmInReplace(len(articleIds)) + `)`
- pars = append(pars, articleIds)
- }
- articleList, e := models.GetArticleList(condition, pars)
- if e != nil {
- err = errors.New("GetArticleList, Err: " + e.Error())
- return
- }
- mapResp = make(map[int]bool, 0)
- for _, v := range articleList {
- mapResp[v.ArticleId] = true
- }
- return
- }
- // GetYanxuanArticleIds 获取研选文章ID
- func GetYanxuanArticleIds() (articleIds []int) {
- var err error
- defer func() {
- if err != nil {
- fmt.Println(err)
- go utils.SendAlarmMsg("获取研选文章ID GetYanxuanArticleIds,失败:"+err.Error(), 2)
- }
- }()
- var condition string
- var pars []interface{}
- condition = ` AND article_type_id > 0 `
- list, e := models.GetCygxCygxArticleIdList(condition, pars)
- if e != nil {
- err = errors.New("GetCygxCygxArticleIdList, Err: " + e.Error())
- return
- }
- for _, v := range list {
- articleIds = append(articleIds, v.ArticleId)
- }
- return
- }
- // GetArticleDetailUserPower 处理用户查看报告详情的权限
- func GetArticleDetailUserPower(user *models.WxUserItem) (havePower bool, err error) {
- userId := user.UserId
- companyId := user.CompanyId
- //判断用户是否开通了个人研选权限
- mfyxUserPermissionTotal := GetMfyxUserPermissionTotal(userId)
- if mfyxUserPermissionTotal == 1 {
- havePower = true
- return
- }
- //用户是否持有有效卡片
- userCardTotal := GetCygxOrderUserCardTotal(user.Mobile)
- fmt.Println("userCardTotal", userCardTotal)
- if userCardTotal == 1 {
- havePower = true
- return
- }
- //是否是权益客户
- raiCount, e := models.GetCompanyProductCount(companyId, utils.COMPANY_PRODUCT_RAI_ID)
- if e != nil {
- err = errors.New("GetCompanyProductCount, Err: " + e.Error())
- return
- }
- if raiCount == 0 {
- return
- }
- productDetail, e := models.GetCompanyProductDetailByCompanyId(companyId, 2)
- if e != nil {
- err = errors.New("GetCompanyProductDetailByCompanyId, Err: " + e.Error())
- return
- }
- // 永续客户无法查看研选权限
- if productDetail.Status == utils.COMPANY_STATUS_FOREVER {
- return
- }
- permissionStr, e := models.GetCompanyPermission(companyId)
- if e != nil {
- err = errors.New("GetCompanyPermission, Err: " + e.Error())
- return
- }
- if strings.Contains(permissionStr, utils.CHART_PERMISSION_NAME_MF_YANXUAN) {
- havePower = true
- return
- }
- return
- }
|