article.go 24 KB

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