123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845 |
- package controllers
- import (
- "bufio"
- "github.com/pdfcpu/pdfcpu/pkg/api"
- "github.com/pdfcpu/pdfcpu/pkg/pdfcpu"
- "io"
- //"bufio"
- "encoding/json"
- "fmt"
- "github.com/medivhzhan/weapp/v2"
- "os"
- //"github.com/pdfcpu/pdfcpu/pkg/api"
- //"github.com/pdfcpu/pdfcpu/pkg/pdfcpu"
- "hongze/hongze_mfyx/models"
- "hongze/hongze_mfyx/services"
- "hongze/hongze_mfyx/utils"
- "html"
- nhttp "net/http"
- "strconv"
- "strings"
- "time"
- )
- type ArticleController struct {
- BaseAuthController
- }
- type ArticleCommonController struct {
- BaseCommonController
- }
- type ArticleControllerMobile struct {
- BaseAuthMobileController
- }
- // @Title 获取报告详情
- // @Description 获取报告详情接口
- // @Param ArticleId query int true "报告ID"
- // @Param IsSendWx query int false "是否是通过微信模版进来的 1是,其它否"
- // @Param InviteShareCode query string false "销售账号邀请码"
- // @Success 200 {object} models.ArticleDetailResp
- // @router /detail [get]
- func (this *ArticleController) Detail() {
- br := new(models.BaseResponse).Init()
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- user := this.User
- if user == nil {
- br.Msg = "请登录"
- br.ErrMsg = "请登录,用户信息为空"
- br.Ret = 408
- return
- }
- uid := user.UserId
- articleId, err := this.GetInt("ArticleId")
- isSendWx, _ := this.GetInt("IsSendWx")
- if articleId <= 0 {
- br.Msg = "文章不存在"
- br.ErrMsg = "文章不存在,文章ID错误"
- return
- }
- inviteShareCode := this.GetString("InviteShareCode")
- detail := new(models.ArticleDetail)
- hasPermission := 0
- var haveResearch bool
- resp := new(models.ArticleDetailResp)
- detail, err = models.GetArticleDetailById(articleId)
- if err != nil {
- br.Msg = "获取信息失败"
- br.ErrMsg = "获取信息失败,Err:" + err.Error()
- return
- }
- //记录分享来源
- if inviteShareCode != "" {
- go services.AddCygxUserAdminShareHistory(user, utils.CYGX_OBJ_ARTICLE, detail.Title, inviteShareCode, articleId)
- }
- detail.PublishDate = utils.TimeRemoveHms2(detail.PublishDate)
- detail.Body = html.UnescapeString(detail.Body)
- detail.Body = strings.Replace(detail.Body, "<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)
- detail.Body = strings.Replace(detail.Body, "pre", "div", -1)
- detail.Abstract = html.UnescapeString(detail.Abstract)
- //detail.Abstract, _ = services.GetReportContentTextSubNew(detail.Abstract)
- //作者头像
- if detail.DepartmentId > 0 {
- departmentDetail, err := models.GetArticleDepartmentDateilById(detail.DepartmentId)
- if err == nil {
- detail.DepartmentImgUrl = departmentDetail.ImgUrl
- }
- }
- // 判断是否属于研选类型的报告
- detail.IsResearch = true
- articleCollectMap, _ := services.GetCygxArticleCollectMap(user.UserId)
- detail.IsCollect = articleCollectMap[detail.ArticleId]
- //是否是通过模板消息进来的
- if isSendWx == 1 {
- var condition string
- var pars []interface{}
- pars = make([]interface{}, 0)
- condition = ` AND article_id = ? `
- reportMappingMap, _ := services.GetReportMappingMap()
- if reportMappingMap[detail.CategoryId] {
- chooseCategoryMap, _ := services.GetChooseCategoryMap(user)
- detail.IsShowFollowButton = true
- detail.IsFollowButton = chooseCategoryMap[detail.CategoryId]
- } else {
- pars = append(pars, articleId)
- industrialList, err := models.GetIndustrialArticleGroupManagementList(condition, pars)
- if err != nil && err.Error() != utils.ErrNoRow() {
- br.Msg = "获取信息失败"
- br.ErrMsg = "获取信息失败,Err:" + err.Error()
- return
- }
- if len(industrialList) > 0 {
- industryUserFollowMap, err := services.GetIndustryUserFollowMap(user)
- if err != nil {
- br.Msg = "获取信息失败"
- br.ErrMsg = "GetActivitySignupResp,Err:" + err.Error()
- return
- }
- for _, v := range industrialList {
- if industryUserFollowMap[v.IndustrialManagementId] {
- detail.IsFollowButton = true
- }
- }
- detail.IsShowFollowButton = true
- }
- }
- }
- havePower, err := services.GetArticleDetailUserPower(user)
- if err != nil {
- br.Msg = "获取信息失败"
- br.ErrMsg = "校验用户权限失败,Err:" + err.Error()
- return
- }
- if havePower {
- hasPermission = 1
- go services.ArticleHistory(articleId, user)
- //30分钟之内阅读同一篇文章不错二次推送
- key := "CYGX_ARTICLE_READ" + strconv.Itoa(articleId) + "_" + strconv.Itoa(uid)
- if !utils.Rc.IsExist(key) {
- go services.ArticleUserRemind(user, detail, 1)
- // 互动提醒
- go services.SendWxCategoryMsgInteractive(user, "阅读报告", articleId, detail.Title)
- utils.Rc.Put(key, 1, 30*time.Second)
- }
- interviewApplyItem, err := models.GetArticleInterviewApply(uid, articleId)
- if err != nil && err.Error() != utils.ErrNoRow() {
- br.Msg = "获取信息失败"
- br.ErrMsg = "判断是否已申请访谈失败,Err:" + strconv.Itoa(uid) + ";articleId" + strconv.Itoa(articleId)
- return
- }
- if interviewApplyItem != nil && interviewApplyItem.InterviewApplyId > 0 {
- detail.IsInterviewApply = true
- detail.InterviewApplyStatus = interviewApplyItem.Status
- }
- //获取销售手机号
- sellerItem, err := models.GetSellerByCompanyId(user.CompanyId)
- if err != nil && err.Error() != utils.ErrNoRow() {
- br.Msg = "获取信息失败"
- br.ErrMsg = "获取销售数据失败2,Err:" + err.Error() + strconv.Itoa(uid) + ";articleId" + strconv.Itoa(articleId)
- return
- }
- if sellerItem != nil {
- detail.SellerMobile = sellerItem.Mobile
- detail.SellerName = sellerItem.RealName
- }
- sellerList, err := models.GetSellerList(articleId)
- if err != nil {
- br.Msg = "获取信息失败"
- br.ErrMsg = "获取销售数据失败,Err:" + err.Error() + strconv.Itoa(uid) + ";articleId" + strconv.Itoa(articleId)
- return
- }
- if detail.ArticleId >= utils.SummaryArticleId && strings.Contains(detail.SellerAndMobile, "-") {
- strnum := strings.Index(detail.SellerAndMobile, "-")
- detail.SellerAndMobile = detail.SellerAndMobile[0:strnum]
- if strnum > 0 {
- nickName := detail.SellerAndMobile[0:strnum]
- sellerAndMobile := &models.SellerRep{
- SellerMobile: "",
- SellerName: nickName,
- }
- sellerList = append(sellerList, sellerAndMobile)
- }
- }
- articleFollowdetail, err := models.GetArticleFollowDetail(articleId, uid)
- if err != nil {
- br.Msg = "获取信息失败"
- br.ErrMsg = "获取关注信息失败,Err:" + err.Error()
- return
- }
- detail.FollowNum = articleFollowdetail.DNum
- detail.CollectionNum = articleFollowdetail.AcNum
- if articleFollowdetail.MdNum > 0 {
- detail.IsFollow = true
- }
- if detail.IsSummary == 1 {
- detail.IsBelongSummary = true
- }
- if detail.IsReport == 1 {
- detail.IsBelongReport = true
- }
- } else {
- hasPermission, err = services.GetUserDetailPermissionCode(user.UserId, user.CompanyId)
- if err != nil && err.Error() != utils.ErrNoRow() {
- br.Msg = "获取信息失败"
- br.ErrMsg = "获取信息失败,GetUserDetailPermissionCode Err:" + err.Error()
- return
- }
- //权益客户对权益销售信息进行展示
- if hasPermission == 2 || hasPermission == 3 {
- sellerItemQy, err := models.GetSellerByCompanyIdCheckFicc(user.CompanyId, 2)
- if err != nil && err.Error() != utils.ErrNoRow() {
- br.Msg = "获取信息失败"
- br.ErrMsg = "获取销售信息失败,Err:" + err.Error()
- return
- }
- if sellerItemQy != nil {
- detail.SellerMobile = sellerItemQy.Mobile
- detail.SellerName = sellerItemQy.RealName
- }
- }
- detail.Body = ""
- detail.BodyText = ""
- resp.IsShowWxPay = utils.IS_SHOW_WX_PAY //是否展示微信支付按钮
- resp.IsCompanyApply = services.GetUserApplyRecordCountByCompanyIdPay(user.CompanyId) //获取客户是否有过历史申请记录
- resp.IsNeedBusinessCard = services.GetCygxUserBusinessCardCount(user.UserId, user.CompanyId) //是否需要上传名片
- resp.GoodsList = services.GetUserGoodsCardList() //日卡月卡商品信息
- for _, v := range resp.GoodsList {
- resp.PopupPriceMsg += v.PopupPriceMsg //价格弹窗信息
- }
- }
- if hasPermission != 1 {
- hasPermission, err = services.GetUserPermissionCode(user.UserId, user.CompanyId)
- if err != nil {
- br.Msg = "获取信息失败"
- br.ErrMsg = "获取用户状态信息失败,Err:" + err.Error()
- return
- }
- }
- if detail.ArticleTypeId == 14 {
- detail.IsApplyAppointmentExpert = true //判断文章类型是否属于专家访谈 查研观向11.0
- }
- if user.UserId == 0 {
- hasPermission = 1
- }
- resp.HasPermission = hasPermission
- resp.HaveResearch = haveResearch
- resp.Detail = detail
- if user.Mobile != "" {
- resp.Mobile = user.Mobile
- } else {
- resp.Mobile = user.Email
- }
- br.Ret = 200
- br.Success = true
- br.Msg = "获取成功"
- br.Data = resp
- }
- // @Title 收藏
- // @Description 收藏
- // @Param request body models.ArticleCollectReq true "type json string"
- // @Success 200 {object} models.FontsCollectResp
- // @router /collect [post]
- func (this *ArticleController) ArticleCollect() {
- br := new(models.BaseResponse).Init()
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- user := this.User
- if user == nil {
- br.Msg = "请重新登录"
- br.Ret = 408
- return
- }
- uid := user.UserId
- var req models.ArticleCollectReq
- err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
- if err != nil {
- br.Msg = "参数解析异常!"
- br.ErrMsg = "参数解析失败,Err:" + err.Error()
- return
- }
- articleId := req.ArticleId
- detail, err := models.GetArticleDetailById(articleId)
- if err != nil {
- br.Msg = "获取信息失败"
- br.ErrMsg = "获取信息失败,Err:" + err.Error()
- return
- }
- count, err := models.GetArticleCollectCount(uid, articleId)
- if err != nil {
- br.Msg = "获取数据失败!"
- br.ErrMsg = "获取数据失败,Err:" + err.Error()
- return
- }
- resp := new(models.ArticleCollectResp)
- if count <= 0 {
- item := new(models.CygxArticleCollect)
- item.ArticleId = req.ArticleId
- item.UserId = uid
- item.CreateTime = time.Now()
- item.Mobile = user.Mobile
- item.Email = user.Email
- item.CompanyId = user.CompanyId
- item.CompanyName = user.CompanyName
- item.RealName = user.RealName
- _, err = models.AddCygxArticleCollect(item)
- if err != nil {
- br.Msg = "收藏失败"
- br.ErrMsg = "收藏失败,Err:" + err.Error()
- return
- }
- br.Msg = "收藏成功"
- resp.Status = 1
- // 文章收藏消息发送
- //go services.ArticleUserRemind(user, detail, 2)
- go services.ArticleHistoryUserLabelLogAdd(articleId, user.UserId)
- // 互动提醒
- go services.SendWxCategoryMsgInteractive(user, "收藏报告", articleId, detail.Title)
- } else {
- err = models.RemoveArticleCollect(uid, articleId)
- if err != nil {
- br.Msg = "取消收藏失败"
- br.ErrMsg = "取消收藏失败,Err:" + err.Error()
- return
- }
- br.Msg = "已取消收藏"
- resp.Status = 2
- }
- collectTotal, err := models.GetArticleCollectUsersCount(articleId)
- if err != nil {
- br.Msg = "获取数据失败"
- br.ErrMsg = "获取数据失败,Err:" + err.Error()
- return
- }
- resp.CollectCount = collectTotal
- br.Ret = 200
- br.Success = true
- br.Data = resp
- }
- // @Title 访谈申请
- // @Description 访谈申请
- // @Param request body models.ArticleInterviewApplyReq true "type json string"
- // @Success 200 {object} models.FontsCollectResp
- // @router /interview/apply [post]
- func (this *ArticleController) InterviewApply() {
- br := new(models.BaseResponse).Init()
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- user := this.User
- if user == nil {
- br.Msg = "请重新登录"
- br.Ret = 408
- return
- }
- uid := user.UserId
- var req models.ArticleInterviewApplyReq
- err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
- if err != nil {
- br.Msg = "参数解析异常!"
- br.ErrMsg = "参数解析失败,Err:" + err.Error()
- return
- }
- article, err := models.GetArticleDetailById(req.ArticleId)
- if err != nil {
- br.Msg = "获取纪要失败!"
- br.ErrMsg = "获取纪要失败,Err:" + err.Error()
- return
- }
- count, err := models.GetArticleInterviewApplyCount(uid, req.ArticleId)
- if err != nil {
- br.Msg = "获取数据失败!"
- br.ErrMsg = "获取数据失败,Err:" + err.Error()
- return
- }
- resp := new(models.ArticleInterviewApplyResp)
- if count <= 0 {
- item := new(models.CygxInterviewApply)
- item.ArticleId = req.ArticleId
- item.UserId = uid
- item.CompanyId = user.CompanyId
- item.Status = "待邀请"
- item.Sort = 1
- item.ArticleTitle = article.Title
- item.CreateTime = time.Now()
- item.ModifyTime = time.Now()
- item.ArticleIdMd5 = article.ArticleIdMd5
- _, err = models.AddCygxInterviewApply(item)
- if err != nil {
- br.Msg = "申请失败"
- br.ErrMsg = "申请失败,Err:" + err.Error()
- return
- }
- br.Msg = "申请成功"
- resp.Status = 1
- //发送模板消息
- if user.CompanyId > 1 {
- mobile := user.Mobile
- if mobile == "" {
- mobile = user.Email
- }
- sellerItem, _ := models.GetSellerByCompanyId(user.CompanyId)
- if sellerItem != nil && sellerItem.AdminId > 0 && user.Mobile != "" {
- openIpItem, _ := models.GetUserRecordByMobile(4, sellerItem.Mobile)
- if openIpItem != nil && openIpItem.OpenId != "" {
- go services.SendInterviewApplyTemplateMsg(user.RealName, sellerItem.CompanyName, mobile, article.Title, openIpItem)
- }
- }
- }
- } else {
- err = models.RemoveArticleInterviewApply(uid, req.ArticleId)
- if err != nil {
- br.Msg = "取消申请失败"
- br.ErrMsg = "取消申请失败,Err:" + err.Error()
- return
- }
- br.Msg = "已取消申请"
- resp.Status = 2
- if user.CompanyId > 1 {
- mobile := user.Mobile
- if mobile == "" {
- mobile = user.Email
- }
- sellerItem, _ := models.GetSellerByCompanyId(user.CompanyId)
- if sellerItem != nil && sellerItem.AdminId > 0 && user.Mobile != "" {
- openIpItem, _ := models.GetUserRecordByMobile(4, sellerItem.Mobile)
- if openIpItem != nil && openIpItem.OpenId != "" {
- go services.SendInterviewApplyCancelTemplateMsg(user.RealName, sellerItem.CompanyName, mobile, article.Title, openIpItem)
- }
- }
- }
- }
- br.Ret = 200
- br.Success = true
- br.Data = resp
- }
- // @Title 上传文章阅读时间
- // @Description 上传文章阅读时间接口
- // @Param request body models.AddStopTimeRep true "type json string"
- // @Success 200 {object} models.ArticleDetailResp
- // @router /addStopTime [post]
- func (this *ArticleController) AddStopTime() {
- br := new(models.BaseResponse).Init()
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- user := this.User
- if user == nil {
- br.Msg = "请登录"
- br.ErrMsg = "请登录,用户信息为空"
- br.Ret = 408
- return
- }
- var req models.AddStopTimeRep
- err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
- if err != nil {
- br.Msg = "参数解析异常!"
- br.ErrMsg = "参数解析失败,Err:" + err.Error()
- return
- }
- //uid := user.UserId
- articleId := req.ArticleId
- stopTime := req.StopTime
- outType := req.OutType
- source := req.Source
- if articleId <= 0 {
- br.Msg = "参数错误"
- br.ErrMsg = "参数错误"
- return
- }
- if stopTime == 0 {
- stopTime = 1
- }
- if outType != 2 {
- outType = 1
- }
- if source != "PC" {
- source = "MOBILE"
- }
- hasPermission := 0
- hasFree := 0
- havePower, err := services.GetArticleDetailUserPower(user)
- if err != nil {
- br.Msg = "获取信息失败"
- br.ErrMsg = "校验用户权限失败,Err:" + err.Error()
- return
- }
- if havePower {
- hasPermission = 1
- go services.ArticleHistoryStopTime(articleId, stopTime, outType, user)
- } else {
- hasPermission, err = services.GetUserDetailPermissionCode(user.UserId, user.CompanyId)
- if err != nil && err.Error() != utils.ErrNoRow() {
- br.Msg = "获取信息失败"
- br.ErrMsg = "获取信息失败,GetUserDetailPermissionCode Err:" + err.Error()
- return
- }
- }
- resp := new(models.ArticleDetailAddStopTimeRep)
- resp.HasPermission = hasPermission
- resp.HasFree = hasFree
- br.Ret = 200
- br.Success = true
- br.Msg = "操作成功"
- br.Data = resp
- }
- // @Title 文章带问
- // @Description 新增文章带问接口
- // @Param request body models.AddArticleAskRep true "type json string"
- // @Success Ret=200 新增成功
- // @router /askAdd [post]
- func (this *ArticleController) AskAdd() {
- br := new(models.BaseResponse).Init()
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- user := this.User
- if user == nil {
- br.Msg = "请登录"
- br.ErrMsg = "请登录,SysUser Is Empty"
- br.Ret = 408
- return
- }
- var req models.AddArticleAskRep
- err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
- if err != nil {
- br.Msg = "参数解析异常!"
- br.ErrMsg = "参数解析失败,Err:" + err.Error()
- return
- }
- if req.Content == "" {
- br.Msg = "建议内容不可为空"
- return
- }
- content := req.Content
- itemToken, err := services.WxGetToken()
- if err != nil {
- br.Msg = "GetWxAccessToken Err:" + err.Error()
- return
- }
- if itemToken.AccessToken == "" {
- br.Msg = "accessToken is empty"
- return
- }
- commerr, err := weapp.MSGSecCheck(itemToken.AccessToken, content)
- if err != nil {
- br.Msg = "内容校验失败!"
- br.ErrMsg = "内容校验失败,Err:" + err.Error()
- return
- }
- if commerr.ErrCode != 0 {
- br.Msg = "内容违规,请重新提交!"
- br.ErrMsg = "颜文字内容违规,Err:" + commerr.ErrMSG
- return
- }
- articleId := req.ArticleId
- count, _ := models.GetArticleCountById(articleId)
- if count == 0 {
- br.Msg = "操作失败"
- br.ErrMsg = "文章ID错误,不存在 articleId:" + strconv.Itoa(articleId)
- return
- }
- companyDetail, err := models.GetCompanyDetailById(user.CompanyId)
- if err != nil {
- br.Msg = "提交失败!"
- br.ErrMsg = "获取客户详情失败,Err:" + err.Error()
- return
- }
- if companyDetail == nil {
- br.Msg = "提交失败!"
- br.ErrMsg = "客户不存在,uid:" + strconv.Itoa(user.UserId)
- return
- }
- item := new(models.CygxArticleAsk)
- item.UserId = user.UserId
- item.ArticleId = req.ArticleId
- item.CompanyId = user.CompanyId
- item.CompanyName = companyDetail.CompanyName
- item.CreateTime = time.Now()
- item.Mobile = user.Mobile
- item.Email = user.Email
- item.Content = content
- _, err = models.AddArticleAsk(item)
- if err != nil {
- br.Msg = "提交失败"
- br.ErrMsg = "提交失败,Err:" + err.Error()
- return
- }
- companyItem, err := models.GetSellerDetailAllByCompanyId(user.CompanyId)
- if err != nil {
- br.Msg = "获取信息失败"
- br.ErrMsg = "获取所属销售信息失败,Err:" + err.Error()
- return
- }
- var mobile string
- if utils.RunMode == "release" {
- mobile = utils.WxMsgTemplateIdAskMsgMobileAll + "," + companyItem.Mobile
- } else {
- mobile = utils.WxMsgTemplateIdAskMsgMobile + "," + companyItem.Mobile
- }
- openIdList, err := models.GetWxOpenIdByMobileList(mobile)
- if err != nil {
- br.Msg = "提交失败"
- br.ErrMsg = "提交失败,Err:" + err.Error()
- return
- }
- detail, err := models.GetArticleDetailById(articleId)
- if err != nil {
- br.Msg = "获取信息失败"
- br.ErrMsg = "获取信息失败,Err:" + err.Error()
- return
- }
- companyName := user.CompanyName + "-" + user.RealName + "(" + companyItem.SellerName + ")"
- go services.SendWxMsgWithAsk(companyName, time.Now().Format(utils.FormatDateTime), content, detail.Title, openIdList, req.ArticleId)
- //go services.SendWxCategoryMsgWithAsk(user.CompanyName, user.RealName, companyItem.SellerName, time.Now().Format(utils.FormatDateTimeMinute2), content, openIdList, req.ArticleId)
- br.Ret = 200
- br.Success = true
- br.Msg = "提交成功"
- }
- // @Title 下载PDF打水印
- // @Description 下载PDF打水印接口
- // @Param ArticleId query int true "报告ID"
- // @Success 200 {object} models.ArticleDetailFileLink
- // @router /pdfwatermark [get]
- func (this *ArticleController) Pdfwatermark() {
- br := new(models.BaseResponse).Init()
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- user := this.User
- if user == nil {
- br.Msg = "请登录"
- br.ErrMsg = "请登录,用户信息为空"
- br.Ret = 408
- return
- }
- uid := user.UserId
- articleId, err := this.GetInt("ArticleId")
- if articleId <= 0 {
- br.Msg = "文章不存在"
- br.ErrMsg = "文章不存在,文章ID错误"
- return
- }
- //缓存校验
- cacheKey := fmt.Sprint("xygx:apply_record:add:", uid, "ArticleId_", articleId)
- ttlTime := utils.Rc.GetRedisTTL(cacheKey)
- if ttlTime > 0 && user.CompanyId != 16 {
- br.Msg = "下载失败,下载过于频繁"
- br.ErrMsg = "下载失败,下载过于频繁:mobile" + user.Mobile
- return
- }
- resp := new(models.ArticleDetailFileLink)
- detail := new(models.ArticleDetail)
- detail, err = models.GetArticleDetailById(articleId)
- if err != nil {
- br.Msg = "获取信息失败"
- br.ErrMsg = "获取信息失败,Err:" + err.Error()
- return
- }
- fileLink := detail.FileLink
- if fileLink == "" {
- br.Msg = "下载失败"
- br.ErrMsg = "下载失败,报告链接不存在"
- return
- }
- mobile := user.Mobile
- if mobile == "" {
- mobile = user.Email
- }
- sliceLink := strings.Split(fileLink, "/")
- uploadDir := "static/pdf/"
- //判断文件夹是否存在,不存在则创建
- if !utils.FileIsExist(uploadDir) {
- err = os.MkdirAll(uploadDir, 0755)
- if err != nil {
- br.Msg = "下载失败"
- br.ErrMsg = "存储目录创建失败,Err:" + err.Error()
- return
- }
- }
- var oldFile string
- var newFile string
- //获取PDF源文件名称
- pdfName := sliceLink[len(sliceLink)-1]
- pdfName = utils.MD5(pdfName) + ".pdf"
- oldFile = uploadDir + pdfName
- //判断PDF本地是否存在,不存在则保存到本地
- if !utils.FileIsExist(oldFile) {
- res, err := nhttp.Get(fileLink)
- if err != nil {
- br.Msg = "下载失败"
- br.ErrMsg = "获取源文件失败,Err:" + err.Error()
- return
- }
- defer res.Body.Close()
- // 获得get请求响应的reader对象
- reader := bufio.NewReaderSize(res.Body, 32*1024)
- file, err := os.Create(oldFile)
- if err != nil {
- br.Msg = "下载失败"
- br.ErrMsg = "保存源文件到本地失败,Err:" + err.Error()
- return
- }
- defer file.Close()
- //获得文件的writer对象
- writer := bufio.NewWriter(file)
- written, _ := io.Copy(writer, reader)
- fmt.Printf("Total length: %d", written)
- }
- newFile = uploadDir + "new_" + pdfName
- onTop := true
- wm, err := pdfcpu.ParseTextWatermarkDetails(mobile, " op:.4, pos:c ,points:16 ", onTop, 1)
- if err != nil {
- br.Msg = "下载失败"
- br.ErrMsg = "生成水印文件失败,Err:" + err.Error()
- return
- }
- err = api.AddWatermarksFile(oldFile, newFile, nil, wm, nil)
- if err != nil {
- //br.Msg = "下载失败"
- //br.ErrMsg = "生成水印PDF失败,Err:" + err.Error()
- resp.FileLink = fileLink
- br.Ret = 200
- br.Success = true
- br.Msg = "获取成功"
- br.Data = resp
- return
- }
- randStr := utils.GetRandStringNoSpecialChar(28)
- fileName := randStr + ".pdf"
- savePath := uploadDir + time.Now().Format("200601/20060102/")
- savePath += fileName
- //上传到阿里云
- err = services.UploadFileToAliyun(fileName, newFile, savePath)
- if err != nil {
- br.Msg = "下载失败"
- br.ErrMsg = "文件上传失败,Err:" + err.Error()
- return
- }
- fileHost := "https://hzstatic.hzinsights.com/"
- resourceUrl := fileHost + savePath
- defer func() {
- os.Remove(newFile)
- }()
- utils.Rc.SetNX(cacheKey, user.Mobile, time.Minute*5)
- resp.FileLink = resourceUrl
- br.Ret = 200
- br.Success = true
- br.Msg = "获取成功"
- br.Data = resp
- }
- // @Title 约访专家
- // @Description 约访专家接口
- // @Param request body models.CygxArticleIdReq true "type json string"
- // @Success 200 {object}
- // @router /applyAppointmentExpert [post]
- func (this *ArticleController) ApplyAppointmentExpert() {
- br := new(models.BaseResponse).Init()
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- user := this.User
- if user == nil {
- br.Msg = "请登录"
- br.ErrMsg = "请登录,用户信息为空"
- br.Ret = 408
- return
- }
- fmt.Println(user)
- var req models.CygxArticleIdReq
- err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
- if err != nil {
- br.Msg = "参数解析异常!"
- br.ErrMsg = "参数解析失败,Err:" + err.Error()
- return
- }
- articleId := req.ArticleId
- var condition string
- var pars []interface{}
- condition += ` AND article_id =? AND user_id = ? `
- pars = append(pars, articleId, user.UserId)
- total, err := models.GetCygxArticleApplyAppointmentExpertCount(condition, pars)
- if err != nil {
- br.Msg = "约访专家失败"
- br.ErrMsg = "约访专家失败,Err:" + err.Error()
- return
- }
- if total > 0 {
- br.Msg = "您已提交申请,请勿重复提交。"
- return
- }
- err = services.AddArticleApplyAppointmentExpert(user, articleId)
- if err != nil {
- br.Msg = "约访专家失败"
- br.ErrMsg = "约访专家失败,Err:" + err.Error()
- return
- }
- services.SendArticleApplyAppointmentExpertTemplateMsg(user, articleId)
- br.Ret = 200
- br.Success = true
- br.Msg = "操作成功"
- }
|