article.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "hongze/hongze_cygx/models"
  5. "hongze/hongze_cygx/services"
  6. "hongze/hongze_cygx/utils"
  7. "html"
  8. "strconv"
  9. "strings"
  10. "time"
  11. )
  12. type ArticleController struct {
  13. BaseAuthController
  14. }
  15. type ArticleCommonController struct {
  16. BaseCommonController
  17. }
  18. // @Title 获取报告详情
  19. // @Description 获取报告详情接口
  20. // @Param ArticleId query int true "报告ID"
  21. // @Success 200 {object} models.ArticleDetailResp
  22. // @router /detail [get]
  23. func (this *ArticleController) Detail() {
  24. br := new(models.BaseResponse).Init()
  25. defer func() {
  26. this.Data["json"] = br
  27. this.ServeJSON()
  28. }()
  29. user := this.User
  30. if user == nil {
  31. br.Msg = "请登录"
  32. br.ErrMsg = "请登录,用户信息为空"
  33. br.Ret = 408
  34. return
  35. }
  36. uid := user.UserId
  37. articleId, err := this.GetInt("ArticleId")
  38. if articleId <= 0 {
  39. br.Msg = "参数错误"
  40. br.ErrMsg = "参数错误"
  41. return
  42. }
  43. detail := new(models.ArticleDetail)
  44. hasPermission := 0
  45. hasFree := 0
  46. //判断是否已经申请过
  47. applyCount, err := models.GetApplyRecordCount(uid)
  48. if err != nil && err.Error() != utils.ErrNoRow() {
  49. br.Msg = "获取信息失败"
  50. br.ErrMsg = "判断是否已申请过试用失败,Err:" + err.Error()
  51. return
  52. }
  53. //`description:"1:有该行业权限,正常展示,2:无该行业权限,不存在权益客户下,3:无该品类权限,4:潜在客户,未提交过申请,5:潜在客户,已提交过申请"`
  54. if user.CompanyId > 1 {
  55. companyPermission, err := models.GetCompanyPermission(user.CompanyId)
  56. if err != nil {
  57. br.Msg = "获取信息失败"
  58. br.ErrMsg = "判断是否已申请访谈失败,Err:" + strconv.Itoa(uid) + ";articleId" + strconv.Itoa(articleId)
  59. return
  60. }
  61. detail, err = models.GetArticleDetailById(articleId)
  62. services.BodyAnalysis2(detail.Body)
  63. if err != nil {
  64. br.Msg = "获取信息失败"
  65. br.ErrMsg = "获取信息失败,Err:" + err.Error()
  66. return
  67. }
  68. detail.Body = html.UnescapeString(detail.Body)
  69. //detail.Abstract = html.UnescapeString(detail.Abstract)
  70. detail.Abstract, _ = services.GetReportContentTextSub(detail.Abstract)
  71. if companyPermission == "" {
  72. if applyCount > 0 {
  73. hasPermission = 5
  74. } else {
  75. hasPermission = 2
  76. }
  77. hasFree = 2
  78. goto Loop
  79. } else {
  80. hasFree = 1
  81. // 原有的权限校验 更改于 2021-05-18
  82. //articlePermission, err := models.GetArticlePermission(detail.SubCategoryName)
  83. //fmt.Println(articlePermission)
  84. //fmt.Println(detail.SubCategoryName)
  85. //if err != nil {
  86. // br.Msg = "获取信息失败"
  87. // br.ErrMsg = "获取报告权限失败,Err:" + err.Error() + strconv.Itoa(uid) + ";articleId" + strconv.Itoa(articleId)
  88. // return
  89. //}
  90. //if articlePermission == nil {
  91. // br.Msg = "获取信息失败"
  92. // br.ErrMsg = "报告权限不存在,Err:" + strconv.Itoa(uid) + ";articleId" + strconv.Itoa(articleId)
  93. // return
  94. //}
  95. //for _, p := range articlePermission {
  96. // if strings.Contains(companyPermission, p.PermissionName) {
  97. // hasPermission = 1
  98. // historyRecord := new(models.CygxArticleHistoryRecord)
  99. // historyRecord.UserId = uid
  100. // historyRecord.ArticleId = articleId
  101. // historyRecord.CreateTime = time.Now()
  102. // historyRecord.Mobile = user.Mobile
  103. // historyRecord.Email = user.Email
  104. // historyRecord.CompanyId = user.CompanyId
  105. // historyRecord.CompanyName = user.CompanyName
  106. // go models.AddCygxArticleHistoryRecord(historyRecord)
  107. // break
  108. // } else { //无该行业权限
  109. // hasPermission = 3
  110. // }
  111. //}
  112. articlePermission, err := models.GetArticlePermission(detail.CategoryId)
  113. if err != nil {
  114. br.Msg = "获取信息失败"
  115. br.ErrMsg = "获取报告权限失败,Err:" + err.Error() + strconv.Itoa(uid) + ";articleId" + strconv.Itoa(articleId)
  116. return
  117. }
  118. if articlePermission == nil {
  119. br.Msg = "获取信息失败"
  120. br.ErrMsg = "报告权限不存在,Err:" + strconv.Itoa(uid) + ";articleId" + strconv.Itoa(articleId)
  121. return
  122. }
  123. if strings.Contains(companyPermission, articlePermission.PermissionName) {
  124. hasPermission = 1
  125. historyRecord := new(models.CygxArticleHistoryRecord)
  126. historyRecord.UserId = uid
  127. historyRecord.ArticleId = articleId
  128. historyRecord.CreateTime = time.Now()
  129. historyRecord.Mobile = user.Mobile
  130. historyRecord.Email = user.Email
  131. historyRecord.CompanyId = user.CompanyId
  132. historyRecord.CompanyName = user.CompanyName
  133. go models.AddCygxArticleHistoryRecord(historyRecord)
  134. } else { //无该行业权限
  135. hasPermission = 3
  136. }
  137. if hasPermission == 1 {
  138. key := "CYGX_ARTICLE_" + strconv.Itoa(articleId) + "_" + strconv.Itoa(uid)
  139. if !utils.Rc.IsExist(key) {
  140. //新增浏览记录
  141. record := new(models.CygxArticleViewRecord)
  142. record.UserId = uid
  143. record.ArticleId = articleId
  144. record.CreateTime = time.Now()
  145. record.Mobile = user.Mobile
  146. record.Email = user.Email
  147. record.CompanyId = user.CompanyId
  148. record.CompanyName = user.CompanyName
  149. go models.AddCygxArticleViewRecord(record)
  150. utils.Rc.Put(key, 1, 5*time.Second)
  151. models.ModifyReportLastViewTime(uid)
  152. }
  153. }
  154. }
  155. collectCount, err := models.GetArticleCollectCount(uid, articleId)
  156. if err != nil && err.Error() != utils.ErrNoRow() {
  157. br.Msg = "获取信息失败"
  158. br.ErrMsg = "判断是否已收藏失败,Err:" + strconv.Itoa(uid) + ";articleId" + strconv.Itoa(articleId)
  159. return
  160. }
  161. if collectCount > 0 {
  162. detail.IsCollect = true
  163. }
  164. interviewApplyItem, err := models.GetArticleInterviewApply(uid, articleId)
  165. if err != nil && err.Error() != utils.ErrNoRow() {
  166. br.Msg = "获取信息失败"
  167. br.ErrMsg = "判断是否已申请访谈失败,Err:" + strconv.Itoa(uid) + ";articleId" + strconv.Itoa(articleId)
  168. return
  169. }
  170. if interviewApplyItem != nil && interviewApplyItem.InterviewApplyId > 0 {
  171. detail.IsInterviewApply = true
  172. detail.InterviewApplyStatus = interviewApplyItem.Status
  173. }
  174. //获取销售手机号
  175. sellerItem, err := models.GetSellerByCompanyId(user.CompanyId)
  176. if err != nil {
  177. br.Msg = "获取信息失败"
  178. br.ErrMsg = "获取销售数据失败,Err:" + strconv.Itoa(uid) + ";articleId" + strconv.Itoa(articleId)
  179. return
  180. }
  181. if sellerItem != nil {
  182. detail.SellerMobile = sellerItem.Mobile
  183. detail.SellerName = sellerItem.RealName
  184. }
  185. sellerList, err := models.GetSellerList(articleId)
  186. if err != nil {
  187. br.Msg = "获取信息失败"
  188. br.ErrMsg = "获取销售数据失败,Err:" + err.Error() + strconv.Itoa(uid) + ";articleId" + strconv.Itoa(articleId)
  189. return
  190. }
  191. detail.SellerList = sellerList
  192. } else { //潜在客户
  193. if applyCount > 0 {
  194. hasPermission = 5
  195. } else {
  196. hasPermission = 4
  197. }
  198. }
  199. Loop:
  200. if hasPermission != 1 {
  201. detail.Body = ""
  202. detail.BodyText = ""
  203. }
  204. resp := new(models.ArticleDetailResp)
  205. resp.HasPermission = hasPermission
  206. resp.HasFree = hasFree
  207. resp.Detail = detail
  208. br.Ret = 200
  209. br.Success = true
  210. br.Msg = "获取成功"
  211. br.Data = resp
  212. }
  213. // @Title 收藏
  214. // @Description 收藏
  215. // @Param request body models.ArticleCollectReq true "type json string"
  216. // @Success 200 {object} models.FontsCollectResp
  217. // @router /collect [post]
  218. func (this *ArticleController) ArticleCollect() {
  219. br := new(models.BaseResponse).Init()
  220. defer func() {
  221. this.Data["json"] = br
  222. this.ServeJSON()
  223. }()
  224. user := this.User
  225. if user == nil {
  226. br.Msg = "请重新登录"
  227. br.Ret = 408
  228. return
  229. }
  230. uid := user.UserId
  231. var req models.ArticleCollectReq
  232. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  233. if err != nil {
  234. br.Msg = "参数解析异常!"
  235. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  236. return
  237. }
  238. count, err := models.GetArticleCollectCount(uid, req.ArticleId)
  239. if err != nil {
  240. br.Msg = "获取数据失败!"
  241. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  242. return
  243. }
  244. resp := new(models.ArticleCollectResp)
  245. if count <= 0 {
  246. item := new(models.CygxArticleCollect)
  247. item.ArticleId = req.ArticleId
  248. item.UserId = uid
  249. item.CreateTime = time.Now()
  250. _, err = models.AddCygxArticleCollect(item)
  251. if err != nil {
  252. br.Msg = "收藏失败"
  253. br.ErrMsg = "收藏失败,Err:" + err.Error()
  254. return
  255. }
  256. br.Msg = "收藏成功"
  257. resp.Status = 1
  258. } else {
  259. err = models.RemoveArticleCollect(uid, req.ArticleId)
  260. if err != nil {
  261. br.Msg = "取消收藏失败"
  262. br.ErrMsg = "取消收藏失败,Err:" + err.Error()
  263. return
  264. }
  265. br.Msg = "已取消收藏"
  266. resp.Status = 2
  267. }
  268. collectTotal, err := models.GetArticleCollectUsersCount(req.ArticleId)
  269. if err != nil {
  270. br.Msg = "获取数据失败"
  271. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  272. return
  273. }
  274. resp.CollectCount = collectTotal
  275. br.Ret = 200
  276. br.Success = true
  277. br.Data = resp
  278. }
  279. // @Title 访谈申请
  280. // @Description 访谈申请
  281. // @Param request body models.ArticleInterviewApplyReq true "type json string"
  282. // @Success 200 {object} models.FontsCollectResp
  283. // @router /interview/apply [post]
  284. func (this *ArticleController) InterviewApply() {
  285. br := new(models.BaseResponse).Init()
  286. defer func() {
  287. this.Data["json"] = br
  288. this.ServeJSON()
  289. }()
  290. user := this.User
  291. if user == nil {
  292. br.Msg = "请重新登录"
  293. br.Ret = 408
  294. return
  295. }
  296. uid := user.UserId
  297. var req models.ArticleInterviewApplyReq
  298. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  299. if err != nil {
  300. br.Msg = "参数解析异常!"
  301. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  302. return
  303. }
  304. article, err := models.GetArticleDetailById(req.ArticleId)
  305. if err != nil {
  306. br.Msg = "获取纪要失败!"
  307. br.ErrMsg = "获取纪要失败,Err:" + err.Error()
  308. return
  309. }
  310. count, err := models.GetArticleInterviewApplyCount(uid, req.ArticleId)
  311. if err != nil {
  312. br.Msg = "获取数据失败!"
  313. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  314. return
  315. }
  316. resp := new(models.ArticleInterviewApplyResp)
  317. if count <= 0 {
  318. item := new(models.CygxInterviewApply)
  319. item.ArticleId = req.ArticleId
  320. item.UserId = uid
  321. item.CompanyId = user.CompanyId
  322. item.Status = "待邀请"
  323. item.Sort = 1
  324. item.ArticleTitle = article.Title
  325. item.CreateTime = time.Now()
  326. item.ModifyTime = time.Now()
  327. item.ArticleIdMd5 = article.ArticleIdMd5
  328. _, err = models.AddCygxInterviewApply(item)
  329. if err != nil {
  330. br.Msg = "申请失败"
  331. br.ErrMsg = "申请失败,Err:" + err.Error()
  332. return
  333. }
  334. br.Msg = "申请成功"
  335. resp.Status = 1
  336. //发送模板消息
  337. if user.CompanyId > 1 {
  338. mobile := user.Mobile
  339. if mobile == "" {
  340. mobile = user.Email
  341. }
  342. sellerItem, _ := models.GetSellerByCompanyId(user.CompanyId)
  343. if sellerItem != nil && sellerItem.AdminId > 0 && user.Mobile != "" {
  344. openIpItem, _ := models.GetUserRecordByUserId(sellerItem.UserId, 1)
  345. if openIpItem != nil && openIpItem.OpenId != "" {
  346. go services.SendInterviewApplyTemplateMsg(user.RealName, sellerItem.CompanyName, mobile, article.Title, openIpItem.OpenId)
  347. }
  348. }
  349. }
  350. } else {
  351. err = models.RemoveArticleInterviewApply(uid, req.ArticleId)
  352. if err != nil {
  353. br.Msg = "取消申请失败"
  354. br.ErrMsg = "取消申请失败,Err:" + err.Error()
  355. return
  356. }
  357. br.Msg = "已取消申请"
  358. resp.Status = 2
  359. if user.CompanyId > 1 {
  360. mobile := user.Mobile
  361. if mobile == "" {
  362. mobile = user.Email
  363. }
  364. sellerItem, _ := models.GetSellerByCompanyId(user.CompanyId)
  365. if sellerItem != nil && sellerItem.AdminId > 0 && user.Mobile != "" {
  366. openIpItem, _ := models.GetUserRecordByUserId(sellerItem.UserId, 1)
  367. if openIpItem != nil && openIpItem.OpenId != "" {
  368. go services.SendInterviewApplyCancelTemplateMsg(user.RealName, sellerItem.CompanyName, mobile, article.Title, openIpItem.OpenId)
  369. }
  370. }
  371. }
  372. }
  373. br.Ret = 200
  374. br.Success = true
  375. br.Data = resp
  376. }
  377. // @Title 获取报告详情
  378. // @Description 获取报告详情接口
  379. // @Param ArticleIdMd5 query int true "报告ID"
  380. // @Success 200 {object} models.ArticleDetailResp
  381. // @router /look/detail [get]
  382. func (this *ArticleCommonController) Detail() {
  383. br := new(models.BaseResponse).Init()
  384. defer func() {
  385. this.Data["json"] = br
  386. this.ServeJSON()
  387. }()
  388. articleIdMd5 := this.GetString("ArticleIdMd5")
  389. if articleIdMd5 == "" {
  390. br.Msg = "参数错误"
  391. br.ErrMsg = "参数错误"
  392. return
  393. }
  394. resp := new(models.ArticleDetailResp)
  395. detail, err := models.GetArticleDetailByIdMd5(articleIdMd5)
  396. if err != nil && err.Error() != utils.ErrNoRow() {
  397. br.Msg = "获取信息失败"
  398. br.ErrMsg = "获取信息失败,Err:" + err.Error()
  399. return
  400. }
  401. if detail == nil {
  402. resp.HasPermission = 2
  403. } else {
  404. resp.HasPermission = 1
  405. }
  406. if detail != nil {
  407. detail.Body = html.UnescapeString(detail.Body)
  408. detail.Abstract = html.UnescapeString(detail.Abstract)
  409. }
  410. sellerList, err := models.GetSellerList(detail.ArticleId)
  411. if err != nil {
  412. br.Msg = "获取信息失败"
  413. br.ErrMsg = "获取销售数据失败,Err:" + err.Error() + ";articleId" + strconv.Itoa(detail.ArticleId)
  414. return
  415. }
  416. detail.SellerList = sellerList
  417. resp.Detail = detail
  418. br.Ret = 200
  419. br.Success = true
  420. br.Msg = "获取成功"
  421. br.Data = resp
  422. }
  423. // @Title 上传文章阅读时间
  424. // @Description 上传文章阅读时间接口
  425. // @Param request body models.AddStopTimeRep true "type json string"
  426. // @Success 200 {object} models.ArticleDetailResp
  427. // @router /addStopTime [post]
  428. func (this *ArticleController) AddStopTime() {
  429. br := new(models.BaseResponse).Init()
  430. defer func() {
  431. this.Data["json"] = br
  432. this.ServeJSON()
  433. }()
  434. user := this.User
  435. if user == nil {
  436. br.Msg = "请登录"
  437. br.ErrMsg = "请登录,用户信息为空"
  438. br.Ret = 408
  439. return
  440. }
  441. var req models.AddStopTimeRep
  442. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  443. if err != nil {
  444. br.Msg = "参数解析异常!"
  445. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  446. return
  447. }
  448. uid := user.UserId
  449. articleId := req.ArticleId
  450. stopTime := req.StopTime
  451. if articleId <= 0 {
  452. br.Msg = "参数错误"
  453. br.ErrMsg = "参数错误"
  454. return
  455. }
  456. if stopTime == 0 {
  457. br.Msg = "时间错误"
  458. return
  459. }
  460. detail := new(models.ArticleDetail)
  461. hasPermission := 0
  462. hasFree := 0
  463. //判断是否已经申请过
  464. applyCount, err := models.GetApplyRecordCount(uid)
  465. if err != nil && err.Error() != utils.ErrNoRow() {
  466. br.Msg = "获取信息失败"
  467. br.ErrMsg = "判断是否已申请过试用失败,Err:" + err.Error()
  468. return
  469. }
  470. //`description:"1:有该行业权限,正常展示,2:无该行业权限,不存在权益客户下,3:无该品类权限,4:潜在客户,未提交过申请,5:潜在客户,已提交过申请"`
  471. if user.CompanyId > 1 {
  472. companyPermission, err := models.GetCompanyPermission(user.CompanyId)
  473. if err != nil {
  474. br.Msg = "获取信息失败"
  475. br.ErrMsg = "判断是否已申请访谈失败,Err:" + strconv.Itoa(uid) + ";articleId" + strconv.Itoa(articleId)
  476. return
  477. }
  478. detail, err = models.GetArticleDetailById(articleId)
  479. if err != nil {
  480. br.Msg = "获取信息失败"
  481. br.ErrMsg = "获取信息失败,Err:" + err.Error()
  482. return
  483. }
  484. if companyPermission == "" {
  485. if applyCount > 0 {
  486. hasPermission = 5
  487. } else {
  488. hasPermission = 2
  489. }
  490. hasFree = 2
  491. goto Loop
  492. } else {
  493. hasFree = 1
  494. articlePermission, err := models.GetArticlePermission(detail.CategoryId)
  495. if err != nil {
  496. br.Msg = "获取信息失败"
  497. br.ErrMsg = "获取报告权限失败,Err:" + err.Error() + strconv.Itoa(uid) + ";articleId" + strconv.Itoa(articleId)
  498. return
  499. }
  500. if articlePermission == nil {
  501. br.Msg = "获取信息失败"
  502. br.ErrMsg = "报告权限不存在,Err:" + strconv.Itoa(uid) + ";articleId" + strconv.Itoa(articleId)
  503. return
  504. }
  505. if strings.Contains(companyPermission, articlePermission.PermissionName) {
  506. detailNew, err := models.GetNewArticleHistoryRecord(uid, articleId)
  507. if err != nil {
  508. br.Msg = "获取信息失败"
  509. br.ErrMsg = "获取信息失败,Err:" + err.Error()
  510. return
  511. }
  512. hasPermission = 1
  513. historyRecord := new(models.AddStopTimeNewRep)
  514. historyRecord.StopTime = detailNew.StopTime + stopTime
  515. historyRecord.Id = detailNew.Id
  516. go models.AddArticleStopTime(historyRecord)
  517. } else { //无该行业权限
  518. hasPermission = 3
  519. }
  520. }
  521. } else { //潜在客户
  522. if applyCount > 0 {
  523. hasPermission = 5
  524. } else {
  525. hasPermission = 4
  526. }
  527. }
  528. Loop:
  529. resp := new(models.ArticleDetailAddStopTimeRep)
  530. resp.HasPermission = hasPermission
  531. resp.HasFree = hasFree
  532. br.Ret = 200
  533. br.Success = true
  534. br.Msg = "操作成功"
  535. br.Data = resp
  536. }