g.ts 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. import { director, find, game } from "cc";
  2. import { DataSystem } from "../core/data/DataSystem";
  3. import { Http, HttpResponseCode } from "../core/net/Http";
  4. import { HttpSystem } from "../core/net/HttpSystem";
  5. import { WindowSystem } from "../core/ui/window/WindowSystem";
  6. import { FightData } from "../fight/FightData";
  7. import { TaskData } from "../task/TaskData";
  8. import { HttpErrorCode } from "./HttpErrorCode";
  9. import { ADData } from "./jsb/ADData";
  10. import { DeviceData } from "./jsb/DeviceData";
  11. import { ISJSB, platform } from "./jsb/platform";
  12. import { WechatData } from "./jsb/WechatData";
  13. /**
  14. * 全局数据接口
  15. * @description 真正意义的ECS架构不应该存在这个,这里有是因为网赚项目JSB工程对接的历史遗留问题
  16. * @deprecated 已废弃,请使用DataSystem.getData
  17. */
  18. export class g {
  19. /**
  20. * @deprecated 已废弃,请使用DataSystem.getData
  21. */
  22. public static get deviceData(): DeviceData {
  23. return DataSystem.getData(DeviceData);
  24. }
  25. /**
  26. * @deprecated 已废弃,请使用DataSystem.getData
  27. */
  28. public static get adData(): ADData {
  29. return DataSystem.getData(ADData);
  30. }
  31. /**
  32. * @deprecated 已废弃,请使用DataSystem.getData
  33. */
  34. public static get wechatData(): WechatData {
  35. return DataSystem.getData(WechatData);
  36. }
  37. /**
  38. * 激励视频加载失败
  39. */
  40. public static onRewardVideoADError() {
  41. g.adData.ad_type = 5;
  42. HttpSystem.send("/api/ad/videoLog", true, { adData: g.adData.obj });
  43. this.callback(null);
  44. this.callback = null;
  45. }
  46. /**
  47. * 激励视频展⽰
  48. */
  49. public static onRewardVideoADShow() {
  50. g.adData.ad_type = 4;
  51. HttpSystem.send("/api/ad/videoLog", true, { adData: g.adData.obj });
  52. }
  53. /**
  54. * 放弃视频奖励
  55. */
  56. public static onRewardVideoADLoss() {
  57. this.callback(null);
  58. this.callback = null;
  59. }
  60. private static callback: (value: ADData) => void;
  61. /**
  62. * 显示激励视频
  63. * @returns
  64. */
  65. public static showRewardVideo() {
  66. return new Promise<ADData>((resolve => {
  67. if (!ISJSB()) {//web版本直接返回成功
  68. resolve(DataSystem.getData(ADData));
  69. return;
  70. }
  71. //3秒内只拉起一个视频
  72. let now = Date.now();
  73. if (this.time && now - this.time < 3000) {
  74. resolve(null);
  75. return;
  76. }
  77. this.time = Date.now();
  78. // if (this.callback) {//如果正在展示则返回无奖励
  79. // resolve(null);
  80. // return;
  81. // }
  82. this.callback = resolve;
  83. platform.showRewardVideoAD();
  84. }));
  85. }
  86. private static time: number;
  87. /**
  88. * 视频完播
  89. */
  90. public static onRewardVideoADComplete() {
  91. this.callback(g.adData);
  92. this.callback = null;
  93. }
  94. /**
  95. * 横幅展示
  96. */
  97. public static onBannerADShow() {
  98. g.adData.ad_type = 7;
  99. HttpSystem.send("/api/ad/videoLog", true, { adData: g.adData.obj });
  100. }
  101. /**
  102. * 全屏展示
  103. */
  104. public static onFullScreenADShow() {
  105. g.adData.ad_type = 9;
  106. HttpSystem.send("/api/ad/videoLog", true, { adData: g.adData.obj });
  107. }
  108. /**
  109. * 全屏完播
  110. */
  111. public static onFullScreenADComplete() {
  112. g.adData.ad_type = 8;
  113. HttpSystem.send("/api/ad/videoLog", true, { adData: g.adData.obj });
  114. }
  115. /**
  116. * 插屏展示
  117. */
  118. public static onInterstitalADShow() {
  119. g.adData.ad_type = 2;
  120. HttpSystem.send("/api/ad/videoLog", true, { adData: g.adData.obj });
  121. }
  122. /**
  123. * 信息流展示
  124. */
  125. public static onNativeExpressADShow() {
  126. g.adData.ad_type = 3;
  127. HttpSystem.send("/api/ad/videoLog", true, { adData: g.adData.obj });
  128. }
  129. }
  130. Http.onData = (data: any) => {
  131. if (!data) {
  132. return true;
  133. }
  134. if (data.code == HttpResponseCode.NoLogin) {//需要重新登录
  135. localStorage.removeItem("account");
  136. g.deviceData.needlogin = true;
  137. director.loadScene("launch");
  138. return false;
  139. }
  140. else if (data.code == HttpErrorCode.SendDataError) {//数据错误
  141. localStorage.removeItem("account");
  142. g.deviceData.needlogin = true;
  143. director.loadScene("launch");
  144. WindowSystem.showTips("数据异常,请重新登录");
  145. return false;
  146. }
  147. else if (data.code == 0) {//尾巴验证
  148. if (data.taskReceive !== undefined) {//如果任务完成
  149. DataSystem.getData(TaskData).hasReceived = data.taskReceive;
  150. }
  151. if (data.checkBeAttack) {//玩家被攻击
  152. DataSystem.getData(FightData).beAttack = data.checkBeAttack;
  153. DataSystem.getData(FightData).beAttack = DataSystem.getData(FightData).beAttack;
  154. }
  155. }
  156. return true;
  157. }
  158. Http.onKeyError = () => {
  159. localStorage.removeItem("account");
  160. g.deviceData.needlogin = true;
  161. director.loadScene("launch");
  162. }
  163. globalThis["g"] = g;