123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512 |
- package services
- import (
- "context"
- "encoding/json"
- "fmt"
- "hongze/hongze_clpt/models"
- "hongze/hongze_clpt/utils"
- "io/ioutil"
- nhttp "net/http"
- "strconv"
- "strings"
- "time"
- )
- //get公共请求方法
- func PublicGetDate(url, authorization string) (body []byte, err error) {
- defer func() {
- if err != nil {
- go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"失败提醒", url+"Get ErrMsg:"+err.Error(), utils.EmailSendToUsers)
- }
- }()
- method := "GET"
- client := &nhttp.Client{}
- req, err := nhttp.NewRequest(method, url, nil)
- if err != nil {
- return
- }
- req.Header.Add("Authorization", authorization)
- res, err := client.Do(req)
- if err != nil {
- return
- }
- defer res.Body.Close()
- body, err = ioutil.ReadAll(res.Body)
- if err != nil {
- return
- }
- return
- }
- //同步图表列表
- func GetChartListByApi(cont context.Context) (err error) {
- defer func() {
- if err != nil {
- go utils.SendAlarmMsg("发送邮件:同步用户到策略平台信息失败", 2)
- go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"失败提醒", "GetArticleListByApi ErrMsg:"+err.Error(), utils.EmailSendToUsers)
- }
- }()
- mapChartid := make(map[int]int)
- chartList, err := models.GetChartListAll()
- if err != nil {
- return
- }
- for _, v := range chartList {
- mapChartid[v.ChartId] = v.ChartId
- }
- mapAllChartid := make(map[int]int)
- chartAllList, err := models.GetChartAllListAll()
- if err != nil {
- return
- }
- for _, v := range chartAllList {
- mapAllChartid[v.ChartId] = v.ChartId
- }
- url := utils.ApiUrl + "charts/mp?take=100&skip=0"
- authorization := utils.ApiAuthorization
- body, err := PublicGetDate(url, authorization)
- if err != nil {
- return
- }
- var chartResult models.ChartResultApi
- err = json.Unmarshal(body, &chartResult)
- if err != nil {
- return err
- }
- for _, v := range chartResult.Data {
- item := new(models.CygxChart)
- item.ChartId = v.ChartId
- item.PtagId = v.PtagId
- item.CtagId = v.CtagId
- item.Title = v.Title
- item.TitleEn = v.TitleEn
- item.CreateDateApi = time.Now()
- item.CreateDate = v.CreateDate
- item.PublishStatus = v.PublishStatus
- item.PtagName = v.Ptag.Name
- item.CtagName = v.Ctag.Name
- item.PtagNameTwo = v.PtagTwo.Name
- item.CtagNameTwo = v.CtagTwo.Name
- item.PtagIdTwo = v.PtagTwo.Id
- item.CtagIdTwo = v.CtagTwo.Id
- item.Cover = v.Cover
- item.Iframe = v.Iframe
- //如果没有就新增 有就更新
- if mapChartid[v.ChartId] == 0 {
- _, err := models.AddCygxChart(item)
- if err != nil {
- return err
- }
- } else {
- updateParams := make(map[string]interface{})
- updateParams["Title"] = v.Title
- updateParams["PtagId"] = v.PtagId
- updateParams["CtagId"] = v.CtagId
- updateParams["TitleEn"] = v.TitleEn
- updateParams["CreateDate"] = v.CreateDate
- updateParams["PublishStatus"] = v.PublishStatus
- updateParams["PtagName"] = v.Ptag.Name
- updateParams["CtagName"] = v.Ctag.Name
- updateParams["PtagNameTwo"] = v.PtagTwo.Name
- updateParams["CtagNameTwo"] = v.CtagTwo.Name
- updateParams["PtagIdTwo"] = v.PtagTwo.Id
- updateParams["CtagIdTwo"] = v.CtagTwo.Id
- updateParams["Cover"] = v.Cover
- updateParams["Iframe"] = v.Iframe
- whereParam := map[string]interface{}{"chart_id": v.ChartId}
- err = models.UpdateByExpr(models.CygxChart{}, whereParam, updateParams)
- if err != nil {
- return err
- }
- }
- }
- //策略平台图表,记录所有,的显示用户收藏使用
- url = utils.ApiUrl + "charts?take=100&skip=0" // 获取所有的图表链接
- body, err = PublicGetDate(url, authorization)
- if err != nil {
- return
- }
- err = json.Unmarshal(body, &chartResult)
- if err != nil {
- return err
- }
- for _, v := range chartResult.Data {
- item := new(models.CygxChartAll)
- item.ChartId = v.ChartId
- item.PtagId = v.PtagId
- item.CtagId = v.CtagId
- item.Title = v.Title
- item.TitleEn = v.TitleEn
- item.CreateDateApi = time.Now()
- item.CreateDate = v.CreateDate
- item.PublishStatus = v.PublishStatus
- item.PtagName = v.Ptag.Name
- item.CtagName = v.Ctag.Name
- item.PtagNameTwo = v.PtagTwo.Name
- item.CtagNameTwo = v.CtagTwo.Name
- item.PtagIdTwo = v.PtagTwo.Id
- item.CtagIdTwo = v.CtagTwo.Id
- item.Cover = v.Cover
- item.Iframe = v.Iframe
- //如果没有就新增 有就更新
- if mapAllChartid[v.ChartId] == 0 {
- _, err := models.AddCygxChartAll(item)
- if err != nil {
- return err
- }
- } else {
- updateParams := make(map[string]interface{})
- updateParams["Title"] = v.Title
- updateParams["PtagId"] = v.PtagId
- updateParams["CtagId"] = v.CtagId
- updateParams["TitleEn"] = v.TitleEn
- updateParams["CreateDate"] = v.CreateDate
- updateParams["PublishStatus"] = v.PublishStatus
- updateParams["PtagName"] = v.Ptag.Name
- updateParams["CtagName"] = v.Ctag.Name
- updateParams["PtagNameTwo"] = v.PtagTwo.Name
- updateParams["CtagNameTwo"] = v.CtagTwo.Name
- updateParams["PtagIdTwo"] = v.PtagTwo.Id
- updateParams["CtagIdTwo"] = v.CtagTwo.Id
- updateParams["Cover"] = v.Cover
- updateParams["Iframe"] = v.Iframe
- whereParam := map[string]interface{}{"chart_id": v.ChartId}
- err = models.UpdateByExpr(models.CygxChartAll{}, whereParam, updateParams)
- if err != nil {
- return err
- }
- }
- }
- return
- }
- //获取图表分类
- func GetChartPtagByApi() (items []*models.ChartPtagResp, err error) {
- defer func() {
- if err != nil {
- go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"失败提醒", "GetChartPtagByApi ErrMsg:"+err.Error(), utils.EmailSendToUsers)
- }
- }()
- url := utils.ApiUrl + "charts/ptag"
- authorization := utils.ApiAuthorization
- body, err := PublicGetDate(url, authorization)
- if err != nil {
- return
- }
- var chartResult models.ChartPtagResultApi
- err = json.Unmarshal(body, &chartResult)
- if err != nil {
- return
- }
- for _, v := range chartResult.Data {
- item := new(models.ChartPtagResp)
- item.PermissionName = v.ChartPermissionName
- item.ChartPermissionId = v.ChartPermissionId
- if len(v.Ctag) > 0 {
- for _, v2 := range v.Ctag {
- itemCtag := new(models.CtagResp)
- itemCtag.CtagId = v2.Id
- itemCtag.Name = v2.Name
- item.List = append(item.List, itemCtag)
- }
- }
- items = append(items, item)
- }
- return
- }
- //获取用户的Token
- func GetUserTokenByMobile(mobile string) (token string, err error) {
- //缓存校验
- cacheKey := fmt.Sprint("xygx_chart:chart_token:add:", "Mobile", mobile)
- ttlTime := utils.Rc.GetRedisTTL(cacheKey)
- if ttlTime > 0 {
- token, _ = utils.Rc.RedisString(cacheKey)
- }
- if token == "" {
- url := utils.ApiUrl + "auth/login"
- method := "POST"
- payload := strings.NewReader(`{
- "phone_number":"` + mobile + `",
- "password":"hz123456"}`)
- client := &nhttp.Client{}
- req, errReq := nhttp.NewRequest(method, url, payload)
- if errReq != nil {
- err = errReq
- return
- }
- req.Header.Add("Content-Type", "application/json")
- req.Header.Add("Cookie", "sessionid=naj5j5kl1jjynh7og1rsaxkl1vrsl829")
- res, errReq := client.Do(req)
- if errReq != nil {
- err = errReq
- return
- }
- defer res.Body.Close()
- body, errReq := ioutil.ReadAll(res.Body)
- if errReq != nil {
- err = errReq
- return
- }
- var chartResult models.ChartUserTokenResultApi
- errReq = json.Unmarshal(body, &chartResult)
- if errReq != nil {
- err = errReq
- return
- }
- token = chartResult.Data.AccessToken
- utils.Rc.Put(cacheKey, token, time.Hour*24)
- }
- return
- }
- //获取图表收藏
- func GetChartCollectionByApi(mobile string, take, skip int) (items []*models.HomeChartListResp, err error, total int) {
- defer func() {
- if err != nil {
- //go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"失败提醒", "GetChartPtagByApi ErrMsg:"+err.Error(), utils.EmailSendToUsers)
- }
- }()
- url := utils.ApiUrl + "charts/favorites?take=" + strconv.Itoa(take) + "&skip=" + strconv.Itoa(skip)
- authorization, err := GetUserTokenByMobile(mobile)
- if err != nil {
- return
- }
- authorization = "bearer " + authorization
- body, err := PublicGetDate(url, authorization)
- if err != nil {
- return
- }
- var chartResult models.ChartFavoritesResultApi
- err = json.Unmarshal(body, &chartResult)
- if err != nil {
- return
- }
- for _, v := range chartResult.Data {
- item := new(models.HomeChartListResp)
- item.ChartId = v.ChartId
- item.Title = v.ChartInfo.Title
- item.TitleEn = v.ChartInfo.TitleEn
- item.CreateDate = v.CreateDate
- item.PtagName = v.ChartInfo.Ptag.Name
- item.CtagName = v.ChartInfo.Ctag.Name
- item.BodyHtml = v.ChartInfo.Cover
- item.HttpUrl = utils.CHART_INFO_HTTP_URL + strconv.Itoa(v.ChartId)
- item.IsNeedJump = true
- items = append(items, item)
- }
- total = chartResult.Pagination.Total
- return
- }
- //判断策略平台是否已经添加过收藏
- func GetIsCollectionChart(mobile string, chartId int) (isCollection bool, err error) {
- //获取所有的收藏列表,进行比对看看是否收藏,调用三方接口详情没有是否收藏的字段
- list, err, _ := GetChartCollectionByApi(mobile, 9999, 0)
- if err != nil {
- return
- }
- for _, v := range list {
- if v.ChartId == chartId {
- isCollection = true
- }
- }
- return
- }
- //添加收藏
- func AddCollectionChart(mobile string, chartId int) (err error) {
- authorization, err := GetUserTokenByMobile(mobile)
- if err != nil {
- return
- }
- authorization = "bearer " + authorization
- url := utils.ApiUrl + "charts/favorites"
- method := "POST"
- payload := strings.NewReader(`{
- "chart_id":` + strconv.Itoa(chartId) + `
- }`)
- client := &nhttp.Client{}
- req, err := nhttp.NewRequest(method, url, payload)
- if err != nil {
- return
- }
- req.Header.Add("Authorization", authorization)
- req.Header.Add("Content-Type", "application/json")
- req.Header.Add("Cookie", "sessionid=naj5j5kl1jjynh7og1rsaxkl1vrsl829")
- res, err := client.Do(req)
- if err != nil {
- return
- }
- defer res.Body.Close()
- _, err = ioutil.ReadAll(res.Body)
- if err != nil {
- return
- }
- return
- }
- //移除收藏
- func DeleteCollectionChart(mobile string, chartId int) (err error) {
- authorization, err := GetUserTokenByMobile(mobile)
- if err != nil {
- return
- }
- authorization = "bearer " + authorization
- url := utils.ApiUrl + "charts/favorites/" + strconv.Itoa(chartId)
- method := "DELETE"
- client := &nhttp.Client{}
- req, err := nhttp.NewRequest(method, url, nil)
- if err != nil {
- return
- }
- req.Header.Add("Authorization", authorization)
- req.Header.Add("Cookie", "sessionid=naj5j5kl1jjynh7og1rsaxkl1vrsl829")
- res, err := client.Do(req)
- if err != nil {
- return
- }
- defer res.Body.Close()
- _, err = ioutil.ReadAll(res.Body)
- if err != nil {
- return
- }
- return
- }
- func DoCompany() {
- //listCollect, err := models.GetCygxArticleCollectList()
- //if err != nil {
- // fmt.Println("GetAddCygxArticleCollectList ,Err" + err.Error())
- //}
- //for _, v := range listCollect {
- // user, err := models.GetWxUserItemByUserId(v.UserId)
- // if err != nil && err.Error() != utils.ErrNoRow() {
- // fmt.Println("GetWxUserItemByUserId ,Err" + err.Error())
- // }
- // if user != nil {
- // fmt.Println(user.RealName)
- // err = models.UpdateCygxArticleCollect(user)
- // if err != nil {
- // fmt.Println("UpdateCygxArticleCollect ,Err" + err.Error())
- // }
- // }
- //}
- //listCollect, err := models.GetCygxSearchKeyWordList()
- //if err != nil {
- // fmt.Println("GetAddCygxArticleCollectList ,Err" + err.Error())
- //}
- //for _, v := range listCollect {
- // user, err := models.GetWxUserItemByUserId(v.UserId)
- // if err != nil && err.Error() != utils.ErrNoRow() {
- // fmt.Println("GetWxUserItemByUserId ,Err" + err.Error())
- // }
- // if user != nil {
- // fmt.Println(user.RealName)
- // err = models.UpdateCygxSearchKeyWord(user)
- // if err != nil {
- // fmt.Println("UpdateCygxArticleCollect ,Err" + err.Error())
- // }
- // }
- //}
- //var condition string
- ////updateTime := time.Now().Add(-time.Hour * 25).Format(utils.FormatDateTime)
- //condition = ` AND real_name = '' `
- //listArticlePv, err := models.GetArticleHistoryRecordAllByMobileList(condition)
- //if err != nil {
- // fmt.Println("GetArticleHistoryRecordAllByMobileList ,Err" + err.Error())
- //}
- //fmt.Println("长度", len(listArticlePv))
- //for k, v := range listArticlePv {
- // if v.Mobile != "" {
- // user, err := models.GetWxUserItemByMobile(v.Mobile)
- // if err != nil && err.Error() != utils.ErrNoRow() {
- // fmt.Println("GetWxUserItemByUserId ,Err" + err.Error())
- // }
- // if user != nil {
- // fmt.Println(user.RealName, k)
- // err = models.UpdateCygxArticleHistoryRecordAll(user)
- // if err != nil {
- // fmt.Println("UpdateCygxArticleCollect ,Err" + err.Error())
- // }
- // }
- // }
- //}
- //listIndustryFllow, err := models.GetCygxIndustryFllowList("")
- //if err != nil {
- // fmt.Println("GetArticleDepartmentFollowByMobileList ,Err" + err.Error())
- //}
- //fmt.Println("长度", len(listIndustryFllow))
- //for k, v := range listIndustryFllow {
- // if v.Mobile != "" {
- // user, err := models.GetWxUserItemByMobile(v.Mobile)
- // if err != nil && err.Error() != utils.ErrNoRow() {
- // fmt.Println("GetWxUserItemByUserId ,Err" + err.Error())
- // }
- // if user != nil {
- // fmt.Println(user.RealName, k)
- // err = models.UpdateCygxIndustryFllow(user)
- // if err != nil {
- // fmt.Println("UpdateCygxIndustryFllow ,Err" + err.Error())
- // }
- // }
- // }
- //}
- //
- //lisDepartmentF, err := models.GetArticleDepartmentFollowByMobileList("")
- //if err != nil {
- // fmt.Println("GetArticleDepartmentFollowByMobileList ,Err" + err.Error())
- //}
- //fmt.Println("长度", len(lisDepartmentF))
- //for k, v := range lisDepartmentF {
- // if v.Mobile != "" {
- // user, err := models.GetWxUserItemByMobile(v.Mobile)
- // if err != nil && err.Error() != utils.ErrNoRow() {
- // fmt.Println("GetWxUserItemByUserId ,Err" + err.Error())
- // }
- // if user != nil {
- // fmt.Println(user.RealName, k)
- // err = models.UpdateCygxArticleDepartmentFollow(user)
- // if err != nil {
- // fmt.Println("UpdateCygxArticleDepartmentFollow ,Err" + err.Error())
- // }
- // }
- // }
- //}
- //listChartCollect, err := models.GetCygxChartCollectByMobileList()
- //if err != nil {
- // fmt.Println("GetCygxChartCollectByMobileList ,Err" + err.Error())
- //}
- //fmt.Println("长度", len(listChartCollect))
- //for k, v := range listChartCollect {
- // if v.Mobile != "" {
- // user, err := models.GetWxUserItemByMobile(v.Mobile)
- // if err != nil && err.Error() != utils.ErrNoRow() {
- // fmt.Println("GetWxUserItemByUserId ,Err" + err.Error())
- // }
- // if user != nil {
- // fmt.Println(user.RealName, k)
- // err = models.UpdateCygxChartCollect(user)
- // if err != nil {
- // fmt.Println("UpdateCygxChartCollect ,Err" + err.Error())
- // }
- // }
- // }
- //}
- fmt.Println("end")
- }
|