article.go 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845
  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. // @Param InviteShareCode query string false "销售账号邀请码"
  37. // @Success 200 {object} models.ArticleDetailResp
  38. // @router /detail [get]
  39. func (this *ArticleController) Detail() {
  40. br := new(models.BaseResponse).Init()
  41. defer func() {
  42. this.Data["json"] = br
  43. this.ServeJSON()
  44. }()
  45. user := this.User
  46. if user == nil {
  47. br.Msg = "请登录"
  48. br.ErrMsg = "请登录,用户信息为空"
  49. br.Ret = 408
  50. return
  51. }
  52. uid := user.UserId
  53. articleId, err := this.GetInt("ArticleId")
  54. isSendWx, _ := this.GetInt("IsSendWx")
  55. if articleId <= 0 {
  56. br.Msg = "文章不存在"
  57. br.ErrMsg = "文章不存在,文章ID错误"
  58. return
  59. }
  60. inviteShareCode := this.GetString("InviteShareCode")
  61. detail := new(models.ArticleDetail)
  62. hasPermission := 0
  63. var haveResearch bool
  64. resp := new(models.ArticleDetailResp)
  65. detail, err = models.GetArticleDetailById(articleId)
  66. if err != nil {
  67. br.Msg = "获取信息失败"
  68. br.ErrMsg = "获取信息失败,Err:" + err.Error()
  69. return
  70. }
  71. //记录分享来源
  72. if inviteShareCode != "" {
  73. go services.AddCygxUserAdminShareHistory(user, utils.CYGX_OBJ_ARTICLE, detail.Title, inviteShareCode, articleId)
  74. }
  75. detail.PublishDate = utils.TimeRemoveHms2(detail.PublishDate)
  76. detail.Body = html.UnescapeString(detail.Body)
  77. 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)
  78. detail.Body = strings.Replace(detail.Body, "pre", "div", -1)
  79. detail.Abstract = html.UnescapeString(detail.Abstract)
  80. //detail.Abstract, _ = services.GetReportContentTextSubNew(detail.Abstract)
  81. //作者头像
  82. if detail.DepartmentId > 0 {
  83. departmentDetail, err := models.GetArticleDepartmentDateilById(detail.DepartmentId)
  84. if err == nil {
  85. detail.DepartmentImgUrl = departmentDetail.ImgUrl
  86. }
  87. }
  88. // 判断是否属于研选类型的报告
  89. detail.IsResearch = true
  90. articleCollectMap, _ := services.GetCygxArticleCollectMap(user.UserId)
  91. detail.IsCollect = articleCollectMap[detail.ArticleId]
  92. //是否是通过模板消息进来的
  93. if isSendWx == 1 {
  94. var condition string
  95. var pars []interface{}
  96. pars = make([]interface{}, 0)
  97. condition = ` AND article_id = ? `
  98. reportMappingMap, _ := services.GetReportMappingMap()
  99. if reportMappingMap[detail.CategoryId] {
  100. chooseCategoryMap, _ := services.GetChooseCategoryMap(user)
  101. detail.IsShowFollowButton = true
  102. detail.IsFollowButton = chooseCategoryMap[detail.CategoryId]
  103. } else {
  104. pars = append(pars, articleId)
  105. industrialList, err := models.GetIndustrialArticleGroupManagementList(condition, pars)
  106. if err != nil && err.Error() != utils.ErrNoRow() {
  107. br.Msg = "获取信息失败"
  108. br.ErrMsg = "获取信息失败,Err:" + err.Error()
  109. return
  110. }
  111. if len(industrialList) > 0 {
  112. industryUserFollowMap, err := services.GetIndustryUserFollowMap(user)
  113. if err != nil {
  114. br.Msg = "获取信息失败"
  115. br.ErrMsg = "GetActivitySignupResp,Err:" + err.Error()
  116. return
  117. }
  118. for _, v := range industrialList {
  119. if industryUserFollowMap[v.IndustrialManagementId] {
  120. detail.IsFollowButton = true
  121. }
  122. }
  123. detail.IsShowFollowButton = true
  124. }
  125. }
  126. }
  127. havePower, err := services.GetArticleDetailUserPower(user)
  128. if err != nil {
  129. br.Msg = "获取信息失败"
  130. br.ErrMsg = "校验用户权限失败,Err:" + err.Error()
  131. return
  132. }
  133. if havePower {
  134. hasPermission = 1
  135. go services.ArticleHistory(articleId, user)
  136. //30分钟之内阅读同一篇文章不错二次推送
  137. key := "CYGX_ARTICLE_READ" + strconv.Itoa(articleId) + "_" + strconv.Itoa(uid)
  138. if !utils.Rc.IsExist(key) {
  139. go services.ArticleUserRemind(user, detail, 1)
  140. // 互动提醒
  141. go services.SendWxCategoryMsgInteractive(user, "阅读报告", articleId, detail.Title)
  142. utils.Rc.Put(key, 1, 30*time.Second)
  143. }
  144. interviewApplyItem, err := models.GetArticleInterviewApply(uid, articleId)
  145. if err != nil && err.Error() != utils.ErrNoRow() {
  146. br.Msg = "获取信息失败"
  147. br.ErrMsg = "判断是否已申请访谈失败,Err:" + strconv.Itoa(uid) + ";articleId" + strconv.Itoa(articleId)
  148. return
  149. }
  150. if interviewApplyItem != nil && interviewApplyItem.InterviewApplyId > 0 {
  151. detail.IsInterviewApply = true
  152. detail.InterviewApplyStatus = interviewApplyItem.Status
  153. }
  154. //获取销售手机号
  155. sellerItem, err := models.GetSellerByCompanyId(user.CompanyId)
  156. if err != nil && err.Error() != utils.ErrNoRow() {
  157. br.Msg = "获取信息失败"
  158. br.ErrMsg = "获取销售数据失败2,Err:" + err.Error() + strconv.Itoa(uid) + ";articleId" + strconv.Itoa(articleId)
  159. return
  160. }
  161. if sellerItem != nil {
  162. detail.SellerMobile = sellerItem.Mobile
  163. detail.SellerName = sellerItem.RealName
  164. }
  165. sellerList, err := models.GetSellerList(articleId)
  166. if err != nil {
  167. br.Msg = "获取信息失败"
  168. br.ErrMsg = "获取销售数据失败,Err:" + err.Error() + strconv.Itoa(uid) + ";articleId" + strconv.Itoa(articleId)
  169. return
  170. }
  171. if detail.ArticleId >= utils.SummaryArticleId && strings.Contains(detail.SellerAndMobile, "-") {
  172. strnum := strings.Index(detail.SellerAndMobile, "-")
  173. detail.SellerAndMobile = detail.SellerAndMobile[0:strnum]
  174. if strnum > 0 {
  175. nickName := detail.SellerAndMobile[0:strnum]
  176. sellerAndMobile := &models.SellerRep{
  177. SellerMobile: "",
  178. SellerName: nickName,
  179. }
  180. sellerList = append(sellerList, sellerAndMobile)
  181. }
  182. }
  183. articleFollowdetail, err := models.GetArticleFollowDetail(articleId, uid)
  184. if err != nil {
  185. br.Msg = "获取信息失败"
  186. br.ErrMsg = "获取关注信息失败,Err:" + err.Error()
  187. return
  188. }
  189. detail.FollowNum = articleFollowdetail.DNum
  190. detail.CollectionNum = articleFollowdetail.AcNum
  191. if articleFollowdetail.MdNum > 0 {
  192. detail.IsFollow = true
  193. }
  194. if detail.IsSummary == 1 {
  195. detail.IsBelongSummary = true
  196. }
  197. if detail.IsReport == 1 {
  198. detail.IsBelongReport = true
  199. }
  200. } else {
  201. hasPermission, err = services.GetUserDetailPermissionCode(user.UserId, user.CompanyId)
  202. if err != nil && err.Error() != utils.ErrNoRow() {
  203. br.Msg = "获取信息失败"
  204. br.ErrMsg = "获取信息失败,GetUserDetailPermissionCode Err:" + err.Error()
  205. return
  206. }
  207. //权益客户对权益销售信息进行展示
  208. if hasPermission == 2 || hasPermission == 3 {
  209. sellerItemQy, err := models.GetSellerByCompanyIdCheckFicc(user.CompanyId, 2)
  210. if err != nil && err.Error() != utils.ErrNoRow() {
  211. br.Msg = "获取信息失败"
  212. br.ErrMsg = "获取销售信息失败,Err:" + err.Error()
  213. return
  214. }
  215. if sellerItemQy != nil {
  216. detail.SellerMobile = sellerItemQy.Mobile
  217. detail.SellerName = sellerItemQy.RealName
  218. }
  219. }
  220. detail.Body = ""
  221. detail.BodyText = ""
  222. resp.IsShowWxPay = utils.IS_SHOW_WX_PAY //是否展示微信支付按钮
  223. resp.IsCompanyApply = services.GetUserApplyRecordCountByCompanyIdPay(user.CompanyId) //获取客户是否有过历史申请记录
  224. resp.IsNeedBusinessCard = services.GetCygxUserBusinessCardCount(user.UserId, user.CompanyId) //是否需要上传名片
  225. resp.GoodsList = services.GetUserGoodsCardList() //日卡月卡商品信息
  226. for _, v := range resp.GoodsList {
  227. resp.PopupPriceMsg += v.PopupPriceMsg //价格弹窗信息
  228. }
  229. }
  230. if hasPermission != 1 {
  231. hasPermission, err = services.GetUserPermissionCode(user.UserId, user.CompanyId)
  232. if err != nil {
  233. br.Msg = "获取信息失败"
  234. br.ErrMsg = "获取用户状态信息失败,Err:" + err.Error()
  235. return
  236. }
  237. }
  238. if detail.ArticleTypeId == 14 {
  239. detail.IsApplyAppointmentExpert = true //判断文章类型是否属于专家访谈 查研观向11.0
  240. }
  241. if user.UserId == 0 {
  242. hasPermission = 1
  243. }
  244. resp.HasPermission = hasPermission
  245. resp.HaveResearch = haveResearch
  246. resp.Detail = detail
  247. if user.Mobile != "" {
  248. resp.Mobile = user.Mobile
  249. } else {
  250. resp.Mobile = user.Email
  251. }
  252. br.Ret = 200
  253. br.Success = true
  254. br.Msg = "获取成功"
  255. br.Data = resp
  256. }
  257. // @Title 收藏
  258. // @Description 收藏
  259. // @Param request body models.ArticleCollectReq true "type json string"
  260. // @Success 200 {object} models.FontsCollectResp
  261. // @router /collect [post]
  262. func (this *ArticleController) ArticleCollect() {
  263. br := new(models.BaseResponse).Init()
  264. defer func() {
  265. this.Data["json"] = br
  266. this.ServeJSON()
  267. }()
  268. user := this.User
  269. if user == nil {
  270. br.Msg = "请重新登录"
  271. br.Ret = 408
  272. return
  273. }
  274. uid := user.UserId
  275. var req models.ArticleCollectReq
  276. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  277. if err != nil {
  278. br.Msg = "参数解析异常!"
  279. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  280. return
  281. }
  282. articleId := req.ArticleId
  283. detail, err := models.GetArticleDetailById(articleId)
  284. if err != nil {
  285. br.Msg = "获取信息失败"
  286. br.ErrMsg = "获取信息失败,Err:" + err.Error()
  287. return
  288. }
  289. count, err := models.GetArticleCollectCount(uid, articleId)
  290. if err != nil {
  291. br.Msg = "获取数据失败!"
  292. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  293. return
  294. }
  295. resp := new(models.ArticleCollectResp)
  296. if count <= 0 {
  297. item := new(models.CygxArticleCollect)
  298. item.ArticleId = req.ArticleId
  299. item.UserId = uid
  300. item.CreateTime = time.Now()
  301. item.Mobile = user.Mobile
  302. item.Email = user.Email
  303. item.CompanyId = user.CompanyId
  304. item.CompanyName = user.CompanyName
  305. item.RealName = user.RealName
  306. _, err = models.AddCygxArticleCollect(item)
  307. if err != nil {
  308. br.Msg = "收藏失败"
  309. br.ErrMsg = "收藏失败,Err:" + err.Error()
  310. return
  311. }
  312. br.Msg = "收藏成功"
  313. resp.Status = 1
  314. // 文章收藏消息发送
  315. //go services.ArticleUserRemind(user, detail, 2)
  316. go services.ArticleHistoryUserLabelLogAdd(articleId, user.UserId)
  317. // 互动提醒
  318. go services.SendWxCategoryMsgInteractive(user, "收藏报告", articleId, detail.Title)
  319. } else {
  320. err = models.RemoveArticleCollect(uid, articleId)
  321. if err != nil {
  322. br.Msg = "取消收藏失败"
  323. br.ErrMsg = "取消收藏失败,Err:" + err.Error()
  324. return
  325. }
  326. br.Msg = "已取消收藏"
  327. resp.Status = 2
  328. }
  329. collectTotal, err := models.GetArticleCollectUsersCount(articleId)
  330. if err != nil {
  331. br.Msg = "获取数据失败"
  332. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  333. return
  334. }
  335. resp.CollectCount = collectTotal
  336. br.Ret = 200
  337. br.Success = true
  338. br.Data = resp
  339. }
  340. // @Title 访谈申请
  341. // @Description 访谈申请
  342. // @Param request body models.ArticleInterviewApplyReq true "type json string"
  343. // @Success 200 {object} models.FontsCollectResp
  344. // @router /interview/apply [post]
  345. func (this *ArticleController) InterviewApply() {
  346. br := new(models.BaseResponse).Init()
  347. defer func() {
  348. this.Data["json"] = br
  349. this.ServeJSON()
  350. }()
  351. user := this.User
  352. if user == nil {
  353. br.Msg = "请重新登录"
  354. br.Ret = 408
  355. return
  356. }
  357. uid := user.UserId
  358. var req models.ArticleInterviewApplyReq
  359. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  360. if err != nil {
  361. br.Msg = "参数解析异常!"
  362. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  363. return
  364. }
  365. article, err := models.GetArticleDetailById(req.ArticleId)
  366. if err != nil {
  367. br.Msg = "获取纪要失败!"
  368. br.ErrMsg = "获取纪要失败,Err:" + err.Error()
  369. return
  370. }
  371. count, err := models.GetArticleInterviewApplyCount(uid, req.ArticleId)
  372. if err != nil {
  373. br.Msg = "获取数据失败!"
  374. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  375. return
  376. }
  377. resp := new(models.ArticleInterviewApplyResp)
  378. if count <= 0 {
  379. item := new(models.CygxInterviewApply)
  380. item.ArticleId = req.ArticleId
  381. item.UserId = uid
  382. item.CompanyId = user.CompanyId
  383. item.Status = "待邀请"
  384. item.Sort = 1
  385. item.ArticleTitle = article.Title
  386. item.CreateTime = time.Now()
  387. item.ModifyTime = time.Now()
  388. item.ArticleIdMd5 = article.ArticleIdMd5
  389. _, err = models.AddCygxInterviewApply(item)
  390. if err != nil {
  391. br.Msg = "申请失败"
  392. br.ErrMsg = "申请失败,Err:" + err.Error()
  393. return
  394. }
  395. br.Msg = "申请成功"
  396. resp.Status = 1
  397. //发送模板消息
  398. if user.CompanyId > 1 {
  399. mobile := user.Mobile
  400. if mobile == "" {
  401. mobile = user.Email
  402. }
  403. sellerItem, _ := models.GetSellerByCompanyId(user.CompanyId)
  404. if sellerItem != nil && sellerItem.AdminId > 0 && user.Mobile != "" {
  405. openIpItem, _ := models.GetUserRecordByMobile(4, sellerItem.Mobile)
  406. if openIpItem != nil && openIpItem.OpenId != "" {
  407. go services.SendInterviewApplyTemplateMsg(user.RealName, sellerItem.CompanyName, mobile, article.Title, openIpItem)
  408. }
  409. }
  410. }
  411. } else {
  412. err = models.RemoveArticleInterviewApply(uid, req.ArticleId)
  413. if err != nil {
  414. br.Msg = "取消申请失败"
  415. br.ErrMsg = "取消申请失败,Err:" + err.Error()
  416. return
  417. }
  418. br.Msg = "已取消申请"
  419. resp.Status = 2
  420. if user.CompanyId > 1 {
  421. mobile := user.Mobile
  422. if mobile == "" {
  423. mobile = user.Email
  424. }
  425. sellerItem, _ := models.GetSellerByCompanyId(user.CompanyId)
  426. if sellerItem != nil && sellerItem.AdminId > 0 && user.Mobile != "" {
  427. openIpItem, _ := models.GetUserRecordByMobile(4, sellerItem.Mobile)
  428. if openIpItem != nil && openIpItem.OpenId != "" {
  429. go services.SendInterviewApplyCancelTemplateMsg(user.RealName, sellerItem.CompanyName, mobile, article.Title, openIpItem)
  430. }
  431. }
  432. }
  433. }
  434. br.Ret = 200
  435. br.Success = true
  436. br.Data = resp
  437. }
  438. // @Title 上传文章阅读时间
  439. // @Description 上传文章阅读时间接口
  440. // @Param request body models.AddStopTimeRep true "type json string"
  441. // @Success 200 {object} models.ArticleDetailResp
  442. // @router /addStopTime [post]
  443. func (this *ArticleController) AddStopTime() {
  444. br := new(models.BaseResponse).Init()
  445. defer func() {
  446. this.Data["json"] = br
  447. this.ServeJSON()
  448. }()
  449. user := this.User
  450. if user == nil {
  451. br.Msg = "请登录"
  452. br.ErrMsg = "请登录,用户信息为空"
  453. br.Ret = 408
  454. return
  455. }
  456. var req models.AddStopTimeRep
  457. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  458. if err != nil {
  459. br.Msg = "参数解析异常!"
  460. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  461. return
  462. }
  463. //uid := user.UserId
  464. articleId := req.ArticleId
  465. stopTime := req.StopTime
  466. outType := req.OutType
  467. source := req.Source
  468. if articleId <= 0 {
  469. br.Msg = "参数错误"
  470. br.ErrMsg = "参数错误"
  471. return
  472. }
  473. if stopTime == 0 {
  474. stopTime = 1
  475. }
  476. if outType != 2 {
  477. outType = 1
  478. }
  479. if source != "PC" {
  480. source = "MOBILE"
  481. }
  482. hasPermission := 0
  483. hasFree := 0
  484. havePower, err := services.GetArticleDetailUserPower(user)
  485. if err != nil {
  486. br.Msg = "获取信息失败"
  487. br.ErrMsg = "校验用户权限失败,Err:" + err.Error()
  488. return
  489. }
  490. if havePower {
  491. hasPermission = 1
  492. go services.ArticleHistoryStopTime(articleId, stopTime, outType, user)
  493. } else {
  494. hasPermission, err = services.GetUserDetailPermissionCode(user.UserId, user.CompanyId)
  495. if err != nil && err.Error() != utils.ErrNoRow() {
  496. br.Msg = "获取信息失败"
  497. br.ErrMsg = "获取信息失败,GetUserDetailPermissionCode Err:" + err.Error()
  498. return
  499. }
  500. }
  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. } else {
  602. mobile = utils.WxMsgTemplateIdAskMsgMobile + "," + companyItem.Mobile
  603. }
  604. openIdList, err := models.GetWxOpenIdByMobileList(mobile)
  605. if err != nil {
  606. br.Msg = "提交失败"
  607. br.ErrMsg = "提交失败,Err:" + err.Error()
  608. return
  609. }
  610. detail, err := models.GetArticleDetailById(articleId)
  611. if err != nil {
  612. br.Msg = "获取信息失败"
  613. br.ErrMsg = "获取信息失败,Err:" + err.Error()
  614. return
  615. }
  616. companyName := user.CompanyName + "-" + user.RealName + "(" + companyItem.SellerName + ")"
  617. go services.SendWxMsgWithAsk(companyName, time.Now().Format(utils.FormatDateTime), content, detail.Title, openIdList, req.ArticleId)
  618. //go services.SendWxCategoryMsgWithAsk(user.CompanyName, user.RealName, companyItem.SellerName, time.Now().Format(utils.FormatDateTimeMinute2), content, 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. }