article.go 29 KB

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