article.go 26 KB

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