ads_handler.go 22 KB

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