article.go 25 KB

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