offer.go 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. package http
  2. import (
  3. "encoding/base64"
  4. "fmt"
  5. "net/http"
  6. "net/url"
  7. "strconv"
  8. "strings"
  9. "time"
  10. "go-common/app/interface/main/app-wall/dao/padding"
  11. "go-common/app/interface/main/app-wall/model"
  12. "go-common/library/ecode"
  13. log "go-common/library/log"
  14. bm "go-common/library/net/http/blademaster"
  15. "go-common/library/net/metadata"
  16. )
  17. // wallShikeClick
  18. func wallShikeClick(c *bm.Context) {
  19. params := c.Request.Form
  20. // ip uint32, cid, mac, idfa, cb
  21. appid := params.Get("appid")
  22. mac := params.Get("mac")
  23. idfa := params.Get("idfa")
  24. cb := params.Get("cb")
  25. cid := model.ChannelShike
  26. if appid != model.GdtIOSAppID {
  27. log.Error("gdt click wrong appid(%s)", appid)
  28. c.JSON(nil, ecode.RequestErr)
  29. return
  30. }
  31. if cid == "" || idfa == "" || cb == "" {
  32. log.Error("arg cid(%s) mac(%s) idfa(%s) callback(%s) is empty", cid, idfa, cb)
  33. c.JSON(nil, ecode.RequestErr)
  34. return
  35. }
  36. ip := model.InetAtoN(metadata.String(c, metadata.RemoteIP))
  37. if err := offerSvc.Click(c, ip, cid, mac, idfa, cb, time.Now()); err != nil {
  38. log.Error("offerSvc.Click error(%v)", err)
  39. c.JSON(nil, ecode.NotModified)
  40. return
  41. }
  42. res := map[string]interface{}{}
  43. res["code"] = ecode.OK
  44. returnDataJSON(c, res, nil)
  45. }
  46. // wallGdtClick
  47. func wallGdtClick(c *bm.Context) {
  48. params := c.Request.Form
  49. appID := params.Get("appid")
  50. muid := params.Get("muid")
  51. var now time.Time
  52. ts, _ := strconv.ParseInt(params.Get("click_time"), 10, 64)
  53. if ts != 0 {
  54. now = time.Unix(ts, 0)
  55. } else {
  56. now = time.Now()
  57. }
  58. clickid := params.Get("click_id")
  59. adid := params.Get("advertiser_id")
  60. appType := params.Get("app_type")
  61. if id, ok := model.AppIDGdt[appType]; !ok || appID != id {
  62. log.Error("wallGdtClick app_type(%s) and appid(%s) is illegal", appType, appID)
  63. res := map[string]interface{}{"ret": ecode.RequestErr}
  64. returnDataJSON(c, res, nil)
  65. return
  66. }
  67. if _, ok := model.ChannelGdt[adid]; !ok {
  68. log.Error("wallGdtClick advertiser_id(%s) is illegal", adid)
  69. res := map[string]interface{}{"ret": ecode.RequestErr}
  70. returnDataJSON(c, res, nil)
  71. return
  72. }
  73. ip := model.InetAtoN(metadata.String(c, metadata.RemoteIP))
  74. if appType == model.TypeIOS {
  75. if err := offerSvc.Click(c, ip, adid, "", muid, clickid, now); err != nil {
  76. log.Error("wallGdtClick %+v", err)
  77. res := map[string]interface{}{"ret": ecode.ServerErr}
  78. returnDataJSON(c, res, nil)
  79. return
  80. }
  81. } else if appType == model.TypeAndriod {
  82. if err := offerSvc.ANClick(c, adid, muid, "", "", clickid, ip, now); err != nil {
  83. log.Error("wallGdtClick %+v", err)
  84. res := map[string]interface{}{"ret": ecode.ServerErr}
  85. returnDataJSON(c, res, nil)
  86. return
  87. }
  88. }
  89. res := map[string]interface{}{"ret": ecode.OK}
  90. returnDataJSON(c, res, nil)
  91. }
  92. // wallTestActive
  93. func wallTestActive(c *bm.Context) {
  94. params := c.Request.Form
  95. idfa := params.Get("idfa")
  96. if idfa == "" {
  97. c.JSON(nil, ecode.RequestErr)
  98. return
  99. }
  100. ip := model.InetAtoN(metadata.String(c, metadata.RemoteIP))
  101. if err := offerSvc.Active(c, ip, "0", "", "", idfa, "test by haogw", time.Now()); err != nil {
  102. c.JSON(nil, ecode.RequestErr)
  103. return
  104. }
  105. res := map[string]interface{}{
  106. "code": ecode.OK,
  107. }
  108. returnDataJSON(c, res, nil)
  109. }
  110. // wallActive
  111. func wallActive(c *bm.Context) {
  112. var (
  113. _aesKey = []byte("iHiRzNVIy8mvc4HnKPKeiJEP90zycllH")
  114. _aesIv = []byte("w4gQHf5M7RQdBq2U")
  115. )
  116. body := c.Request.Form.Get("body")
  117. if body == "" {
  118. log.Error("body is empty")
  119. c.JSON(nil, ecode.RequestErr)
  120. return
  121. }
  122. log.Info("body before base64 (%v)", body)
  123. paramBody := c.Request.Form
  124. appkey := paramBody.Get("appkey")
  125. appver := paramBody.Get("appver")
  126. build := paramBody.Get("build")
  127. device := paramBody.Get("device")
  128. filtered := paramBody.Get("filtered")
  129. mobiApp := paramBody.Get("mobi_app")
  130. sign := paramBody.Get("sign")
  131. log.Info("appkey: (%v)\n appver: (%v)\n build: (%v)\n device: (%v)\n filtered: (%v)\n mobi_app: (%v)\n sign: (%v)", appkey, appver, build, device, filtered, mobiApp, sign)
  132. bs, err := base64.StdEncoding.DecodeString(body)
  133. if err != nil {
  134. log.Error("base64.StdEncoding.DecodeString(%s) error(%v)", body, err)
  135. c.JSON(nil, ecode.SignCheckErr)
  136. return
  137. }
  138. bs, err = offerSvc.CBCDecrypt(bs, _aesKey, _aesIv, padding.PKCS5)
  139. if err != nil {
  140. log.Error("aes.CBCDecrypt(%s) error(%v)", base64.StdEncoding.EncodeToString(bs), err)
  141. c.JSON(nil, ecode.SignCheckErr)
  142. return
  143. }
  144. params, err := url.ParseQuery(string(bs))
  145. if err != nil {
  146. log.Error("url.ParseQuery(%s) error(%v)", bs, err)
  147. c.JSON(nil, ecode.RequestErr)
  148. return
  149. }
  150. rmac := params.Get("rmac")
  151. mac := params.Get("mac")
  152. idfa := params.Get("idfa")
  153. name := params.Get("name")
  154. log.Info("rmac: (%v)\n mac: (%v)\n idfa: (%v)\n name: (%v)\n", rmac, mac, idfa, name)
  155. if idfa == "" || name == "" || strings.Contains(idfa, "-") || strings.Contains(rmac, ":") || strings.Contains(mac, ":") {
  156. log.Error("mac(%s) rmac(%s) idfa(%s) format error, or idfa(%s) name(%s) is empty", mac, rmac, idfa, idfa, name)
  157. c.JSON(nil, ecode.RequestErr)
  158. return
  159. }
  160. ip := model.InetAtoN(metadata.String(c, metadata.RemoteIP))
  161. var (
  162. mid int64
  163. )
  164. if midInter, ok := c.Get("mid"); ok {
  165. mid = midInter.(int64)
  166. }
  167. log.Info("mid: (%d)", mid)
  168. err = offerSvc.Active(c, ip, strconv.FormatInt(mid, 10), rmac, mac, idfa, name, time.Now())
  169. if err != nil {
  170. log.Error("offerSvc.Active(%d, %d, %s, %s, %s, %s) error(%v)", ip, mid, rmac, mac, idfa, name, err)
  171. c.JSON(nil, ecode.NotModified)
  172. return
  173. }
  174. log.Info("wall offer active idfa(%s)", idfa)
  175. res := map[string]interface{}{
  176. "code": ecode.OK,
  177. }
  178. returnDataJSON(c, res, nil)
  179. }
  180. func wallExist(c *bm.Context) {
  181. params := c.Request.Form
  182. // ip uint32, cid, mac, idfa, cb
  183. appID := params.Get("appid")
  184. idfa := params.Get("idfa")
  185. if (appID != model.GdtIOSAppID && appID != model.GdtAndroidAppID) || idfa == "" {
  186. log.Error("arg cid(%s) idfa(%s) is empty", appID, idfa)
  187. c.JSON(nil, ecode.RequestErr)
  188. return
  189. }
  190. exist, _ := offerSvc.Exists(c, strings.Replace(idfa, "-", "", -1))
  191. const (
  192. ret = `{"%s":"%d"}`
  193. )
  194. e := 1
  195. if !exist {
  196. e = 0
  197. }
  198. bs := []byte(fmt.Sprintf(ret, idfa, e))
  199. if _, err := c.Writer.Write(bs); err != nil {
  200. log.Error("w.Write error(%v)", err)
  201. }
  202. res := map[string]interface{}{
  203. "code": ecode.OK,
  204. }
  205. returnDataJSON(c, res, nil)
  206. }
  207. func wallDotinappClick(c *bm.Context) {
  208. params := c.Request.Form
  209. appid := params.Get("appid")
  210. // pid := params.Get("pid")
  211. // subid := params.Get("subid")
  212. // ua := params.Get("ua")
  213. idfa := params.Get("idfa")
  214. clickid := params.Get("clickid")
  215. pid := model.ChannelDontin
  216. if appid != model.GdtIOSAppID {
  217. log.Error("dotinapp click wrong appid(%s)", appid)
  218. c.JSON(nil, ecode.RequestErr)
  219. return
  220. }
  221. if pid == "" || idfa == "" || clickid == "" {
  222. log.Error("pid(%s) idfa(%s) clickid(%s) is empty", pid, idfa, clickid)
  223. c.JSON(nil, ecode.RequestErr)
  224. return
  225. }
  226. ip := model.InetAtoN(metadata.String(c, metadata.RemoteIP))
  227. if err := offerSvc.Click(c, ip, pid, "", idfa, clickid, time.Now()); err != nil {
  228. log.Error("offerSvc.Click error(%v)", err)
  229. c.JSON(nil, ecode.NotModified)
  230. return
  231. }
  232. res := map[string]interface{}{
  233. "code": ecode.OK,
  234. }
  235. returnDataJSON(c, res, nil)
  236. }
  237. func wallToutiaoClick(c *bm.Context) {
  238. const (
  239. OSAndroid = "0"
  240. // OSIOS = "1"
  241. )
  242. params := c.Request.Form
  243. os := params.Get("os")
  244. if os != OSAndroid {
  245. log.Error("wallToutiaoClick os(%s) is illegal", os)
  246. c.AbortWithStatus(http.StatusBadRequest)
  247. return
  248. }
  249. imei := params.Get("imei")
  250. androidid := params.Get("androidid")
  251. if imei == "" && androidid == "" {
  252. log.Error("wallToutiaoClick imei(%s) and androidid(%s) is illegal", imei, androidid)
  253. c.AbortWithStatus(http.StatusBadRequest)
  254. return
  255. }
  256. cb := params.Get("callback_url")
  257. if cb == "" {
  258. log.Error("wallToutiaoClick callback_url(%s) is illegal", cb)
  259. c.AbortWithStatus(http.StatusBadRequest)
  260. return
  261. }
  262. mac := params.Get("mac")
  263. ip := model.InetAtoN(params.Get("ip"))
  264. var now time.Time
  265. ts, _ := strconv.ParseInt(params.Get("timestamp"), 10, 64)
  266. if ts != 0 {
  267. now = time.Unix(0, ts*1e6)
  268. } else {
  269. now = time.Now()
  270. }
  271. if os == OSAndroid {
  272. if err := offerSvc.ANClick(c, model.ChannelToutiao, imei, androidid, mac, cb, ip, now); err != nil {
  273. log.Error("%+v", err)
  274. c.AbortWithStatus(http.StatusInternalServerError)
  275. return
  276. }
  277. }
  278. }
  279. func wallActive2(c *bm.Context) {
  280. params := c.Request.Form
  281. os := params.Get("os")
  282. if _, ok := model.AppIDGdt[os]; !ok {
  283. log.Error("wallActive2 os(%s) is illegal", os)
  284. c.JSON(nil, ecode.RequestErr)
  285. return
  286. }
  287. if os != model.TypeAndriod {
  288. log.Error("wallActive2 os(%s) is illegal", os)
  289. c.JSON(nil, ecode.RequestErr)
  290. return
  291. }
  292. imei := params.Get("imei")
  293. androidid := params.Get("androidid")
  294. if imei == "" && androidid == "" {
  295. log.Error("wallActive2 imei(%s) and androidid(%s) is illegal", imei, androidid)
  296. c.JSON(nil, ecode.RequestErr)
  297. return
  298. }
  299. mac := params.Get("mac")
  300. err := offerSvc.ANActive(c, imei, androidid, mac, time.Now())
  301. if err != nil {
  302. log.Error("wallActive2 %+v", err)
  303. }
  304. c.JSON(nil, err)
  305. }