RedBoxItem.ts 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. import { _decorator, Component, Node, Label, RichText, EventHandler } from 'cc';
  2. import { Http } from '../core/net/Http';
  3. import { OpenWindow } from '../core/ui/window/OpenWindow';
  4. import { WindowManager } from '../core/ui/window/WindowManager';
  5. import { WindowOpenMode } from '../core/ui/window/WindowOpenMode';
  6. import { Utils } from '../core/utils/Utils';
  7. import { MultipleType } from '../Data/ADData';
  8. import { g } from '../Data/g';
  9. import { LoginWaitWin } from '../Windows/LoginWaitWin';
  10. const { ccclass, property } = _decorator;
  11. @ccclass('RedBoxItem')
  12. export class RedBoxItem extends Component {
  13. @property({ tooltip: "红包等级", type: Node }) public redLv: Node;
  14. @property({ tooltip: "红包id", type: Node }) public redID: Node;
  15. @property({ tooltip: "倒计时", type: Node }) public redTime: Node;
  16. @property({ tooltip: "http服务", type: Http }) public http: Http;
  17. @property({ tooltip: "领取神石回调", type: EventHandler }) public getDiamondBackFun: EventHandler;
  18. @property({ tooltip: "红点", type: Node }) public redDot: Node;
  19. public redType = 0;//红包类型0:免费红包,1:切磋红包
  20. public opentime = 0;//红包开启时间
  21. public redId = 0;//红包id
  22. start() {
  23. g.gameData.isPlayVideo = false;
  24. }
  25. public setData(data: any) {
  26. this.redType = data.type;
  27. this.opentime = data.canOpenTime;
  28. this.redId = data.id;
  29. if (data.type == 0) {
  30. this.redLv.getComponent(Label).string = g.userData.lv + "级红包";
  31. this.redID.getComponent(Label).string = "免费红包";
  32. this.redTime.getComponent(RichText).string = "<color=#55a87e>看视频领取</color>";
  33. } else {
  34. this.redLv.getComponent(Label).string = data.lv + "级红包";
  35. this.redID.getComponent(Label).string = "" + data.enemyName;
  36. let _time = this.opentime * 1000 - Date.now();
  37. this.redTime.getComponent(RichText).string = _time <= 0 ? "<color=#55a87e>可领取</color>" :
  38. "<color=#ff2905>" + Utils.formatCountDown(_time) + "后可领取</color>";
  39. if (_time > 0) {
  40. this.schedule(this.scheduleCallback, 1);
  41. } else {
  42. this.redDot.active = true;
  43. }
  44. }
  45. }
  46. //定时回调
  47. public scheduleCallback() {
  48. let _time = this.opentime * 1000 - Date.now();
  49. this.redTime.getComponent(RichText).string = _time <= 0 ? "<color=#55a87e>可领取</color>" :
  50. "<color=#ff2905>" + Utils.formatCountDown(_time) + "后可领取</color>";
  51. if (_time <= 0) {
  52. this.redDot.active = true;
  53. this.unschedule(this.scheduleCallback);
  54. }
  55. }
  56. public isSend = false;
  57. public async getRedBox() {
  58. if (g.gameData.isPlayVideo) {
  59. return;
  60. }
  61. g.gameData.isPlayVideo = true;
  62. if (this.redType == 0) {
  63. let data = await this.http.send("/api/ad/WatchVideoAD");
  64. switch (data.code) {
  65. case 0:
  66. //播放视频
  67. let adData = await g.showRewardVideo();
  68. if (adData) {
  69. this.videoBack();
  70. } else {
  71. //视频观看失败
  72. WindowManager.showTips("视频观看时间不足");
  73. }
  74. break;
  75. case 108:
  76. WindowManager.showTips("今日视频次数已用完,请明日再继续");
  77. g.gameData.isPlayVideo = false;
  78. break;
  79. default:
  80. // WindowManager.showTips(g.CodeMsg[data.code]);
  81. WindowManager.showTips("视频准备中,请稍后再试");
  82. g.gameData.isPlayVideo = false;
  83. break;
  84. }
  85. } else {
  86. if (this.opentime - (Date.now() / 1000) <= 0) {
  87. this.openRedBox();
  88. } else {
  89. WindowManager.showTips("倒计时结束后可抢红包");
  90. g.gameData.isPlayVideo = false;
  91. }
  92. }
  93. }
  94. //视频结束回调
  95. public async videoBack() {
  96. //#region 打开加载等待界面
  97. // let _window = await WindowManager.open("Prefabs/Windows/加载等待窗口", WindowOpenMode.NotCloseAndCover);
  98. //#endregion
  99. let data = await this.http.send("/api/wealth/AddDiamondForVideo", { multipleType: MultipleType.Default, adData: g.adData });
  100. // _window.getComponent(LoginWaitWin).close();//关闭加载等待界面
  101. if (data.code == 0) {
  102. if (data.data.addDiamond > 0) {
  103. this.getComponent(OpenWindow).prefabPath = "Prefabs/Windows/神石领取界面";
  104. let _window = this.getComponent(OpenWindow);
  105. _window.open([data.data.addDiamond, -1, null, this.getDiamondBackFun]);
  106. } else {
  107. this.getDiamondBackFun.emit([]);
  108. }
  109. } else {
  110. WindowManager.showTips(g.CodeMsg[data.code]);
  111. g.gameData.isPlayVideo = false;
  112. }
  113. }
  114. public async openRedBox() {
  115. //#region 打开加载等待界面
  116. let _window = await WindowManager.open("Prefabs/Windows/加载等待窗口", WindowOpenMode.NotCloseAndCover);
  117. //#endregion
  118. let data = await this.http.send("/api/redPacket/OpenRedPacket", { redPacketID: this.redId });
  119. _window.getComponent(LoginWaitWin).close();//关闭加载等待界面
  120. if (data.code == 0) {
  121. let _window = this.getComponent(OpenWindow);
  122. if (data.data.openID == g.userData.id) {//打开红包
  123. _window.prefabPath = "Prefabs/Windows/切磋红包奖品";
  124. _window.open([data.data.prizes]);
  125. } else {//红包被抢
  126. _window.prefabPath = "Prefabs/Windows/红包被抢弹窗";
  127. _window.open({ name: data.data.uerName, avator: data.data.userAvator, time: data.data.openTime });
  128. }
  129. for (let i = 0; i < g.gameData.myRedPacketDatas.length; i++) {
  130. if (this.redId == g.gameData.myRedPacketDatas[i].id) {
  131. g.gameData.myRedPacketDatas.splice(i, 1);
  132. break;
  133. }
  134. }
  135. this.close();
  136. } else {
  137. g.gameData.isPlayVideo = false;
  138. WindowManager.showTips(g.CodeMsg[data.code]);
  139. }
  140. }
  141. public close() {
  142. this.node.removeFromParent();
  143. this.destroy();
  144. }
  145. onDestroy() {
  146. g.gameData.isPlayVideo = false;
  147. }
  148. }