article.go 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982
  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. //作者头像
  259. if detail.DepartmentId > 0 {
  260. departmentDetail, err := models.GetArticleDepartmentDateilById(detail.DepartmentId)
  261. if err == nil {
  262. detail.DepartmentImgUrl = departmentDetail.ImgUrl
  263. }
  264. }
  265. } else { //潜在客户
  266. if applyCount > 0 {
  267. hasPermission = 5
  268. } else {
  269. hasPermission = 4
  270. }
  271. }
  272. Loop:
  273. if hasPermission != 1 {
  274. detail.Body = ""
  275. detail.BodyText = ""
  276. } else {
  277. articleFollowdetail, err := models.GetArticleFollowDetail(articleId, uid)
  278. if err != nil {
  279. br.Msg = "获取信息失败"
  280. br.ErrMsg = "获取关注信息失败,Err:" + err.Error()
  281. return
  282. }
  283. detail.FollowNum = articleFollowdetail.DNum
  284. detail.CollectionNum = articleFollowdetail.AcNum
  285. if articleFollowdetail.MdNum > 0 {
  286. detail.IsFollow = true
  287. }
  288. if detail.IsSummary == 1 {
  289. detail.IsBelongSummary = true
  290. }
  291. if detail.IsReport == 1 {
  292. detail.IsBelongReport = true
  293. }
  294. haveResearch = true
  295. }
  296. if hasPermission == 2 || hasPermission == 4 {
  297. //获取销售手机号
  298. sellerItemQy, err := models.GetSellerByCompanyIdCheckFicc(user.CompanyId, 2)
  299. if err != nil && err.Error() != utils.ErrNoRow() {
  300. br.Msg = "获取信息失败"
  301. br.ErrMsg = "获取销售信息失败,Err:" + err.Error()
  302. return
  303. }
  304. if sellerItemQy != nil {
  305. hasPermission = 3
  306. detail.SellerMobile = sellerItemQy.Mobile
  307. detail.SellerName = sellerItemQy.RealName
  308. }
  309. }
  310. resp := new(models.ArticleDetailResp)
  311. resp.HasPermission = hasPermission
  312. resp.HaveResearch = haveResearch
  313. resp.HasFree = hasFree
  314. resp.Detail = detail
  315. if user.Mobile != "" {
  316. resp.Mobile = user.Mobile
  317. } else {
  318. resp.Mobile = user.Email
  319. }
  320. br.Ret = 200
  321. br.Success = true
  322. br.Msg = "获取成功"
  323. br.Data = resp
  324. }
  325. // @Title 收藏
  326. // @Description 收藏
  327. // @Param request body models.ArticleCollectReq true "type json string"
  328. // @Success 200 {object} models.FontsCollectResp
  329. // @router /collect [post]
  330. func (this *ArticleController) ArticleCollect() {
  331. br := new(models.BaseResponse).Init()
  332. defer func() {
  333. this.Data["json"] = br
  334. this.ServeJSON()
  335. }()
  336. user := this.User
  337. if user == nil {
  338. br.Msg = "请重新登录"
  339. br.Ret = 408
  340. return
  341. }
  342. uid := user.UserId
  343. var req models.ArticleCollectReq
  344. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  345. if err != nil {
  346. br.Msg = "参数解析异常!"
  347. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  348. return
  349. }
  350. count, err := models.GetArticleCollectCount(uid, req.ArticleId)
  351. if err != nil {
  352. br.Msg = "获取数据失败!"
  353. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  354. return
  355. }
  356. resp := new(models.ArticleCollectResp)
  357. if count <= 0 {
  358. item := new(models.CygxArticleCollect)
  359. item.ArticleId = req.ArticleId
  360. item.UserId = uid
  361. item.CreateTime = time.Now()
  362. _, err = models.AddCygxArticleCollect(item)
  363. if err != nil {
  364. br.Msg = "收藏失败"
  365. br.ErrMsg = "收藏失败,Err:" + err.Error()
  366. return
  367. }
  368. br.Msg = "收藏成功"
  369. resp.Status = 1
  370. } else {
  371. err = models.RemoveArticleCollect(uid, req.ArticleId)
  372. if err != nil {
  373. br.Msg = "取消收藏失败"
  374. br.ErrMsg = "取消收藏失败,Err:" + err.Error()
  375. return
  376. }
  377. br.Msg = "已取消收藏"
  378. resp.Status = 2
  379. }
  380. collectTotal, err := models.GetArticleCollectUsersCount(req.ArticleId)
  381. if err != nil {
  382. br.Msg = "获取数据失败"
  383. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  384. return
  385. }
  386. resp.CollectCount = collectTotal
  387. br.Ret = 200
  388. br.Success = true
  389. br.Data = resp
  390. }
  391. // @Title 访谈申请
  392. // @Description 访谈申请
  393. // @Param request body models.ArticleInterviewApplyReq true "type json string"
  394. // @Success 200 {object} models.FontsCollectResp
  395. // @router /interview/apply [post]
  396. func (this *ArticleController) InterviewApply() {
  397. br := new(models.BaseResponse).Init()
  398. defer func() {
  399. this.Data["json"] = br
  400. this.ServeJSON()
  401. }()
  402. user := this.User
  403. if user == nil {
  404. br.Msg = "请重新登录"
  405. br.Ret = 408
  406. return
  407. }
  408. uid := user.UserId
  409. var req models.ArticleInterviewApplyReq
  410. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  411. if err != nil {
  412. br.Msg = "参数解析异常!"
  413. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  414. return
  415. }
  416. article, err := models.GetArticleDetailById(req.ArticleId)
  417. if err != nil {
  418. br.Msg = "获取纪要失败!"
  419. br.ErrMsg = "获取纪要失败,Err:" + err.Error()
  420. return
  421. }
  422. count, err := models.GetArticleInterviewApplyCount(uid, req.ArticleId)
  423. if err != nil {
  424. br.Msg = "获取数据失败!"
  425. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  426. return
  427. }
  428. resp := new(models.ArticleInterviewApplyResp)
  429. if count <= 0 {
  430. item := new(models.CygxInterviewApply)
  431. item.ArticleId = req.ArticleId
  432. item.UserId = uid
  433. item.CompanyId = user.CompanyId
  434. item.Status = "待邀请"
  435. item.Sort = 1
  436. item.ArticleTitle = article.Title
  437. item.CreateTime = time.Now()
  438. item.ModifyTime = time.Now()
  439. item.ArticleIdMd5 = article.ArticleIdMd5
  440. _, err = models.AddCygxInterviewApply(item)
  441. if err != nil {
  442. br.Msg = "申请失败"
  443. br.ErrMsg = "申请失败,Err:" + err.Error()
  444. return
  445. }
  446. br.Msg = "申请成功"
  447. resp.Status = 1
  448. //发送模板消息
  449. if user.CompanyId > 1 {
  450. mobile := user.Mobile
  451. if mobile == "" {
  452. mobile = user.Email
  453. }
  454. sellerItem, _ := models.GetSellerByCompanyId(user.CompanyId)
  455. if sellerItem != nil && sellerItem.AdminId > 0 && user.Mobile != "" {
  456. openIpItem, _ := models.GetUserRecordByUserIdByXzs(sellerItem.Mobile, 4)
  457. fmt.Println(openIpItem)
  458. if openIpItem != nil && openIpItem.OpenId != "" {
  459. go services.SendInterviewApplyTemplateMsg(user.RealName, sellerItem.CompanyName, mobile, article.Title, openIpItem.OpenId)
  460. }
  461. }
  462. }
  463. } else {
  464. err = models.RemoveArticleInterviewApply(uid, req.ArticleId)
  465. if err != nil {
  466. br.Msg = "取消申请失败"
  467. br.ErrMsg = "取消申请失败,Err:" + err.Error()
  468. return
  469. }
  470. br.Msg = "已取消申请"
  471. resp.Status = 2
  472. if user.CompanyId > 1 {
  473. mobile := user.Mobile
  474. if mobile == "" {
  475. mobile = user.Email
  476. }
  477. sellerItem, _ := models.GetSellerByCompanyId(user.CompanyId)
  478. if sellerItem != nil && sellerItem.AdminId > 0 && user.Mobile != "" {
  479. openIpItem, _ := models.GetUserRecordByUserIdByXzs(sellerItem.Mobile, 4)
  480. fmt.Println(openIpItem)
  481. if openIpItem != nil && openIpItem.OpenId != "" {
  482. go services.SendInterviewApplyCancelTemplateMsg(user.RealName, sellerItem.CompanyName, mobile, article.Title, openIpItem.OpenId)
  483. }
  484. }
  485. }
  486. }
  487. br.Ret = 200
  488. br.Success = true
  489. br.Data = resp
  490. }
  491. // @Title 获取报告详情
  492. // @Description 获取报告详情接口
  493. // @Param ArticleIdMd5 query int true "报告ID"
  494. // @Success 200 {object} models.ArticleDetailResp
  495. // @router /look/detail [get]
  496. func (this *ArticleCommonController) Detail() {
  497. br := new(models.BaseResponse).Init()
  498. defer func() {
  499. this.Data["json"] = br
  500. this.ServeJSON()
  501. }()
  502. articleIdMd5 := this.GetString("ArticleIdMd5")
  503. if articleIdMd5 == "" {
  504. br.Msg = "参数错误"
  505. br.ErrMsg = "参数错误"
  506. return
  507. }
  508. resp := new(models.ArticleDetailResp)
  509. detail, err := models.GetArticleDetailByIdMd5(articleIdMd5)
  510. if err != nil {
  511. br.Msg = "获取信息失败"
  512. br.ErrMsg = "获取信息失败,Err:" + err.Error()
  513. return
  514. }
  515. detail.Body = html.UnescapeString(detail.Body)
  516. //detail.Abstract = html.UnescapeString(detail.Abstract)
  517. sellerList, err := models.GetSellerList(detail.ArticleId)
  518. if err != nil {
  519. br.Msg = "获取信息失败"
  520. br.ErrMsg = "获取销售数据失败,Err:" + err.Error() + ";articleId" + strconv.Itoa(detail.ArticleId)
  521. return
  522. }
  523. if detail.ArticleId > 1000000 {
  524. var hrefRegexp = regexp.MustCompile("[0-9]\\d*")
  525. match := hrefRegexp.FindAllString(detail.SellerAndMobile, -1)
  526. if match != nil {
  527. for _, v := range match {
  528. sellerAndMobile := &models.SellerRep{
  529. SellerMobile: v,
  530. SellerName: strings.Replace(detail.SellerAndMobile, v, "", -1),
  531. }
  532. sellerList = append(sellerList, sellerAndMobile)
  533. }
  534. }
  535. }
  536. resp.HasPermission = 1
  537. detail.Abstract, _ = services.GetReportContentTextSub(detail.Abstract)
  538. detail.SellerList = sellerList
  539. resp.Detail = detail
  540. br.Ret = 200
  541. br.Success = true
  542. br.Msg = "获取成功"
  543. br.Data = resp
  544. }
  545. // @Title 上传文章阅读时间
  546. // @Description 上传文章阅读时间接口
  547. // @Param request body models.AddStopTimeRep true "type json string"
  548. // @Success 200 {object} models.ArticleDetailResp
  549. // @router /addStopTime [post]
  550. func (this *ArticleController) AddStopTime() {
  551. br := new(models.BaseResponse).Init()
  552. defer func() {
  553. this.Data["json"] = br
  554. this.ServeJSON()
  555. }()
  556. user := this.User
  557. if user == nil {
  558. br.Msg = "请登录"
  559. br.ErrMsg = "请登录,用户信息为空"
  560. br.Ret = 408
  561. return
  562. }
  563. var req models.AddStopTimeRep
  564. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  565. if err != nil {
  566. br.Msg = "参数解析异常!"
  567. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  568. return
  569. }
  570. uid := user.UserId
  571. articleId := req.ArticleId
  572. stopTime := req.StopTime
  573. outType := req.OutType
  574. source := req.Source
  575. if articleId <= 0 {
  576. br.Msg = "参数错误"
  577. br.ErrMsg = "参数错误"
  578. return
  579. }
  580. if stopTime == 0 {
  581. stopTime = 1
  582. }
  583. if outType != 2 {
  584. outType = 1
  585. }
  586. if source != "PC" {
  587. source = "MOBILE"
  588. }
  589. detail := new(models.ArticleDetail)
  590. hasPermission := 0
  591. hasFree := 0
  592. //判断是否已经申请过
  593. applyCount, err := models.GetApplyRecordCount(uid)
  594. if err != nil && err.Error() != utils.ErrNoRow() {
  595. br.Msg = "获取信息失败"
  596. br.ErrMsg = "判断是否已申请过试用失败,Err:" + err.Error()
  597. return
  598. }
  599. //`description:"1:有该行业权限,正常展示,2:无该行业权限,不存在权益客户下,3:无该品类权限,4:潜在客户,未提交过申请,5:潜在客户,已提交过申请"`
  600. if user.CompanyId > 1 {
  601. companyPermission, err := models.GetCompanyPermission(user.CompanyId)
  602. if err != nil {
  603. br.Msg = "获取信息失败"
  604. br.ErrMsg = "判断是否已申请访谈失败,Err:" + strconv.Itoa(uid) + ";articleId" + strconv.Itoa(articleId)
  605. return
  606. }
  607. detail, err = models.GetArticleDetailById(articleId)
  608. if err != nil {
  609. br.Msg = "获取信息失败"
  610. br.ErrMsg = "获取文章信息失败,Err:" + err.Error() + strconv.Itoa(uid) + ";articleId" + strconv.Itoa(articleId)
  611. return
  612. }
  613. if companyPermission == "" {
  614. if applyCount > 0 {
  615. hasPermission = 5
  616. } else {
  617. hasPermission = 2
  618. }
  619. hasFree = 2
  620. goto Loop
  621. } else {
  622. hasFree = 1
  623. var articlePermissionPermissionName string
  624. if detail.CategoryId > 0 {
  625. articlePermission, err := models.GetArticlePermission(detail.CategoryId)
  626. if err != nil {
  627. br.Msg = "获取信息失败"
  628. br.ErrMsg = "获取报告权限失败,Err:" + err.Error() + strconv.Itoa(uid) + ";articleId" + strconv.Itoa(articleId)
  629. return
  630. }
  631. if articlePermission == nil {
  632. br.Msg = "获取信息失败"
  633. br.ErrMsg = "报告权限不存在,Err:" + strconv.Itoa(uid) + ";articleId" + strconv.Itoa(articleId)
  634. return
  635. }
  636. articlePermissionPermissionName = articlePermission.PermissionName
  637. } else {
  638. articlePermissionPermissionName = detail.CategoryName
  639. }
  640. var hasPersion bool
  641. slice := strings.Split(articlePermissionPermissionName, ",")
  642. for _, v := range slice {
  643. if strings.Contains(companyPermission, v) {
  644. hasPersion = true
  645. }
  646. }
  647. if hasPersion {
  648. detailNew, err := models.GetNewArticleHistoryRecord(uid, articleId)
  649. if err == nil {
  650. hasPermission = 1
  651. historyRecord := new(models.AddStopTimeNewRep)
  652. historyRecord.StopTime = detailNew.StopTime + stopTime
  653. historyRecord.Id = detailNew.Id
  654. historyRecord.OutType = outType
  655. go models.UpdateArticleStopTime(historyRecord)
  656. }
  657. //不统计本公司的阅读记录、正常退出的不做时间差统计
  658. if user.CompanyId != 16 && stopTime > 3 {
  659. key := "CYGX_ARTICLE_PV" + strconv.Itoa(articleId) + "_" + strconv.Itoa(uid) + "_" + strconv.Itoa(user.CompanyId) + "_" + strconv.Itoa(outType)
  660. record := new(models.CygxArticleHistoryRecordNewpv)
  661. record.UserId = uid
  662. record.ArticleId = articleId
  663. record.CreateTime = time.Now().Add(-time.Second * time.Duration(stopTime))
  664. record.ModifyTime = time.Now()
  665. record.Mobile = user.Mobile
  666. record.Email = user.Email
  667. record.CompanyId = user.CompanyId
  668. record.CompanyName = user.CompanyName
  669. record.StopTime = stopTime
  670. record.OutType = outType
  671. record.Source = source
  672. if !utils.Rc.IsExist(key) || outType != 2 {
  673. //新增浏览记录
  674. go models.AddCygxArticleViewRecordNewpv(record)
  675. recordRedis := new(services.ReportViewRecord)
  676. recordRedis.UserId = user.UserId
  677. recordRedis.ReportId = articleId
  678. recordRedis.Mobile = user.Mobile
  679. recordRedis.Email = user.Email
  680. recordRedis.RealName = user.RealName
  681. recordRedis.CompanyName = user.CompanyName
  682. recordRedis.CreateTime = time.Now().Add(-time.Second * time.Duration(stopTime))
  683. go services.PushViewRecordNewRedisData(recordRedis, user.CompanyId)
  684. } else {
  685. go models.UpdateCygxArticleViewRecordNewpv(record, stopTime)
  686. }
  687. utils.Rc.Put(key, 1, 10*time.Minute)
  688. }
  689. models.ModifyReportLastViewTime(uid)
  690. } else { //无该行业权限
  691. hasPermission = 3
  692. }
  693. }
  694. } else { //潜在客户
  695. if applyCount > 0 {
  696. hasPermission = 5
  697. } else {
  698. hasPermission = 4
  699. }
  700. }
  701. Loop:
  702. resp := new(models.ArticleDetailAddStopTimeRep)
  703. resp.HasPermission = hasPermission
  704. resp.HasFree = hasFree
  705. br.Ret = 200
  706. br.Success = true
  707. br.Msg = "操作成功"
  708. br.Data = resp
  709. }
  710. // @Title 文章带问
  711. // @Description 新增文章带问接口
  712. // @Param request body models.AddArticleAskRep true "type json string"
  713. // @Success Ret=200 新增成功
  714. // @router /askAdd [post]
  715. func (this *ArticleController) AskAdd() {
  716. br := new(models.BaseResponse).Init()
  717. defer func() {
  718. this.Data["json"] = br
  719. this.ServeJSON()
  720. }()
  721. user := this.User
  722. if user == nil {
  723. br.Msg = "请登录"
  724. br.ErrMsg = "请登录,SysUser Is Empty"
  725. br.Ret = 408
  726. return
  727. }
  728. var req models.AddArticleAskRep
  729. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  730. if err != nil {
  731. br.Msg = "参数解析异常!"
  732. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  733. return
  734. }
  735. if req.Content == "" {
  736. br.Msg = "建议内容不可为空"
  737. return
  738. }
  739. content := req.Content
  740. itemToken, err := services.WxGetToken()
  741. if err != nil {
  742. br.Msg = "GetWxAccessToken Err:" + err.Error()
  743. return
  744. }
  745. if itemToken.AccessToken == "" {
  746. br.Msg = "accessToken is empty"
  747. return
  748. }
  749. commerr, err := weapp.MSGSecCheck(itemToken.AccessToken, content)
  750. if err != nil {
  751. br.Msg = "内容校验失败!"
  752. br.ErrMsg = "内容校验失败,Err:" + err.Error()
  753. return
  754. }
  755. if commerr.ErrCode != 0 {
  756. br.Msg = "内容违规,请重新提交!"
  757. br.ErrMsg = "颜文字内容违规,Err:" + commerr.ErrMSG
  758. return
  759. }
  760. articleId := req.ArticleId
  761. count, _ := models.GetArticleCountById(articleId)
  762. if count == 0 {
  763. br.Msg = "操作失败"
  764. br.ErrMsg = "文章ID错误,不存在 articleId:" + strconv.Itoa(articleId)
  765. return
  766. }
  767. companyDetail, err := models.GetCompanyDetailById(user.CompanyId)
  768. if err != nil {
  769. br.Msg = "提交失败!"
  770. br.ErrMsg = "获取客户详情失败,Err:" + err.Error()
  771. return
  772. }
  773. if companyDetail == nil {
  774. br.Msg = "提交失败!"
  775. br.ErrMsg = "客户不存在,uid:" + strconv.Itoa(user.UserId)
  776. return
  777. }
  778. item := new(models.CygxArticleAsk)
  779. item.UserId = user.UserId
  780. item.ArticleId = req.ArticleId
  781. item.CompanyId = user.CompanyId
  782. item.CompanyName = companyDetail.CompanyName
  783. item.CreateTime = time.Now()
  784. item.Mobile = user.Mobile
  785. item.Email = user.Email
  786. item.Content = content
  787. _, err = models.AddArticleAsk(item)
  788. if err != nil {
  789. br.Msg = "提交失败"
  790. br.ErrMsg = "提交失败,Err:" + err.Error()
  791. return
  792. }
  793. companyItem, err := models.GetSellerDetailAllByCompanyId(user.CompanyId)
  794. if err != nil {
  795. br.Msg = "获取信息失败"
  796. br.ErrMsg = "获取所属销售信息失败,Err:" + err.Error()
  797. return
  798. }
  799. var mobile string
  800. if utils.RunMode == "release" {
  801. //mobile = utils.WxMsgTemplateIdAskMsgMobileAll + "," + companyItem.Mobile
  802. mobile = utils.WxMsgTemplateIdAskMsgMobileAll
  803. } else {
  804. mobile = utils.WxMsgTemplateIdAskMsgMobile
  805. }
  806. openIdList, err := models.GetWxOpenIdByMobileList(mobile)
  807. if err != nil {
  808. br.Msg = "提交失败"
  809. br.ErrMsg = "提交失败,Err:" + err.Error()
  810. return
  811. }
  812. detail, err := models.GetArticleDetailById(articleId)
  813. if err != nil {
  814. br.Msg = "获取信息失败"
  815. br.ErrMsg = "获取信息失败,Err:" + err.Error()
  816. return
  817. }
  818. companyName := user.CompanyName + "-" + user.RealName + "(" + companyItem.SellerName + ")"
  819. go services.SendWxMsgWithAsk(companyName, time.Now().Format(utils.FormatDateTime), content, detail.Title, openIdList, req.ArticleId)
  820. br.Ret = 200
  821. br.Success = true
  822. br.Msg = "提交成功"
  823. }
  824. // @Title 下载PDF打水印
  825. // @Description 下载PDF打水印接口
  826. // @Param ArticleId query int true "报告ID"
  827. // @Success 200 {object} models.ArticleDetailFileLink
  828. // @router /pdfwatermark [get]
  829. func (this *ArticleController) Pdfwatermark() {
  830. br := new(models.BaseResponse).Init()
  831. defer func() {
  832. this.Data["json"] = br
  833. this.ServeJSON()
  834. }()
  835. user := this.User
  836. if user == nil {
  837. br.Msg = "请登录"
  838. br.ErrMsg = "请登录,用户信息为空"
  839. br.Ret = 408
  840. return
  841. }
  842. uid := user.UserId
  843. articleId, err := this.GetInt("ArticleId")
  844. if articleId <= 0 {
  845. br.Msg = "文章不存在"
  846. br.ErrMsg = "文章不存在,文章ID错误"
  847. return
  848. }
  849. //缓存校验
  850. cacheKey := fmt.Sprint("xygx:apply_record:add:", uid, "ArticleId_", articleId)
  851. ttlTime := utils.Rc.GetRedisTTL(cacheKey)
  852. if ttlTime > 0 {
  853. br.Msg = "下载失败,下载过于频繁"
  854. br.ErrMsg = "下载失败,下载过于频繁:mobile" + user.Mobile
  855. return
  856. }
  857. detail := new(models.ArticleDetail)
  858. detail, err = models.GetArticleDetailById(articleId)
  859. if err != nil {
  860. br.Msg = "获取信息失败"
  861. br.ErrMsg = "获取信息失败,Err:" + err.Error()
  862. return
  863. }
  864. fileLink := detail.FileLink
  865. if fileLink == "" {
  866. br.Msg = "下载失败"
  867. br.ErrMsg = "下载失败,报告链接不存在"
  868. return
  869. }
  870. mobile := user.Mobile
  871. if mobile == "" {
  872. mobile = user.Email
  873. }
  874. sliceLink := strings.Split(fileLink, "/")
  875. uploadDir := "static/pdf/"
  876. //判断文件夹是否存在,不存在则创建
  877. if !utils.FileIsExist(uploadDir) {
  878. err = os.MkdirAll(uploadDir, 0755)
  879. if err != nil {
  880. br.Msg = "下载失败"
  881. br.ErrMsg = "存储目录创建失败,Err:" + err.Error()
  882. return
  883. }
  884. }
  885. var oldFile string
  886. var newFile string
  887. //获取PDF源文件名称
  888. pdfName := sliceLink[len(sliceLink)-1]
  889. pdfName = utils.MD5(pdfName) + ".pdf"
  890. oldFile = uploadDir + pdfName
  891. //判断PDF本地是否存在,不存在则保存到本地
  892. if !utils.FileIsExist(oldFile) {
  893. res, err := nhttp.Get(fileLink)
  894. if err != nil {
  895. br.Msg = "下载失败"
  896. br.ErrMsg = "获取源文件失败,Err:" + err.Error()
  897. return
  898. }
  899. defer res.Body.Close()
  900. // 获得get请求响应的reader对象
  901. reader := bufio.NewReaderSize(res.Body, 32*1024)
  902. file, err := os.Create(oldFile)
  903. if err != nil {
  904. br.Msg = "下载失败"
  905. br.ErrMsg = "保存源文件到本地失败,Err:" + err.Error()
  906. return
  907. }
  908. defer file.Close()
  909. //获得文件的writer对象
  910. writer := bufio.NewWriter(file)
  911. written, _ := io.Copy(writer, reader)
  912. fmt.Printf("Total length: %d", written)
  913. }
  914. newFile = uploadDir + "new_" + pdfName
  915. onTop := true
  916. wm, err := pdfcpu.ParseTextWatermarkDetails(mobile, " op:.4, pos:c ,points:16 ", onTop, 1)
  917. if err != nil {
  918. br.Msg = "下载失败"
  919. br.ErrMsg = "生成水印文件失败,Err:" + err.Error()
  920. return
  921. }
  922. err = api.AddWatermarksFile(oldFile, newFile, nil, wm, nil)
  923. if err != nil {
  924. br.Msg = "下载失败"
  925. br.ErrMsg = "生成水印PDF失败,Err:" + err.Error()
  926. return
  927. }
  928. resp := new(models.ArticleDetailFileLink)
  929. randStr := utils.GetRandStringNoSpecialChar(28)
  930. fileName := randStr + ".pdf"
  931. savePath := uploadDir + time.Now().Format("200601/20060102/")
  932. savePath += fileName
  933. //上传到阿里云
  934. err = services.UploadFileToAliyun(fileName, newFile, savePath)
  935. if err != nil {
  936. br.Msg = "下载失败"
  937. br.ErrMsg = "文件上传失败,Err:" + err.Error()
  938. return
  939. }
  940. fileHost := "https://hzstatic.hzinsights.com/"
  941. resourceUrl := fileHost + savePath
  942. defer func() {
  943. os.Remove(newFile)
  944. }()
  945. utils.Rc.SetNX(cacheKey, user.Mobile, time.Minute*5)
  946. resp.FileLink = resourceUrl
  947. br.Ret = 200
  948. br.Success = true
  949. br.Msg = "获取成功"
  950. br.Data = resp
  951. }