article.go 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968
  1. package controllers
  2. import (
  3. "bufio"
  4. "github.com/pdfcpu/pdfcpu/pkg/api"
  5. "github.com/pdfcpu/pdfcpu/pkg/pdfcpu"
  6. "io"
  7. //"bufio"
  8. "encoding/json"
  9. "fmt"
  10. "github.com/medivhzhan/weapp/v2"
  11. "os"
  12. //"github.com/pdfcpu/pdfcpu/pkg/api"
  13. //"github.com/pdfcpu/pdfcpu/pkg/pdfcpu"
  14. "hongze/hongze_cygx/models"
  15. "hongze/hongze_cygx/services"
  16. "hongze/hongze_cygx/utils"
  17. "html"
  18. nhttp "net/http"
  19. //"os"
  20. "regexp"
  21. "strconv"
  22. "strings"
  23. "time"
  24. )
  25. type ArticleController struct {
  26. BaseAuthController
  27. }
  28. type ArticleCommonController struct {
  29. BaseCommonController
  30. }
  31. // @Title 获取报告详情
  32. // @Description 获取报告详情接口
  33. // @Param ArticleId query int true "报告ID"
  34. // @Success 200 {object} models.ArticleDetailResp
  35. // @router /detail [get]
  36. func (this *ArticleController) Detail() {
  37. br := new(models.BaseResponse).Init()
  38. defer func() {
  39. this.Data["json"] = br
  40. this.ServeJSON()
  41. }()
  42. user := this.User
  43. if user == nil {
  44. br.Msg = "请登录"
  45. br.ErrMsg = "请登录,用户信息为空"
  46. br.Ret = 408
  47. return
  48. }
  49. uid := user.UserId
  50. articleId, err := this.GetInt("ArticleId")
  51. if articleId <= 0 {
  52. br.Msg = "文章不存在"
  53. br.ErrMsg = "文章不存在,文章ID错误"
  54. return
  55. }
  56. detail := new(models.ArticleDetail)
  57. hasPermission := 0
  58. hasFree := 0
  59. var haveResearch bool
  60. //判断是否已经申请过
  61. applyCount, err := models.GetApplyRecordCount(uid)
  62. if err != nil && err.Error() != utils.ErrNoRow() {
  63. br.Msg = "获取信息失败"
  64. br.ErrMsg = "判断是否已申请过试用失败,Err:" + err.Error()
  65. return
  66. }
  67. //`description:"1:有该行业权限,正常展示,2:无该行业权限,不存在权益客户下,3:无该品类权限,4:潜在客户,未提交过申请,5:潜在客户,已提交过申请"`
  68. if user.CompanyId > 1 {
  69. companyPermission, err := models.GetCompanyPermission(user.CompanyId)
  70. if err != nil {
  71. br.Msg = "获取信息失败"
  72. br.ErrMsg = "判断是否已申请访谈失败,Err:" + strconv.Itoa(uid) + ";articleId" + strconv.Itoa(articleId)
  73. return
  74. }
  75. detail, err = models.GetArticleDetailById(articleId)
  76. if err != nil {
  77. br.Msg = "获取信息失败"
  78. br.ErrMsg = "获取信息失败,Err:" + err.Error()
  79. return
  80. }
  81. detail.Body = html.UnescapeString(detail.Body)
  82. 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)
  83. detail.Body = strings.Replace(detail.Body, "pre", "div", -1)
  84. //detail.Abstract = html.UnescapeString(detail.Abstract)
  85. detail.Abstract, _ = services.GetReportContentTextSub(detail.Abstract)
  86. if companyPermission == "" {
  87. if applyCount > 0 {
  88. hasPermission = 5
  89. } else {
  90. hasPermission = 2
  91. }
  92. hasFree = 2
  93. goto Loop
  94. } else {
  95. hasFree = 1
  96. var articlePermissionPermissionName string
  97. if detail.CategoryId > 0 {
  98. articlePermission, err := models.GetArticlePermission(detail.CategoryId)
  99. if err != nil {
  100. br.Msg = "获取信息失败"
  101. br.ErrMsg = "获取报告权限失败,Err:" + err.Error() + strconv.Itoa(uid) + ";articleId" + strconv.Itoa(articleId)
  102. return
  103. }
  104. if articlePermission == nil {
  105. br.Msg = "获取信息失败"
  106. br.ErrMsg = "报告权限不存在,Err:" + strconv.Itoa(uid) + ";articleId" + strconv.Itoa(articleId)
  107. return
  108. }
  109. articlePermissionPermissionName = articlePermission.PermissionName
  110. } else {
  111. articlePermissionPermissionName = detail.CategoryName
  112. }
  113. var hasPersion bool
  114. slice := strings.Split(articlePermissionPermissionName, ",")
  115. for _, v := range slice {
  116. if strings.Contains(companyPermission, v) {
  117. hasPersion = true
  118. }
  119. }
  120. if strings.Contains(detail.CategoryName, "研选") {
  121. detail.IsResearch = true
  122. }
  123. userType, _, err := services.GetUserType(user.CompanyId)
  124. if err != nil {
  125. br.Msg = "获取信息失败"
  126. br.ErrMsg = "获取用户信息失败,Err:" + err.Error()
  127. return
  128. }
  129. if userType == 1 && strings.Contains(detail.CategoryName, "研选") {
  130. hasPersion = false
  131. }
  132. if detail.IsReport == 1 {
  133. fmt.Println("报告权限")
  134. detailCategory, err := models.GetdetailByCategoryIdSando(detail.CategoryId)
  135. if err != nil && err.Error() != utils.ErrNoRow() {
  136. br.Msg = "获取信息失败"
  137. br.ErrMsg = "获取信息失败,Err:" + err.Error() + "categoryID 不存在:" + strconv.Itoa(detail.CategoryId)
  138. return
  139. }
  140. fmt.Println(detailCategory)
  141. permissionStr, err := models.GetCompanyPermissionByUser(user.CompanyId)
  142. if err != nil {
  143. br.Msg = "获取信息失败"
  144. br.ErrMsg = "获取客户信息失败,Err:" + err.Error()
  145. return
  146. }
  147. if detailCategory != nil {
  148. if detailCategory.PermissionType == 1 {
  149. if !strings.Contains(permissionStr, detailCategory.ChartPermissionName+"(主观)") {
  150. hasPersion = false
  151. }
  152. } else if detailCategory.PermissionType == 2 {
  153. if !strings.Contains(permissionStr, detailCategory.ChartPermissionName+"(客观)") {
  154. hasPersion = false
  155. }
  156. }
  157. }
  158. }
  159. if hasPersion {
  160. hasPermission = 1
  161. historyRecord := new(models.CygxArticleHistoryRecord)
  162. historyRecord.UserId = uid
  163. historyRecord.ArticleId = articleId
  164. historyRecord.CreateTime = time.Now()
  165. historyRecord.Mobile = user.Mobile
  166. historyRecord.Email = user.Email
  167. historyRecord.CompanyId = user.CompanyId
  168. historyRecord.CompanyName = user.CompanyName
  169. recordCount, _ := models.GetNoAddStoptimeArticleCount(uid, articleId)
  170. if recordCount == 0 {
  171. go models.AddCygxArticleHistoryRecord(historyRecord)
  172. } else {
  173. detailNew, err := models.GetNewArticleHistoryRecord(uid, articleId)
  174. if err != nil {
  175. br.Msg = "获取信息失败"
  176. br.ErrMsg = "获取信息失败,Err:" + err.Error()
  177. return
  178. }
  179. if detailNew.StopTime > 0 {
  180. go models.AddCygxArticleHistoryRecord(historyRecord)
  181. }
  182. }
  183. } else { //无该行业权限
  184. companyDetail, err := models.GetCompanyDetailById(user.CompanyId)
  185. if err == nil && companyDetail.ProductId == 1 {
  186. hasPermission = 2
  187. } else {
  188. hasPermission = 3
  189. }
  190. }
  191. if hasPermission == 1 {
  192. key := "CYGX_ARTICLE_" + strconv.Itoa(articleId) + "_" + strconv.Itoa(uid)
  193. if !utils.Rc.IsExist(key) {
  194. //新增浏览记录
  195. record := new(models.CygxArticleViewRecord)
  196. record.UserId = uid
  197. record.ArticleId = articleId
  198. record.CreateTime = time.Now()
  199. record.Mobile = user.Mobile
  200. record.Email = user.Email
  201. record.CompanyId = user.CompanyId
  202. record.CompanyName = user.CompanyName
  203. go models.AddCygxArticleViewRecord(record)
  204. utils.Rc.Put(key, 1, 5*time.Second)
  205. models.ModifyReportLastViewTime(uid)
  206. }
  207. }
  208. }
  209. collectCount, err := models.GetArticleCollectCount(uid, articleId)
  210. if err != nil && err.Error() != utils.ErrNoRow() {
  211. br.Msg = "获取信息失败"
  212. br.ErrMsg = "判断是否已收藏失败,Err:" + strconv.Itoa(uid) + ";articleId" + strconv.Itoa(articleId)
  213. return
  214. }
  215. if collectCount > 0 {
  216. detail.IsCollect = true
  217. }
  218. interviewApplyItem, err := models.GetArticleInterviewApply(uid, articleId)
  219. if err != nil && err.Error() != utils.ErrNoRow() {
  220. br.Msg = "获取信息失败"
  221. br.ErrMsg = "判断是否已申请访谈失败,Err:" + strconv.Itoa(uid) + ";articleId" + strconv.Itoa(articleId)
  222. return
  223. }
  224. if interviewApplyItem != nil && interviewApplyItem.InterviewApplyId > 0 {
  225. detail.IsInterviewApply = true
  226. detail.InterviewApplyStatus = interviewApplyItem.Status
  227. }
  228. //获取销售手机号
  229. sellerItem, err := models.GetSellerByCompanyId(user.CompanyId)
  230. if err != nil {
  231. br.Msg = "获取信息失败"
  232. br.ErrMsg = "获取销售数据失败2,Err:" + strconv.Itoa(uid) + ";articleId" + strconv.Itoa(articleId)
  233. return
  234. }
  235. if sellerItem != nil {
  236. detail.SellerMobile = sellerItem.Mobile
  237. detail.SellerName = sellerItem.RealName
  238. }
  239. sellerList, err := models.GetSellerList(articleId)
  240. if err != nil {
  241. br.Msg = "获取信息失败"
  242. br.ErrMsg = "获取销售数据失败,Err:" + err.Error() + strconv.Itoa(uid) + ";articleId" + strconv.Itoa(articleId)
  243. return
  244. }
  245. if detail.ArticleId >= utils.SummaryArticleId && strings.Contains(detail.SellerAndMobile, "-") {
  246. strnum := strings.Index(detail.SellerAndMobile, "-")
  247. detail.SellerAndMobile = detail.SellerAndMobile[0:strnum]
  248. if strnum > 0 {
  249. nickName := detail.SellerAndMobile[0:strnum]
  250. sellerAndMobile := &models.SellerRep{
  251. SellerMobile: "",
  252. SellerName: nickName,
  253. }
  254. sellerList = append(sellerList, sellerAndMobile)
  255. }
  256. }
  257. detail.SellerList = sellerList
  258. } else { //潜在客户
  259. if applyCount > 0 {
  260. hasPermission = 5
  261. } else {
  262. hasPermission = 4
  263. }
  264. }
  265. Loop:
  266. if hasPermission != 1 {
  267. detail.Body = ""
  268. detail.BodyText = ""
  269. } else {
  270. articleFollowdetail, err := models.GetArticleFollowDetail(articleId, uid)
  271. if err != nil {
  272. br.Msg = "获取信息失败"
  273. br.ErrMsg = "获取关注信息失败,Err:" + err.Error()
  274. return
  275. }
  276. detail.FollowNum = articleFollowdetail.DNum
  277. detail.CollectionNum = articleFollowdetail.AcNum
  278. if articleFollowdetail.MdNum > 0 {
  279. detail.IsFollow = true
  280. }
  281. if detail.IsSummary == 1 {
  282. detail.IsBelongSummary = true
  283. }
  284. if detail.IsReport == 1 {
  285. detail.IsBelongReport = true
  286. }
  287. haveResearch = true
  288. }
  289. fmt.Println("hasPermission", hasPermission)
  290. if hasPermission == 2 || hasPermission == 4 {
  291. //获取销售手机号
  292. sellerItemQy, err := models.GetSellerByCompanyIdCheckFicc(user.CompanyId, 2)
  293. if err != nil && err.Error() != utils.ErrNoRow() {
  294. br.Msg = "获取信息失败"
  295. br.ErrMsg = "获取销售信息失败,Err:" + err.Error()
  296. return
  297. }
  298. if sellerItemQy != nil {
  299. hasPermission = 3
  300. detail.SellerMobile = sellerItemQy.Mobile
  301. detail.SellerName = sellerItemQy.RealName
  302. }
  303. }
  304. detail.HttpUrl = "https://vmp.hzinsights.com/v2/articles/" + strconv.Itoa(articleId)
  305. resp := new(models.ArticleDetailResp)
  306. resp.HasPermission = hasPermission
  307. resp.HaveResearch = haveResearch
  308. resp.HasFree = hasFree
  309. resp.Detail = detail
  310. if user.Mobile != "" {
  311. resp.Mobile = user.Mobile
  312. } else {
  313. resp.Mobile = user.Email
  314. }
  315. br.Ret = 200
  316. br.Success = true
  317. br.Msg = "获取成功"
  318. br.Data = resp
  319. }
  320. // @Title 收藏
  321. // @Description 收藏
  322. // @Param request body models.ArticleCollectReq true "type json string"
  323. // @Success 200 {object} models.FontsCollectResp
  324. // @router /collect [post]
  325. func (this *ArticleController) ArticleCollect() {
  326. br := new(models.BaseResponse).Init()
  327. defer func() {
  328. this.Data["json"] = br
  329. this.ServeJSON()
  330. }()
  331. user := this.User
  332. if user == nil {
  333. br.Msg = "请重新登录"
  334. br.Ret = 408
  335. return
  336. }
  337. uid := user.UserId
  338. var req models.ArticleCollectReq
  339. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  340. if err != nil {
  341. br.Msg = "参数解析异常!"
  342. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  343. return
  344. }
  345. count, err := models.GetArticleCollectCount(uid, req.ArticleId)
  346. if err != nil {
  347. br.Msg = "获取数据失败!"
  348. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  349. return
  350. }
  351. resp := new(models.ArticleCollectResp)
  352. if count <= 0 {
  353. item := new(models.CygxArticleCollect)
  354. item.ArticleId = req.ArticleId
  355. item.UserId = uid
  356. item.CreateTime = time.Now()
  357. _, err = models.AddCygxArticleCollect(item)
  358. if err != nil {
  359. br.Msg = "收藏失败"
  360. br.ErrMsg = "收藏失败,Err:" + err.Error()
  361. return
  362. }
  363. br.Msg = "收藏成功"
  364. resp.Status = 1
  365. } else {
  366. err = models.RemoveArticleCollect(uid, req.ArticleId)
  367. if err != nil {
  368. br.Msg = "取消收藏失败"
  369. br.ErrMsg = "取消收藏失败,Err:" + err.Error()
  370. return
  371. }
  372. br.Msg = "已取消收藏"
  373. resp.Status = 2
  374. }
  375. collectTotal, err := models.GetArticleCollectUsersCount(req.ArticleId)
  376. if err != nil {
  377. br.Msg = "获取数据失败"
  378. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  379. return
  380. }
  381. resp.CollectCount = collectTotal
  382. br.Ret = 200
  383. br.Success = true
  384. br.Data = resp
  385. }
  386. // @Title 访谈申请
  387. // @Description 访谈申请
  388. // @Param request body models.ArticleInterviewApplyReq true "type json string"
  389. // @Success 200 {object} models.FontsCollectResp
  390. // @router /interview/apply [post]
  391. func (this *ArticleController) InterviewApply() {
  392. br := new(models.BaseResponse).Init()
  393. defer func() {
  394. this.Data["json"] = br
  395. this.ServeJSON()
  396. }()
  397. user := this.User
  398. if user == nil {
  399. br.Msg = "请重新登录"
  400. br.Ret = 408
  401. return
  402. }
  403. uid := user.UserId
  404. var req models.ArticleInterviewApplyReq
  405. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  406. if err != nil {
  407. br.Msg = "参数解析异常!"
  408. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  409. return
  410. }
  411. article, err := models.GetArticleDetailById(req.ArticleId)
  412. if err != nil {
  413. br.Msg = "获取纪要失败!"
  414. br.ErrMsg = "获取纪要失败,Err:" + err.Error()
  415. return
  416. }
  417. count, err := models.GetArticleInterviewApplyCount(uid, req.ArticleId)
  418. if err != nil {
  419. br.Msg = "获取数据失败!"
  420. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  421. return
  422. }
  423. resp := new(models.ArticleInterviewApplyResp)
  424. if count <= 0 {
  425. item := new(models.CygxInterviewApply)
  426. item.ArticleId = req.ArticleId
  427. item.UserId = uid
  428. item.CompanyId = user.CompanyId
  429. item.Status = "待邀请"
  430. item.Sort = 1
  431. item.ArticleTitle = article.Title
  432. item.CreateTime = time.Now()
  433. item.ModifyTime = time.Now()
  434. item.ArticleIdMd5 = article.ArticleIdMd5
  435. _, err = models.AddCygxInterviewApply(item)
  436. if err != nil {
  437. br.Msg = "申请失败"
  438. br.ErrMsg = "申请失败,Err:" + err.Error()
  439. return
  440. }
  441. br.Msg = "申请成功"
  442. resp.Status = 1
  443. //发送模板消息
  444. if user.CompanyId > 1 {
  445. mobile := user.Mobile
  446. if mobile == "" {
  447. mobile = user.Email
  448. }
  449. sellerItem, _ := models.GetSellerByCompanyId(user.CompanyId)
  450. if sellerItem != nil && sellerItem.AdminId > 0 && user.Mobile != "" {
  451. openIpItem, _ := models.GetUserRecordByUserIdByXzs(sellerItem.Mobile, 4)
  452. fmt.Println(openIpItem)
  453. if openIpItem != nil && openIpItem.OpenId != "" {
  454. go services.SendInterviewApplyTemplateMsg(user.RealName, sellerItem.CompanyName, mobile, article.Title, openIpItem.OpenId)
  455. }
  456. }
  457. }
  458. } else {
  459. err = models.RemoveArticleInterviewApply(uid, req.ArticleId)
  460. if err != nil {
  461. br.Msg = "取消申请失败"
  462. br.ErrMsg = "取消申请失败,Err:" + err.Error()
  463. return
  464. }
  465. br.Msg = "已取消申请"
  466. resp.Status = 2
  467. if user.CompanyId > 1 {
  468. mobile := user.Mobile
  469. if mobile == "" {
  470. mobile = user.Email
  471. }
  472. sellerItem, _ := models.GetSellerByCompanyId(user.CompanyId)
  473. if sellerItem != nil && sellerItem.AdminId > 0 && user.Mobile != "" {
  474. openIpItem, _ := models.GetUserRecordByUserIdByXzs(sellerItem.Mobile, 4)
  475. fmt.Println(openIpItem)
  476. if openIpItem != nil && openIpItem.OpenId != "" {
  477. go services.SendInterviewApplyCancelTemplateMsg(user.RealName, sellerItem.CompanyName, mobile, article.Title, openIpItem.OpenId)
  478. }
  479. }
  480. }
  481. }
  482. br.Ret = 200
  483. br.Success = true
  484. br.Data = resp
  485. }
  486. // @Title 获取报告详情
  487. // @Description 获取报告详情接口
  488. // @Param ArticleIdMd5 query int true "报告ID"
  489. // @Success 200 {object} models.ArticleDetailResp
  490. // @router /look/detail [get]
  491. func (this *ArticleCommonController) Detail() {
  492. br := new(models.BaseResponse).Init()
  493. defer func() {
  494. this.Data["json"] = br
  495. this.ServeJSON()
  496. }()
  497. articleIdMd5 := this.GetString("ArticleIdMd5")
  498. if articleIdMd5 == "" {
  499. br.Msg = "参数错误"
  500. br.ErrMsg = "参数错误"
  501. return
  502. }
  503. resp := new(models.ArticleDetailResp)
  504. detail, err := models.GetArticleDetailByIdMd5(articleIdMd5)
  505. if err != nil {
  506. br.Msg = "获取信息失败"
  507. br.ErrMsg = "获取信息失败,Err:" + err.Error()
  508. return
  509. }
  510. detail.Body = html.UnescapeString(detail.Body)
  511. //detail.Abstract = html.UnescapeString(detail.Abstract)
  512. sellerList, err := models.GetSellerList(detail.ArticleId)
  513. if err != nil {
  514. br.Msg = "获取信息失败"
  515. br.ErrMsg = "获取销售数据失败,Err:" + err.Error() + ";articleId" + strconv.Itoa(detail.ArticleId)
  516. return
  517. }
  518. if detail.ArticleId > 1000000 {
  519. var hrefRegexp = regexp.MustCompile("[0-9]\\d*")
  520. match := hrefRegexp.FindAllString(detail.SellerAndMobile, -1)
  521. if match != nil {
  522. for _, v := range match {
  523. sellerAndMobile := &models.SellerRep{
  524. SellerMobile: v,
  525. SellerName: strings.Replace(detail.SellerAndMobile, v, "", -1),
  526. }
  527. sellerList = append(sellerList, sellerAndMobile)
  528. }
  529. }
  530. }
  531. resp.HasPermission = 1
  532. detail.Abstract, _ = services.GetReportContentTextSub(detail.Abstract)
  533. detail.SellerList = sellerList
  534. resp.Detail = detail
  535. br.Ret = 200
  536. br.Success = true
  537. br.Msg = "获取成功"
  538. br.Data = resp
  539. }
  540. // @Title 上传文章阅读时间
  541. // @Description 上传文章阅读时间接口
  542. // @Param request body models.AddStopTimeRep true "type json string"
  543. // @Success 200 {object} models.ArticleDetailResp
  544. // @router /addStopTime [post]
  545. func (this *ArticleController) AddStopTime() {
  546. br := new(models.BaseResponse).Init()
  547. defer func() {
  548. this.Data["json"] = br
  549. this.ServeJSON()
  550. }()
  551. user := this.User
  552. if user == nil {
  553. br.Msg = "请登录"
  554. br.ErrMsg = "请登录,用户信息为空"
  555. br.Ret = 408
  556. return
  557. }
  558. var req models.AddStopTimeRep
  559. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  560. if err != nil {
  561. br.Msg = "参数解析异常!"
  562. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  563. return
  564. }
  565. uid := user.UserId
  566. articleId := req.ArticleId
  567. stopTime := req.StopTime
  568. outType := req.OutType
  569. source := req.Source
  570. if articleId <= 0 {
  571. br.Msg = "参数错误"
  572. br.ErrMsg = "参数错误"
  573. return
  574. }
  575. if stopTime == 0 {
  576. stopTime = 1
  577. }
  578. if outType != 2 {
  579. outType = 1
  580. }
  581. if source != "PC" {
  582. source = "MOBILE"
  583. }
  584. detail := new(models.ArticleDetail)
  585. hasPermission := 0
  586. hasFree := 0
  587. //判断是否已经申请过
  588. applyCount, err := models.GetApplyRecordCount(uid)
  589. if err != nil && err.Error() != utils.ErrNoRow() {
  590. br.Msg = "获取信息失败"
  591. br.ErrMsg = "判断是否已申请过试用失败,Err:" + err.Error()
  592. return
  593. }
  594. //`description:"1:有该行业权限,正常展示,2:无该行业权限,不存在权益客户下,3:无该品类权限,4:潜在客户,未提交过申请,5:潜在客户,已提交过申请"`
  595. if user.CompanyId > 1 {
  596. companyPermission, err := models.GetCompanyPermission(user.CompanyId)
  597. if err != nil {
  598. br.Msg = "获取信息失败"
  599. br.ErrMsg = "判断是否已申请访谈失败,Err:" + strconv.Itoa(uid) + ";articleId" + strconv.Itoa(articleId)
  600. return
  601. }
  602. detail, err = models.GetArticleDetailById(articleId)
  603. if err != nil {
  604. br.Msg = "获取信息失败"
  605. br.ErrMsg = "获取文章信息失败,Err:" + err.Error() + strconv.Itoa(uid) + ";articleId" + strconv.Itoa(articleId)
  606. return
  607. }
  608. if companyPermission == "" {
  609. if applyCount > 0 {
  610. hasPermission = 5
  611. } else {
  612. hasPermission = 2
  613. }
  614. hasFree = 2
  615. goto Loop
  616. } else {
  617. hasFree = 1
  618. var articlePermissionPermissionName string
  619. if detail.CategoryId > 0 {
  620. articlePermission, err := models.GetArticlePermission(detail.CategoryId)
  621. if err != nil {
  622. br.Msg = "获取信息失败"
  623. br.ErrMsg = "获取报告权限失败,Err:" + err.Error() + strconv.Itoa(uid) + ";articleId" + strconv.Itoa(articleId)
  624. return
  625. }
  626. if articlePermission == nil {
  627. br.Msg = "获取信息失败"
  628. br.ErrMsg = "报告权限不存在,Err:" + strconv.Itoa(uid) + ";articleId" + strconv.Itoa(articleId)
  629. return
  630. }
  631. articlePermissionPermissionName = articlePermission.PermissionName
  632. } else {
  633. articlePermissionPermissionName = detail.CategoryName
  634. }
  635. var hasPersion bool
  636. slice := strings.Split(articlePermissionPermissionName, ",")
  637. for _, v := range slice {
  638. if strings.Contains(companyPermission, v) {
  639. hasPersion = true
  640. }
  641. }
  642. if hasPersion {
  643. detailNew, err := models.GetNewArticleHistoryRecord(uid, articleId)
  644. if err == nil {
  645. hasPermission = 1
  646. historyRecord := new(models.AddStopTimeNewRep)
  647. historyRecord.StopTime = detailNew.StopTime + stopTime
  648. historyRecord.Id = detailNew.Id
  649. historyRecord.OutType = outType
  650. go models.UpdateArticleStopTime(historyRecord)
  651. }
  652. //不统计本公司的阅读记录、正常退出的不做时间差统计
  653. if user.CompanyId != 16 && stopTime > 3 {
  654. key := "CYGX_ARTICLE_PV" + strconv.Itoa(articleId) + "_" + strconv.Itoa(uid) + "_" + strconv.Itoa(user.CompanyId) + "_" + strconv.Itoa(outType)
  655. record := new(models.CygxArticleHistoryRecordNewpv)
  656. record.UserId = uid
  657. record.ArticleId = articleId
  658. record.CreateTime = time.Now().Add(-time.Second * time.Duration(stopTime))
  659. record.ModifyTime = time.Now()
  660. record.Mobile = user.Mobile
  661. record.Email = user.Email
  662. record.CompanyId = user.CompanyId
  663. record.CompanyName = user.CompanyName
  664. record.StopTime = stopTime
  665. record.OutType = outType
  666. record.Source = source
  667. if !utils.Rc.IsExist(key) || outType != 2 {
  668. //新增浏览记录
  669. go models.AddCygxArticleViewRecordNewpv(record)
  670. } else {
  671. go models.UpdateCygxArticleViewRecordNewpv(record, stopTime)
  672. }
  673. utils.Rc.Put(key, 1, 10*time.Minute)
  674. }
  675. models.ModifyReportLastViewTime(uid)
  676. } else { //无该行业权限
  677. hasPermission = 3
  678. }
  679. }
  680. } else { //潜在客户
  681. if applyCount > 0 {
  682. hasPermission = 5
  683. } else {
  684. hasPermission = 4
  685. }
  686. }
  687. Loop:
  688. resp := new(models.ArticleDetailAddStopTimeRep)
  689. resp.HasPermission = hasPermission
  690. resp.HasFree = hasFree
  691. br.Ret = 200
  692. br.Success = true
  693. br.Msg = "操作成功"
  694. br.Data = resp
  695. }
  696. // @Title 文章带问
  697. // @Description 新增文章带问接口
  698. // @Param request body models.AddArticleAskRep true "type json string"
  699. // @Success Ret=200 新增成功
  700. // @router /askAdd [post]
  701. func (this *ArticleController) AskAdd() {
  702. br := new(models.BaseResponse).Init()
  703. defer func() {
  704. this.Data["json"] = br
  705. this.ServeJSON()
  706. }()
  707. user := this.User
  708. if user == nil {
  709. br.Msg = "请登录"
  710. br.ErrMsg = "请登录,SysUser Is Empty"
  711. br.Ret = 408
  712. return
  713. }
  714. var req models.AddArticleAskRep
  715. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  716. if err != nil {
  717. br.Msg = "参数解析异常!"
  718. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  719. return
  720. }
  721. if req.Content == "" {
  722. br.Msg = "建议内容不可为空"
  723. return
  724. }
  725. content := req.Content
  726. itemToken, err := services.WxGetToken()
  727. if err != nil {
  728. br.Msg = "GetWxAccessToken Err:" + err.Error()
  729. return
  730. }
  731. if itemToken.AccessToken == "" {
  732. br.Msg = "accessToken is empty"
  733. return
  734. }
  735. commerr, err := weapp.MSGSecCheck(itemToken.AccessToken, content)
  736. if err != nil {
  737. br.Msg = "内容校验失败!"
  738. br.ErrMsg = "内容校验失败,Err:" + err.Error()
  739. return
  740. }
  741. if commerr.ErrCode != 0 {
  742. br.Msg = "内容违规,请重新提交!"
  743. br.ErrMsg = "颜文字内容违规,Err:" + commerr.ErrMSG
  744. return
  745. }
  746. articleId := req.ArticleId
  747. count, _ := models.GetArticleCountById(articleId)
  748. if count == 0 {
  749. br.Msg = "操作失败"
  750. br.ErrMsg = "文章ID错误,不存在 articleId:" + strconv.Itoa(articleId)
  751. return
  752. }
  753. companyDetail, err := models.GetCompanyDetailById(user.CompanyId)
  754. if err != nil {
  755. br.Msg = "提交失败!"
  756. br.ErrMsg = "获取客户详情失败,Err:" + err.Error()
  757. return
  758. }
  759. if companyDetail == nil {
  760. br.Msg = "提交失败!"
  761. br.ErrMsg = "客户不存在,uid:" + strconv.Itoa(user.UserId)
  762. return
  763. }
  764. item := new(models.CygxArticleAsk)
  765. item.UserId = user.UserId
  766. item.ArticleId = req.ArticleId
  767. item.CompanyId = user.CompanyId
  768. item.CompanyName = companyDetail.CompanyName
  769. item.CreateTime = time.Now()
  770. item.Mobile = user.Mobile
  771. item.Email = user.Email
  772. item.Content = content
  773. _, err = models.AddArticleAsk(item)
  774. if err != nil {
  775. br.Msg = "提交失败"
  776. br.ErrMsg = "提交失败,Err:" + err.Error()
  777. return
  778. }
  779. companyItem, err := models.GetSellerDetailAllByCompanyId(user.CompanyId)
  780. if err != nil {
  781. br.Msg = "获取信息失败"
  782. br.ErrMsg = "获取所属销售信息失败,Err:" + err.Error()
  783. return
  784. }
  785. var mobile string
  786. if utils.RunMode == "release" {
  787. //mobile = utils.WxMsgTemplateIdAskMsgMobileAll + "," + companyItem.Mobile
  788. mobile = utils.WxMsgTemplateIdAskMsgMobileAll
  789. } else {
  790. mobile = utils.WxMsgTemplateIdAskMsgMobile
  791. }
  792. openIdList, err := models.GetWxOpenIdByMobileList(mobile)
  793. if err != nil {
  794. br.Msg = "提交失败"
  795. br.ErrMsg = "提交失败,Err:" + err.Error()
  796. return
  797. }
  798. detail, err := models.GetArticleDetailById(articleId)
  799. if err != nil {
  800. br.Msg = "获取信息失败"
  801. br.ErrMsg = "获取信息失败,Err:" + err.Error()
  802. return
  803. }
  804. companyName := user.CompanyName + "-" + user.RealName + "(" + companyItem.SellerName + ")"
  805. go services.SendWxMsgWithAsk(companyName, time.Now().Format(utils.FormatDateTime), content, detail.Title, openIdList, req.ArticleId)
  806. br.Ret = 200
  807. br.Success = true
  808. br.Msg = "提交成功"
  809. }
  810. // @Title 下载PDF打水印
  811. // @Description 下载PDF打水印接口
  812. // @Param ArticleId query int true "报告ID"
  813. // @Success 200 {object} models.ArticleDetailFileLink
  814. // @router /pdfwatermark [get]
  815. func (this *ArticleController) Pdfwatermark() {
  816. br := new(models.BaseResponse).Init()
  817. defer func() {
  818. this.Data["json"] = br
  819. this.ServeJSON()
  820. }()
  821. user := this.User
  822. if user == nil {
  823. br.Msg = "请登录"
  824. br.ErrMsg = "请登录,用户信息为空"
  825. br.Ret = 408
  826. return
  827. }
  828. uid := user.UserId
  829. articleId, err := this.GetInt("ArticleId")
  830. if articleId <= 0 {
  831. br.Msg = "文章不存在"
  832. br.ErrMsg = "文章不存在,文章ID错误"
  833. return
  834. }
  835. //缓存校验
  836. cacheKey := fmt.Sprint("xygx:apply_record:add:", uid, "ArticleId_", articleId)
  837. ttlTime := utils.Rc.GetRedisTTL(cacheKey)
  838. if ttlTime > 0 {
  839. br.Msg = "下载失败,下载过于频繁"
  840. br.ErrMsg = "下载失败,下载过于频繁:mobile" + user.Mobile
  841. return
  842. }
  843. detail := new(models.ArticleDetail)
  844. detail, err = models.GetArticleDetailById(articleId)
  845. if err != nil {
  846. br.Msg = "获取信息失败"
  847. br.ErrMsg = "获取信息失败,Err:" + err.Error()
  848. return
  849. }
  850. fileLink := detail.FileLink
  851. if fileLink == "" {
  852. br.Msg = "下载失败"
  853. br.ErrMsg = "下载失败,报告链接不存在"
  854. return
  855. }
  856. mobile := user.Mobile
  857. if mobile == "" {
  858. mobile = user.Email
  859. }
  860. sliceLink := strings.Split(fileLink, "/")
  861. uploadDir := "static/pdf/"
  862. //判断文件夹是否存在,不存在则创建
  863. if !utils.FileIsExist(uploadDir) {
  864. err = os.MkdirAll(uploadDir, 0755)
  865. if err != nil {
  866. br.Msg = "下载失败"
  867. br.ErrMsg = "存储目录创建失败,Err:" + err.Error()
  868. return
  869. }
  870. }
  871. var oldFile string
  872. var newFile string
  873. //获取PDF源文件名称
  874. pdfName := sliceLink[len(sliceLink)-1]
  875. pdfName = utils.MD5(pdfName) + ".pdf"
  876. oldFile = uploadDir + pdfName
  877. //判断PDF本地是否存在,不存在则保存到本地
  878. if !utils.FileIsExist(oldFile) {
  879. res, err := nhttp.Get(fileLink)
  880. if err != nil {
  881. br.Msg = "下载失败"
  882. br.ErrMsg = "获取源文件失败,Err:" + err.Error()
  883. return
  884. }
  885. defer res.Body.Close()
  886. // 获得get请求响应的reader对象
  887. reader := bufio.NewReaderSize(res.Body, 32*1024)
  888. file, err := os.Create(oldFile)
  889. if err != nil {
  890. br.Msg = "下载失败"
  891. br.ErrMsg = "保存源文件到本地失败,Err:" + err.Error()
  892. return
  893. }
  894. defer file.Close()
  895. //获得文件的writer对象
  896. writer := bufio.NewWriter(file)
  897. written, _ := io.Copy(writer, reader)
  898. fmt.Printf("Total length: %d", written)
  899. }
  900. newFile = uploadDir + "new_" + pdfName
  901. onTop := true
  902. wm, err := pdfcpu.ParseTextWatermarkDetails(mobile, " op:.4, pos:c ,points:16 ", onTop, 1)
  903. if err != nil {
  904. br.Msg = "下载失败"
  905. br.ErrMsg = "生成水印文件失败,Err:" + err.Error()
  906. return
  907. }
  908. err = api.AddWatermarksFile(oldFile, newFile, nil, wm, nil)
  909. if err != nil {
  910. br.Msg = "下载失败"
  911. br.ErrMsg = "生成水印PDF失败,Err:" + err.Error()
  912. return
  913. }
  914. resp := new(models.ArticleDetailFileLink)
  915. randStr := utils.GetRandStringNoSpecialChar(28)
  916. fileName := randStr + ".pdf"
  917. savePath := uploadDir + time.Now().Format("200601/20060102/")
  918. savePath += fileName
  919. //上传到阿里云
  920. err = services.UploadFileToAliyun(fileName, newFile, savePath)
  921. if err != nil {
  922. br.Msg = "下载失败"
  923. br.ErrMsg = "文件上传失败,Err:" + err.Error()
  924. return
  925. }
  926. fileHost := "https://hzstatic.hzinsights.com/"
  927. resourceUrl := fileHost + savePath
  928. defer func() {
  929. os.Remove(newFile)
  930. }()
  931. utils.Rc.SetNX(cacheKey, user.Mobile, time.Minute*5)
  932. resp.FileLink = resourceUrl
  933. br.Ret = 200
  934. br.Success = true
  935. br.Msg = "获取成功"
  936. br.Data = resp
  937. }