DiamondExchangeUI.ts 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. import { _decorator, Component, Node, Label } from 'cc';
  2. import { RewardVideoSystem } from '../../ad/RewardVideoSystem';
  3. import { DataSystem } from '../../core/data/DataSystem';
  4. import { Http, HttpResponseCode, ResponseType } from '../../core/net/Http';
  5. import { HttpSystem } from '../../core/net/HttpSystem';
  6. import { WindowOpenMode } from '../../core/ui/window/WindowOpenMode';
  7. import { WindowSystem } from '../../core/ui/window/WindowSystem';
  8. import { ConfigData } from '../../data/ConfigData';
  9. import { platform } from '../../data/jsb/platform';
  10. import { UserData } from '../../data/UserData';
  11. import { ReportThinking } from '../../ReportThinking';
  12. const { ccclass, property } = _decorator;
  13. /**
  14. * 元宝兑换页
  15. * @author 郑聂华
  16. */
  17. @ccclass('DiamondExchangeUI')
  18. export class DiamondExchangeUI extends Component {
  19. @property({ type: Label, displayName: '文本_红包', tooltip: "文本_红包" }) txtRb: Label = null;
  20. @property({ type: Label, displayName: '文本_元宝', tooltip: "文本_元宝" }) txtDiamond: Label = null;
  21. @property({ type: Node, displayName: '箭头' }) arrawNode: Node = null;
  22. @property({ type: Node, displayName: '红包节点' }) redBagNode: Node = null;
  23. @property({ type: Node, displayName: '广告图标' }) adIcon: Node = null;
  24. @property({ type: Label, displayName: '文本_已兑换次数' }) txtTimes: Label = null;
  25. /**兑换配置 {exchangeBonus:1,exchangeDiamond:2,times:1,type:"bonus"}*/
  26. private exChangeCfg: any;
  27. private usedExchangeTimes: number;
  28. /**最高兑换次数*/
  29. private limitNum: number;
  30. private enterType: number;
  31. onParam(param) {
  32. this.enterType = parseInt(param);
  33. }
  34. start() {
  35. this.initPanel();
  36. }
  37. /**界面初始化*/
  38. private async initPanel() {
  39. let result = await this.getComponent(Http).send("/api/recruit/GetExchange");
  40. if (result && result.code == HttpResponseCode.Success) {
  41. this.limitNum = Object.keys(result.data.config).length;
  42. this.usedExchangeTimes = result.data.hadExchangedTimes;
  43. this.exChangeCfg = this.usedExchangeTimes == this.limitNum ? result.data.config[this.limitNum + ""] : result.data.config[(this.usedExchangeTimes + 1) + ""];
  44. //this.limitNum = result.data.config["bonus"].times + result.data.config["video"].times;
  45. //this.usedExchangeTimes = this.limitNum - (result.data.freeTimes + result.data.videoTimes);
  46. //if (result.data.freeTimes > 0) { this.exChangeCfg = result.data.config["bonus"]; }
  47. //else { this.exChangeCfg = result.data.config["video"]; }
  48. this.txtRb.string = this.exChangeCfg.exchangeBonus + "";
  49. this.txtDiamond.string = this.exChangeCfg.exchangeDiamond + "";
  50. this.txtTimes.string = this.usedExchangeTimes + "/" + this.limitNum; //剩余次数数据待定
  51. this.adIcon.active = this.exChangeCfg.type == "video";
  52. this.arrawNode.active = this.exChangeCfg.type == "bonus";
  53. this.redBagNode.active = this.exChangeCfg.type == "bonus";
  54. }
  55. }
  56. /**红包兑换*/
  57. private async exchangeByBonus() {
  58. let result = await this.getComponent(Http).send("/api/recruit/Exchange", { type: "bonus" });
  59. if (result && result.code == HttpResponseCode.Success) {
  60. let userData = DataSystem.getData(UserData);
  61. ReportThinking.currency_decrease('bonus', userData.bonus, this.exChangeCfg.exchangeBonus, userData.bonus - this.exChangeCfg.exchangeBonus, 'exchange');
  62. DataSystem.getData(UserData).bonus -= this.exChangeCfg.exchangeBonus;
  63. //DataSystem.getData(UserData).diamond += this.exChangeCfg.exchangeDiamond;
  64. ReportThinking.currency_increase('diamond', userData.diamond, this.exChangeCfg.exchangeDiamond, this.exChangeCfg.exchangeDiamond + userData.diamond, 'exchange');
  65. WindowSystem.close(this.node);
  66. WindowSystem.open("prefabs/ui/turntable/prizeFly", WindowOpenMode.NotCloseAndAdd, [{ type: this.enterType, num: this.exChangeCfg.exchangeDiamond }]);
  67. WindowSystem.showTips("兑换成功");
  68. } else {
  69. WindowSystem.showTips("兑换失败");
  70. }
  71. }
  72. /**视频免费获取*/
  73. private async exchangeByVideo() {
  74. let resultAd = await this.getComponent(Http).send("/api/ad/watchVideoAD");
  75. if (resultAd && resultAd.code == HttpResponseCode.Success) { //可以观看视频
  76. ReportThinking.ad_init('exchange_diamond');
  77. let adData = await RewardVideoSystem.show();
  78. if (adData) {
  79. let result = await this.getComponent(Http).send("/api/recruit/Exchange", { type: "video", adData: adData.obj });
  80. if (result && result.code == HttpResponseCode.Success) {
  81. ReportThinking.ad_end('exchange_diamond');
  82. let userData = DataSystem.getData(UserData);
  83. //DataSystem.getData(UserData).diamond += this.exChangeCfg.exchangeDiamond;
  84. WindowSystem.close(this.node);
  85. ReportThinking.currency_increase('diamond', userData.diamond, this.exChangeCfg.exchangeDiamond, this.exChangeCfg.exchangeDiamond + userData.diamond, 'exchange_video');
  86. WindowSystem.open("prefabs/ui/turntable/prizeFly", WindowOpenMode.NotCloseAndAdd, [{ type: this.enterType, num: this.exChangeCfg.exchangeDiamond }]);
  87. WindowSystem.showTips("兑换成功");
  88. } else {
  89. WindowSystem.showTips("兑换失败");
  90. }
  91. } else {
  92. WindowSystem.showTips("观看视频失败");
  93. }
  94. }
  95. }
  96. private onClickExchange() {
  97. if (this.exChangeCfg.type == "bonus") {
  98. if (this.usedExchangeTimes < this.limitNum && DataSystem.getData(UserData).bonus >= this.exChangeCfg.exchangeBonus) {
  99. this.exchangeByBonus();
  100. } else {
  101. if (this.usedExchangeTimes >= this.limitNum) { WindowSystem.showTips("兑换次数用完"); return; }
  102. DataSystem.getData(UserData).bonus < this.exChangeCfg.exchangeBonus && WindowSystem.showTips("红包币不足");
  103. }
  104. } else {
  105. if (this.usedExchangeTimes < this.limitNum) {
  106. this.exchangeByVideo();
  107. } else {
  108. WindowSystem.showTips("兑换次数用完");
  109. }
  110. }
  111. }
  112. }