DayPrize.ts 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. import { _decorator, Component, Prefab, instantiate, UITransform, Node, Label, Sprite, Button } from 'cc';
  2. import InfiniteCell from '../core/InfiniteList/InfiniteCell';
  3. import { IFDataSource, InfiniteList } from '../core/InfiniteList/InfiniteList';
  4. import { Http } from '../core/net/Http';
  5. import { BitmapFont } from '../core/ui/BitmapFont';
  6. import { OpenWindow } from '../core/ui/window/OpenWindow';
  7. import { WindowManager } from '../core/ui/window/WindowManager';
  8. import { WindowOpenMode } from '../core/ui/window/WindowOpenMode';
  9. import { Utils } from '../core/utils/Utils';
  10. import { g } from '../Data/g';
  11. import { HttpErrorCode } from '../Data/HttpErrorCode';
  12. import { platform } from '../Data/platform';
  13. import { DayPrizeItem } from './DayPrizeItem';
  14. const { ccclass, property } = _decorator;
  15. @ccclass('DayPrize')
  16. export class DayPrize extends Component implements IFDataSource {
  17. @property({ type: Prefab, tooltip: "子项节点" })
  18. public dayPrizeItem: Prefab = null;
  19. @property({ type: Node, tooltip: "列表节点" })
  20. public list: Node = null;
  21. @property({ type: Sprite })
  22. public button: Sprite = null;
  23. @property({ type: BitmapFont })
  24. public times: BitmapFont = null;
  25. private http: Http;
  26. private prefabHeight: number;
  27. private itemArr: Array<any>;
  28. private infiniteList: InfiniteList;
  29. private _timesNum: number = 0;
  30. set timesNum(times: number) {
  31. this._timesNum = times;
  32. this.times.string = times + "";
  33. }
  34. get timesNum() {
  35. return this._timesNum;
  36. }
  37. async start() {
  38. let prefab = instantiate(this.dayPrizeItem);
  39. this.prefabHeight = prefab.getComponent(UITransform).height;
  40. prefab.destroy();
  41. this.infiniteList = this.list.getComponent(InfiniteList);
  42. this.http = this.getComponent(Http);
  43. await this.queryData();
  44. this.infiniteList.Init(this);
  45. }
  46. /**
  47. * 观看视频
  48. */
  49. async onVideo() {
  50. let applyReturn = await this.http.send("/api/ad/watchVideoAD");
  51. //可以观看视频
  52. if (applyReturn.code == 0) {
  53. platform.reportThinking("ad_init", JSON.stringify({ ad_id: "daily_process", ad_time: Utils.formatDate(new Date()), id: g.userData.id, role_name: g.userData.nickName, level: g.userData.getLevel() }));
  54. //播放视频
  55. let adData = await g.showRewardVideo();
  56. if (adData) {
  57. this.onVideoOver();
  58. } else {
  59. //视频观看失败
  60. WindowManager.showTips("视频观看时间不足");
  61. }
  62. } else if (applyReturn.code == 106) {
  63. WindowManager.showTips("今日视频次数已用完,请明日再继续");
  64. } else if (applyReturn.code == HttpErrorCode.VideoADCD) {
  65. WindowManager.showTips("请求频繁,请稍后再试");
  66. } else {
  67. WindowManager.showTips("视频准备中,请稍后再试");
  68. }
  69. }
  70. /**
  71. * 视频播放完毕
  72. */
  73. async onVideoOver() {
  74. let data = await this.http.send("/api/dailyprize/watchDailyPrizeVideoAD", { adData: g.adData });
  75. if (data.code == 0) {
  76. platform.reportThinking("ad_end", JSON.stringify({ ad_id: "diamond_get", ad_time: Utils.formatDate(new Date()), id: g.userData.id, role_name: g.userData.nickName, level: g.userData.getLevel() }));
  77. platform.umUp("dailyReward");//每日奖励埋点
  78. this.timesNum = this.timesNum - 1;
  79. this.updateItem(data.data);
  80. this.infiniteList.Refresh();
  81. //观看成功
  82. WindowManager.open("Prefabs/DayPrize/DayPrizeTips", WindowOpenMode.NotCloseAndAdd, " ");
  83. } else if (data.code == HttpErrorCode.VideoADCD) {
  84. WindowManager.showTips("请求频繁,请稍后再试");
  85. }
  86. }
  87. /**
  88. * 查询每日奖励数据
  89. */
  90. private async queryData() {
  91. let data = await this.http.send("/api/dailyprize/getdailyprize");
  92. if (data.code == 0) {
  93. this.itemArr = data.data.prizeList;
  94. this.timesNum = data.data.videoTimes;
  95. this.infiniteList.Refresh()
  96. } else {
  97. WindowManager.showTips("发生错误,请重试");
  98. }
  99. }
  100. /**
  101. * 根据次数禁用观看视频按钮
  102. */
  103. update() {
  104. if (this.timesNum == 0) {
  105. this.button.grayscale = true;
  106. this.button.getComponent(Button).interactable = false;
  107. } else {
  108. this.button.grayscale = false;
  109. this.button.getComponent(Button).interactable = true;
  110. }
  111. }
  112. /**
  113. * 更新子项进度
  114. * @returns
  115. */
  116. public updateItem(updateData: any) {
  117. for (let i = 0; i < this.itemArr.length; i++) {
  118. let id = this.itemArr[i].id;
  119. let value = updateData[`${id}`];
  120. this.itemArr[i]["value"] = value;
  121. }
  122. }
  123. /**
  124. * 向子项提供数据
  125. * @returns
  126. */
  127. GetCellNumber(): number {
  128. return this.itemArr.length;
  129. }
  130. GetCellIdentifer(dataIndex: number): string {
  131. return "DayPrizeItem";
  132. }
  133. GetCellSize(dataIndex: number): number {
  134. return this.prefabHeight;
  135. }
  136. GetCellView(dataIndex: number, identifier?: string): InfiniteCell {
  137. let prefab = instantiate(this.dayPrizeItem);
  138. return prefab.getComponent(DayPrizeItem);
  139. }
  140. GetCellData?(dataIndex: number) {
  141. return this.itemArr[dataIndex];
  142. }
  143. }