g.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. import { director, find } from "cc";
  2. import { Http } from "../core/net/Http";
  3. import { HttpSystem } from "../core/net/HttpSystem";
  4. import { WindowManager } from "../core/ui/window/WindowManager";
  5. import { Random } from "../core/utils/Random";
  6. import { ADData } from "./ADData";
  7. import { DeviceData } from "./DeviceData";
  8. import { GameData } from "./GameData";
  9. import { HttpErrorCode } from "./HttpErrorCode";
  10. import { ISJSB, platform } from "./platform";
  11. import { User } from "./User";
  12. import { WechatData } from "./WechatData";
  13. export class g {
  14. public static userData = new User();
  15. public static gameData = new GameData();
  16. public static deviceData = new DeviceData();
  17. public static adData = globalThis["adData"] || new ADData();
  18. public static wechatData = new WechatData();
  19. /**
  20. * 激励视频加载失败
  21. */
  22. public static onRewardVideoADError() {
  23. g.adData.ad_type = 5;
  24. HttpSystem.send("/api/ad/videoLog", true, { adData: g.adData });
  25. this.callback(null);
  26. this.callback = null;
  27. }
  28. /**
  29. * 激励视频展⽰
  30. */
  31. public static onRewardVideoADShow() {
  32. g.adData.ad_type = 4;
  33. HttpSystem.send("/api/ad/videoLog", true, { adData: g.adData });
  34. }
  35. /**
  36. * 放弃视频奖励
  37. */
  38. public static onRewardVideoADLoss() {
  39. this.callback(null);
  40. this.callback = null;
  41. }
  42. private static callback: (value: ADData) => void;
  43. /**
  44. * 显示激励视频
  45. * @returns
  46. */
  47. public static showRewardVideo() {
  48. return new Promise<ADData>((resolve => {
  49. if (!ISJSB) {//web版本直接返回成功
  50. resolve(g.adData);
  51. return;
  52. }
  53. //3秒内只拉起一个视频
  54. let now = Date.now();
  55. if (this.time && now - this.time < 3000) {
  56. resolve(null);
  57. return;
  58. }
  59. this.time = Date.now();
  60. // if (this.callback) {//如果正在展示则返回无奖励
  61. // resolve(null);
  62. // return;
  63. // }
  64. this.callback = resolve;
  65. platform.showRewardVideoAD();
  66. }));
  67. }
  68. private static time: number;
  69. /**
  70. * 视频完播
  71. */
  72. public static onRewardVideoADComplete() {
  73. this.callback(g.adData);
  74. this.callback = null;
  75. }
  76. /**
  77. * 横幅展示
  78. */
  79. public static onBannerADShow() {
  80. g.adData.ad_type = 7;
  81. HttpSystem.send("/api/ad/videoLog", true, { adData: g.adData });
  82. }
  83. /**
  84. * 全屏展示
  85. */
  86. public static onFullScreenADShow() {
  87. g.adData.ad_type = 9;
  88. HttpSystem.send("/api/ad/videoLog", true, { adData: g.adData });
  89. }
  90. /**
  91. * 全屏完播
  92. */
  93. public static onFullScreenADComplete() {
  94. g.adData.ad_type = 8;
  95. HttpSystem.send("/api/ad/videoLog", true, { adData: g.adData });
  96. }
  97. /**
  98. * 插屏展示
  99. */
  100. public static onInterstitalADShow() {
  101. g.adData.ad_type = 2;
  102. HttpSystem.send("/api/ad/videoLog", true, { adData: g.adData });
  103. }
  104. /**
  105. * 信息流展示
  106. */
  107. public static onNativeExpressADShow() {
  108. g.adData.ad_type = 3;
  109. HttpSystem.send("/api/ad/videoLog", true, { adData: g.adData });
  110. }
  111. }
  112. Http.onData = (data: any) => {
  113. if (data.code == 1) {//需要重新登录
  114. g.deviceData.needlogin = true;
  115. director.loadScene("Launch");
  116. return false;
  117. }
  118. if (data.code == 0) {
  119. if (data.taskReceive !== undefined) {
  120. g.gameData.taskReceive = data.taskReceive;
  121. }
  122. if (data.threeDayTaskReceive !== undefined) {
  123. g.gameData.threeDayTaskReceive = data.threeDayTaskReceive;
  124. }
  125. }
  126. if (data.code == HttpErrorCode.ReloadData) {
  127. g.gameData.needReloadData = true;
  128. }
  129. return true;
  130. }
  131. Http.onKeyError = () => {
  132. g.deviceData.needlogin = true;
  133. director.loadScene("Launch");
  134. }
  135. globalThis["g"] = g;