123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597 |
- package controllers
- import (
- "encoding/json"
- "hongze/hongze_cygx/models"
- "hongze/hongze_cygx/services"
- "hongze/hongze_cygx/utils"
- "html"
- "regexp"
- "strconv"
- "strings"
- "time"
- )
- type ArticleController struct {
- BaseAuthController
- }
- type ArticleCommonController struct {
- BaseCommonController
- }
- // @Title 获取报告详情
- // @Description 获取报告详情接口
- // @Param ArticleId query int true "报告ID"
- // @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")
- if articleId <= 0 {
- br.Msg = "文章不存在"
- br.ErrMsg = "文章不存在,文章ID错误"
- return
- }
- detail := new(models.ArticleDetail)
- hasPermission := 0
- hasFree := 0
- //判断是否已经申请过
- applyCount, err := models.GetApplyRecordCount(uid)
- if err != nil && err.Error() != utils.ErrNoRow() {
- br.Msg = "获取信息失败"
- br.ErrMsg = "判断是否已申请过试用失败,Err:" + err.Error()
- return
- }
- //`description:"1:有该行业权限,正常展示,2:无该行业权限,不存在权益客户下,3:无该品类权限,4:潜在客户,未提交过申请,5:潜在客户,已提交过申请"`
- if user.CompanyId > 1 {
- companyPermission, err := models.GetCompanyPermission(user.CompanyId)
- if err != nil {
- br.Msg = "获取信息失败"
- br.ErrMsg = "判断是否已申请访谈失败,Err:" + strconv.Itoa(uid) + ";articleId" + strconv.Itoa(articleId)
- return
- }
- detail, err = models.GetArticleDetailById(articleId)
- if err != nil {
- br.Msg = "获取信息失败"
- br.ErrMsg = "获取信息失败,Err:" + err.Error()
- return
- }
- detail.Body = html.UnescapeString(detail.Body)
- //detail.Abstract = html.UnescapeString(detail.Abstract)
- detail.Abstract, _ = services.GetReportContentTextSub(detail.Abstract)
- if companyPermission == "" {
- if applyCount > 0 {
- hasPermission = 5
- } else {
- hasPermission = 2
- }
- hasFree = 2
- goto Loop
- } else {
- hasFree = 1
- // 原有的权限校验 更改于 2021-05-18
- //articlePermission, err := models.GetArticlePermission(detail.SubCategoryName)
- //fmt.Println(articlePermission)
- //fmt.Println(detail.SubCategoryName)
- //if err != nil {
- // br.Msg = "获取信息失败"
- // br.ErrMsg = "获取报告权限失败,Err:" + err.Error() + strconv.Itoa(uid) + ";articleId" + strconv.Itoa(articleId)
- // return
- //}
- //if articlePermission == nil {
- // br.Msg = "获取信息失败"
- // br.ErrMsg = "报告权限不存在,Err:" + strconv.Itoa(uid) + ";articleId" + strconv.Itoa(articleId)
- // return
- //}
- //for _, p := range articlePermission {
- // if strings.Contains(companyPermission, p.PermissionName) {
- // hasPermission = 1
- // historyRecord := new(models.CygxArticleHistoryRecord)
- // historyRecord.UserId = uid
- // historyRecord.ArticleId = articleId
- // historyRecord.CreateTime = time.Now()
- // historyRecord.Mobile = user.Mobile
- // historyRecord.Email = user.Email
- // historyRecord.CompanyId = user.CompanyId
- // historyRecord.CompanyName = user.CompanyName
- // go models.AddCygxArticleHistoryRecord(historyRecord)
- // break
- // } else { //无该行业权限
- // hasPermission = 3
- // }
- //}
- var articlePermissionPermissionName string
- if detail.CategoryId > 0 {
- articlePermission, err := models.GetArticlePermission(detail.CategoryId)
- if err != nil {
- br.Msg = "获取信息失败"
- br.ErrMsg = "获取报告权限失败,Err:" + err.Error() + strconv.Itoa(uid) + ";articleId" + strconv.Itoa(articleId)
- return
- }
- if articlePermission == nil {
- br.Msg = "获取信息失败"
- br.ErrMsg = "报告权限不存在,Err:" + strconv.Itoa(uid) + ";articleId" + strconv.Itoa(articleId)
- return
- }
- articlePermissionPermissionName = articlePermission.PermissionName
- } else {
- articlePermissionPermissionName = detail.CategoryName
- }
- if strings.Contains(companyPermission, articlePermissionPermissionName) {
- hasPermission = 1
- var detailNew *models.AddStopTimeNewRep
- detailNew, _ = models.GetNewArticleHistoryRecord(uid, articleId)
- if detailNew == nil || detailNew.StopTime != 0 {
- historyRecord := new(models.CygxArticleHistoryRecord)
- historyRecord.UserId = uid
- historyRecord.ArticleId = articleId
- historyRecord.CreateTime = time.Now()
- historyRecord.Mobile = user.Mobile
- historyRecord.Email = user.Email
- historyRecord.CompanyId = user.CompanyId
- historyRecord.CompanyName = user.CompanyName
- go models.AddCygxArticleHistoryRecord(historyRecord)
- }
- } else { //无该行业权限
- hasPermission = 3
- }
- if hasPermission == 1 {
- key := "CYGX_ARTICLE_" + strconv.Itoa(articleId) + "_" + strconv.Itoa(uid)
- if !utils.Rc.IsExist(key) {
- //新增浏览记录
- record := new(models.CygxArticleViewRecord)
- record.UserId = uid
- record.ArticleId = articleId
- record.CreateTime = time.Now()
- record.Mobile = user.Mobile
- record.Email = user.Email
- record.CompanyId = user.CompanyId
- record.CompanyName = user.CompanyName
- go models.AddCygxArticleViewRecord(record)
- utils.Rc.Put(key, 1, 5*time.Second)
- models.ModifyReportLastViewTime(uid)
- }
- }
- }
- collectCount, err := models.GetArticleCollectCount(uid, articleId)
- if err != nil && err.Error() != utils.ErrNoRow() {
- br.Msg = "获取信息失败"
- br.ErrMsg = "判断是否已收藏失败,Err:" + strconv.Itoa(uid) + ";articleId" + strconv.Itoa(articleId)
- return
- }
- if collectCount > 0 {
- detail.IsCollect = true
- }
- 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 {
- br.Msg = "获取信息失败"
- br.ErrMsg = "获取销售数据失败,Err:" + 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 > 1000000 {
- var hrefRegexp = regexp.MustCompile("[0-9]\\d*")
- match := hrefRegexp.FindAllString(detail.SellerAndMobile, -1)
- if match != nil {
- for _, v := range match {
- sellerAndMobile := &models.SellerRep{
- SellerMobile: v,
- SellerName: strings.Replace(detail.SellerAndMobile, v, "", -1),
- }
- sellerList = append(sellerList, sellerAndMobile)
- }
- }
- }
- detail.SellerList = sellerList
- } else { //潜在客户
- if applyCount > 0 {
- hasPermission = 5
- } else {
- hasPermission = 4
- }
- }
- Loop:
- if hasPermission != 1 {
- detail.Body = ""
- detail.BodyText = ""
- }
- resp := new(models.ArticleDetailResp)
- resp.HasPermission = hasPermission
- resp.HasFree = hasFree
- resp.Detail = detail
- 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
- }
- count, err := models.GetArticleCollectCount(uid, req.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()
- _, err = models.AddCygxArticleCollect(item)
- if err != nil {
- br.Msg = "收藏失败"
- br.ErrMsg = "收藏失败,Err:" + err.Error()
- return
- }
- br.Msg = "收藏成功"
- resp.Status = 1
- } else {
- err = models.RemoveArticleCollect(uid, req.ArticleId)
- if err != nil {
- br.Msg = "取消收藏失败"
- br.ErrMsg = "取消收藏失败,Err:" + err.Error()
- return
- }
- br.Msg = "已取消收藏"
- resp.Status = 2
- }
- collectTotal, err := models.GetArticleCollectUsersCount(req.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.GetUserRecordByUserId(sellerItem.UserId, 1)
- if openIpItem != nil && openIpItem.OpenId != "" {
- go services.SendInterviewApplyTemplateMsg(user.RealName, sellerItem.CompanyName, mobile, article.Title, openIpItem.OpenId)
- }
- }
- }
- } 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.GetUserRecordByUserId(sellerItem.UserId, 1)
- if openIpItem != nil && openIpItem.OpenId != "" {
- go services.SendInterviewApplyCancelTemplateMsg(user.RealName, sellerItem.CompanyName, mobile, article.Title, openIpItem.OpenId)
- }
- }
- }
- }
- br.Ret = 200
- br.Success = true
- br.Data = resp
- }
- // @Title 获取报告详情
- // @Description 获取报告详情接口
- // @Param ArticleIdMd5 query int true "报告ID"
- // @Success 200 {object} models.ArticleDetailResp
- // @router /look/detail [get]
- func (this *ArticleCommonController) Detail() {
- br := new(models.BaseResponse).Init()
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- articleIdMd5 := this.GetString("ArticleIdMd5")
- if articleIdMd5 == "" {
- br.Msg = "参数错误"
- br.ErrMsg = "参数错误"
- return
- }
- resp := new(models.ArticleDetailResp)
- detail, err := models.GetArticleDetailByIdMd5(articleIdMd5)
- if err != nil && err.Error() != utils.ErrNoRow() {
- br.Msg = "获取信息失败"
- br.ErrMsg = "获取信息失败,Err:" + err.Error()
- return
- }
- if detail == nil {
- resp.HasPermission = 2
- } else {
- resp.HasPermission = 1
- }
- if detail != nil {
- detail.Body = html.UnescapeString(detail.Body)
- detail.Abstract = html.UnescapeString(detail.Abstract)
- }
- sellerList, err := models.GetSellerList(detail.ArticleId)
- if err != nil {
- br.Msg = "获取信息失败"
- br.ErrMsg = "获取销售数据失败,Err:" + err.Error() + ";articleId" + strconv.Itoa(detail.ArticleId)
- return
- }
- if detail.ArticleId > 1000000 {
- var hrefRegexp = regexp.MustCompile("[0-9]\\d*")
- match := hrefRegexp.FindAllString(detail.SellerAndMobile, -1)
- if match != nil {
- for _, v := range match {
- sellerAndMobile := &models.SellerRep{
- SellerMobile: v,
- SellerName: strings.Replace(detail.SellerAndMobile, v, "", -1),
- }
- sellerList = append(sellerList, sellerAndMobile)
- }
- }
- }
- detail.SellerList = sellerList
- resp.Detail = detail
- br.Ret = 200
- br.Success = true
- br.Msg = "获取成功"
- 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
- if articleId <= 0 {
- br.Msg = "参数错误"
- br.ErrMsg = "参数错误"
- return
- }
- if stopTime == 0 {
- stopTime = 1
- //br.Msg = "时间格式错误"
- //br.ErrMsg = "时间错误"
- //return
- }
- detail := new(models.ArticleDetail)
- hasPermission := 0
- hasFree := 0
- //判断是否已经申请过
- applyCount, err := models.GetApplyRecordCount(uid)
- if err != nil && err.Error() != utils.ErrNoRow() {
- br.Msg = "获取信息失败"
- br.ErrMsg = "判断是否已申请过试用失败,Err:" + err.Error()
- return
- }
- //`description:"1:有该行业权限,正常展示,2:无该行业权限,不存在权益客户下,3:无该品类权限,4:潜在客户,未提交过申请,5:潜在客户,已提交过申请"`
- if user.CompanyId > 1 {
- companyPermission, err := models.GetCompanyPermission(user.CompanyId)
- if err != nil {
- br.Msg = "获取信息失败"
- br.ErrMsg = "判断是否已申请访谈失败,Err:" + strconv.Itoa(uid) + ";articleId" + strconv.Itoa(articleId)
- return
- }
- detail, err = models.GetArticleDetailById(articleId)
- if err != nil {
- br.Msg = "获取信息失败"
- br.ErrMsg = "获取信息失败,Err:" + err.Error()
- return
- }
- if companyPermission == "" {
- if applyCount > 0 {
- hasPermission = 5
- } else {
- hasPermission = 2
- }
- hasFree = 2
- goto Loop
- } else {
- hasFree = 1
- var articlePermissionPermissionName string
- if detail.CategoryId > 0 {
- articlePermission, err := models.GetArticlePermission(detail.CategoryId)
- if err != nil {
- br.Msg = "获取信息失败"
- br.ErrMsg = "获取报告权限失败,Err:" + err.Error() + strconv.Itoa(uid) + ";articleId" + strconv.Itoa(articleId)
- return
- }
- if articlePermission == nil {
- br.Msg = "获取信息失败"
- br.ErrMsg = "报告权限不存在,Err:" + strconv.Itoa(uid) + ";articleId" + strconv.Itoa(articleId)
- return
- }
- articlePermissionPermissionName = articlePermission.PermissionName
- } else {
- articlePermissionPermissionName = detail.CategoryName
- }
- if strings.Contains(companyPermission, articlePermissionPermissionName) {
- detailNew, err := models.GetNewArticleHistoryRecord(uid, articleId)
- if err != nil {
- br.Msg = "获取信息失败"
- br.ErrMsg = "获取信息失败,Err:" + err.Error()
- return
- }
- hasPermission = 1
- historyRecord := new(models.AddStopTimeNewRep)
- historyRecord.StopTime = detailNew.StopTime + stopTime
- historyRecord.Id = detailNew.Id
- go models.AddArticleStopTime(historyRecord)
- } else { //无该行业权限
- hasPermission = 3
- }
- }
- } else { //潜在客户
- if applyCount > 0 {
- hasPermission = 5
- } else {
- hasPermission = 4
- }
- }
- Loop:
- resp := new(models.ArticleDetailAddStopTimeRep)
- resp.HasPermission = hasPermission
- resp.HasFree = hasFree
- br.Ret = 200
- br.Success = true
- br.Msg = "操作成功"
- br.Data = resp
- }
|