ads_handler.go 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655
  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "github.com/gin-gonic/gin"
  6. "math/rand"
  7. "miads/adslib"
  8. "miads/adslib/addata"
  9. "miads/adslib/ads_checker"
  10. "miads/adslib/city"
  11. "miads/adslib/device"
  12. "miads/adslib/encrypt"
  13. "miads/adslib/graylog"
  14. "miads/adslib/ip2region"
  15. "miads/adslib/redis_data"
  16. "miads/adslib/utils"
  17. "net/url"
  18. "strconv"
  19. "strings"
  20. "time"
  21. log "github.com/sirupsen/logrus"
  22. )
  23. type Response struct {
  24. Result int `json:"result"`
  25. Msg string `json:"msg"`
  26. AdsResult string `json:"ads_result"`
  27. addata.AdData
  28. }
  29. func adsHandler(c *gin.Context) {
  30. c.Header("Content-Type", "application/json")
  31. request := adslib.Request{}
  32. request.Parse(c)
  33. log.Printf("%+v", request)
  34. // 放这里主要为了拿到reqeust id做日志记录
  35. dspInfo := utils.DspParam{}
  36. dspInfo.Init()
  37. advertiser := "xiaomi"
  38. uaClient := request.UaClient
  39. // 获取ua
  40. if uaClient == "" {
  41. uaClient = c.GetHeader("User-Agent")
  42. if uaClient == "" {
  43. uaClient = "Dalvik/2.1.0 (Linux; U; Android 6.0.1; MI 4LTE MIUI/6.9.1)"
  44. }
  45. }
  46. // 获取ip
  47. checkReqSourceFlag, err := ads_checker.CheckReqSource(request.ReqSource)
  48. if err != nil {
  49. c.String(404, "check req source failed: %s", err)
  50. return
  51. }
  52. ip := ""
  53. if checkReqSourceFlag {
  54. ip = request.ReqSourceIp
  55. }
  56. if ip == "" {
  57. ip = c.GetHeader("X-Forwarded-For")
  58. }
  59. if ip == "" {
  60. ip = c.GetHeader("X-Real-IP")
  61. }
  62. if ip == "" {
  63. ip = c.Request.RemoteAddr
  64. }
  65. if strings.Index(ip, ",") != -1 {
  66. ip = strings.Split(ip, ",")[0]
  67. }
  68. ipInfo, err := ip2region.Ip2Region(ip)
  69. if err != nil {
  70. c.String(404, "ip 2 region failed: %s", err)
  71. return
  72. }
  73. fmt.Printf("ip 2 region: %s, %+v\n", ip, ipInfo)
  74. // 上報
  75. if request.NewAdsFlag == 1 {
  76. grayLogData := struct {
  77. Ip string
  78. Imei string
  79. Model string
  80. NetworkType int `json:"network_type"`
  81. Province string
  82. City string
  83. ScreeSize string `json:"screen_size"`
  84. Ua string
  85. Brand string
  86. Androidid string `json:"android_id"`
  87. ShortMessage string `json:"short_message"`
  88. ReqSource string `json:"req_source"`
  89. }{
  90. Ip: ip,
  91. Imei: request.Imei,
  92. Model: request.Model,
  93. NetworkType: request.NetworkType,
  94. Province: ipInfo.Province,
  95. City: ipInfo.City,
  96. ScreeSize: request.ScreenSize,
  97. Ua: request.UaClient,
  98. Brand: request.Brand,
  99. Androidid: request.Androidid,
  100. ShortMessage: "ads_api_log",
  101. ReqSource: "zhiku",
  102. }
  103. graylog.Log(grayLogData)
  104. }
  105. // 定制city code
  106. cityCode, err := city.GetCityCode(ipInfo.City)
  107. log.WithField("request_id", dspInfo.RequestId).Infof("got city code: %d", cityCode)
  108. if err != nil {
  109. c.String(404, "get city code failed: %s", err)
  110. return
  111. }
  112. freqControlInterval := 600
  113. // 深圳,东莞强行用600秒控制, 其他地区从配置读取
  114. if strings.Index(ipInfo.City, "深圳") == -1 && strings.Index(ipInfo.City, "东莞") == -1 {
  115. // 频率控制
  116. freqControlConf, err := redis_data.GetFreqControlConf(request.ReqSource)
  117. if err != nil {
  118. log.WithField("request_id", dspInfo.RequestId).Errorf("get freq control conf failed: %s", err)
  119. c.String(404, "get freq control conf failed: %s", err)
  120. return
  121. }
  122. log.WithField("request_id", dspInfo.RequestId).Infof("freq control conf: %+v", freqControlConf)
  123. hour, _ := strconv.Atoi(time.Now().Format("01"))
  124. tmpControlInterval, ok := freqControlConf.GetControlTime(hour)
  125. if ok {
  126. freqControlInterval = tmpControlInterval
  127. }
  128. }
  129. lastReqTime, err := device.GetIpReqTime(ip)
  130. if err != nil {
  131. fmt.Println(err)
  132. c.String(404, "get last req time failed: %s", err)
  133. return
  134. }
  135. needControl := false
  136. if request.ReqSource != "wzb_h5" && lastReqTime != 0 && time.Now().Unix()-lastReqTime < int64(freqControlInterval) {
  137. // 需要凭空
  138. log.WithField("request_id", dspInfo.RequestId).Infof("need control: %d", lastReqTime)
  139. needControl = true
  140. device.SetAdsTagLog(advertiser, request.ReqSource, "DS_FREQ_0", cityCode)
  141. } else {
  142. device.SetIpReqTime(ip, time.Now().Unix())
  143. device.SetAdsTagLog(advertiser, request.ReqSource, "DS_FREQ_1", cityCode)
  144. }
  145. //### 检查是否是ip黑名单
  146. isIpBlack, err := ads_checker.CheckBlackIp(ip)
  147. if err != nil {
  148. log.WithField("request_id", dspInfo.RequestId).Errorf("check black ip failed: %s", err)
  149. c.String(404, "check black ip failed: %s", err)
  150. return
  151. }
  152. if isIpBlack {
  153. graylog.LogApi("black_ip_list", ip, "", request.ReqSource)
  154. device.SetAdsTagLog(advertiser, request.ReqSource, "DS_BLACK_IP", cityCode)
  155. }
  156. // 获取渠道的黑白性
  157. reqChannelFlag, err := redis_data.GetChannelFlag(request.ReqSource, "ads_req")
  158. if err != nil {
  159. log.WithField("request_id", dspInfo.RequestId).Errorf("get req channel flag failed: %s", err)
  160. c.String(404, "get req channel flag failed: %s", err)
  161. return
  162. }
  163. flowWeight := reqChannelFlag.Weigth
  164. flowRandomNum := rand.Intn(100)
  165. flowFlag := 0 // 有效的百分比
  166. if flowRandomNum <= flowWeight {
  167. flowFlag = 1
  168. }
  169. if reqChannelFlag.ChannelFlag == 1 {
  170. device.SetAdsTagLog(advertiser, request.ReqSource, fmt.Sprintf("DS_REQ_SOURCE_{%d}", reqChannelFlag.ChannelFlag), cityCode)
  171. }
  172. // 替换成小米的设备
  173. replaceFlag := 0
  174. imei := request.Imei
  175. sendPhoneType := 0
  176. userFlag := 0
  177. originImei := ""
  178. // 检查是否是小米的设备
  179. specialDeviceNum := 0
  180. if request.ReqSource == "wzb_h5" {
  181. specialDeviceNum = 6 // wzb_h5渠道只綁定一个imei, 具体愿意不知道
  182. }
  183. if request.ReqSource == "moyuntv" || (!request.IsMiDevice() &&
  184. (!isIpBlack && !needControl && reqChannelFlag.ChannelFlag == 1) && flowFlag == 1) {
  185. randomNum := 3
  186. deviceConf, realRedisKey, err := device.GetMiDeviceConf(ip, randomNum, specialDeviceNum)
  187. if err != nil {
  188. log.WithField("request_id", dspInfo.RequestId).Errorf("get mi device conf failed: %s", err)
  189. c.String(404, "get mi device conf failed: %s", err)
  190. return
  191. }
  192. if deviceConf != nil {
  193. log.WithField("request_id", dspInfo.RequestId).Infof("use cache imei: %+v", deviceConf)
  194. device.SetAdsTagLog(advertiser, request.ReqSource, "DS_CACHE", cityCode) // 通过缓存请求
  195. imei = deviceConf.Imei
  196. uaClient = deviceConf.Ua
  197. originImei = deviceConf.OriginImei
  198. if imei != "" {
  199. replaceFlag = 1
  200. userFlag = 1
  201. }
  202. } else {
  203. // 先从对应的城市找,找不到在走原来的逻辑
  204. // 上报事件
  205. graylog.LogApi("city_search_city_code", strconv.Itoa(cityCode), ipInfo.City, request.ReqSource)
  206. device.SetAdsTagLog(advertiser, request.ReqSource, "DS_CODE_REQ", cityCode) // 可做城市替换
  207. if cityCode != 0 {
  208. device.SetAdsTagLog(advertiser, request.ReqSource, "DS_CODE", cityCode) // 替换成功
  209. fakeImei := ""
  210. fakeDeviceConf, leftCnt, err := device.GetDailyFakeDeviceConfByCityCode(cityCode)
  211. if err != nil {
  212. log.WithField("request_id", dspInfo.RequestId).Errorf("get device conf failed: %s", err)
  213. c.String(404, "get device conf failed: %s", err)
  214. return
  215. }
  216. // 0 没有获取到, 1 获取到了
  217. gotCityFakeDeviceConfFlag := 0
  218. replaceUa := ""
  219. if fakeDeviceConf != nil {
  220. device.SetAdsTagLog(advertiser, request.ReqSource, "DS_REPL", cityCode)
  221. err := device.SetMiDeviceConf(realRedisKey, *fakeDeviceConf)
  222. if err != nil {
  223. log.WithField("request_id", dspInfo.RequestId).Errorf("set mi device conf failed: %s", err)
  224. }
  225. imei = fakeDeviceConf.Imei
  226. uaClient = fakeDeviceConf.Ua
  227. originImei = fakeDeviceConf.OriginImei
  228. fakeImei = fakeDeviceConf.Imei
  229. if fakeDeviceConf.Imei != "" {
  230. gotCityFakeDeviceConfFlag = 1
  231. userFlag = 2
  232. replaceFlag = 1
  233. }
  234. }
  235. grayLogData := struct {
  236. ReqSource string `json:"req_source"`
  237. OldImei string `json:"old_imei"`
  238. NewImei string `json:"new_imei"`
  239. IP string `json:"ip"`
  240. City string `json:"city"`
  241. CityCode int `json:"city_code"`
  242. Province string
  243. HitFlag int `json:"hit_flag"`
  244. LeftCnt int `json:"left_cnt"`
  245. ReplaceUa string `json:"replace_ua"`
  246. ShortMessage string `json:"short_message"`
  247. }{
  248. ReqSource: request.ReqSource,
  249. OldImei: request.Imei,
  250. NewImei: fakeImei,
  251. IP: ip,
  252. City: ipInfo.City,
  253. CityCode: cityCode,
  254. Province: ipInfo.Province,
  255. HitFlag: gotCityFakeDeviceConfFlag,
  256. LeftCnt: leftCnt,
  257. ReplaceUa: replaceUa,
  258. ShortMessage: "",
  259. }
  260. // 进行日志的上报
  261. graylog.Log(grayLogData)
  262. }
  263. }
  264. // 解码ua_client
  265. uaClient, _ = url.QueryUnescape(uaClient)
  266. } else if request.IsMiDevice() {
  267. log.WithField("request_id", dspInfo.RequestId).Info("real mi")
  268. device.SetAdsTagLog(advertiser, request.ReqSource, "DS_MI_REAL", cityCode) // 是小米设备
  269. userFlag = 3
  270. sendPhoneType = 1
  271. }
  272. // 检查下,替换后的设备是否是黑名单, 再次检查, 防止放入cache后, 新增了黑名单
  273. checkImei := imei
  274. // 替换过的imei从redis取的, 都是md5后的, 不需要再md5
  275. if replaceFlag != 1 {
  276. checkImei = utils.Md5(imei)
  277. }
  278. isBlackImei, err := device.CheckIsBlackImei(checkImei)
  279. if err != nil {
  280. log.WithField("request_id", dspInfo.RequestId).Errorf("get device conf failed: %s", err)
  281. c.String(404, "get device conf failed: %s", err)
  282. return
  283. }
  284. log.WithField("request_id", dspInfo.RequestId).Tracef("replace flag: %d", replaceFlag)
  285. if isBlackImei {
  286. if replaceFlag == 1 {
  287. log.WithField("request_id", dspInfo.RequestId).Trace("replace")
  288. device.SetAdsTagLog(advertiser, request.ReqSource, "DS_BLACK_IMEI_REPLACE", cityCode)
  289. } else {
  290. log.WithField("request_id", dspInfo.RequestId).Trace("not replace")
  291. device.SetAdsTagLog(advertiser, request.ReqSource, "DS_BLACK_IMEI", cityCode)
  292. }
  293. }
  294. // 组装公共的dsp_info
  295. dspInfo.DspCityCode = cityCode
  296. dspInfo.Imei = imei
  297. dspInfo.OriginImei = originImei
  298. dspInfo.OsVersion = request.OsVersion
  299. dspInfo.Mac = strings.ToUpper(request.Mac)
  300. dspInfo.OriginMac = request.Mac
  301. dspInfo.Idfa = request.Idfa
  302. dspInfo.Model = request.Model
  303. dspInfo.Brand = request.Brand
  304. dspInfo.ScreenSize = request.ScreenSize
  305. dspInfo.NetworkType = request.NetworkType
  306. dspInfo.Androidid = request.Androidid
  307. dspInfo.Platform = request.Platform
  308. dspInfo.Ip = ip
  309. dspInfo.Ua = uaClient
  310. dspInfo.City = ipInfo.City
  311. dspInfo.Province = ipInfo.Province
  312. dspInfo.UaOrigin = request.UaClient
  313. dspInfo.ReqSource = request.ReqSource
  314. if request.IsMiDevice() {
  315. dspInfo.RealMiFlag = 1
  316. }
  317. dspInfo.ReplaceFlag = replaceFlag
  318. dspInfo.RealReqSource = request.ReqSource
  319. dspInfo.SendPhoneType = sendPhoneType
  320. md5Imei := imei
  321. if dspInfo.OriginImei != "" {
  322. md5Imei = utils.Md5(dspInfo.OriginImei)
  323. }
  324. dspInfo.RealMd5Imei = md5Imei
  325. log.WithField("request_id", dspInfo.RequestId).Infof("dsp: %+v", dspInfo)
  326. //ads_item = None
  327. //xiaomi_response = []
  328. hour, _ := strconv.Atoi(time.Now().Format("01"))
  329. canRequest := true
  330. if 0 <= hour && hour <= 1 {
  331. randNum := rand.Intn(100)
  332. if randNum > 5 {
  333. canRequest = false
  334. }
  335. }
  336. log.WithField("request_id", dspInfo.RequestId).Infof("can request: %t", canRequest)
  337. var adData *addata.AdData
  338. // 非频率控制,设备黑名单,ip黑名单,白名单渠道,并且替换了小米设备的
  339. if canRequest && !isBlackImei && reqChannelFlag.ChannelFlag == 1 && flowFlag == 1 && !isIpBlack && advertiser == "xiaomi" && request.ReqSource != "" && !needControl && (replaceFlag == 1 || request.IsMiDevice()) {
  340. adData, err = addata.GetAdsInfos(&dspInfo, advertiser)
  341. if err != nil {
  342. log.WithField("request_id", dspInfo.RequestId).Errorf("get mi ad data failed: %s", err)
  343. c.String(404, "get mi ad data failed: %s", err)
  344. return
  345. }
  346. log.WithField("request_id", dspInfo.RequestId).Infof("get mi ads data: %+v", adData)
  347. }
  348. canMixFlag := 1
  349. xiaomiResponseFlag := 0
  350. response := Response{}
  351. response.Msg = "ok"
  352. if adData != nil && len(adData.TargetAddition) > 1 {
  353. log.WithField("request_id", dspInfo.RequestId).Infof("add target js order")
  354. xiaomiResponseFlag = 1
  355. realTarget := ""
  356. if adData.Target != "" {
  357. md5Skip := utils.Md5(adData.Target)
  358. conf := adslib.GetConf()
  359. realTarget = conf.Host + fmt.Sprintf("?action=LOADING&req_source=%s&advertiser=video&skip=%s&brand=%s&request_id=%s&skip_other=%s",
  360. dspInfo.ReqSource, "", dspInfo.Brand, dspInfo.RequestId, md5Skip)
  361. // 塞入Redis中
  362. redis_data.SetSkipInfo(md5Skip, adData.Target)
  363. }
  364. adData.Target = realTarget
  365. adData.JsOrderId, err = redis_data.GetMinScriptOrderByAdv(advertiser)
  366. if err != nil {
  367. log.WithField("request_id", dspInfo.RequestId).Errorf("get min script order by adv failed: %s", err)
  368. c.String(404, "get min script order by adv failed: %s", err)
  369. return
  370. }
  371. log.WithField("request_id", dspInfo.RequestId).Infof("got min script order: %d", adData.JsOrderId)
  372. adData.UserAgent = uaClient
  373. if dspInfo.ReqSource == "kuxin" && adData.Target != "" {
  374. adData.DpReport = fmt.Sprintf("%s?action=DP_CLICK&advertiser=video&req_source=%s", adslib.GetConf().HostIos, dspInfo.ReqSource)
  375. adData.Dp = adslib.GetConf().DpTest
  376. }
  377. if dspInfo.SendPhoneType == 0 {
  378. device.SetAdsTagLog(advertiser, dspInfo.ReqSource, "ADS_GET", cityCode) // 广告获取成功
  379. } else if dspInfo.SendPhoneType == 1 {
  380. device.SetAdsTagLog(advertiser, dspInfo.ReqSource, "ADS_GET_1", cityCode) // 广告获取成功
  381. }
  382. device.SetAdsTagLog(advertiser, dspInfo.ReqSource, fmt.Sprintf("ADS_USER_%d", userFlag), cityCode) // 广告类型,缓存,新用户,小米手机
  383. // // 判断这个渠道是否要去融合和融合的流量占比
  384. // mixChannelFlag, err := redis_data.GetChannelFlag(dspInfo.ReqSource, "ads_mix")
  385. // if err != nil {
  386. // log.WithField("request_id", dspInfo.RequestId).Errorf("get device conf failed: %s", err)
  387. // c.String(404, "get device conf failed: %s", err)
  388. // return
  389. // }
  390. // flowRandomNum = rand.Intn(100)
  391. //isOverFlow := false
  392. //if flowRandomNum > mixChannelFlag.Weigth {
  393. // isOverFlow = true
  394. //}
  395. //if mixChannelFlag.ChannelFlag != 1 || isOverFlow {
  396. // //extra_infos = []
  397. //}
  398. // 组装返回去的action
  399. serverActionResponse := map[string]int{"server_view": 0, "server_click": 0, "server_close": 0, "server_video_finish": 0, "server_video_timer": 0}
  400. for _, targetInfo := range adData.TargetAddition {
  401. if targetInfo.Type == "VIEW" {
  402. serverActionResponse["server_view"] = 1
  403. redis_data.SetReqSourceView(advertiser, dspInfo.ReqSource, "xiafa")
  404. } else if targetInfo.Type == "CLICK" {
  405. serverActionResponse["server_click"] = 1
  406. } else if targetInfo.Type == "CLOSE" {
  407. serverActionResponse["server_close"] = 1
  408. } else if targetInfo.Type == "VIDEO_FINISH" {
  409. serverActionResponse["server_video_finish"] = 1
  410. } else if targetInfo.Type == "VIDEO_TIMER" {
  411. serverActionResponse["server_video_timer"] = 1
  412. }
  413. }
  414. log.WithField("request_id", dspInfo.RequestId).Infof("server action response: %+v", serverActionResponse)
  415. // 增加跟随订单
  416. if len(adData.TargetAddition) == 2 && serverActionResponse["server_video_finish"] == 1 && (dspInfo.ReqSource == "kuxin" || dspInfo.ReqSource == "zhiku") {
  417. log.WithField("request_id", dspInfo.RequestId).Infof("add more order")
  418. adData, err = addata.CombineOrderBy(adData, &dspInfo)
  419. if err != nil {
  420. log.WithField("request_id", dspInfo.RequestId).Errorf("combine order failed: %s", err)
  421. c.String(404, "combine order failed: %s", err.Error())
  422. return
  423. }
  424. log.WithField("request_id", dspInfo.RequestId).Infof("get custom order: %+v", adData)
  425. }
  426. //# 检查最后是否有click
  427. log.WithField("request_id", dspInfo.RequestId).Infof("check ads item new: %+v", adData)
  428. for _, targetAddition := range adData.TargetAddition {
  429. if targetAddition.Type == "CLICK" || targetAddition.Type == "VIDEO_TIMER" {
  430. log.WithField("request_id", dspInfo.RequestId).Infof("can't mix, targetAddition: %+v", targetAddition)
  431. canMixFlag = 0
  432. break
  433. }
  434. }
  435. if request.NewAdsFlag == 0 {
  436. response.AdData = *adData
  437. log.WithField("request_id", dspInfo.RequestId).Infof("update response: +%v", response)
  438. } else {
  439. jsonBytes, err := json.Marshal(*adData)
  440. if err != nil {
  441. c.String(404, "marshal AdData failed: %s", err.Error())
  442. return
  443. }
  444. encryptData, _ := encrypt.Encrypt(jsonBytes, []byte(adslib.GetConf().SecretKey))
  445. response.AdsResult = encryptData
  446. }
  447. }
  448. log.WithField("request_id", dspInfo.RequestId).Infof("can mix: %d, xiaomi rsp flag: %d", canMixFlag, xiaomiResponseFlag)
  449. if canMixFlag == 1 {
  450. response.Result = 2
  451. response.Msg = "no ads"
  452. if xiaomiResponseFlag == 1 {
  453. response.Result = 0
  454. response.Msg = "ok"
  455. }
  456. // 增加打底广告
  457. if (dspInfo.ReqSource == "kuxin" || dspInfo.ReqSource == "zhiku") && !needControl && !isBlackImei {
  458. shortMessage := "kong"
  459. // 先跑定投
  460. customAdData, err := addata.GetCustomAdsInfos(&dspInfo, 0, 0, xiaomiResponseFlag)
  461. if err != nil {
  462. c.String(404, "get custom ads info failed: %s", err.Error())
  463. return
  464. }
  465. log.WithField("request_id", dspInfo.RequestId).Infof("dingtou: %+v", customAdData)
  466. // 定投没有就跑其他的
  467. if customAdData == nil {
  468. customAdData, err = addata.GetCustomAdsInfos(&dspInfo, 0, 1, xiaomiResponseFlag)
  469. if err != nil {
  470. c.String(404, "get fix custom ads info failed: %s", err.Error())
  471. return
  472. }
  473. }
  474. device.SetAdsTagLog(advertiser, dspInfo.ReqSource, "ADS_ZIYOU_GET", cityCode) // 广告获取成功
  475. if customAdData != nil {
  476. shortMessage = "ads_tt_thirds"
  477. }
  478. if customAdData != nil && (shortMessage == "ads_vast_response" || shortMessage == "ads_tt_thirds") {
  479. adDataBytes, err := json.Marshal(*customAdData)
  480. if err != nil {
  481. log.WithField("request_id", dspInfo.RequestId).Errorf("marshal adData failed: %s\n", err)
  482. }
  483. grayLogData := struct {
  484. Ip string
  485. ShortMessage string `json:"short_message"`
  486. ReqSource string `json:"req_source"`
  487. ResponseVast string `json:"response_vast"`
  488. OrderName string `json:"order_name"`
  489. Advertiser string `json:"advertiser"`
  490. }{
  491. Ip: ip,
  492. ShortMessage: "ads_api_log",
  493. ReqSource: "zhiku",
  494. ResponseVast: string(adDataBytes),
  495. OrderName: customAdData.OrderName,
  496. Advertiser: advertiser,
  497. }
  498. graylog.Log(grayLogData)
  499. }
  500. if shortMessage == "ads_tt_thirds" {
  501. // 组装最后的信息
  502. lastAdData := addata.AdData{}
  503. rspTargetAddition := make([]addata.AdAction, 0, 50)
  504. // 当小米有填充的时候
  505. if len(response.TargetAddition) != 0 && len(customAdData.TargetAddition) != 0 {
  506. rspTargetAddition = response.TargetAddition
  507. lastAdData.UserAgent = response.UserAgent
  508. // 当只有view的时候
  509. if len(customAdData.TargetAddition) == 1 {
  510. for _, targetUrl := range customAdData.TargetAddition[0].Urls {
  511. rspTargetAddition[0].Urls = append(rspTargetAddition[0].Urls, targetUrl)
  512. }
  513. } else {
  514. // 当自由订单有view和click的时候
  515. for _, targetUrl := range customAdData.TargetAddition[0].Urls {
  516. rspTargetAddition[0].Urls = append(rspTargetAddition[0].Urls, targetUrl)
  517. }
  518. for _, targetUrl := range customAdData.TargetAddition[1].Urls {
  519. rspTargetAddition[1].Urls = append(rspTargetAddition[1].Urls, targetUrl)
  520. }
  521. rspTargetAddition[1].Type = "CLICK"
  522. }
  523. lastAdData.Target = customAdData.Target
  524. lastAdData.JsOrderId = customAdData.JsOrderId
  525. lastAdData.TargetAddition = rspTargetAddition
  526. lastAdData.DpReport = response.DpReport
  527. lastAdData.Dp = response.Dp
  528. adDataBytes, err := json.Marshal(lastAdData)
  529. if err != nil {
  530. log.WithField("request_id", dspInfo.RequestId).Errorf("marshal adData failed: %s", err)
  531. }
  532. grayLogData := struct {
  533. ShortMessage string `json:"short_message"`
  534. ResponseVast string `json:"response_vast"`
  535. }{
  536. ShortMessage: "third_mix_xiaomi",
  537. ResponseVast: string(adDataBytes),
  538. }
  539. graylog.Log(grayLogData)
  540. log.WithField("request_id", dspInfo.RequestId).Infof("replace addata: %+v", lastAdData)
  541. customAdData = &lastAdData
  542. }
  543. }
  544. if customAdData != nil {
  545. device.SetAdsTagLog(advertiser, dspInfo.ReqSource, "ADS_ZIYOU_HAVE", cityCode) // 广告获取成功
  546. if dspInfo.ReqSource == "zhiku" {
  547. response.Result = 0
  548. response.Msg = "ok"
  549. log.WithField("request_id", dspInfo.RequestId).Infof("rsp1: %+v", customAdData)
  550. jsonBytes, err := json.Marshal(customAdData)
  551. if err != nil {
  552. c.String(404, "marsha custom addata failed: %s", err.Error())
  553. return
  554. }
  555. log.WithField("request_id", dspInfo.RequestId).Infof("rsp1: %s", jsonBytes)
  556. encryptData, _ := encrypt.Encrypt(jsonBytes, []byte(adslib.GetConf().SecretKey))
  557. response.AdsResult = encryptData
  558. log.WithField("request_id", dspInfo.RequestId).Infof("encrypt: %s", encryptData)
  559. s, err := encrypt.Decrypt(encryptData, []byte(adslib.GetConf().SecretKey))
  560. log.WithField("request_id", dspInfo.RequestId).Info(s, err)
  561. graylog.ReportGrayLog(request, dspInfo, 0, 0)
  562. rspBytes, err := json.Marshal(response)
  563. if err != nil {
  564. c.String(404, "marshal Response failed: %s", err.Error())
  565. return
  566. }
  567. log.WithField("request_id", dspInfo.RequestId).Infof("rsp1: %s", rspBytes)
  568. c.String(200, string(rspBytes))
  569. return
  570. } else {
  571. customAdData.Result = 0
  572. customAdData.Msg = "ok"
  573. graylog.ReportGrayLog(request, dspInfo, 0, 0)
  574. rspBytes, err := json.Marshal(customAdData)
  575. if err != nil {
  576. c.String(404, err.Error())
  577. return
  578. }
  579. c.String(200, string(rspBytes))
  580. return
  581. }
  582. }
  583. }
  584. }
  585. redis_data.SetAdsRealRequestNum(advertiser)
  586. rspBytes, err := json.Marshal(response)
  587. if err != nil {
  588. c.String(404, err.Error())
  589. return
  590. }
  591. if request.NewAdsFlag == 0 {
  592. graylog.ReportGrayLog(request, dspInfo, 0, 0)
  593. }
  594. device.SetAdsTagLog(advertiser, dspInfo.ReqSource, "DS_REQ", cityCode) // 所有请求
  595. log.WithField("request_id", dspInfo.RequestId).Infof("rsp3: %s", rspBytes)
  596. c.String(200, string(rspBytes))
  597. }