article.go 29 KB

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