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, "

Froala Editor

", "", -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, "
", "", -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 + "
" } } } annotationHtml = strings.TrimRight(annotationHtml, "
") annotationHtml = "

" + annotationHtml + "

" 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 + "
" } } } annotationHtml = strings.TrimRight(annotationHtml, "
") annotationHtml = "

" + annotationHtml + "

" annotation = annotationHtml } return } // 处理产品内测展示规则 func ProductInteriorHtml(bodyText string) (annotation string) { if bodyText == "" { return } sliceBody := strings.Split(bodyText, "

") annotation, _ = GetReportContentTextSub(sliceBody[0]) return } // 解析研选内容中的核心观点 func YxArticleAnnotation(article *models.HomeArticle) (annotation string) { //如果不规范,就获取内容主体 if strings.Count(article.Body, "", "") body = strings.ReplaceAll(body, "", "") body = strings.ReplaceAll(body, "", "") body = strings.ReplaceAll(body, "
    ", "
    ") body = strings.ReplaceAll(body, "", "

    ") body = strings.ReplaceAll(body, "
  1. ", "

    ") re, _ := regexp.Compile("") body = re.ReplaceAllString(body, "") reLi, _ := regexp.Compile("") 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, " 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 }