report.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "github.com/rdlucklib/rdluck_tools/common"
  6. "hongze/hongze_open_api/logic"
  7. "hongze/hongze_open_api/models/request/article"
  8. "hongze/hongze_open_api/models/response/report"
  9. celuePushTable "hongze/hongze_open_api/models/tables/article"
  10. "hongze/hongze_open_api/models/tables/rddp/classify"
  11. tables "hongze/hongze_open_api/models/tables/report"
  12. "hongze/hongze_open_api/models/tables/wx_user"
  13. "hongze/hongze_open_api/utils"
  14. "net/url"
  15. "strconv"
  16. "strings"
  17. "time"
  18. )
  19. // 报告模块
  20. type ReportController struct {
  21. BaseAuth
  22. }
  23. // 报告模块
  24. type ReportControllerCommon struct {
  25. BaseCommon
  26. }
  27. // ListReport
  28. // @Title 获取报告列表接口
  29. // @Description 获取报告列表
  30. // @Param _page_size query int true "每页数据条数"
  31. // @Param _page query int true "当前页页码,从1开始"
  32. // @Param report_type query string true "类型 day:晨报 、week :周报、two_week:双周报 、month:月报、other :点评 (默认为day:晨报) "
  33. // @Param keyword query string true "搜索关键词"
  34. // @Param mobile query string true "用户手机号(加密后的)"
  35. // @Success 200 {object} report.ReportListResp
  36. // @router /list [get]
  37. func (c *ReportController) ListReport() {
  38. pageSize, _ := c.GetInt("_page_size")
  39. currentIndex, _ := c.GetInt("_page")
  40. keyWord := c.GetString("keyword")
  41. reportType := c.GetString("report_type")
  42. //mobile := c.GetString("mobile")
  43. var startSize int
  44. if pageSize <= 0 {
  45. pageSize = utils.PageSize20
  46. }
  47. if currentIndex <= 0 {
  48. currentIndex = 1
  49. }
  50. startSize = utils.StartIndex(currentIndex, pageSize)
  51. //通过url获取mobile中的参数
  52. var mobile string
  53. URL, err := url.Parse(c.Ctx.Input.URI())
  54. if err != nil {
  55. fmt.Println(err)
  56. c.FailWithMessage("获取报告失败")
  57. return
  58. }
  59. urlParameter := URL.RawQuery
  60. sliceUrl := strings.Split(urlParameter, "&")
  61. if len(sliceUrl) > 0 {
  62. for _, v := range sliceUrl {
  63. if strings.Contains(v, "mobile=") {
  64. mobile = strings.Replace(v, "mobile=", "", -1)
  65. }
  66. }
  67. }
  68. if mobile == "" {
  69. c.FailWithMessage("mobile 必传")
  70. return
  71. }
  72. mobile, err = url.QueryUnescape(mobile)
  73. if err != nil {
  74. c.FailWithMessage("mobile 必传")
  75. return
  76. }
  77. var dateTxt = []byte(mobile)
  78. resultDe := utils.DesBase64Decrypt(dateTxt)
  79. deMobile := string(resultDe)
  80. if deMobile == "" {
  81. c.FailWithMessage("手机号加密格式错误")
  82. return
  83. }
  84. totalFicc, err := tables.GetUserReportFiccCount(deMobile)
  85. if err != nil {
  86. c.FailWithMessage("获取失败")
  87. return
  88. }
  89. if totalFicc < 1 {
  90. c.FailWithMessage("用户不存在")
  91. return
  92. }
  93. var condition string
  94. var pars []interface{}
  95. condition = ` AND enabled = 1 `
  96. if keyWord != "" {
  97. condition += ` AND research_report_name LIKE '%` + keyWord + `%'`
  98. }
  99. //day:晨报 、week :周报、two_week:双周报 、month:月报、other :点评
  100. if reportType != "week" && reportType != "two_week" && reportType != "month" && reportType != "other" {
  101. reportType = "day"
  102. }
  103. if reportType != "" {
  104. condition += ` AND type = ? AND status = 'report' `
  105. pars = append(pars, reportType)
  106. }
  107. total, err := tables.GetReportListCount(condition, pars)
  108. if err != nil {
  109. c.FailWithMessage("获取失败")
  110. return
  111. }
  112. list, err := tables.GetReportList(condition, pars, startSize, pageSize)
  113. if err != nil {
  114. c.FailWithMessage("获取失败")
  115. return
  116. }
  117. mobile = url.QueryEscape(mobile) //转义 +
  118. nonceStr := common.GetRandString(10)
  119. timeUnix := strconv.FormatInt(time.Now().Unix(), 10)
  120. if len(list) > 0 {
  121. for k, v := range list {
  122. postData := make(map[string]string)
  123. reportId := strconv.Itoa(v.ResearchReportId)
  124. parameter := "mobile=" + mobile + "&research_report_id=" + reportId + "&nonce_str=" + nonceStr + "&timestamp=" + timeUnix
  125. postData["mobile"] = mobile
  126. postData["research_report_id"] = reportId
  127. postData["appid"] = utils.ReportAppid
  128. postData["nonce_str"] = nonceStr
  129. postData["timestamp"] = timeUnix
  130. sign := utils.GetSign(postData)
  131. list[k].HttpUrl = utils.ResearchReportUrl + "hzsl/report/detail?" + parameter + "&sign=" + sign
  132. }
  133. }
  134. page := utils.GetPaging(currentIndex, pageSize, total)
  135. resp := tables.ReportListResp{
  136. List: list,
  137. Paging: page,
  138. }
  139. c.OkDetailed(resp, "获取成功")
  140. }
  141. // GetReportInfo
  142. // @Title 获取报告详情
  143. // @Description 获取报告详情
  144. // @Param research_report_id query int true "报告ID"
  145. // @Param mobile query string true "用户手机号(加密后的)"
  146. // @Success 200 {object} report.ResearchReportInfo
  147. // @router /getReportInfo [get]
  148. func (c *ReportControllerCommon) GetReportInfo() {
  149. researchReportId, _ := c.GetInt("research_report_id")
  150. //mobile := c.GetString("mobile")
  151. if researchReportId < 1 {
  152. c.FailWithMessage("请传入报告id")
  153. return
  154. }
  155. mobile := c.GetString("mobile")
  156. if mobile == "" {
  157. c.FailWithMessage("mobile 必传")
  158. return
  159. }
  160. var dateTxt = []byte(mobile)
  161. resultDe := utils.DesBase64Decrypt(dateTxt)
  162. deMobile := string(resultDe)
  163. if deMobile == "" {
  164. c.FailWithMessage("手机号加密格式错误")
  165. return
  166. }
  167. totalFicc, err := tables.GetUserReportFiccCount(deMobile)
  168. if err != nil {
  169. c.FailWithMessage("获取失败")
  170. return
  171. }
  172. if totalFicc < 1 {
  173. c.FailWithMessage("用户不存在")
  174. return
  175. }
  176. userInfo, err := wx_user.GetWxUserByMobileStr(deMobile)
  177. if err != nil {
  178. c.FailWithMessage("找不到该用户")
  179. return
  180. }
  181. reportInfo, hasPermission, err := tables.GetResearchReportInfo(researchReportId, userInfo.UserId)
  182. if err != nil {
  183. fmt.Println(err)
  184. c.FailWithMessage("获取报告失败")
  185. return
  186. }
  187. if !hasPermission {
  188. c.FailWithMessage("无权限")
  189. return
  190. }
  191. mobile = url.QueryEscape(mobile) //转义 +
  192. nonceStr := common.GetRandString(10)
  193. timeUnix := strconv.FormatInt(time.Now().Unix(), 10)
  194. if len(reportInfo.ResearchReportTypeList) > 1 {
  195. for k, v := range reportInfo.ResearchReportTypeList {
  196. postData := make(map[string]string)
  197. reportId := strconv.Itoa(int(v.ResearchReportTypeId))
  198. parameter := "mobile=" + mobile + "&ResearchReportTypeId=" + reportId + "&appid=" + utils.ReportAppid + "&nonce_str=" + nonceStr + "&timestamp=" + timeUnix
  199. postData["mobile"] = mobile
  200. postData["ResearchReportTypeId"] = reportId
  201. postData["appid"] = utils.ReportAppid
  202. postData["nonce_str"] = nonceStr
  203. postData["timestamp"] = timeUnix
  204. sign := utils.GetSign(postData)
  205. reportInfo.ResearchReportTypeList[k].HttpUrl = utils.ResearchReportUrl + "report/?" + parameter + "&sign=" + sign
  206. }
  207. }
  208. c.OkDetailed(reportInfo, "获取成功")
  209. }
  210. // GetResearchReportChapter
  211. // @Title 获取章节详情接口
  212. // @Description 获取章节详情
  213. // @Param ResearchReportTypeId query int true "章节ID"
  214. // @Param mobile query string false "用户手机号(加密后的)"
  215. // @Success 200 {object} report.ResearchReportTypeContentInfo
  216. // @router /getReportChapterInfo [get]
  217. func (c *ReportControllerCommon) GetResearchReportChapter() {
  218. researchReportTypeId, _ := c.GetInt("ResearchReportTypeId")
  219. //mobile := c.GetString("mobile")
  220. if researchReportTypeId < 1 {
  221. c.FailWithMessage("请传入章节id")
  222. return
  223. }
  224. mobile := c.GetString("mobile")
  225. if mobile == "" {
  226. c.FailWithMessage("mobile 必传")
  227. return
  228. }
  229. var dateTxt = []byte(mobile)
  230. resultDe := utils.DesBase64Decrypt(dateTxt)
  231. deMobile := string(resultDe)
  232. if deMobile == "" {
  233. c.FailWithMessage("手机号加密格式错误")
  234. return
  235. }
  236. totalFicc, err := tables.GetUserReportFiccCount(deMobile)
  237. if err != nil {
  238. c.FailWithMessage("获取失败")
  239. return
  240. }
  241. if totalFicc < 1 {
  242. c.FailWithMessage("用户不存在")
  243. return
  244. }
  245. userInfo, err := wx_user.GetWxUserByMobileStr(deMobile)
  246. if err != nil {
  247. c.FailWithMessage("找不到该用户")
  248. return
  249. }
  250. reportInfo, hasPermission, err := tables.GetResearchReportTypeContentInfo(uint64(researchReportTypeId), uint64(userInfo.UserId))
  251. if err != nil {
  252. fmt.Println(err)
  253. c.FailWithMessage("获取报告失败")
  254. return
  255. }
  256. if !hasPermission {
  257. c.FailWithMessage("无权限")
  258. return
  259. }
  260. c.OkDetailed(reportInfo, "获取成功")
  261. }
  262. // ArticleChange
  263. // @Title 报告变更通知的插入点接口
  264. // @Description 报告变更通知的插入点接口
  265. // @Param request body article.CreatArticleCeluePushReq true "type json string"
  266. // @Success 200 创建成功
  267. // @router /article/change [post]
  268. func (c *ReportController) ArticleChange() {
  269. var req article.CreatArticleCeluePushReq
  270. err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
  271. if err != nil {
  272. c.FailWithMessage("参数解析异常")
  273. return
  274. }
  275. //appid权限校验
  276. appid := req.Appid
  277. if utils.RunMode == "release" && appid != "XVuGlcyEEVNYVWx5" {
  278. c.FailWithMessage("无权限")
  279. return
  280. }
  281. articleId := req.ArticleId
  282. action := req.Action
  283. if articleId < 0 {
  284. c.FailWithMessage("缺少 article_id 参数")
  285. return
  286. }
  287. if action != "add" && action != "edit" && action != "move" {
  288. c.FailWithMessage("action参数类型错误")
  289. return
  290. }
  291. item := new(celuePushTable.CygxArticleCeluePush)
  292. item.ArticleId = articleId
  293. item.Action = action
  294. item.CreateTime = time.Now()
  295. item.ModifyTime = time.Now()
  296. err = celuePushTable.AddCygxArticleCeluePush(item)
  297. if err != nil {
  298. c.OkWithMessage("创建失败")
  299. return
  300. }
  301. c.OkWithMessage("创建成功")
  302. }
  303. // ClassifyList
  304. // @Title 获取报告分类列表接口
  305. // @Description 获取报告分类列表接口
  306. // @Param _page_size query int true "每页数据条数"
  307. // @Param _page query int true "当前页页码,从1开始"
  308. // @Param report_type query string true "类型 day:晨报 、week :周报、two_week:双周报 、month:月报、other :点评 (默认为day:晨报) "
  309. // @Param keyword query string true "搜索关键词"
  310. // @Param mobile query string true "用户手机号(加密后的)"
  311. // @Success 200 {object} classify.ClassifyList
  312. // @router /classify/list [get]
  313. func (c *ReportController) ClassifyList() {
  314. list, err := classify.GetClassifyList()
  315. if err != nil {
  316. c.FailWithMessage("获取分类失败")
  317. return
  318. }
  319. for _, v := range list {
  320. childList, err := classify.GetClassifyListByParentId(v.Id)
  321. if err != nil {
  322. c.FailWithMessage("获取下级分类失败")
  323. return
  324. }
  325. for _, childV := range childList {
  326. childV.Child = make([]*classify.ClassifyList, 0)
  327. }
  328. if len(childList) == 0 {
  329. tmpClassifyList := *v
  330. tmpClassifyList.Child = make([]*classify.ClassifyList, 0)
  331. childList = []*classify.ClassifyList{&tmpClassifyList}
  332. }
  333. v.Child = childList
  334. }
  335. c.OkDetailed(list, "获取成功")
  336. }
  337. // ListReportV2
  338. // @Title 获取报告列表接口
  339. // @Description 获取报告列表
  340. // @Param _page_size query int true "每页数据条数"
  341. // @Param _page query int true "当前页页码,从1开始"
  342. // @Param classify_id query int true "分类id"
  343. // @Param keyword query string true "搜索关键词"
  344. // @Param mobile query string true "用户手机号(加密后的)"
  345. // @Success 200 {object} report.ReportListResp
  346. // @router /list/v2 [get]
  347. func (c *ReportController) ListReportV2() {
  348. pageSize, _ := c.GetInt("_page_size")
  349. currentIndex, _ := c.GetInt("_page")
  350. keyword := c.GetString("keyword")
  351. classifyId, _ := c.GetInt("classify_id")
  352. //mobile := c.GetString("mobile")
  353. var startSize int
  354. if pageSize <= 0 {
  355. pageSize = utils.PageSize20
  356. }
  357. if currentIndex <= 0 {
  358. currentIndex = 1
  359. }
  360. startSize = utils.StartIndex(currentIndex, pageSize)
  361. //通过url获取mobile中的参数
  362. var mobile string
  363. URL, err := url.Parse(c.Ctx.Input.URI())
  364. if err != nil {
  365. fmt.Println(err)
  366. c.FailWithMessage("获取报告失败")
  367. return
  368. }
  369. urlParameter := URL.RawQuery
  370. sliceUrl := strings.Split(urlParameter, "&")
  371. if len(sliceUrl) > 0 {
  372. for _, v := range sliceUrl {
  373. if strings.Contains(v, "mobile=") {
  374. mobile = strings.Replace(v, "mobile=", "", -1)
  375. }
  376. }
  377. }
  378. if mobile == "" {
  379. c.FailWithMessage("mobile 必传")
  380. return
  381. }
  382. mobile, err = url.QueryUnescape(mobile)
  383. if err != nil {
  384. c.FailWithMessage("mobile 必传")
  385. return
  386. }
  387. var dateTxt = []byte(mobile)
  388. resultDe := utils.DesBase64Decrypt(dateTxt)
  389. deMobile := string(resultDe)
  390. if deMobile == "" {
  391. c.FailWithMessage("手机号加密格式错误")
  392. return
  393. }
  394. if classifyId <= 0 {
  395. c.FailWithMessage("分类必传")
  396. return
  397. }
  398. total, list, err, errMsg := logic.GetReportList(classifyId, deMobile, keyword, startSize, pageSize)
  399. if err != nil {
  400. c.FailWithMessage(errMsg)
  401. return
  402. }
  403. page := utils.GetPaging(currentIndex, pageSize, total)
  404. resp := report.ReportListResp{
  405. List: list,
  406. Paging: page,
  407. }
  408. c.OkDetailed(resp, "获取成功")
  409. }
  410. // GetReportInfoV2
  411. // @Title 获取报告详情
  412. // @Description 获取报告详情
  413. // @Param research_report_id query int true "报告ID"
  414. // @Param mobile query string true "用户手机号(加密后的)"
  415. // @Success 200 {object} report.ResearchReportInfo
  416. // @router /getReportInfo/v2 [get]
  417. func (c *ReportControllerCommon) GetReportInfoV2() {
  418. reportId, _ := c.GetInt("report_id")
  419. //mobile := c.GetString("mobile")
  420. if reportId < 1 {
  421. c.FailWithMessage("请传入报告id")
  422. return
  423. }
  424. mobile := c.GetString("mobile")
  425. if mobile == "" {
  426. c.FailWithMessage("mobile 必传")
  427. return
  428. }
  429. var dateTxt = []byte(mobile)
  430. resultDe := utils.DesBase64Decrypt(dateTxt)
  431. deMobile := string(resultDe)
  432. if deMobile == "" {
  433. c.FailWithMessage("手机号加密格式错误")
  434. return
  435. }
  436. reportDetail, err, errMsg := logic.GetReportDetail(reportId, deMobile)
  437. if err != nil {
  438. //c.FailWithMessage(errMsg)
  439. c.FailWithMessageErr(errMsg, err.Error())
  440. return
  441. }
  442. c.OkDetailed(reportDetail, "获取成功")
  443. }
  444. // GetResearchReportChapterV2
  445. // @Title 获取章节详情接口
  446. // @Description 获取章节详情
  447. // @Param ResearchReportTypeId query int true "章节ID"
  448. // @Param mobile query string false "用户手机号(加密后的)"
  449. // @Success 200 {object} report.ResearchReportTypeContentInfo
  450. // @router /getReportChapterInfo/v2 [get]
  451. func (c *ReportControllerCommon) GetResearchReportChapterV2() {
  452. reportChapterId, _ := c.GetInt("report_chapter_id")
  453. if reportChapterId < 1 {
  454. c.FailWithMessage("请传入章节id")
  455. return
  456. }
  457. mobile := c.GetString("mobile")
  458. if mobile == "" {
  459. c.FailWithMessage("mobile 必传")
  460. return
  461. }
  462. var dateTxt = []byte(mobile)
  463. resultDe := utils.DesBase64Decrypt(dateTxt)
  464. deMobile := string(resultDe)
  465. if deMobile == "" {
  466. c.FailWithMessage("手机号加密格式错误")
  467. return
  468. }
  469. chapterDetail, err, errMsg := logic.GetChapterDetail(deMobile, reportChapterId)
  470. if err != nil {
  471. //c.FailWithMessage(errMsg)
  472. c.FailWithMessageErr(errMsg, err.Error())
  473. return
  474. }
  475. c.OkDetailed(chapterDetail, "获取成功")
  476. }