article.go 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950
  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_cygx/models"
  15. "hongze/hongze_cygx/services"
  16. "hongze/hongze_cygx/utils"
  17. "html"
  18. nhttp "net/http"
  19. //"os"
  20. "regexp"
  21. "strconv"
  22. "strings"
  23. "time"
  24. )
  25. type ArticleController struct {
  26. BaseAuthController
  27. }
  28. type ArticleCommonController struct {
  29. BaseCommonController
  30. }
  31. // @Title 获取报告详情
  32. // @Description 获取报告详情接口
  33. // @Param ArticleId query int true "报告ID"
  34. // @Success 200 {object} models.ArticleDetailResp
  35. // @router /detail [get]
  36. func (this *ArticleController) Detail() {
  37. br := new(models.BaseResponse).Init()
  38. defer func() {
  39. this.Data["json"] = br
  40. this.ServeJSON()
  41. }()
  42. user := this.User
  43. if user == nil {
  44. br.Msg = "请登录"
  45. br.ErrMsg = "请登录,用户信息为空"
  46. br.Ret = 408
  47. return
  48. }
  49. uid := user.UserId
  50. articleId, err := this.GetInt("ArticleId")
  51. if articleId <= 0 {
  52. br.Msg = "文章不存在"
  53. br.ErrMsg = "文章不存在,文章ID错误"
  54. return
  55. }
  56. detail := new(models.ArticleDetail)
  57. hasPermission := 0
  58. hasFree := 0
  59. var haveResearch bool
  60. //判断是否已经申请过
  61. applyCount, err := models.GetApplyRecordCount(uid)
  62. if err != nil && err.Error() != utils.ErrNoRow() {
  63. br.Msg = "获取信息失败"
  64. br.ErrMsg = "判断是否已申请过试用失败,Err:" + err.Error()
  65. return
  66. }
  67. //`description:"1:有该行业权限,正常展示,2:无该行业权限,不存在权益客户下,3:无该品类权限,4:潜在客户,未提交过申请,5:潜在客户,已提交过申请"`
  68. if user.CompanyId > 1 {
  69. companyPermission, err := models.GetCompanyPermission(user.CompanyId)
  70. if err != nil {
  71. br.Msg = "获取信息失败"
  72. br.ErrMsg = "判断是否已申请访谈失败,Err:" + strconv.Itoa(uid) + ";articleId" + strconv.Itoa(articleId)
  73. return
  74. }
  75. detail, err = models.GetArticleDetailById(articleId)
  76. if err != nil {
  77. br.Msg = "获取信息失败"
  78. br.ErrMsg = "获取信息失败,Err:" + err.Error()
  79. return
  80. }
  81. detail.Body = html.UnescapeString(detail.Body)
  82. 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)
  83. detail.Body = strings.Replace(detail.Body, "pre", "div", -1)
  84. //detail.Abstract = html.UnescapeString(detail.Abstract)
  85. detail.Abstract, _ = services.GetReportContentTextSub(detail.Abstract)
  86. if companyPermission == "" {
  87. if applyCount > 0 {
  88. hasPermission = 5
  89. } else {
  90. hasPermission = 2
  91. }
  92. hasFree = 2
  93. goto Loop
  94. } else {
  95. hasFree = 1
  96. var articlePermissionPermissionName string
  97. if detail.CategoryId > 0 {
  98. articlePermission, err := models.GetArticlePermission(detail.CategoryId)
  99. if err != nil {
  100. br.Msg = "获取信息失败"
  101. br.ErrMsg = "获取报告权限失败,Err:" + err.Error() + strconv.Itoa(uid) + ";articleId" + strconv.Itoa(articleId)
  102. return
  103. }
  104. if articlePermission == nil {
  105. br.Msg = "获取信息失败"
  106. br.ErrMsg = "报告权限不存在,Err:" + strconv.Itoa(uid) + ";articleId" + strconv.Itoa(articleId)
  107. return
  108. }
  109. articlePermissionPermissionName = articlePermission.PermissionName
  110. } else {
  111. articlePermissionPermissionName = detail.CategoryName
  112. }
  113. var hasPersion bool
  114. slice := strings.Split(articlePermissionPermissionName, ",")
  115. for _, v := range slice {
  116. if strings.Contains(companyPermission, v) {
  117. hasPersion = true
  118. }
  119. }
  120. if strings.Contains(detail.CategoryName, "研选") {
  121. detail.IsResearch = true
  122. }
  123. userType, _, err := services.GetUserType(user.CompanyId)
  124. if err != nil {
  125. br.Msg = "获取信息失败"
  126. br.ErrMsg = "获取用户信息失败,Err:" + err.Error()
  127. return
  128. }
  129. if userType == 1 && strings.Contains(detail.CategoryName, "研选") {
  130. hasPersion = false
  131. }
  132. if detail.IsReport == 1 {
  133. fmt.Println("报告权限")
  134. detailCategory, err := models.GetdetailByCategoryIdSando(detail.CategoryId)
  135. if err != nil && err.Error() != utils.ErrNoRow() {
  136. br.Msg = "获取信息失败"
  137. br.ErrMsg = "获取信息失败,Err:" + err.Error() + "categoryID 不存在:" + strconv.Itoa(detail.CategoryId)
  138. return
  139. }
  140. fmt.Println(detailCategory)
  141. permissionStr, err := models.GetCompanyPermissionByUser(user.CompanyId)
  142. if err != nil {
  143. br.Msg = "获取信息失败"
  144. br.ErrMsg = "获取客户信息失败,Err:" + err.Error()
  145. return
  146. }
  147. if detailCategory != nil {
  148. if detailCategory.PermissionType == 1 {
  149. if !strings.Contains(permissionStr, detailCategory.ChartPermissionName+"(主观)") {
  150. hasPersion = false
  151. }
  152. } else if detailCategory.PermissionType == 2 {
  153. if !strings.Contains(permissionStr, detailCategory.ChartPermissionName+"(客观)") {
  154. hasPersion = false
  155. }
  156. }
  157. }
  158. }
  159. if hasPersion {
  160. hasPermission = 1
  161. historyRecord := new(models.CygxArticleHistoryRecord)
  162. historyRecord.UserId = uid
  163. historyRecord.ArticleId = articleId
  164. historyRecord.CreateTime = time.Now()
  165. historyRecord.Mobile = user.Mobile
  166. historyRecord.Email = user.Email
  167. historyRecord.CompanyId = user.CompanyId
  168. historyRecord.CompanyName = user.CompanyName
  169. recordCount, _ := models.GetNoAddStoptimeArticleCount(uid, articleId)
  170. if recordCount == 0 {
  171. go models.AddCygxArticleHistoryRecord(historyRecord)
  172. } else {
  173. detailNew, err := models.GetNewArticleHistoryRecord(uid, articleId)
  174. if err != nil {
  175. br.Msg = "获取信息失败"
  176. br.ErrMsg = "获取信息失败,Err:" + err.Error()
  177. return
  178. }
  179. if detailNew.StopTime > 0 {
  180. go models.AddCygxArticleHistoryRecord(historyRecord)
  181. }
  182. }
  183. } else { //无该行业权限
  184. companyDetail, err := models.GetCompanyDetailById(user.CompanyId)
  185. if err == nil && companyDetail.ProductId == 1 {
  186. hasPermission = 4
  187. } else {
  188. fmt.Println("这里", hasPermission)
  189. hasPermission = 3
  190. }
  191. }
  192. if hasPermission == 1 {
  193. key := "CYGX_ARTICLE_" + strconv.Itoa(articleId) + "_" + strconv.Itoa(uid)
  194. if !utils.Rc.IsExist(key) {
  195. //新增浏览记录
  196. record := new(models.CygxArticleViewRecord)
  197. record.UserId = uid
  198. record.ArticleId = articleId
  199. record.CreateTime = time.Now()
  200. record.Mobile = user.Mobile
  201. record.Email = user.Email
  202. record.CompanyId = user.CompanyId
  203. record.CompanyName = user.CompanyName
  204. go models.AddCygxArticleViewRecord(record)
  205. utils.Rc.Put(key, 1, 5*time.Second)
  206. models.ModifyReportLastViewTime(uid)
  207. }
  208. }
  209. }
  210. collectCount, err := models.GetArticleCollectCount(uid, articleId)
  211. if err != nil && err.Error() != utils.ErrNoRow() {
  212. br.Msg = "获取信息失败"
  213. br.ErrMsg = "判断是否已收藏失败,Err:" + strconv.Itoa(uid) + ";articleId" + strconv.Itoa(articleId)
  214. return
  215. }
  216. if collectCount > 0 {
  217. detail.IsCollect = true
  218. }
  219. interviewApplyItem, err := models.GetArticleInterviewApply(uid, articleId)
  220. if err != nil && err.Error() != utils.ErrNoRow() {
  221. br.Msg = "获取信息失败"
  222. br.ErrMsg = "判断是否已申请访谈失败,Err:" + strconv.Itoa(uid) + ";articleId" + strconv.Itoa(articleId)
  223. return
  224. }
  225. if interviewApplyItem != nil && interviewApplyItem.InterviewApplyId > 0 {
  226. detail.IsInterviewApply = true
  227. detail.InterviewApplyStatus = interviewApplyItem.Status
  228. }
  229. //获取销售手机号
  230. sellerItem, err := models.GetSellerByCompanyId(user.CompanyId)
  231. if err != nil {
  232. br.Msg = "获取信息失败"
  233. br.ErrMsg = "获取销售数据失败2,Err:" + strconv.Itoa(uid) + ";articleId" + strconv.Itoa(articleId)
  234. return
  235. }
  236. if sellerItem != nil {
  237. detail.SellerMobile = sellerItem.Mobile
  238. detail.SellerName = sellerItem.RealName
  239. }
  240. sellerList, err := models.GetSellerList(articleId)
  241. if err != nil {
  242. br.Msg = "获取信息失败"
  243. br.ErrMsg = "获取销售数据失败,Err:" + err.Error() + strconv.Itoa(uid) + ";articleId" + strconv.Itoa(articleId)
  244. return
  245. }
  246. if detail.ArticleId >= utils.SummaryArticleId && strings.Contains(detail.SellerAndMobile, "-") {
  247. strnum := strings.Index(detail.SellerAndMobile, "-")
  248. detail.SellerAndMobile = detail.SellerAndMobile[0:strnum]
  249. if strnum > 0 {
  250. nickName := detail.SellerAndMobile[0:strnum]
  251. sellerAndMobile := &models.SellerRep{
  252. SellerMobile: "",
  253. SellerName: nickName,
  254. }
  255. sellerList = append(sellerList, sellerAndMobile)
  256. }
  257. }
  258. detail.SellerList = sellerList
  259. } else { //潜在客户
  260. if applyCount > 0 {
  261. hasPermission = 5
  262. } else {
  263. hasPermission = 4
  264. }
  265. }
  266. Loop:
  267. if hasPermission != 1 {
  268. detail.Body = ""
  269. detail.BodyText = ""
  270. } else {
  271. articleFollowdetail, err := models.GetArticleFollowDetail(articleId, uid)
  272. if err != nil {
  273. br.Msg = "获取信息失败"
  274. br.ErrMsg = "获取关注信息失败,Err:" + err.Error()
  275. return
  276. }
  277. detail.FollowNum = articleFollowdetail.DNum
  278. detail.CollectionNum = articleFollowdetail.AcNum
  279. if articleFollowdetail.MdNum > 0 {
  280. detail.IsFollow = true
  281. }
  282. if detail.IsSummary == 1 {
  283. detail.IsBelongSummary = true
  284. }
  285. if detail.IsReport == 1 {
  286. detail.IsBelongReport = true
  287. }
  288. haveResearch = true
  289. }
  290. fmt.Println("hasPermission", hasPermission)
  291. if hasPermission == 2 || hasPermission == 4 {
  292. //获取销售手机号
  293. sellerItemQy, err := models.GetSellerByCompanyIdCheckFicc(user.CompanyId, 2)
  294. if err != nil && err.Error() != utils.ErrNoRow() {
  295. br.Msg = "获取信息失败"
  296. br.ErrMsg = "获取销售信息失败,Err:" + err.Error()
  297. return
  298. }
  299. if sellerItemQy != nil {
  300. hasPermission = 3
  301. detail.SellerMobile = sellerItemQy.Mobile
  302. detail.SellerName = sellerItemQy.RealName
  303. }
  304. }
  305. resp := new(models.ArticleDetailResp)
  306. resp.HasPermission = hasPermission
  307. resp.HaveResearch = haveResearch
  308. resp.HasFree = hasFree
  309. resp.Detail = detail
  310. if user.Mobile != "" {
  311. resp.Mobile = user.Mobile
  312. } else {
  313. resp.Mobile = user.Email
  314. }
  315. br.Ret = 200
  316. br.Success = true
  317. br.Msg = "获取成功"
  318. br.Data = resp
  319. }
  320. // @Title 收藏
  321. // @Description 收藏
  322. // @Param request body models.ArticleCollectReq true "type json string"
  323. // @Success 200 {object} models.FontsCollectResp
  324. // @router /collect [post]
  325. func (this *ArticleController) ArticleCollect() {
  326. br := new(models.BaseResponse).Init()
  327. defer func() {
  328. this.Data["json"] = br
  329. this.ServeJSON()
  330. }()
  331. user := this.User
  332. if user == nil {
  333. br.Msg = "请重新登录"
  334. br.Ret = 408
  335. return
  336. }
  337. uid := user.UserId
  338. var req models.ArticleCollectReq
  339. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  340. if err != nil {
  341. br.Msg = "参数解析异常!"
  342. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  343. return
  344. }
  345. count, err := models.GetArticleCollectCount(uid, req.ArticleId)
  346. if err != nil {
  347. br.Msg = "获取数据失败!"
  348. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  349. return
  350. }
  351. resp := new(models.ArticleCollectResp)
  352. if count <= 0 {
  353. item := new(models.CygxArticleCollect)
  354. item.ArticleId = req.ArticleId
  355. item.UserId = uid
  356. item.CreateTime = time.Now()
  357. _, err = models.AddCygxArticleCollect(item)
  358. if err != nil {
  359. br.Msg = "收藏失败"
  360. br.ErrMsg = "收藏失败,Err:" + err.Error()
  361. return
  362. }
  363. br.Msg = "收藏成功"
  364. resp.Status = 1
  365. } else {
  366. err = models.RemoveArticleCollect(uid, req.ArticleId)
  367. if err != nil {
  368. br.Msg = "取消收藏失败"
  369. br.ErrMsg = "取消收藏失败,Err:" + err.Error()
  370. return
  371. }
  372. br.Msg = "已取消收藏"
  373. resp.Status = 2
  374. }
  375. collectTotal, err := models.GetArticleCollectUsersCount(req.ArticleId)
  376. if err != nil {
  377. br.Msg = "获取数据失败"
  378. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  379. return
  380. }
  381. resp.CollectCount = collectTotal
  382. br.Ret = 200
  383. br.Success = true
  384. br.Data = resp
  385. }
  386. // @Title 访谈申请
  387. // @Description 访谈申请
  388. // @Param request body models.ArticleInterviewApplyReq true "type json string"
  389. // @Success 200 {object} models.FontsCollectResp
  390. // @router /interview/apply [post]
  391. func (this *ArticleController) InterviewApply() {
  392. br := new(models.BaseResponse).Init()
  393. defer func() {
  394. this.Data["json"] = br
  395. this.ServeJSON()
  396. }()
  397. user := this.User
  398. if user == nil {
  399. br.Msg = "请重新登录"
  400. br.Ret = 408
  401. return
  402. }
  403. uid := user.UserId
  404. var req models.ArticleInterviewApplyReq
  405. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  406. if err != nil {
  407. br.Msg = "参数解析异常!"
  408. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  409. return
  410. }
  411. article, err := models.GetArticleDetailById(req.ArticleId)
  412. if err != nil {
  413. br.Msg = "获取纪要失败!"
  414. br.ErrMsg = "获取纪要失败,Err:" + err.Error()
  415. return
  416. }
  417. count, err := models.GetArticleInterviewApplyCount(uid, req.ArticleId)
  418. if err != nil {
  419. br.Msg = "获取数据失败!"
  420. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  421. return
  422. }
  423. resp := new(models.ArticleInterviewApplyResp)
  424. if count <= 0 {
  425. item := new(models.CygxInterviewApply)
  426. item.ArticleId = req.ArticleId
  427. item.UserId = uid
  428. item.CompanyId = user.CompanyId
  429. item.Status = "待邀请"
  430. item.Sort = 1
  431. item.ArticleTitle = article.Title
  432. item.CreateTime = time.Now()
  433. item.ModifyTime = time.Now()
  434. item.ArticleIdMd5 = article.ArticleIdMd5
  435. _, err = models.AddCygxInterviewApply(item)
  436. if err != nil {
  437. br.Msg = "申请失败"
  438. br.ErrMsg = "申请失败,Err:" + err.Error()
  439. return
  440. }
  441. br.Msg = "申请成功"
  442. resp.Status = 1
  443. //发送模板消息
  444. if user.CompanyId > 1 {
  445. mobile := user.Mobile
  446. if mobile == "" {
  447. mobile = user.Email
  448. }
  449. sellerItem, _ := models.GetSellerByCompanyId(user.CompanyId)
  450. if sellerItem != nil && sellerItem.AdminId > 0 && user.Mobile != "" {
  451. openIpItem, _ := models.GetUserRecordByUserIdByXzs(sellerItem.Mobile, 4)
  452. fmt.Println(openIpItem)
  453. if openIpItem != nil && openIpItem.OpenId != "" {
  454. go services.SendInterviewApplyTemplateMsg(user.RealName, sellerItem.CompanyName, mobile, article.Title, openIpItem.OpenId)
  455. }
  456. }
  457. }
  458. } else {
  459. err = models.RemoveArticleInterviewApply(uid, req.ArticleId)
  460. if err != nil {
  461. br.Msg = "取消申请失败"
  462. br.ErrMsg = "取消申请失败,Err:" + err.Error()
  463. return
  464. }
  465. br.Msg = "已取消申请"
  466. resp.Status = 2
  467. if user.CompanyId > 1 {
  468. mobile := user.Mobile
  469. if mobile == "" {
  470. mobile = user.Email
  471. }
  472. sellerItem, _ := models.GetSellerByCompanyId(user.CompanyId)
  473. if sellerItem != nil && sellerItem.AdminId > 0 && user.Mobile != "" {
  474. openIpItem, _ := models.GetUserRecordByUserIdByXzs(sellerItem.Mobile, 4)
  475. fmt.Println(openIpItem)
  476. if openIpItem != nil && openIpItem.OpenId != "" {
  477. go services.SendInterviewApplyCancelTemplateMsg(user.RealName, sellerItem.CompanyName, mobile, article.Title, openIpItem.OpenId)
  478. }
  479. }
  480. }
  481. }
  482. br.Ret = 200
  483. br.Success = true
  484. br.Data = resp
  485. }
  486. // @Title 获取报告详情
  487. // @Description 获取报告详情接口
  488. // @Param ArticleIdMd5 query int true "报告ID"
  489. // @Success 200 {object} models.ArticleDetailResp
  490. // @router /look/detail [get]
  491. func (this *ArticleCommonController) Detail() {
  492. br := new(models.BaseResponse).Init()
  493. defer func() {
  494. this.Data["json"] = br
  495. this.ServeJSON()
  496. }()
  497. articleIdMd5 := this.GetString("ArticleIdMd5")
  498. if articleIdMd5 == "" {
  499. br.Msg = "参数错误"
  500. br.ErrMsg = "参数错误"
  501. return
  502. }
  503. resp := new(models.ArticleDetailResp)
  504. detail, err := models.GetArticleDetailByIdMd5(articleIdMd5)
  505. if err != nil {
  506. br.Msg = "获取信息失败"
  507. br.ErrMsg = "获取信息失败,Err:" + err.Error()
  508. return
  509. }
  510. detail.Body = html.UnescapeString(detail.Body)
  511. //detail.Abstract = html.UnescapeString(detail.Abstract)
  512. sellerList, err := models.GetSellerList(detail.ArticleId)
  513. if err != nil {
  514. br.Msg = "获取信息失败"
  515. br.ErrMsg = "获取销售数据失败,Err:" + err.Error() + ";articleId" + strconv.Itoa(detail.ArticleId)
  516. return
  517. }
  518. if detail.ArticleId > 1000000 {
  519. var hrefRegexp = regexp.MustCompile("[0-9]\\d*")
  520. match := hrefRegexp.FindAllString(detail.SellerAndMobile, -1)
  521. if match != nil {
  522. for _, v := range match {
  523. sellerAndMobile := &models.SellerRep{
  524. SellerMobile: v,
  525. SellerName: strings.Replace(detail.SellerAndMobile, v, "", -1),
  526. }
  527. sellerList = append(sellerList, sellerAndMobile)
  528. }
  529. }
  530. }
  531. resp.HasPermission = 1
  532. detail.Abstract, _ = services.GetReportContentTextSub(detail.Abstract)
  533. detail.SellerList = sellerList
  534. resp.Detail = detail
  535. br.Ret = 200
  536. br.Success = true
  537. br.Msg = "获取成功"
  538. br.Data = resp
  539. }
  540. // @Title 上传文章阅读时间
  541. // @Description 上传文章阅读时间接口
  542. // @Param request body models.AddStopTimeRep true "type json string"
  543. // @Success 200 {object} models.ArticleDetailResp
  544. // @router /addStopTime [post]
  545. func (this *ArticleController) AddStopTime() {
  546. br := new(models.BaseResponse).Init()
  547. defer func() {
  548. this.Data["json"] = br
  549. this.ServeJSON()
  550. }()
  551. user := this.User
  552. if user == nil {
  553. br.Msg = "请登录"
  554. br.ErrMsg = "请登录,用户信息为空"
  555. br.Ret = 408
  556. return
  557. }
  558. var req models.AddStopTimeRep
  559. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  560. if err != nil {
  561. br.Msg = "参数解析异常!"
  562. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  563. return
  564. }
  565. uid := user.UserId
  566. articleId := req.ArticleId
  567. stopTime := req.StopTime
  568. outType := req.OutType
  569. if articleId <= 0 {
  570. br.Msg = "参数错误"
  571. br.ErrMsg = "参数错误"
  572. return
  573. }
  574. if stopTime == 0 {
  575. stopTime = 1
  576. }
  577. if outType != 2 {
  578. outType = 1
  579. }
  580. detail := new(models.ArticleDetail)
  581. hasPermission := 0
  582. hasFree := 0
  583. //判断是否已经申请过
  584. applyCount, err := models.GetApplyRecordCount(uid)
  585. if err != nil && err.Error() != utils.ErrNoRow() {
  586. br.Msg = "获取信息失败"
  587. br.ErrMsg = "判断是否已申请过试用失败,Err:" + err.Error()
  588. return
  589. }
  590. //`description:"1:有该行业权限,正常展示,2:无该行业权限,不存在权益客户下,3:无该品类权限,4:潜在客户,未提交过申请,5:潜在客户,已提交过申请"`
  591. if user.CompanyId > 1 {
  592. companyPermission, err := models.GetCompanyPermission(user.CompanyId)
  593. if err != nil {
  594. br.Msg = "获取信息失败"
  595. br.ErrMsg = "判断是否已申请访谈失败,Err:" + strconv.Itoa(uid) + ";articleId" + strconv.Itoa(articleId)
  596. return
  597. }
  598. detail, err = models.GetArticleDetailById(articleId)
  599. if err != nil {
  600. br.Msg = "获取信息失败"
  601. br.ErrMsg = "获取文章信息失败,Err:" + err.Error() + strconv.Itoa(uid) + ";articleId" + strconv.Itoa(articleId)
  602. return
  603. }
  604. if companyPermission == "" {
  605. if applyCount > 0 {
  606. hasPermission = 5
  607. } else {
  608. hasPermission = 2
  609. }
  610. hasFree = 2
  611. goto Loop
  612. } else {
  613. hasFree = 1
  614. var articlePermissionPermissionName string
  615. if detail.CategoryId > 0 {
  616. articlePermission, err := models.GetArticlePermission(detail.CategoryId)
  617. if err != nil {
  618. br.Msg = "获取信息失败"
  619. br.ErrMsg = "获取报告权限失败,Err:" + err.Error() + strconv.Itoa(uid) + ";articleId" + strconv.Itoa(articleId)
  620. return
  621. }
  622. if articlePermission == nil {
  623. br.Msg = "获取信息失败"
  624. br.ErrMsg = "报告权限不存在,Err:" + strconv.Itoa(uid) + ";articleId" + strconv.Itoa(articleId)
  625. return
  626. }
  627. articlePermissionPermissionName = articlePermission.PermissionName
  628. } else {
  629. articlePermissionPermissionName = detail.CategoryName
  630. }
  631. var hasPersion bool
  632. slice := strings.Split(articlePermissionPermissionName, ",")
  633. for _, v := range slice {
  634. if strings.Contains(companyPermission, v) {
  635. hasPersion = true
  636. }
  637. }
  638. if hasPersion {
  639. detailNew, err := models.GetNewArticleHistoryRecord(uid, articleId)
  640. if err == nil {
  641. hasPermission = 1
  642. historyRecord := new(models.AddStopTimeNewRep)
  643. historyRecord.StopTime = detailNew.StopTime + stopTime
  644. historyRecord.Id = detailNew.Id
  645. historyRecord.OutType = outType
  646. go models.UpdateArticleStopTime(historyRecord)
  647. }
  648. //不统计本公司的阅读记录、正常退出的不做时间差统计
  649. if user.CompanyId != 16 && stopTime > 3 {
  650. key := "CYGX_ARTICLE_PV" + strconv.Itoa(articleId) + "_" + strconv.Itoa(uid) + "_" + strconv.Itoa(user.CompanyId) + "_" + strconv.Itoa(outType)
  651. record := new(models.CygxArticleHistoryRecordNewpv)
  652. record.UserId = uid
  653. record.ArticleId = articleId
  654. record.CreateTime = time.Now()
  655. record.ModifyTime = time.Now()
  656. record.Mobile = user.Mobile
  657. record.Email = user.Email
  658. record.CompanyId = user.CompanyId
  659. record.CompanyName = user.CompanyName
  660. record.StopTime = stopTime
  661. record.OutType = outType
  662. if !utils.Rc.IsExist(key) || outType != 2 {
  663. //新增浏览记录
  664. go models.AddCygxArticleViewRecordNewpv(record)
  665. } else {
  666. go models.UpdateCygxArticleViewRecordNewpv(record, stopTime)
  667. }
  668. utils.Rc.Put(key, 1, 10*time.Minute)
  669. }
  670. models.ModifyReportLastViewTime(uid)
  671. } else { //无该行业权限
  672. hasPermission = 3
  673. }
  674. }
  675. } else { //潜在客户
  676. if applyCount > 0 {
  677. hasPermission = 5
  678. } else {
  679. hasPermission = 4
  680. }
  681. }
  682. Loop:
  683. resp := new(models.ArticleDetailAddStopTimeRep)
  684. resp.HasPermission = hasPermission
  685. resp.HasFree = hasFree
  686. br.Ret = 200
  687. br.Success = true
  688. br.Msg = "操作成功"
  689. br.Data = resp
  690. }
  691. // @Title 文章带问
  692. // @Description 新增文章带问接口
  693. // @Param request body models.AddArticleAskRep true "type json string"
  694. // @Success Ret=200 新增成功
  695. // @router /askAdd [post]
  696. func (this *ArticleController) AskAdd() {
  697. br := new(models.BaseResponse).Init()
  698. defer func() {
  699. this.Data["json"] = br
  700. this.ServeJSON()
  701. }()
  702. user := this.User
  703. if user == nil {
  704. br.Msg = "请登录"
  705. br.ErrMsg = "请登录,SysUser Is Empty"
  706. br.Ret = 408
  707. return
  708. }
  709. var req models.AddArticleAskRep
  710. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  711. if err != nil {
  712. br.Msg = "参数解析异常!"
  713. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  714. return
  715. }
  716. if req.Content == "" {
  717. br.Msg = "建议内容不可为空"
  718. return
  719. }
  720. content := req.Content
  721. itemToken, err := services.WxGetToken()
  722. if err != nil {
  723. br.Msg = "GetWxAccessToken Err:" + err.Error()
  724. return
  725. }
  726. if itemToken.AccessToken == "" {
  727. br.Msg = "accessToken is empty"
  728. return
  729. }
  730. commerr, err := weapp.MSGSecCheck(itemToken.AccessToken, content)
  731. if err != nil {
  732. br.Msg = "内容校验失败!"
  733. br.ErrMsg = "内容校验失败,Err:" + err.Error()
  734. return
  735. }
  736. if commerr.ErrCode != 0 {
  737. br.Msg = "内容违规,请重新提交!"
  738. br.ErrMsg = "颜文字内容违规,Err:" + commerr.ErrMSG
  739. return
  740. }
  741. articleId := req.ArticleId
  742. count, _ := models.GetArticleCountById(articleId)
  743. if count == 0 {
  744. br.Msg = "操作失败"
  745. br.ErrMsg = "文章ID错误,不存在 articleId:" + strconv.Itoa(articleId)
  746. return
  747. }
  748. companyDetail, err := models.GetCompanyDetailById(user.CompanyId)
  749. if err != nil {
  750. br.Msg = "提交失败!"
  751. br.ErrMsg = "获取客户详情失败,Err:" + err.Error()
  752. return
  753. }
  754. if companyDetail == nil {
  755. br.Msg = "提交失败!"
  756. br.ErrMsg = "客户不存在,uid:" + strconv.Itoa(user.UserId)
  757. return
  758. }
  759. item := new(models.CygxArticleAsk)
  760. item.UserId = user.UserId
  761. item.ArticleId = req.ArticleId
  762. item.CompanyId = user.CompanyId
  763. item.CompanyName = companyDetail.CompanyName
  764. item.CreateTime = time.Now()
  765. item.Mobile = user.Mobile
  766. item.Email = user.Email
  767. item.Content = content
  768. _, err = models.AddArticleAsk(item)
  769. if err != nil {
  770. br.Msg = "提交失败"
  771. br.ErrMsg = "提交失败,Err:" + err.Error()
  772. return
  773. }
  774. companyItem, err := models.GetSellerDetailAllByCompanyId(user.CompanyId)
  775. if err != nil {
  776. br.Msg = "获取信息失败"
  777. br.ErrMsg = "获取所属销售信息失败,Err:" + err.Error()
  778. return
  779. }
  780. var mobile string
  781. if utils.RunMode == "release" {
  782. //mobile = utils.WxMsgTemplateIdAskMsgMobileAll + "," + companyItem.Mobile
  783. mobile = utils.WxMsgTemplateIdAskMsgMobileAll
  784. } else {
  785. mobile = utils.WxMsgTemplateIdAskMsgMobile
  786. }
  787. openIdList, err := models.GetWxOpenIdByMobileList(mobile)
  788. if err != nil {
  789. br.Msg = "提交失败"
  790. br.ErrMsg = "提交失败,Err:" + err.Error()
  791. return
  792. }
  793. detail, err := models.GetArticleDetailById(articleId)
  794. if err != nil {
  795. br.Msg = "获取信息失败"
  796. br.ErrMsg = "获取信息失败,Err:" + err.Error()
  797. return
  798. }
  799. companyName := user.CompanyName + "-" + user.RealName + "(" + companyItem.SellerName + ")"
  800. go services.SendWxMsgWithAsk(companyName, time.Now().Format(utils.FormatDateTime), content, detail.Title, openIdList, req.ArticleId)
  801. br.Ret = 200
  802. br.Success = true
  803. br.Msg = "提交成功"
  804. }
  805. // @Title 下载PDF打水印
  806. // @Description 下载PDF打水印接口
  807. // @Param ArticleId query int true "报告ID"
  808. // @Success 200 {object} models.ArticleDetailFileLink
  809. // @router /pdfwatermark [get]
  810. func (this *ArticleController) Pdfwatermark() {
  811. br := new(models.BaseResponse).Init()
  812. defer func() {
  813. this.Data["json"] = br
  814. this.ServeJSON()
  815. }()
  816. user := this.User
  817. if user == nil {
  818. br.Msg = "请登录"
  819. br.ErrMsg = "请登录,用户信息为空"
  820. br.Ret = 408
  821. return
  822. }
  823. articleId, err := this.GetInt("ArticleId")
  824. if articleId <= 0 {
  825. br.Msg = "文章不存在"
  826. br.ErrMsg = "文章不存在,文章ID错误"
  827. return
  828. }
  829. detail := new(models.ArticleDetail)
  830. detail, err = models.GetArticleDetailById(articleId)
  831. if err != nil {
  832. br.Msg = "获取信息失败"
  833. br.ErrMsg = "获取信息失败,Err:" + err.Error()
  834. return
  835. }
  836. fileLink := detail.FileLink
  837. if fileLink == "" {
  838. br.Msg = "下载失败"
  839. br.ErrMsg = "下载失败,报告链接不存在"
  840. return
  841. }
  842. mobile := user.Mobile
  843. if mobile == "" {
  844. mobile = user.Email
  845. }
  846. sliceLink := strings.Split(fileLink, "/")
  847. uploadDir := "static/pdf/"
  848. //判断文件夹是否存在,不存在则创建
  849. if !utils.FileIsExist(uploadDir) {
  850. err = os.MkdirAll(uploadDir, 0755)
  851. if err != nil {
  852. br.Msg = "下载失败"
  853. br.ErrMsg = "存储目录创建失败,Err:" + err.Error()
  854. return
  855. }
  856. }
  857. var oldFile string
  858. var newFile string
  859. //获取PDF源文件名称
  860. pdfName := sliceLink[len(sliceLink)-1]
  861. pdfName = utils.MD5(pdfName) + ".pdf"
  862. oldFile = uploadDir + pdfName
  863. //判断PDF本地是否存在,不存在则保存到本地
  864. if !utils.FileIsExist(oldFile) {
  865. res, err := nhttp.Get(fileLink)
  866. if err != nil {
  867. br.Msg = "下载失败"
  868. br.ErrMsg = "获取源文件失败,Err:" + err.Error()
  869. return
  870. }
  871. defer res.Body.Close()
  872. // 获得get请求响应的reader对象
  873. reader := bufio.NewReaderSize(res.Body, 32*1024)
  874. file, err := os.Create(oldFile)
  875. if err != nil {
  876. br.Msg = "下载失败"
  877. br.ErrMsg = "保存源文件到本地失败,Err:" + err.Error()
  878. return
  879. }
  880. defer file.Close()
  881. //获得文件的writer对象
  882. writer := bufio.NewWriter(file)
  883. written, _ := io.Copy(writer, reader)
  884. fmt.Printf("Total length: %d", written)
  885. }
  886. newFile = uploadDir + "new_" + pdfName
  887. onTop := true
  888. wm, err := pdfcpu.ParseTextWatermarkDetails(mobile, " op:.4, pos:c ,points:16 ", onTop, 1)
  889. if err != nil {
  890. br.Msg = "下载失败"
  891. br.ErrMsg = "生成水印文件失败,Err:" + err.Error()
  892. return
  893. }
  894. err = api.AddWatermarksFile(oldFile, newFile, nil, wm, nil)
  895. if err != nil {
  896. br.Msg = "下载失败"
  897. br.ErrMsg = "生成水印PDF失败,Err:" + err.Error()
  898. return
  899. }
  900. resp := new(models.ArticleDetailFileLink)
  901. randStr := utils.GetRandStringNoSpecialChar(28)
  902. fileName := randStr + ".pdf"
  903. savePath := uploadDir + time.Now().Format("200601/20060102/")
  904. savePath += fileName
  905. //上传到阿里云
  906. err = services.UploadFileToAliyun(fileName, newFile, savePath)
  907. if err != nil {
  908. br.Msg = "下载失败"
  909. br.ErrMsg = "文件上传失败,Err:" + err.Error()
  910. return
  911. }
  912. fileHost := "https://hzstatic.hzinsights.com/"
  913. resourceUrl := fileHost + savePath
  914. defer func() {
  915. os.Remove(newFile)
  916. }()
  917. resp.FileLink = resourceUrl
  918. br.Ret = 200
  919. br.Success = true
  920. br.Msg = "获取成功"
  921. br.Data = resp
  922. }