article.go 25 KB

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