12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172 |
- package services
- import (
- "encoding/json"
- "errors"
- "eta/eta_api/models"
- "eta/eta_api/models/company"
- "eta/eta_api/models/report"
- "eta/eta_api/models/report_approve"
- "eta/eta_api/models/system"
- "eta/eta_api/services/alarm_msg"
- "eta/eta_api/services/public_api"
- "eta/eta_api/utils"
- "fmt"
- "github.com/PuerkitoBio/goquery"
- "html"
- "os"
- "regexp"
- "strconv"
- "strings"
- "time"
- )
- func GetReportContentSub(content string) (contentSub string, err error) {
- content = html.UnescapeString(content)
- doc, err := goquery.NewDocumentFromReader(strings.NewReader(content))
- if err != nil {
- fmt.Println("create doc err:", err.Error())
- return
- }
- n := 0
- doc.Find("p").Each(func(i int, s *goquery.Selection) {
- if n >= 5 {
- return
- }
- n++
- phtml, err := s.Html()
- if err != nil {
- fmt.Println("get html err", err.Error())
- return
- }
- if s.Text() != "" || strings.Contains(phtml, "src") {
- contentSub = contentSub + "<p>" + phtml + "</p>"
- }
- })
- return
- }
- func PublishDayWeekReport(reportInfo *models.Report) (tips string, err error, errMsg string) {
- reportId := reportInfo.Id
- if reportInfo.State == 2 {
- return
- }
-
- chapters, err := models.GetChapterListByReportId(reportId)
- if err != nil {
- return
- }
- chapterLen := len(chapters)
- if chapterLen <= 0 {
- tips = "报告章节为空,不可发布"
- errMsg = tips
- err = errors.New(tips)
- return
- }
- reportChapterIdList := make([]int, 0)
-
- chapterList, err := models.GetChapterListByReportId(reportId)
- if err != nil {
- return
- }
- for _, chapter := range chapterList {
- if chapter.PublishState == 1 {
- tips = "还存在未发布的章节"
- errMsg = tips
- err = errors.New(tips)
- return
- }
- reportChapterIdList = append(reportChapterIdList, chapter.ReportChapterId)
- }
-
- state, e := CheckReportCurrState(report_approve.FlowReportTypeChinese, reportInfo.ClassifyIdFirst, reportInfo.ClassifyIdSecond, reportInfo.ClassifyIdThird, models.ReportOperatePublish)
- if e != nil {
-
- err = errors.New("校验报告当前状态失败, Err: " + e.Error())
- return
- }
-
- if state != models.ReportStatePublished {
-
- if e = models.ResetReportById(reportId, state); e != nil {
-
- err = fmt.Errorf("重置报告状态失败, Err: %s, ReportId: %d", e.Error(), reportId)
- return
- }
- return
- }
-
- updateCols := make([]string, 0)
- updateCols = append(updateCols, "Title", "State", "ModifyTime")
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- reportInfo.State = state
-
- if reportInfo.MsgIsSend == 0 || reportInfo.PublishTime.IsZero() {
- reportInfo.PublishTime = time.Now().Local()
- updateCols = append(updateCols, "PublishTime")
- }
- reportInfo.ModifyTime = time.Now().Local()
- if e := models.PublishReportAndChapter(reportInfo, true, updateCols); e != nil {
- err = errors.New("发布报告及章节失败")
- return
- }
-
- if len(reportChapterIdList) > 0 {
- go func() {
- _ = UpdateChaptersVideo(reportChapterIdList)
- }()
- }
-
- go func() {
- _ = UpdateReportEs(reportInfo.Id, 2)
- }()
-
- go SaveReportLogs(reportInfo, chapters, reportInfo.AdminId, reportInfo.AdminRealName)
- return
- }
- func UpdateChaptersVideo(ids []int) (err error) {
- defer func() {
- if err != nil {
- utils.FileLog.Error("UpdateChaptersVideo, chapterIds:%v, Err:%s", ids, err.Error())
- go alarm_msg.SendAlarmMsg(fmt.Sprintf("更新章节音频失败, 章节ID: %v; Err: "+err.Error(), ids), 3)
- }
- }()
- if len(ids) <= 0 {
- return
- }
- chapterList, err := models.GetChapterListByChapterIds(ids)
- if err != nil {
- return
- }
-
- nowTime := time.Now()
- updateCols := make([]string, 0)
- updateCols = append(updateCols, "VideoUrl", "VideoName", "VideoSize", "VideoPlaySeconds")
- for i := 0; i < len(chapterList); i++ {
- item := chapterList[i]
-
- if item.VideoUrl != "" && item.VideoName != "" && item.VideoSize != "" && item.VideoPlaySeconds != "" {
- continue
- }
- videoUrl, videoName, videoSize, videoPlaySeconds, e := CreateReportVideo(item.Title, html.UnescapeString(item.Content), nowTime.Format(utils.FormatDateTime))
- if e != nil {
- err = e
- return
- }
- item.VideoUrl = videoUrl
- item.VideoName = videoName
- item.VideoSize = videoSize
- item.VideoPlaySeconds = fmt.Sprintf("%.2f", videoPlaySeconds)
- if e = item.UpdateChapter(updateCols); e != nil {
- err = e
- }
- }
- return
- }
- func PublishTodayDayReport() (err error) {
- nowTime := time.Now()
- startTime := time.Date(nowTime.Year(), nowTime.Month(), nowTime.Day(), 0, 0, 0, 0, time.Local)
- endTime := time.Date(nowTime.Year(), nowTime.Month(), nowTime.Day(), 23, 59, 59, 0, time.Local)
- todayReport, err := models.GetUnPublishDayReport(startTime, endTime)
- if err != nil {
- if err.Error() == utils.ErrNoRow() {
- err = nil
- }
- return
- }
- if todayReport != nil {
- if _, tmpErr, _ := PublishDayWeekReport(todayReport); tmpErr != nil {
- err = tmpErr
- return
- }
-
- reportDetail, tmpErr := models.GetReportById(todayReport.Id)
- if tmpErr != nil {
- err = tmpErr
- return
- }
-
- if tmpErr = SendMiniProgramReportWxMsg(todayReport.Id); tmpErr != nil {
- err = tmpErr
- return
- }
- if tmpErr = models.ModifyReportThsMsgIsSend(reportDetail); tmpErr != nil {
- err = tmpErr
- return
- }
- }
- return
- }
- func UpdateReportEs(reportId int, publishState int) (err error) {
- if reportId <= 0 {
- return
- }
- reportInfo, err := models.GetReportByReportId(reportId)
- if err != nil {
- return
- }
- categories := ""
- if reportInfo.HasChapter == 1 {
-
- chapterList, tmpErr := models.GetPublishedChapterListByReportId(reportInfo.Id)
- if tmpErr != nil {
- return
- }
- if len(chapterList) > 0 {
-
- for _, chapterInfo := range chapterList {
- err = updateReportChapterEsByChapter(chapterInfo)
- if err != nil {
- return
- }
- }
- }
- } else {
-
- permissionList, tmpErr := models.GetChartPermissionNameFromMappingByKeyword("rddp", reportInfo.ClassifyIdSecond)
- if tmpErr != nil {
- return
- }
- categoryArr := make([]string, 0)
- for i := 0; i < len(permissionList); i++ {
- categoryArr = append(categoryArr, permissionList[i].PermissionName)
- }
- aliasArr, _ := addCategoryAliasToArr(categoryArr)
- categories = strings.Join(aliasArr, ",")
-
- }
-
- minClassifyId, minClassifyName, err := getMinClassify(reportInfo)
- if err != nil {
- return
- }
-
- esReport := &models.ElasticReportDetail{
- ReportId: reportInfo.Id,
- ReportChapterId: 0,
- Title: reportInfo.Title,
- Abstract: reportInfo.Abstract,
- BodyContent: utils.TrimHtml(html.UnescapeString(reportInfo.Content)),
- PublishTime: reportInfo.PublishTime.Format(utils.FormatDateTime),
- PublishState: publishState,
- Author: reportInfo.Author,
- ClassifyIdFirst: reportInfo.ClassifyIdFirst,
- ClassifyNameFirst: reportInfo.ClassifyNameFirst,
- ClassifyIdSecond: reportInfo.ClassifyIdSecond,
- ClassifyNameSecond: reportInfo.ClassifyNameSecond,
- ClassifyId: minClassifyId,
- ClassifyName: minClassifyName,
- Categories: categories,
- StageStr: strconv.Itoa(reportInfo.Stage),
- }
- docId := fmt.Sprintf("%d-%d", reportInfo.Id, 0)
- if err = EsAddOrEditReport(utils.EsReportIndexName, docId, esReport); err != nil {
- return
- }
- return
- }
- func addCategoryAliasToArr(categoryArr []string) (aliasArr []string, err error) {
- aliasArr = categoryArr
- if len(categoryArr) > 0 {
- for i := 0; i < len(categoryArr); i++ {
- if strings.Contains(categoryArr[i], "沥青") {
- aliasArr = append(aliasArr, "BU")
- }
- if strings.Contains(categoryArr[i], "MEG") {
- aliasArr = append(aliasArr, "EG", "乙二醇")
- }
- if strings.Contains(categoryArr[i], "聚酯") {
- aliasArr = append(aliasArr, "长丝", "短纤", "瓶片")
- }
- if strings.Contains(categoryArr[i], "纯苯+苯乙烯") {
- aliasArr = append(aliasArr, "EB")
- }
- if strings.Contains(categoryArr[i], "聚乙烯") {
- aliasArr = append(aliasArr, "PP", "PE")
- }
- if strings.Contains(categoryArr[i], "玻璃纯碱") {
- aliasArr = append(aliasArr, "玻璃", "纯碱", "FG", "SA")
- }
- if strings.Contains(categoryArr[i], "甲醇") {
- aliasArr = append(aliasArr, "甲醇", "MA")
- }
- if strings.Contains(categoryArr[i], "橡胶") {
- aliasArr = append(aliasArr, "橡胶", "RU")
- }
- }
- }
- return
- }
- func UpdateReportChapterEs(reportChapterId int) (err error) {
- if reportChapterId <= 0 {
- return
- }
- chapterInfo, err := models.GetReportChapterInfoById(reportChapterId)
- if err != nil {
- return
- }
- err = updateReportChapterEsByChapter(chapterInfo)
- if err != nil {
- return
- }
- return
- }
- func updateReportChapterEsByChapter(chapterInfo *models.ReportChapter) (err error) {
-
-
-
-
-
-
-
-
- obj := report.ReportChapterPermissionMapping{}
- permissionList, tmpErr := obj.GetPermissionItemListById(chapterInfo.ReportChapterId)
- if tmpErr != nil {
- return
- }
- categoryArr := make([]string, 0)
- if len(permissionList) > 0 {
- for ii := 0; ii < len(permissionList); ii++ {
- categoryArr = append(categoryArr, permissionList[ii].ChartPermissionName)
- }
- }
- aliasArr, _ := addCategoryAliasToArr(categoryArr)
- categories := strings.Join(aliasArr, ",")
-
- esChapter := &models.ElasticReportDetail{
- ReportId: chapterInfo.ReportId,
- ReportChapterId: chapterInfo.ReportChapterId,
- Title: chapterInfo.Title,
- Abstract: chapterInfo.Abstract,
- BodyContent: utils.TrimHtml(html.UnescapeString(chapterInfo.Content)),
- PublishTime: chapterInfo.PublishTime.Format(utils.FormatDateTime),
- PublishState: chapterInfo.PublishState,
- Author: chapterInfo.Author,
- ClassifyIdFirst: chapterInfo.ClassifyIdFirst,
- ClassifyNameFirst: chapterInfo.ClassifyNameFirst,
- ClassifyIdSecond: 0,
- ClassifyNameSecond: "",
- ClassifyId: chapterInfo.ClassifyIdFirst,
- ClassifyName: chapterInfo.ClassifyNameFirst,
- Categories: categories,
- StageStr: strconv.Itoa(chapterInfo.Stage),
- }
- chapterDocId := fmt.Sprintf("%d-%d", chapterInfo.ReportId, chapterInfo.ReportChapterId)
- if err = EsAddOrEditReport(utils.EsReportIndexName, chapterDocId, esChapter); err != nil {
- return
- }
- return
- }
- func DeleteReportAndChapter(reportId int) (err error) {
- reportInfo, err := models.GetReportByReportId(reportId)
- if err != nil {
- err = errors.New("报告信息有误, Err: " + err.Error())
- return
- }
- if reportInfo.State == 2 {
- err = errors.New("报告已发布,不可删除")
- return
- }
-
- _ = UpdateReportEs(reportId, 1)
-
- if reportInfo.HasChapter == 1 && (reportInfo.ChapterType == utils.REPORT_TYPE_DAY || reportInfo.ChapterType == utils.REPORT_TYPE_WEEK) {
- err = models.DeleteDayWeekReportAndChapter(reportId)
- } else {
- err = models.DeleteReport(reportId)
- }
- if err != nil {
- err = errors.New("删除失败, Err: " + err.Error())
- return
- }
-
- go func() {
- _ = ResetPPTReport(reportId, false)
- }()
- return
- }
- func replaceReportBase64ToImg(content string) (newContent string, err error) {
- if content == "" {
- return
- }
- pattern := "data:([a-z]+\\/[a-z0-9-+.]+(;[a-z-]+=[a-z0-9-]+)?)?(;base64)?,([A-Za-z0-9+/]{4})*([A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==)?"
- re, _ := regexp.Compile(pattern)
- matcher := re.FindAllString(content, 999)
- if len(matcher) > 0 {
- for _, v := range matcher {
- imgUrl, tmpErr := reportBase64ToImg(v)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- content = strings.ReplaceAll(content, v, imgUrl)
- }
- }
- newContent = content
- return
- }
- func reportBase64ToImg(imageBase64 string) (resourceUrl string, err error) {
- if imageBase64 == "" {
- err = errors.New("图片为空")
- return
- }
- ext := ".png"
- uploadDir := "./static"
- randStr := utils.GetRandStringNoSpecialChar(28)
- fileName := randStr + ext
- fpath := uploadDir + "/" + fileName
- b, _ := regexp.MatchString(`^data:\s*image\/(\w+);base64,`, imageBase64)
- if !b {
- err = errors.New("图片格式不正确")
- return
- }
- re, _ := regexp.Compile(`^data:\s*image\/(\w+);base64,`)
- base64Str := re.ReplaceAllString(imageBase64, "")
- base64Str = strings.Replace(base64Str, " ", "", -1)
- err = utils.SaveBase64ToFile(base64Str, fpath)
- if err != nil {
- err = errors.New("图片保存失败" + err.Error())
- return
- }
- defer os.Remove(fpath)
- hzUploadDir := utils.RESOURCE_DIR + "images/"
- savePath := hzUploadDir + time.Now().Format("200601/20060102/")
- savePath += fileName
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- ossClient := NewOssClient()
- if ossClient == nil {
- err = fmt.Errorf("初始化OSS服务失败")
- return
- }
- resourceUrl, err = ossClient.UploadFile(fileName, fpath, savePath)
- if err != nil {
- err = fmt.Errorf("文件上传失败, Err: %s", err.Error())
- return
- }
- item := new(models.Resource)
- item.ResourceUrl = resourceUrl
- item.ResourceType = 1
- item.CreateTime = time.Now()
- _, err = models.AddResource(item)
- if err != nil {
- err = errors.New("资源上传失败" + err.Error())
- return
- }
- return
- }
- func UpdateReportVideo(reportId int) (err error) {
- defer func() {
- if err != nil {
- utils.FileLog.Error("UpdateReportVideo, reportId:%s, Err:%s", strconv.Itoa(reportId), err.Error())
- go alarm_msg.SendAlarmMsg("更新报告音频失败, 报告ID: "+strconv.Itoa(reportId)+", Err: "+err.Error(), 3)
-
- }
- }()
- if reportId == 0 {
- return
- }
- reportInfo, err := models.GetReportByReportId(reportId)
- if err != nil {
- return
- }
- if reportInfo.HasChapter == 1 {
-
- chapterList, tmpErr := models.GetPublishedChapterListByReportId(reportInfo.Id)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- chapterIdArr := make([]int, 0)
- for i := 0; i < len(chapterList); i++ {
- chapterIdArr = append(chapterIdArr, chapterList[i].ReportChapterId)
- }
-
- err = UpdateChaptersVideo(chapterIdArr)
- } else {
-
- if reportInfo.VideoUrl != "" {
- return
- }
- nowTime := time.Now()
- updateCols := make([]string, 0)
- updateCols = append(updateCols, "VideoUrl", "VideoName", "VideoSize", "VideoPlaySeconds")
- videoUrl, videoName, videoSize, videoPlaySeconds, tmpErr := CreateReportVideo(reportInfo.Title, html.UnescapeString(reportInfo.Content), nowTime.Format(utils.FormatDateTime))
- reportInfo.VideoUrl = videoUrl
- reportInfo.VideoName = videoName
- reportInfo.VideoSize = videoSize
- reportInfo.VideoPlaySeconds = fmt.Sprintf("%.2f", videoPlaySeconds)
- tmpErr = reportInfo.UpdateReport(updateCols)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- }
- return
- }
- func UpdateEmptyVideoReportVideo() (err error) {
- list, err := models.GetSyncEmptyVideoReport()
- if err != nil {
- return
- }
- listLen := len(list)
- if listLen <= 0 {
- fmt.Println("无报告需要更新音频")
- return
- }
- fmt.Println("Start 待更新报告音频数: ", listLen)
- for i := 0; i < listLen; i++ {
- if err = UpdateReportVideo(list[i].Id); err != nil {
- fmt.Printf("更新音频失败")
- fmt.Println(err.Error())
- return
- }
- }
- fmt.Println("End 报告音频更新完毕")
- return
- }
- func checkDayWeekChapterWrite(chapters []*models.ReportChapter, reportType string) (publishReport bool, tips string, publishIdArr, unPublishIdArr []int, err error) {
- nowTime := time.Now().Local()
- updateTypeArr := make([]int, 0)
- publishIdArr = make([]int, 0)
- unPublishIdArr = make([]int, 0)
-
- if reportType == utils.REPORT_TYPE_DAY {
-
- isEmpty := true
- for i := 0; i < len(chapters); i++ {
- if chapters[i].Content != "" && chapters[i].Title != "" {
- isEmpty = false
- break
- }
- }
- if isEmpty {
- err = errors.New("报告章节内容均为空或标题为空,不可发布")
- return
- }
- } else {
-
- editNum := 0
- for i := 0; i < len(chapters); i++ {
- if chapters[i].IsEdit == 1 && chapters[i].Title != "" {
- editNum += 1
- }
- }
- if editNum == 0 {
- err = errors.New("报告均未编辑或标题为空,不可发布")
- return
- }
- }
-
- types, e := models.GetReportChapterTypeListByResearchType(reportType)
- if e != nil {
- err = errors.New("获取章节类型列表失败")
- return
- }
-
- typeLen := len(types)
- for i := 0; i < typeLen; i++ {
- if types[i].IsSet != 1 && types[i].Enabled != 0 {
-
- updateTypeArr = append(updateTypeArr, types[i].ReportChapterTypeId)
- } else {
-
- if types[i].PauseStartTime == utils.EmptyDateStr && types[i].PauseEndTime == utils.EmptyDateStr {
- updateTypeArr = append(updateTypeArr, types[i].ReportChapterTypeId)
- continue
- }
-
- startTime, _ := time.Parse(utils.FormatDate, types[i].PauseStartTime)
- endTime, _ := time.Parse(utils.FormatDate, types[i].PauseEndTime)
- if nowTime.Before(startTime) || nowTime.After(endTime.AddDate(0, 0, 1)) {
- updateTypeArr = append(updateTypeArr, types[i].ReportChapterTypeId)
- }
- }
- }
-
- chapterLen := len(chapters)
- updateTypeLen := len(updateTypeArr)
- tipsArr := make([]string, 0)
- for i := 0; i < chapterLen; i++ {
- isWrite := false
- for ii := 0; ii < updateTypeLen; ii++ {
-
- if chapters[i].TypeId == updateTypeArr[ii] {
-
- if chapters[i].Title == "" || chapters[i].Content == "" {
- tipsArr = append(tipsArr, chapters[i].TypeName)
- break
- }
- isWrite = true
- break
- }
- }
- if isWrite {
- publishIdArr = append(publishIdArr, chapters[i].ReportChapterId)
- } else {
- unPublishIdArr = append(unPublishIdArr, chapters[i].ReportChapterId)
- }
- }
- if len(tipsArr) > 0 {
- tips = "部分章节未发布:" + strings.Join(tipsArr, "、") + "未填写标题/内容"
- }
-
- if reportType == utils.REPORT_TYPE_DAY {
- publishReport = true
- } else {
- if len(publishIdArr) == updateTypeLen {
- publishReport = true
- }
- }
- return
- }
- func PcCreateAndUploadSunCode(scene, page string) (imgUrl string, err error) {
- if page == "" {
- err = errors.New("page不能为空")
- return
- }
-
- sceneMD5 := "a=1"
- if scene != "" {
- sceneMD5 = utils.MD5(scene)
- }
- picByte, err := GetSunCode(page, sceneMD5)
- if err != nil {
- return
- }
-
- localPath := "./static/imgs"
- fileName := utils.GetRandStringNoSpecialChar(28) + ".png"
- fpath := fmt.Sprint(localPath, "/", fileName)
- f, err := os.Create(fpath)
- if err != nil {
- fmt.Println("11111")
- return
- }
- if _, err = f.Write(picByte); err != nil {
- return
- }
- defer func() {
- f.Close()
- os.Remove(fpath)
- }()
-
- fileDir := "yb/suncode/"
-
-
-
-
-
-
-
-
-
-
-
-
- savePath := fileDir + time.Now().Format("200601/20060102/") + fileName
- ossClient := NewOssClient()
- if ossClient == nil {
- err = fmt.Errorf("初始化OSS服务失败")
- return
- }
- imgUrl, err = ossClient.UploadFile(fileName, fpath, savePath)
- if err != nil {
- err = fmt.Errorf("文件上传失败, Err: %s", err.Error())
- return
- }
- if err != nil {
- return
- }
-
- if scene != "" {
- newSuncode := &models.YbPcSuncode{
- Scene: scene,
- SceneMd5: sceneMD5,
- CodePage: page,
- SuncodeUrl: imgUrl,
- CreateTime: time.Now(),
- }
- err = models.AddYbPcSunCode(newSuncode)
- }
-
- if scene != "" {
- newPars := &models.YbSuncodePars{
- Scene: scene,
- SceneKey: sceneMD5,
- CreateTime: time.Now(),
- }
- err = models.AddYbSuncodePars(newPars)
- }
- return
- }
- func CreateNewReport(req models.AddReq, adminInfo *system.Admin) (newReportId int64, reportCode, errMsg string, err error) {
- contentSub := ""
- if req.Content != "" {
- e := utils.ContentXssCheck(req.Content)
- if e != nil {
- errMsg = "存在非法标签"
- err = errors.New("存在非法标签, Err: " + e.Error())
- return
- }
- contentClean, e := FilterReportContentBr(req.Content)
- if e != nil {
- errMsg = "内容去除前后空格失败"
- err = errors.New("内容去除前后空格失败, Err: " + e.Error())
- return
- }
- req.Content = contentClean
- sub, e := GetReportContentSub(req.Content)
- if e != nil {
- go alarm_msg.SendAlarmMsg("ContentSub 失败,Err:"+e.Error(), 3)
- }
- contentSub = sub
- }
- maxStage, e := models.GetReportStage(req.ClassifyIdFirst, req.ClassifyIdSecond, req.ClassifyIdThird)
- if e != nil {
- errMsg = "期数获取失败!"
- err = errors.New("期数获取失败,Err:" + e.Error())
- return
- }
- item := new(models.Report)
- item.AddType = req.AddType
- item.ClassifyIdFirst = req.ClassifyIdFirst
- item.ClassifyNameFirst = req.ClassifyNameFirst
- item.ClassifyIdSecond = req.ClassifyIdSecond
- item.ClassifyNameSecond = req.ClassifyNameSecond
- item.Title = req.Title
- item.Abstract = req.Abstract
- item.Author = req.Author
- item.Frequency = req.Frequency
- item.State = req.State
- item.Content = html.EscapeString(req.Content)
- item.Stage = maxStage + 1
- item.ContentSub = html.EscapeString(contentSub)
- item.CreateTime = req.CreateTime
- item.ModifyTime = time.Now()
- item.ReportVersion = req.ReportVersion
- item.AdminId = adminInfo.AdminId
- item.AdminRealName = adminInfo.RealName
- item.ClassifyIdThird = req.ClassifyIdThird
- item.ClassifyNameThird = req.ClassifyNameThird
-
- if req.CollaborateType == 2 {
- item.HasChapter = 1
- item.ChapterType = ""
- }
- item.LastModifyAdminId = adminInfo.AdminId
- item.LastModifyAdminName = adminInfo.RealName
- item.ContentModifyTime = time.Now()
- item.NeedSplice = 1
- item.ContentStruct = html.EscapeString(req.ContentStruct)
- item.HeadImg = req.HeadImg
- item.EndImg = req.EndImg
- item.CanvasColor = req.CanvasColor
- item.HeadResourceId = req.HeadResourceId
- item.EndResourceId = req.EndResourceId
- item.CollaborateType = req.CollaborateType
- item.ReportLayout = req.ReportLayout
- item.IsPublicPublish = req.IsPublicPublish
- item.ReportCreateTime = time.Now()
- err, errMsg = AddReportAndChapter(item, 0, req.GrantAdminIdList)
- return
- }
- func FilterReportContentBr(content string) (res string, err error) {
- newContent := content
-
- content = html.UnescapeString(content)
- if content == "" {
- return
- }
-
- content = strings.Replace(content, "<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)
- defer func() {
- if err != nil {
- go alarm_msg.SendAlarmMsg("过滤报告正文前后换行符及空格失败, ErrMsg: "+err.Error(), 3)
- }
- }()
-
- configKey := "report_filter_br"
- conf, e := company.GetConfigDetailByCode(configKey)
- if e != nil {
- err = errors.New("获取报告过滤配置失败, Err: " + e.Error())
- return
- }
- if conf.ConfigValue != "1" {
- return content, nil
- }
-
- re := regexp.MustCompile(`(?is:<p(.*?)</p>)`)
- arr := re.FindAllString(content, -1)
- indexArr := re.FindAllIndex([]byte(content), -1)
-
- emptyRe := `<p[^>]*>(<br>|<br/>)+</p>`
- startIsBr := false
- countEmptyBr := 0
- lastBrRange := 0
-
- for i := range arr {
- byteRange := indexArr[i]
- if len(byteRange) == 2 {
-
- if i == 0 && byteRange[0] == 0 {
- startIsBr = true
- }
- if !startIsBr {
- break
- }
- if lastBrRange != 0 {
-
- if lastBrRange != byteRange[0] {
- break
- }
- }
-
- m, e := regexp.Match(emptyRe, []byte(arr[i]))
- if e != nil {
- err = e
- return
- }
- if m {
- countEmptyBr += 1
- lastBrRange = byteRange[1]
- continue
- }
-
- break
- }
- }
- if countEmptyBr > 0 {
- reg, e := regexp.Compile(emptyRe)
- if e != nil {
- err = errors.New("正则解析失败, Err: " + e.Error())
- return
- }
- counted := 0
- res = reg.ReplaceAllStringFunc(content, func(s string) string {
- counted += 1
- if counted <= countEmptyBr {
- return ""
- } else {
- return s
- }
- })
- if res == "" {
- res = newContent
- }
- } else {
- res = content
- if res == "" {
- res = newContent
- }
- }
- return
- }
- func GetEnglishReportOverview(content string) (res string, err error) {
- content = html.UnescapeString(content)
- doc, e := goquery.NewDocumentFromReader(strings.NewReader(content))
- if e != nil {
- err = errors.New("Create Doc Err: " + e.Error())
- return
- }
- target := "overview"
- label := "</strong>"
- start := -1
- end := -1
- doc.Find("p").Each(func(i int, s *goquery.Selection) {
- h, e := s.Html()
- if e != nil {
- err = errors.New("Get Html1 Err: " + e.Error())
- return
- }
- h = strings.ToLower(h)
- t := s.Text()
- t = strings.ToLower(t)
- if strings.Contains(h, label) && t != "" && strings.Contains(t, target) {
- start = i
- }
- if start != -1 && end == -1 && i > start && strings.Contains(h, label) {
- end = i
- }
- })
- if start != -1 && end != -1 {
- doc.Find("p").Each(func(i int, s *goquery.Selection) {
- if i > start && i < end {
- h, e := s.Html()
- if e != nil {
- err = errors.New("Get Html2 Err: " + e.Error())
- return
- }
-
- if strings.Contains(h, "iframe") {
- return
- }
- res += `<p>` + h + `</p>`
- }
- })
- }
- return
- }
- func GetReportContentSubWithoutIframe(content string) (contentSub string, err error) {
- content = html.UnescapeString(content)
- doc, err := goquery.NewDocumentFromReader(strings.NewReader(content))
- if err != nil {
- fmt.Println("create doc err:", err.Error())
- return
- }
- label := "iframe"
- n := 0
- doc.Find("p").Each(func(i int, s *goquery.Selection) {
- if n >= 5 {
- return
- }
- n++
- h, err := s.Html()
- if err != nil {
- fmt.Println("get html err", err.Error())
- return
- }
-
- if strings.Contains(h, label) {
- return
- }
- if s.Text() != "" || strings.Contains(h, "src") {
- contentSub = contentSub + "<p>" + h + "</p>"
- }
- })
- return
- }
- func UpdateReportEditMark(reportId, reportChapterId, nowUserId, status int, nowUserName, lang string) (ret models.MarkReportResp, err error) {
-
- key := fmt.Sprint(`crm:report:edit:`, reportId)
-
- if reportChapterId > 0 {
- key = fmt.Sprint(key, ":", reportChapterId)
- }
- ret.Status = 0
- ret.Msg = "无人编辑"
- opUserId, e := utils.Rc.RedisInt(key)
- var opUser models.MarkReportItem
- var classifyNameFirst string
- if e != nil {
- opUserInfoStr, tErr := utils.Rc.RedisString(key)
- if tErr == nil {
- tErr = json.Unmarshal([]byte(opUserInfoStr), &opUser)
- if tErr == nil {
- opUserId = opUser.AdminId
- }
- }
- }
-
- var reportInfo *models.ReportDetail
- classifyNameFirst = opUser.ReportClassifyNameFirst
- if reportId > 0 && status != 2 && classifyNameFirst == "" {
-
- reportInfo, err = models.GetReportById(reportId)
- if err != nil {
- err = fmt.Errorf("报告不存在")
- return
- }
- classifyNameFirst = reportInfo.ClassifyNameFirst
- }
- if opUserId > 0 && opUserId != nowUserId {
- editor := opUser.Editor
- if editor == "" {
-
- otherInfo, e := system.GetSysAdminById(opUserId)
- if e != nil {
- err = fmt.Errorf("查询其他编辑者信息失败")
- return
- }
- editor = otherInfo.RealName
- }
- ret.Status = 1
- if lang == utils.EnLangVersion {
- ret.Msg = fmt.Sprintf("%s is currently editing the report", editor)
- } else {
- ret.Msg = fmt.Sprintf("当前%s正在编辑报告", editor)
- }
- ret.Editor = editor
- return
- }
- if status == 1 {
- nowUser := &models.MarkReportItem{AdminId: nowUserId, Editor: nowUserName, ReportClassifyNameFirst: classifyNameFirst}
- bt, e := json.Marshal(nowUser)
- if e != nil {
- err = fmt.Errorf("格式化编辑者信息失败")
- return
- }
- if opUserId > 0 {
- utils.Rc.Do("SETEX", key, int64(60), string(bt))
- } else {
- utils.Rc.SetNX(key, string(bt), time.Second*60*1)
- }
- } else if status == 3 {
-
- _ = utils.Rc.Delete(key)
- }
- return
- }
- func HandleVideoDecibel(chapterInfo *models.ReportChapter) {
- public_api.HandleVideoDecibel(chapterInfo.ReportChapterId)
- return
- }
- func SaveReportLogs(item *models.Report, chapters []*models.ReportChapter, adminId int, adminRealName string) {
- if item == nil && len(chapters) == 0 {
- return
- }
- var err error
- defer func() {
- if err != nil {
- tips := fmt.Sprintf("报告日志记录, SaveReportLogs error: %s", err.Error())
- go alarm_msg.SendAlarmMsg(tips, 2)
- }
- }()
- if item != nil {
- e := models.AddReportSaveLog(item.Id, item.AdminId, item.Content, item.ContentSub, item.ContentStruct, item.CanvasColor, item.AdminRealName, item.HeadResourceId, item.EndResourceId)
- if e != nil {
- err = fmt.Errorf("AddReportSaveLog: %s", e.Error())
- return
- }
- }
- if len(chapters) > 0 {
- e := models.MultiAddReportChaptersSaveLog(chapters, adminId, adminRealName)
- if e != nil {
- err = fmt.Errorf("MultiAddReportChaptersSaveLog: %s", e.Error())
- return
- }
- }
- return
- }
|