ads_handler.go 22 KB

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