article.go 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843
  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_mfyx/models"
  15. "hongze/hongze_mfyx/services"
  16. "hongze/hongze_mfyx/utils"
  17. "html"
  18. nhttp "net/http"
  19. "strconv"
  20. "strings"
  21. "time"
  22. )
  23. type ArticleController struct {
  24. BaseAuthController
  25. }
  26. type ArticleCommonController struct {
  27. BaseCommonController
  28. }
  29. type ArticleControllerMobile struct {
  30. BaseAuthMobileController
  31. }
  32. // @Title 获取报告详情
  33. // @Description 获取报告详情接口
  34. // @Param ArticleId query int true "报告ID"
  35. // @Param IsSendWx query int false "是否是通过微信模版进来的 1是,其它否"
  36. // @Success 200 {object} models.ArticleDetailResp
  37. // @router /detail [get]
  38. func (this *ArticleController) Detail() {
  39. br := new(models.BaseResponse).Init()
  40. defer func() {
  41. this.Data["json"] = br
  42. this.ServeJSON()
  43. }()
  44. user := this.User
  45. if user == nil {
  46. br.Msg = "请登录"
  47. br.ErrMsg = "请登录,用户信息为空"
  48. br.Ret = 408
  49. return
  50. }
  51. uid := user.UserId
  52. articleId, err := this.GetInt("ArticleId")
  53. isSendWx, _ := this.GetInt("IsSendWx")
  54. if articleId <= 0 {
  55. br.Msg = "文章不存在"
  56. br.ErrMsg = "文章不存在,文章ID错误"
  57. return
  58. }
  59. detail := new(models.ArticleDetail)
  60. hasPermission := 0
  61. var haveResearch bool
  62. resp := new(models.ArticleDetailResp)
  63. detail, err = models.GetArticleDetailById(articleId)
  64. if err != nil {
  65. br.Msg = "获取信息失败"
  66. br.ErrMsg = "获取信息失败,Err:" + err.Error()
  67. return
  68. }
  69. detail.PublishDate = utils.TimeRemoveHms2(detail.PublishDate)
  70. detail.Body = html.UnescapeString(detail.Body)
  71. 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)
  72. detail.Body = strings.Replace(detail.Body, "pre", "div", -1)
  73. detail.Abstract = html.UnescapeString(detail.Abstract)
  74. //detail.Abstract, _ = services.GetReportContentTextSubNew(detail.Abstract)
  75. //作者头像
  76. if detail.DepartmentId > 0 {
  77. departmentDetail, err := models.GetArticleDepartmentDateilById(detail.DepartmentId)
  78. if err == nil {
  79. detail.DepartmentImgUrl = departmentDetail.ImgUrl
  80. }
  81. }
  82. // 判断是否属于研选类型的报告
  83. detail.IsResearch = true
  84. articleCollectMap, _ := services.GetCygxArticleCollectMap(user.UserId)
  85. detail.IsCollect = articleCollectMap[detail.ArticleId]
  86. //是否是通过模板消息进来的
  87. if isSendWx == 1 {
  88. var condition string
  89. var pars []interface{}
  90. pars = make([]interface{}, 0)
  91. condition = ` AND article_id = ? `
  92. reportMappingMap, _ := services.GetReportMappingMap()
  93. if reportMappingMap[detail.CategoryId] {
  94. chooseCategoryMap, _ := services.GetChooseCategoryMap(user)
  95. detail.IsShowFollowButton = true
  96. detail.IsFollowButton = chooseCategoryMap[detail.CategoryId]
  97. } else {
  98. pars = append(pars, articleId)
  99. industrialList, err := models.GetIndustrialArticleGroupManagementList(condition, pars)
  100. if err != nil && err.Error() != utils.ErrNoRow() {
  101. br.Msg = "获取信息失败"
  102. br.ErrMsg = "获取信息失败,Err:" + err.Error()
  103. return
  104. }
  105. if len(industrialList) > 0 {
  106. industryUserFollowMap, err := services.GetIndustryUserFollowMap(user)
  107. if err != nil {
  108. br.Msg = "获取信息失败"
  109. br.ErrMsg = "GetActivitySignupResp,Err:" + err.Error()
  110. return
  111. }
  112. for _, v := range industrialList {
  113. if industryUserFollowMap[v.IndustrialManagementId] {
  114. detail.IsFollowButton = true
  115. }
  116. }
  117. detail.IsShowFollowButton = true
  118. }
  119. }
  120. }
  121. havePower, err := services.GetArticleDetailUserPower(user, detail)
  122. if err != nil {
  123. br.Msg = "获取信息失败"
  124. br.ErrMsg = "校验用户权限失败,Err:" + err.Error()
  125. return
  126. }
  127. if havePower {
  128. hasPermission = 1
  129. go services.ArticleHistory(articleId, user)
  130. //30分钟之内阅读同一篇文章不错二次推送
  131. key := "CYGX_ARTICLE_READ" + strconv.Itoa(articleId) + "_" + strconv.Itoa(uid)
  132. if !utils.Rc.IsExist(key) {
  133. go services.ArticleUserRemind(user, detail, 1)
  134. utils.Rc.Put(key, 1, 30*time.Second)
  135. }
  136. articleFollowdetail, err := models.GetArticleFollowDetail(articleId, uid)
  137. if err != nil {
  138. br.Msg = "获取信息失败"
  139. br.ErrMsg = "获取关注信息失败,Err:" + err.Error()
  140. return
  141. }
  142. detail.FollowNum = articleFollowdetail.DNum
  143. detail.CollectionNum = articleFollowdetail.AcNum
  144. if articleFollowdetail.MdNum > 0 {
  145. detail.IsFollow = true
  146. }
  147. if detail.IsSummary == 1 {
  148. detail.IsBelongSummary = true
  149. }
  150. if detail.IsReport == 1 {
  151. detail.IsBelongReport = true
  152. }
  153. } else {
  154. hasPermission, err = services.GetUserDetailPermissionCode(user.UserId, user.CompanyId)
  155. if err != nil && err.Error() != utils.ErrNoRow() {
  156. br.Msg = "获取信息失败"
  157. br.ErrMsg = "获取信息失败,GetUserDetailPermissionCode Err:" + err.Error()
  158. return
  159. }
  160. //权益客户对权益销售信息进行展示
  161. if hasPermission == 2 || hasPermission == 3 {
  162. sellerItemQy, err := models.GetSellerByCompanyIdCheckFicc(user.CompanyId, 2)
  163. if err != nil && err.Error() != utils.ErrNoRow() {
  164. br.Msg = "获取信息失败"
  165. br.ErrMsg = "获取销售信息失败,Err:" + err.Error()
  166. return
  167. }
  168. if sellerItemQy != nil {
  169. detail.SellerMobile = sellerItemQy.Mobile
  170. detail.SellerName = sellerItemQy.RealName
  171. }
  172. }
  173. detail.Body = ""
  174. detail.BodyText = ""
  175. resp.PopupPriceMsg = "您可以购买畅读卡后参与(¥99/日,¥999/月)" //价格弹窗信息
  176. resp.IsShowWxPay = utils.IS_SHOW_WX_PAY //是否展示微信支付按钮
  177. resp.IsCompanyApply = services.GetUserApplyRecordCountByCompanyIdPay(user.CompanyId) //获取客户是否有过历史申请记录
  178. resp.IsNeedBusinessCard = services.GetCygxUserBusinessCardCount(user.UserId, user.CompanyId) //是否需要上传名片
  179. resp.GoodsList = services.GetUserGoodsCardList() //日卡月卡商品信息
  180. }
  181. if detail.ArticleTypeId == 14 {
  182. detail.IsApplyAppointmentExpert = true //判断文章类型是否属于专家访谈 查研观向11.0
  183. }
  184. resp.HasPermission = hasPermission
  185. resp.HaveResearch = haveResearch
  186. resp.Detail = detail
  187. if user.Mobile != "" {
  188. resp.Mobile = user.Mobile
  189. } else {
  190. resp.Mobile = user.Email
  191. }
  192. br.Ret = 200
  193. br.Success = true
  194. br.Msg = "获取成功"
  195. br.Data = resp
  196. }
  197. // @Title 收藏
  198. // @Description 收藏
  199. // @Param request body models.ArticleCollectReq true "type json string"
  200. // @Success 200 {object} models.FontsCollectResp
  201. // @router /collect [post]
  202. func (this *ArticleController) ArticleCollect() {
  203. br := new(models.BaseResponse).Init()
  204. defer func() {
  205. this.Data["json"] = br
  206. this.ServeJSON()
  207. }()
  208. user := this.User
  209. if user == nil {
  210. br.Msg = "请重新登录"
  211. br.Ret = 408
  212. return
  213. }
  214. uid := user.UserId
  215. var req models.ArticleCollectReq
  216. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  217. if err != nil {
  218. br.Msg = "参数解析异常!"
  219. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  220. return
  221. }
  222. articleId := req.ArticleId
  223. detail, err := models.GetArticleDetailById(articleId)
  224. if err != nil {
  225. br.Msg = "获取信息失败"
  226. br.ErrMsg = "获取信息失败,Err:" + err.Error()
  227. return
  228. }
  229. count, err := models.GetArticleCollectCount(uid, articleId)
  230. if err != nil {
  231. br.Msg = "获取数据失败!"
  232. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  233. return
  234. }
  235. resp := new(models.ArticleCollectResp)
  236. if count <= 0 {
  237. item := new(models.CygxArticleCollect)
  238. item.ArticleId = req.ArticleId
  239. item.UserId = uid
  240. item.CreateTime = time.Now()
  241. item.Mobile = user.Mobile
  242. item.Email = user.Email
  243. item.CompanyId = user.CompanyId
  244. item.CompanyName = user.CompanyName
  245. item.RealName = user.RealName
  246. _, err = models.AddCygxArticleCollect(item)
  247. if err != nil {
  248. br.Msg = "收藏失败"
  249. br.ErrMsg = "收藏失败,Err:" + err.Error()
  250. return
  251. }
  252. br.Msg = "收藏成功"
  253. resp.Status = 1
  254. // 文章收藏消息发送
  255. go services.ArticleUserRemind(user, detail, 2)
  256. go services.ArticleHistoryUserLabelLogAdd(articleId, user.UserId)
  257. } else {
  258. err = models.RemoveArticleCollect(uid, articleId)
  259. if err != nil {
  260. br.Msg = "取消收藏失败"
  261. br.ErrMsg = "取消收藏失败,Err:" + err.Error()
  262. return
  263. }
  264. br.Msg = "已取消收藏"
  265. resp.Status = 2
  266. }
  267. collectTotal, err := models.GetArticleCollectUsersCount(articleId)
  268. if err != nil {
  269. br.Msg = "获取数据失败"
  270. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  271. return
  272. }
  273. resp.CollectCount = collectTotal
  274. br.Ret = 200
  275. br.Success = true
  276. br.Data = resp
  277. }
  278. // @Title 访谈申请
  279. // @Description 访谈申请
  280. // @Param request body models.ArticleInterviewApplyReq true "type json string"
  281. // @Success 200 {object} models.FontsCollectResp
  282. // @router /interview/apply [post]
  283. func (this *ArticleController) InterviewApply() {
  284. br := new(models.BaseResponse).Init()
  285. defer func() {
  286. this.Data["json"] = br
  287. this.ServeJSON()
  288. }()
  289. user := this.User
  290. if user == nil {
  291. br.Msg = "请重新登录"
  292. br.Ret = 408
  293. return
  294. }
  295. uid := user.UserId
  296. var req models.ArticleInterviewApplyReq
  297. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  298. if err != nil {
  299. br.Msg = "参数解析异常!"
  300. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  301. return
  302. }
  303. article, err := models.GetArticleDetailById(req.ArticleId)
  304. if err != nil {
  305. br.Msg = "获取纪要失败!"
  306. br.ErrMsg = "获取纪要失败,Err:" + err.Error()
  307. return
  308. }
  309. count, err := models.GetArticleInterviewApplyCount(uid, req.ArticleId)
  310. if err != nil {
  311. br.Msg = "获取数据失败!"
  312. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  313. return
  314. }
  315. resp := new(models.ArticleInterviewApplyResp)
  316. if count <= 0 {
  317. item := new(models.CygxInterviewApply)
  318. item.ArticleId = req.ArticleId
  319. item.UserId = uid
  320. item.CompanyId = user.CompanyId
  321. item.Status = "待邀请"
  322. item.Sort = 1
  323. item.ArticleTitle = article.Title
  324. item.CreateTime = time.Now()
  325. item.ModifyTime = time.Now()
  326. item.ArticleIdMd5 = article.ArticleIdMd5
  327. _, err = models.AddCygxInterviewApply(item)
  328. if err != nil {
  329. br.Msg = "申请失败"
  330. br.ErrMsg = "申请失败,Err:" + err.Error()
  331. return
  332. }
  333. br.Msg = "申请成功"
  334. resp.Status = 1
  335. //发送模板消息
  336. if user.CompanyId > 1 {
  337. mobile := user.Mobile
  338. if mobile == "" {
  339. mobile = user.Email
  340. }
  341. sellerItem, _ := models.GetSellerByCompanyId(user.CompanyId)
  342. if sellerItem != nil && sellerItem.AdminId > 0 && user.Mobile != "" {
  343. openIpItem, _ := models.GetUserRecordByMobile(4, sellerItem.Mobile)
  344. if openIpItem != nil && openIpItem.OpenId != "" {
  345. go services.SendInterviewApplyTemplateMsg(user.RealName, sellerItem.CompanyName, mobile, article.Title, openIpItem)
  346. }
  347. }
  348. }
  349. } else {
  350. err = models.RemoveArticleInterviewApply(uid, req.ArticleId)
  351. if err != nil {
  352. br.Msg = "取消申请失败"
  353. br.ErrMsg = "取消申请失败,Err:" + err.Error()
  354. return
  355. }
  356. br.Msg = "已取消申请"
  357. resp.Status = 2
  358. if user.CompanyId > 1 {
  359. mobile := user.Mobile
  360. if mobile == "" {
  361. mobile = user.Email
  362. }
  363. sellerItem, _ := models.GetSellerByCompanyId(user.CompanyId)
  364. if sellerItem != nil && sellerItem.AdminId > 0 && user.Mobile != "" {
  365. openIpItem, _ := models.GetUserRecordByMobile(4, sellerItem.Mobile)
  366. if openIpItem != nil && openIpItem.OpenId != "" {
  367. go services.SendInterviewApplyCancelTemplateMsg(user.RealName, sellerItem.CompanyName, mobile, article.Title, openIpItem)
  368. }
  369. }
  370. }
  371. }
  372. br.Ret = 200
  373. br.Success = true
  374. br.Data = resp
  375. }
  376. // @Title 上传文章阅读时间
  377. // @Description 上传文章阅读时间接口
  378. // @Param request body models.AddStopTimeRep true "type json string"
  379. // @Success 200 {object} models.ArticleDetailResp
  380. // @router /addStopTime [post]
  381. func (this *ArticleController) AddStopTime() {
  382. br := new(models.BaseResponse).Init()
  383. defer func() {
  384. this.Data["json"] = br
  385. this.ServeJSON()
  386. }()
  387. user := this.User
  388. if user == nil {
  389. br.Msg = "请登录"
  390. br.ErrMsg = "请登录,用户信息为空"
  391. br.Ret = 408
  392. return
  393. }
  394. var req models.AddStopTimeRep
  395. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  396. if err != nil {
  397. br.Msg = "参数解析异常!"
  398. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  399. return
  400. }
  401. uid := user.UserId
  402. articleId := req.ArticleId
  403. stopTime := req.StopTime
  404. outType := req.OutType
  405. source := req.Source
  406. if articleId <= 0 {
  407. br.Msg = "参数错误"
  408. br.ErrMsg = "参数错误"
  409. return
  410. }
  411. if stopTime == 0 {
  412. stopTime = 1
  413. }
  414. if outType != 2 {
  415. outType = 1
  416. }
  417. if source != "PC" {
  418. source = "MOBILE"
  419. }
  420. detail := new(models.ArticleDetail)
  421. hasPermission := 0
  422. hasFree := 0
  423. //判断是否已经申请过
  424. applyCount, err := models.GetApplyRecordCount(uid)
  425. if err != nil && err.Error() != utils.ErrNoRow() {
  426. br.Msg = "获取信息失败"
  427. br.ErrMsg = "判断是否已申请过试用失败,Err:" + err.Error()
  428. return
  429. }
  430. //`description:"1:有该行业权限,正常展示,2:无该行业权限,不存在权益客户下,3:无该品类权限,4:潜在客户,未提交过申请,5:潜在客户,已提交过申请"`
  431. if user.CompanyId > 1 {
  432. companyPermission, err := models.GetCompanyPermission(user.CompanyId)
  433. if err != nil {
  434. br.Msg = "获取信息失败"
  435. br.ErrMsg = "判断是否已申请访谈失败,Err:" + strconv.Itoa(uid) + ";articleId" + strconv.Itoa(articleId)
  436. return
  437. }
  438. detail, err = models.GetArticleDetailById(articleId)
  439. if err != nil {
  440. br.Msg = "获取信息失败"
  441. br.ErrMsg = "获取文章信息失败,Err:" + err.Error() + strconv.Itoa(uid) + ";articleId" + strconv.Itoa(articleId)
  442. return
  443. }
  444. if companyPermission == "" {
  445. if applyCount > 0 {
  446. hasPermission = 5
  447. } else {
  448. hasPermission = 2
  449. }
  450. hasFree = 2
  451. goto Loop
  452. } else {
  453. hasFree = 1
  454. var articlePermissionPermissionName string
  455. if detail.CategoryId > 0 {
  456. articlePermission, err := models.GetArticlePermission(detail.CategoryId)
  457. if err != nil {
  458. br.Msg = "获取信息失败"
  459. br.ErrMsg = "获取报告权限失败,Err:" + err.Error() + strconv.Itoa(uid) + ";articleId" + strconv.Itoa(articleId)
  460. return
  461. }
  462. if articlePermission == nil {
  463. br.Msg = "获取信息失败"
  464. br.ErrMsg = "报告权限不存在,Err:" + strconv.Itoa(uid) + ";articleId" + strconv.Itoa(articleId)
  465. return
  466. }
  467. articlePermissionPermissionName = articlePermission.PermissionName
  468. } else {
  469. articlePermissionPermissionName = detail.CategoryName
  470. }
  471. var hasPersion bool
  472. slice := strings.Split(articlePermissionPermissionName, ",")
  473. //判断用户是否开通了个人研选权限,如果有权限后缀拼接权限名称
  474. mfyxUserPermissionTotal := services.GetMfyxUserPermissionTotal(uid)
  475. if mfyxUserPermissionTotal == 1 {
  476. companyPermission += "," + utils.CHART_PERMISSION_NAME_MF_YANXUAN
  477. companyPermission += "," + utils.MAI_FANG_YAN_XUAN_NAME
  478. }
  479. //如果有研选订阅的权限,那么就拼接一个 买方研选的权限做校验
  480. if strings.Contains(companyPermission, utils.CHART_PERMISSION_NAME_MF_YANXUAN) {
  481. companyPermission += "," + utils.MAI_FANG_YAN_XUAN_NAME
  482. }
  483. for _, v := range slice {
  484. if strings.Contains(companyPermission, v) {
  485. hasPersion = true
  486. }
  487. }
  488. if hasPersion {
  489. go services.ArticleHistoryStopTime(articleId, stopTime, outType, user)
  490. } else { //无该行业权限
  491. hasPermission = 3
  492. }
  493. }
  494. } else { //潜在客户
  495. if applyCount > 0 {
  496. hasPermission = 5
  497. } else {
  498. hasPermission = 4
  499. }
  500. }
  501. Loop:
  502. resp := new(models.ArticleDetailAddStopTimeRep)
  503. resp.HasPermission = hasPermission
  504. resp.HasFree = hasFree
  505. br.Ret = 200
  506. br.Success = true
  507. br.Msg = "操作成功"
  508. br.Data = resp
  509. }
  510. // @Title 文章带问
  511. // @Description 新增文章带问接口
  512. // @Param request body models.AddArticleAskRep true "type json string"
  513. // @Success Ret=200 新增成功
  514. // @router /askAdd [post]
  515. func (this *ArticleController) AskAdd() {
  516. br := new(models.BaseResponse).Init()
  517. defer func() {
  518. this.Data["json"] = br
  519. this.ServeJSON()
  520. }()
  521. user := this.User
  522. if user == nil {
  523. br.Msg = "请登录"
  524. br.ErrMsg = "请登录,SysUser Is Empty"
  525. br.Ret = 408
  526. return
  527. }
  528. var req models.AddArticleAskRep
  529. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  530. if err != nil {
  531. br.Msg = "参数解析异常!"
  532. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  533. return
  534. }
  535. if req.Content == "" {
  536. br.Msg = "建议内容不可为空"
  537. return
  538. }
  539. content := req.Content
  540. itemToken, err := services.WxGetToken()
  541. if err != nil {
  542. br.Msg = "GetWxAccessToken Err:" + err.Error()
  543. return
  544. }
  545. if itemToken.AccessToken == "" {
  546. br.Msg = "accessToken is empty"
  547. return
  548. }
  549. commerr, err := weapp.MSGSecCheck(itemToken.AccessToken, content)
  550. if err != nil {
  551. br.Msg = "内容校验失败!"
  552. br.ErrMsg = "内容校验失败,Err:" + err.Error()
  553. return
  554. }
  555. if commerr.ErrCode != 0 {
  556. br.Msg = "内容违规,请重新提交!"
  557. br.ErrMsg = "颜文字内容违规,Err:" + commerr.ErrMSG
  558. return
  559. }
  560. articleId := req.ArticleId
  561. count, _ := models.GetArticleCountById(articleId)
  562. if count == 0 {
  563. br.Msg = "操作失败"
  564. br.ErrMsg = "文章ID错误,不存在 articleId:" + strconv.Itoa(articleId)
  565. return
  566. }
  567. companyDetail, err := models.GetCompanyDetailById(user.CompanyId)
  568. if err != nil {
  569. br.Msg = "提交失败!"
  570. br.ErrMsg = "获取客户详情失败,Err:" + err.Error()
  571. return
  572. }
  573. if companyDetail == nil {
  574. br.Msg = "提交失败!"
  575. br.ErrMsg = "客户不存在,uid:" + strconv.Itoa(user.UserId)
  576. return
  577. }
  578. item := new(models.CygxArticleAsk)
  579. item.UserId = user.UserId
  580. item.ArticleId = req.ArticleId
  581. item.CompanyId = user.CompanyId
  582. item.CompanyName = companyDetail.CompanyName
  583. item.CreateTime = time.Now()
  584. item.Mobile = user.Mobile
  585. item.Email = user.Email
  586. item.Content = content
  587. _, err = models.AddArticleAsk(item)
  588. if err != nil {
  589. br.Msg = "提交失败"
  590. br.ErrMsg = "提交失败,Err:" + err.Error()
  591. return
  592. }
  593. companyItem, err := models.GetSellerDetailAllByCompanyId(user.CompanyId)
  594. if err != nil {
  595. br.Msg = "获取信息失败"
  596. br.ErrMsg = "获取所属销售信息失败,Err:" + err.Error()
  597. return
  598. }
  599. var mobile string
  600. if utils.RunMode == "release" {
  601. //mobile = utils.WxMsgTemplateIdAskMsgMobileAll + "," + companyItem.Mobile
  602. mobile = utils.WxMsgTemplateIdAskMsgMobileAll
  603. } else {
  604. mobile = utils.WxMsgTemplateIdAskMsgMobile
  605. }
  606. openIdList, err := models.GetWxOpenIdByMobileList(mobile)
  607. if err != nil {
  608. br.Msg = "提交失败"
  609. br.ErrMsg = "提交失败,Err:" + err.Error()
  610. return
  611. }
  612. detail, err := models.GetArticleDetailById(articleId)
  613. if err != nil {
  614. br.Msg = "获取信息失败"
  615. br.ErrMsg = "获取信息失败,Err:" + err.Error()
  616. return
  617. }
  618. companyName := user.CompanyName + "-" + user.RealName + "(" + companyItem.SellerName + ")"
  619. go services.SendWxMsgWithAsk(companyName, time.Now().Format(utils.FormatDateTime), content, detail.Title, openIdList, req.ArticleId)
  620. br.Ret = 200
  621. br.Success = true
  622. br.Msg = "提交成功"
  623. }
  624. // @Title 下载PDF打水印
  625. // @Description 下载PDF打水印接口
  626. // @Param ArticleId query int true "报告ID"
  627. // @Success 200 {object} models.ArticleDetailFileLink
  628. // @router /pdfwatermark [get]
  629. func (this *ArticleController) Pdfwatermark() {
  630. br := new(models.BaseResponse).Init()
  631. defer func() {
  632. this.Data["json"] = br
  633. this.ServeJSON()
  634. }()
  635. user := this.User
  636. if user == nil {
  637. br.Msg = "请登录"
  638. br.ErrMsg = "请登录,用户信息为空"
  639. br.Ret = 408
  640. return
  641. }
  642. uid := user.UserId
  643. articleId, err := this.GetInt("ArticleId")
  644. if articleId <= 0 {
  645. br.Msg = "文章不存在"
  646. br.ErrMsg = "文章不存在,文章ID错误"
  647. return
  648. }
  649. //缓存校验
  650. cacheKey := fmt.Sprint("xygx:apply_record:add:", uid, "ArticleId_", articleId)
  651. ttlTime := utils.Rc.GetRedisTTL(cacheKey)
  652. if ttlTime > 0 && user.CompanyId != 16 {
  653. br.Msg = "下载失败,下载过于频繁"
  654. br.ErrMsg = "下载失败,下载过于频繁:mobile" + user.Mobile
  655. return
  656. }
  657. resp := new(models.ArticleDetailFileLink)
  658. detail := new(models.ArticleDetail)
  659. detail, err = models.GetArticleDetailById(articleId)
  660. if err != nil {
  661. br.Msg = "获取信息失败"
  662. br.ErrMsg = "获取信息失败,Err:" + err.Error()
  663. return
  664. }
  665. fileLink := detail.FileLink
  666. if fileLink == "" {
  667. br.Msg = "下载失败"
  668. br.ErrMsg = "下载失败,报告链接不存在"
  669. return
  670. }
  671. mobile := user.Mobile
  672. if mobile == "" {
  673. mobile = user.Email
  674. }
  675. sliceLink := strings.Split(fileLink, "/")
  676. uploadDir := "static/pdf/"
  677. //判断文件夹是否存在,不存在则创建
  678. if !utils.FileIsExist(uploadDir) {
  679. err = os.MkdirAll(uploadDir, 0755)
  680. if err != nil {
  681. br.Msg = "下载失败"
  682. br.ErrMsg = "存储目录创建失败,Err:" + err.Error()
  683. return
  684. }
  685. }
  686. var oldFile string
  687. var newFile string
  688. //获取PDF源文件名称
  689. pdfName := sliceLink[len(sliceLink)-1]
  690. pdfName = utils.MD5(pdfName) + ".pdf"
  691. oldFile = uploadDir + pdfName
  692. //判断PDF本地是否存在,不存在则保存到本地
  693. if !utils.FileIsExist(oldFile) {
  694. res, err := nhttp.Get(fileLink)
  695. if err != nil {
  696. br.Msg = "下载失败"
  697. br.ErrMsg = "获取源文件失败,Err:" + err.Error()
  698. return
  699. }
  700. defer res.Body.Close()
  701. // 获得get请求响应的reader对象
  702. reader := bufio.NewReaderSize(res.Body, 32*1024)
  703. file, err := os.Create(oldFile)
  704. if err != nil {
  705. br.Msg = "下载失败"
  706. br.ErrMsg = "保存源文件到本地失败,Err:" + err.Error()
  707. return
  708. }
  709. defer file.Close()
  710. //获得文件的writer对象
  711. writer := bufio.NewWriter(file)
  712. written, _ := io.Copy(writer, reader)
  713. fmt.Printf("Total length: %d", written)
  714. }
  715. newFile = uploadDir + "new_" + pdfName
  716. onTop := true
  717. wm, err := pdfcpu.ParseTextWatermarkDetails(mobile, " op:.4, pos:c ,points:16 ", onTop, 1)
  718. if err != nil {
  719. br.Msg = "下载失败"
  720. br.ErrMsg = "生成水印文件失败,Err:" + err.Error()
  721. return
  722. }
  723. err = api.AddWatermarksFile(oldFile, newFile, nil, wm, nil)
  724. if err != nil {
  725. //br.Msg = "下载失败"
  726. //br.ErrMsg = "生成水印PDF失败,Err:" + err.Error()
  727. resp.FileLink = fileLink
  728. br.Ret = 200
  729. br.Success = true
  730. br.Msg = "获取成功"
  731. br.Data = resp
  732. return
  733. }
  734. randStr := utils.GetRandStringNoSpecialChar(28)
  735. fileName := randStr + ".pdf"
  736. savePath := uploadDir + time.Now().Format("200601/20060102/")
  737. savePath += fileName
  738. //上传到阿里云
  739. err = services.UploadFileToAliyun(fileName, newFile, savePath)
  740. if err != nil {
  741. br.Msg = "下载失败"
  742. br.ErrMsg = "文件上传失败,Err:" + err.Error()
  743. return
  744. }
  745. fileHost := "https://hzstatic.hzinsights.com/"
  746. resourceUrl := fileHost + savePath
  747. defer func() {
  748. os.Remove(newFile)
  749. }()
  750. utils.Rc.SetNX(cacheKey, user.Mobile, time.Minute*5)
  751. resp.FileLink = resourceUrl
  752. br.Ret = 200
  753. br.Success = true
  754. br.Msg = "获取成功"
  755. br.Data = resp
  756. }
  757. // @Title 约访专家
  758. // @Description 约访专家接口
  759. // @Param request body models.CygxArticleIdReq true "type json string"
  760. // @Success 200 {object}
  761. // @router /applyAppointmentExpert [post]
  762. func (this *ArticleController) ApplyAppointmentExpert() {
  763. br := new(models.BaseResponse).Init()
  764. defer func() {
  765. this.Data["json"] = br
  766. this.ServeJSON()
  767. }()
  768. user := this.User
  769. if user == nil {
  770. br.Msg = "请登录"
  771. br.ErrMsg = "请登录,用户信息为空"
  772. br.Ret = 408
  773. return
  774. }
  775. fmt.Println(user)
  776. var req models.CygxArticleIdReq
  777. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  778. if err != nil {
  779. br.Msg = "参数解析异常!"
  780. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  781. return
  782. }
  783. articleId := req.ArticleId
  784. var condition string
  785. var pars []interface{}
  786. condition += ` AND article_id =? AND user_id = ? `
  787. pars = append(pars, articleId, user.UserId)
  788. total, err := models.GetCygxArticleApplyAppointmentExpertCount(condition, pars)
  789. if err != nil {
  790. br.Msg = "约访专家失败"
  791. br.ErrMsg = "约访专家失败,Err:" + err.Error()
  792. return
  793. }
  794. if total > 0 {
  795. br.Msg = "您已提交申请,请勿重复提交。"
  796. return
  797. }
  798. err = services.AddArticleApplyAppointmentExpert(user, articleId)
  799. if err != nil {
  800. br.Msg = "约访专家失败"
  801. br.ErrMsg = "约访专家失败,Err:" + err.Error()
  802. return
  803. }
  804. services.SendArticleApplyAppointmentExpertTemplateMsg(user, articleId)
  805. br.Ret = 200
  806. br.Success = true
  807. br.Msg = "操作成功"
  808. }