chart.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. package services
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "hongze/hongze_cygx/models"
  7. "hongze/hongze_cygx/utils"
  8. "io/ioutil"
  9. nhttp "net/http"
  10. "strconv"
  11. "strings"
  12. "time"
  13. )
  14. //get公共请求方法
  15. func PublicGetDate(url, authorization string) (body []byte, err error) {
  16. defer func() {
  17. if err != nil {
  18. go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"失败提醒", url+"Get ErrMsg:"+err.Error(), utils.EmailSendToUsers)
  19. }
  20. }()
  21. method := "GET"
  22. client := &nhttp.Client{}
  23. req, err := nhttp.NewRequest(method, url, nil)
  24. if err != nil {
  25. return
  26. }
  27. req.Header.Add("Authorization", authorization)
  28. res, err := client.Do(req)
  29. if err != nil {
  30. return
  31. }
  32. defer res.Body.Close()
  33. body, err = ioutil.ReadAll(res.Body)
  34. if err != nil {
  35. return
  36. }
  37. return
  38. }
  39. //获取列表
  40. func GetChartListByApi(cont context.Context) (err error) {
  41. defer func() {
  42. if err != nil {
  43. go utils.SendAlarmMsg("发送邮件:同步用户到策略平台信息失败", 2)
  44. go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"失败提醒", "GetArticleListByApi ErrMsg:"+err.Error(), utils.EmailSendToUsers)
  45. }
  46. }()
  47. url := utils.ApiUrl + "charts/mp?take=100&skip=0"
  48. authorization := utils.ApiAuthorization
  49. body, err := PublicGetDate(url, authorization)
  50. if err != nil {
  51. return
  52. }
  53. var chartResult models.ChartResultApi
  54. err = json.Unmarshal(body, &chartResult)
  55. if err != nil {
  56. return err
  57. }
  58. for _, v := range chartResult.Data {
  59. item := new(models.CygxChart)
  60. item.ChartId = v.ChartId
  61. item.PtagId = v.PtagId
  62. item.CtagId = v.CtagId
  63. item.Title = v.Title
  64. item.TitleEn = v.TitleEn
  65. item.CreateDateApi = time.Now()
  66. item.CreateDate = v.CreateDate
  67. item.PublishStatus = v.PublishStatus
  68. item.PtagName = v.Ptag.Name
  69. item.CtagName = v.Ctag.Name
  70. item.PtagNameTwo = v.PtagTwo.Name
  71. item.CtagNameTwo = v.CtagTwo.Name
  72. item.PtagIdTwo = v.PtagTwo.Id
  73. item.CtagIdTwo = v.CtagTwo.Id
  74. item.Cover = v.Cover
  75. item.Iframe = v.Iframe
  76. count, err := models.GetChartCountById(v.ChartId)
  77. if err != nil && err.Error() != utils.ErrNoRow() {
  78. return err
  79. }
  80. if count == 0 {
  81. _, err := models.AddCygxChart(item)
  82. if err != nil {
  83. return err
  84. }
  85. } else {
  86. updateParams := make(map[string]interface{})
  87. updateParams["Title"] = v.Title
  88. updateParams["PtagId"] = v.PtagId
  89. updateParams["CtagId"] = v.CtagId
  90. updateParams["TitleEn"] = v.TitleEn
  91. updateParams["CreateDate"] = v.CreateDate
  92. updateParams["PublishStatus"] = v.PublishStatus
  93. updateParams["PtagName"] = v.Ptag.Name
  94. updateParams["CtagName"] = v.Ctag.Name
  95. updateParams["PtagNameTwo"] = v.PtagTwo.Name
  96. updateParams["CtagNameTwo"] = v.CtagTwo.Name
  97. updateParams["PtagIdTwo"] = v.PtagTwo.Id
  98. updateParams["CtagIdTwo"] = v.CtagTwo.Id
  99. updateParams["Cover"] = v.Cover
  100. updateParams["Iframe"] = v.Iframe
  101. whereParam := map[string]interface{}{"chart_id": v.ChartId}
  102. err = models.UpdateByExpr(models.CygxChart{}, whereParam, updateParams)
  103. }
  104. }
  105. return
  106. }
  107. //获取图表分类
  108. func GetChartPtagByApi() (items []*models.ChartPtagResp, err error) {
  109. defer func() {
  110. if err != nil {
  111. go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"失败提醒", "GetChartPtagByApi ErrMsg:"+err.Error(), utils.EmailSendToUsers)
  112. }
  113. }()
  114. url := utils.ApiUrl + "charts/ptag"
  115. authorization := utils.ApiAuthorization
  116. body, err := PublicGetDate(url, authorization)
  117. if err != nil {
  118. return
  119. }
  120. var chartResult models.ChartPtagResultApi
  121. err = json.Unmarshal(body, &chartResult)
  122. if err != nil {
  123. return
  124. }
  125. for _, v := range chartResult.Data {
  126. item := new(models.ChartPtagResp)
  127. item.PermissionName = v.ChartPermissionName
  128. item.ChartPermissionId = v.ChartPermissionId
  129. if len(v.Ctag) > 0 {
  130. for _, v2 := range v.Ctag {
  131. itemCtag := new(models.CtagResp)
  132. itemCtag.CtagId = v2.Id
  133. itemCtag.Name = v2.Name
  134. item.List = append(item.List, itemCtag)
  135. }
  136. }
  137. items = append(items, item)
  138. }
  139. return
  140. }
  141. //获取用户的Token
  142. func GetUserTokenByMobile(mobile string) (token string, err error) {
  143. //缓存校验
  144. cacheKey := fmt.Sprint("xygx_chart:chart_token:add:", "Mobile", mobile)
  145. ttlTime := utils.Rc.GetRedisTTL(cacheKey)
  146. if ttlTime > 0 {
  147. token, _ = utils.Rc.RedisString(cacheKey)
  148. }
  149. if token == "" {
  150. url := utils.ApiUrl + "auth/login"
  151. method := "POST"
  152. payload := strings.NewReader(`{
  153. "phone_number":"` + mobile + `",
  154. "password":"hz123456"}`)
  155. client := &nhttp.Client{}
  156. req, errReq := nhttp.NewRequest(method, url, payload)
  157. if errReq != nil {
  158. err = errReq
  159. return
  160. }
  161. req.Header.Add("Content-Type", "application/json")
  162. req.Header.Add("Cookie", "sessionid=naj5j5kl1jjynh7og1rsaxkl1vrsl829")
  163. res, errReq := client.Do(req)
  164. if errReq != nil {
  165. err = errReq
  166. return
  167. }
  168. defer res.Body.Close()
  169. body, errReq := ioutil.ReadAll(res.Body)
  170. if errReq != nil {
  171. err = errReq
  172. return
  173. }
  174. var chartResult models.ChartUserTokenResultApi
  175. errReq = json.Unmarshal(body, &chartResult)
  176. if errReq != nil {
  177. err = errReq
  178. return
  179. }
  180. token = chartResult.Data.AccessToken
  181. utils.Rc.Put(cacheKey, token, time.Hour*24)
  182. }
  183. return
  184. }
  185. //获取图表收藏
  186. func GetChartCollectionByApi(mobile string, take, skip int) (items []*models.HomeChartListResp, err error, total int) {
  187. defer func() {
  188. if err != nil {
  189. //go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"失败提醒", "GetChartPtagByApi ErrMsg:"+err.Error(), utils.EmailSendToUsers)
  190. }
  191. }()
  192. url := utils.ApiUrl + "charts/favorites?take=" + strconv.Itoa(take) + "&skip=" + strconv.Itoa(skip)
  193. authorization, err := GetUserTokenByMobile(mobile)
  194. if err != nil {
  195. return
  196. }
  197. authorization = "bearer " + authorization
  198. body, err := PublicGetDate(url, authorization)
  199. if err != nil {
  200. return
  201. }
  202. var chartResult models.ChartFavoritesResultApi
  203. err = json.Unmarshal(body, &chartResult)
  204. if err != nil {
  205. return
  206. }
  207. for _, v := range chartResult.Data {
  208. item := new(models.HomeChartListResp)
  209. item.ChartId = v.ChartId
  210. item.Title = v.ChartInfo.Title
  211. item.TitleEn = v.ChartInfo.TitleEn
  212. item.CreateDate = v.CreateDate
  213. item.PtagName = v.ChartInfo.Ptag.Name
  214. item.CtagName = v.ChartInfo.Ctag.Name
  215. item.BodyHtml = v.ChartInfo.Cover
  216. item.HttpUrl = "https://vmp.hzinsights.com/v2/charts/" + strconv.Itoa(v.ChartId)
  217. item.IsNeedJump = true
  218. items = append(items, item)
  219. }
  220. total = chartResult.Pagination.Total
  221. return
  222. }
  223. //判断策略平台是否已经添加过收藏
  224. func GetIsCollectionChart(mobile string, chartId int) (isCollection bool, err error) {
  225. //获取所有的收藏列表,进行比对看看是否收藏,调用三方接口详情没有是否收藏的字段
  226. list, err, _ := GetChartCollectionByApi(mobile, 9999, 0)
  227. if err != nil {
  228. return
  229. }
  230. for _, v := range list {
  231. if v.ChartId == chartId {
  232. isCollection = true
  233. }
  234. }
  235. return
  236. }
  237. //添加收藏
  238. func AddCollectionChart(mobile string, chartId int) (err error) {
  239. authorization, err := GetUserTokenByMobile(mobile)
  240. if err != nil {
  241. return
  242. }
  243. authorization = "bearer " + authorization
  244. url := utils.ApiUrl + "charts/favorites"
  245. method := "POST"
  246. payload := strings.NewReader(`{
  247. "chart_id":` + strconv.Itoa(chartId) + `
  248. }`)
  249. client := &nhttp.Client{}
  250. req, err := nhttp.NewRequest(method, url, payload)
  251. if err != nil {
  252. return
  253. }
  254. req.Header.Add("Authorization", authorization)
  255. req.Header.Add("Content-Type", "application/json")
  256. req.Header.Add("Cookie", "sessionid=naj5j5kl1jjynh7og1rsaxkl1vrsl829")
  257. res, err := client.Do(req)
  258. if err != nil {
  259. return
  260. }
  261. defer res.Body.Close()
  262. _, err = ioutil.ReadAll(res.Body)
  263. if err != nil {
  264. return
  265. }
  266. return
  267. }
  268. //移除收藏
  269. func DeleteCollectionChart(mobile string, chartId int) (err error) {
  270. authorization, err := GetUserTokenByMobile(mobile)
  271. if err != nil {
  272. return
  273. }
  274. authorization = "bearer " + authorization
  275. url := utils.ApiUrl + "charts/favorites/" + strconv.Itoa(chartId)
  276. method := "DELETE"
  277. client := &nhttp.Client{}
  278. req, err := nhttp.NewRequest(method, url, nil)
  279. if err != nil {
  280. return
  281. }
  282. req.Header.Add("Authorization", authorization)
  283. req.Header.Add("Cookie", "sessionid=naj5j5kl1jjynh7og1rsaxkl1vrsl829")
  284. res, err := client.Do(req)
  285. if err != nil {
  286. return
  287. }
  288. defer res.Body.Close()
  289. _, err = ioutil.ReadAll(res.Body)
  290. if err != nil {
  291. return
  292. }
  293. return
  294. }
  295. func DoCompany() {
  296. //listCollect, err := models.GetCygxArticleCollectList()
  297. //if err != nil {
  298. // fmt.Println("GetAddCygxArticleCollectList ,Err" + err.Error())
  299. //}
  300. //for _, v := range listCollect {
  301. // user, err := models.GetWxUserItemByUserId(v.UserId)
  302. // if err != nil && err.Error() != utils.ErrNoRow() {
  303. // fmt.Println("GetWxUserItemByUserId ,Err" + err.Error())
  304. // }
  305. // if user != nil {
  306. // fmt.Println(user.RealName)
  307. // err = models.UpdateCygxArticleCollect(user)
  308. // if err != nil {
  309. // fmt.Println("UpdateCygxArticleCollect ,Err" + err.Error())
  310. // }
  311. // }
  312. //}
  313. //listCollect, err := models.GetCygxSearchKeyWordList()
  314. //if err != nil {
  315. // fmt.Println("GetAddCygxArticleCollectList ,Err" + err.Error())
  316. //}
  317. //for _, v := range listCollect {
  318. // user, err := models.GetWxUserItemByUserId(v.UserId)
  319. // if err != nil && err.Error() != utils.ErrNoRow() {
  320. // fmt.Println("GetWxUserItemByUserId ,Err" + err.Error())
  321. // }
  322. // if user != nil {
  323. // fmt.Println(user.RealName)
  324. // err = models.UpdateCygxSearchKeyWord(user)
  325. // if err != nil {
  326. // fmt.Println("UpdateCygxArticleCollect ,Err" + err.Error())
  327. // }
  328. // }
  329. //}
  330. //var condition string
  331. ////updateTime := time.Now().Add(-time.Hour * 25).Format(utils.FormatDateTime)
  332. //condition = ` AND real_name = '' `
  333. //listArticlePv, err := models.GetArticleHistoryRecordAllByMobileList(condition)
  334. //if err != nil {
  335. // fmt.Println("GetArticleHistoryRecordAllByMobileList ,Err" + err.Error())
  336. //}
  337. //fmt.Println("长度", len(listArticlePv))
  338. //for k, v := range listArticlePv {
  339. // if v.Mobile != "" {
  340. // user, err := models.GetWxUserItemByMobile(v.Mobile)
  341. // if err != nil && err.Error() != utils.ErrNoRow() {
  342. // fmt.Println("GetWxUserItemByUserId ,Err" + err.Error())
  343. // }
  344. // if user != nil {
  345. // fmt.Println(user.RealName, k)
  346. // err = models.UpdateCygxArticleHistoryRecordAll(user)
  347. // if err != nil {
  348. // fmt.Println("UpdateCygxArticleCollect ,Err" + err.Error())
  349. // }
  350. // }
  351. // }
  352. //}
  353. //listIndustryFllow, err := models.GetCygxIndustryFllowList("")
  354. //if err != nil {
  355. // fmt.Println("GetArticleDepartmentFollowByMobileList ,Err" + err.Error())
  356. //}
  357. //fmt.Println("长度", len(listIndustryFllow))
  358. //for k, v := range listIndustryFllow {
  359. // if v.Mobile != "" {
  360. // user, err := models.GetWxUserItemByMobile(v.Mobile)
  361. // if err != nil && err.Error() != utils.ErrNoRow() {
  362. // fmt.Println("GetWxUserItemByUserId ,Err" + err.Error())
  363. // }
  364. // if user != nil {
  365. // fmt.Println(user.RealName, k)
  366. // err = models.UpdateCygxIndustryFllow(user)
  367. // if err != nil {
  368. // fmt.Println("UpdateCygxIndustryFllow ,Err" + err.Error())
  369. // }
  370. // }
  371. // }
  372. //}
  373. //
  374. //lisDepartmentF, err := models.GetArticleDepartmentFollowByMobileList("")
  375. //if err != nil {
  376. // fmt.Println("GetArticleDepartmentFollowByMobileList ,Err" + err.Error())
  377. //}
  378. //fmt.Println("长度", len(lisDepartmentF))
  379. //for k, v := range lisDepartmentF {
  380. // if v.Mobile != "" {
  381. // user, err := models.GetWxUserItemByMobile(v.Mobile)
  382. // if err != nil && err.Error() != utils.ErrNoRow() {
  383. // fmt.Println("GetWxUserItemByUserId ,Err" + err.Error())
  384. // }
  385. // if user != nil {
  386. // fmt.Println(user.RealName, k)
  387. // err = models.UpdateCygxArticleDepartmentFollow(user)
  388. // if err != nil {
  389. // fmt.Println("UpdateCygxArticleDepartmentFollow ,Err" + err.Error())
  390. // }
  391. // }
  392. // }
  393. //}
  394. //listChartCollect, err := models.GetCygxChartCollectByMobileList()
  395. //if err != nil {
  396. // fmt.Println("GetCygxChartCollectByMobileList ,Err" + err.Error())
  397. //}
  398. //fmt.Println("长度", len(listChartCollect))
  399. //for k, v := range listChartCollect {
  400. // if v.Mobile != "" {
  401. // user, err := models.GetWxUserItemByMobile(v.Mobile)
  402. // if err != nil && err.Error() != utils.ErrNoRow() {
  403. // fmt.Println("GetWxUserItemByUserId ,Err" + err.Error())
  404. // }
  405. // if user != nil {
  406. // fmt.Println(user.RealName, k)
  407. // err = models.UpdateCygxChartCollect(user)
  408. // if err != nil {
  409. // fmt.Println("UpdateCygxChartCollect ,Err" + err.Error())
  410. // }
  411. // }
  412. // }
  413. //}
  414. fmt.Println("end")
  415. }