ads_handler.go 22 KB

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