article.go 28 KB

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