ads_handler.go 22 KB

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