package adser import ( "91porn-server/models/v/dailytaskmod" "encoding/json" "errors" "fmt" "net/http" "time" "91porn-server/app/appg" "91porn-server/app/service/taskser" "91porn-server/common/constant/redisconst" "91porn-server/common/httputil" "91porn-server/common/log" "91porn-server/models/commod" "91porn-server/models/l/adsclicklogmod" "91porn-server/models/v/usermod" "go.mongodb.org/mongo-driver/bson/primitive" ) type AdvertiseReq struct { AppId int32 `form:"appId" json:"appId" binding:"required"` // 应用id LocId *int32 `form:"locId" json:"locId" binding:"omitempty"` // 位置id UserId *uint64 `form:"userId" json:"userId" binding:"omitempty"` // 用户ID IP *string `form:"ip" json:"ip" binding:"omitempty"` // 用户IP } type AdvertiseRes struct { AdvertiseList []AdvertiseInfo `json:"advertiseList"` // 广告列表 } type AdsClickReq struct { Id string `from:"id" json:"id" binding:"required"` // 广告id AppId int32 `from:"appId" json:"appId" binding:"required"` // 应用id ChannelCode string `from:"channelCode" json:"channelCode" binding:"channelCode"` } type AdsClickResp struct { Code int64 `from:"code" json:"code"` // 错误骂 } // 外部广告信息 type AdvertiseInfo struct { Id int64 `json:"id"` // 广告id Title string `json:"title"` // 广告标题 CoverImg string `json:"coverImg"` // 封面 LocId int32 `json:"locId"` // 位置id JumpType int32 `json:"jumpType"` // 跳转方式 0:外部浏览器跳转 1:内部浏览器跳转 2:app内部跳转 Link string `json:"link"` // 链接地址 Sort int64 `json:"sort"` // 排序 } func AdvertiseThreeServer(userId uint64, ip string) (*AdvertiseRes, error) { str, err := appg.Redis.Get(redisconst.CenterAdvertiseCache) if err != nil { log.Warn(fmt.Sprintf("用户ID:%d;IP:%s;缓存获取广告列表信息异常:%v", userId, ip, err)) } var resp AdvertiseRes if str != nil { if err = json.Unmarshal([]byte(*str), &resp); err == nil { return &resp, nil } log.Warn(fmt.Sprintf("用户ID:%d;IP:%s;解析缓存数据异常:%v", userId, ip, err)) } req := AdvertiseReq{ AppId: commod.KFK_APPID, IP: &ip, } if userId > 0 { req.UserId = &userId } url := appg.Conf.URL.ProductUrl + "/api/stat/ads/get" bodyStr, _ := json.Marshal(req) code, err := httputil.DefaultClientPostJsonWithResp(&resp, url, nil, bodyStr) if err != nil { log.Error("AdvertiseThreeServer POSTWithJResp ", log.Any("url", url), log.E(err)) return nil, err } if code != http.StatusOK { log.Error("AdvertiseThreeServer response status ", log.Any("code", code)) return nil, fmt.Errorf("response status err") } data, err := json.Marshal(&resp) if err != nil { log.Warn(fmt.Sprintf("用户ID:%d;IP:%s;json序列化缓存数据异常:%v", userId, ip, err)) return &resp, nil } if err = appg.Redis.Set(redisconst.CenterAdvertiseCache, data, 45*time.Second); err != nil { log.Warn(fmt.Sprintf("用户ID:%d;IP:%s;保存缓存数据异常:%v", userId, ip, err)) } // log.Info("AdvertiseThreeServer response", log.Any("resp", resp)) return &resp, nil } func AdvertiseClickThreeServer(advertiseId string, uid uint64) error { //查询用户信息 u, err := usermod.FindUserByUID(uid) if err != nil { return err } if u.DistrictCode == "" { u.DistrictCode = "system" } req := AdsClickReq{ AppId: commod.KFK_APPID, Id: advertiseId, ChannelCode: u.DistrictCode, } resp := AdsClickResp{} url := appg.Conf.URL.ProductUrl + "/api/product/ads/dayClick" bodyStr, err := json.Marshal(req) if err != nil { log.Info(fmt.Sprintf("AdvertiseClickThreeServer json.Marshal is fail error:%+v/data:%+v", err, req)) return err } code, err := httputil.DefaultClientPostJsonWithResp(&resp, url, nil, bodyStr) if err != nil { log.Error("AdvertiseClickThreeServer POSTWithJResp ", log.Any("url", url), log.E(err)) return err } if code != http.StatusOK { log.Error("AdvertiseClickThreeServer response status ", log.Any("code", code)) return fmt.Errorf("response status err") } return nil } func AdvertiseNavigate(advertiseId string, uid uint64, ua, logType string) error { advId, err := primitive.ObjectIDFromHex(advertiseId) if err != nil { return err } if advId.IsZero() { return errors.New("empty advid") } header := map[string]string{ "log-id": advertiseId, "User-Agent": ua, "log-type": logType, } targetUrl := appg.Conf.URL.NavigateUrl + "/api/application/dHH9h0Kp1074" if _, _, err = httputil.DefaultClientGetBytes(targetUrl, header, nil); err != nil { log.Error("navigate report fail", log.E(err)) return err } if err := adsclicklogmod.AddClick(uid, advId, logType, 1); err != nil { log.Error("adclick fail", log.E(err)) return err } go func() { _ = taskser.CompleteDailyTask(nil, uid, dailytaskmod.DailyTaskTypeAdsClick) }() return nil }