MyWalletUI.ts 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. import { _decorator, Component, Node, Prefab, instantiate, EventHandler, Label, Button, Sprite, SpriteFrame, Texture2D, ImageAsset, color } from 'cc';
  2. import { ADHelper } from '../ad/ADHelper';
  3. import { DataSystem } from '../core/data/DataSystem';
  4. import { Http } from '../core/net/Http';
  5. import { ResourceLoader } from '../core/resourceManager/ResourceLoader';
  6. import { WindowSystem } from '../core/ui/window/WindowSystem';
  7. import { ISJSB } from '../data/jsb/platform';
  8. import { UserData } from '../data/UserData';
  9. import { ReportThinking } from '../ReportThinking';
  10. import { WithdrawalItem } from './WithdrawalItem';
  11. const { ccclass, property, requireComponent } = _decorator;
  12. @ccclass('MyWalletUI')
  13. @requireComponent(ResourceLoader)
  14. export class MyWalletUI extends Component {
  15. @property({ tooltip: "头像", type: Sprite }) private headIcon: Sprite;
  16. @property({ tooltip: "提现列表父级", type: Node }) private itemParent: Node;
  17. @property({ tooltip: "选中提现档次变动回调", type: EventHandler }) private chooseChangeBack: EventHandler;
  18. @property({ tooltip: "提现提示文本", type: Label }) private withdrawalTipTxt: Label;
  19. @property({ tooltip: "提现按钮", type: Node }) private withdrawlBtn: Node;
  20. @property({ tooltip: "总资产文本", type: Label }) private assetsTxt: Label;
  21. @property({ tooltip: "广告", type: ADHelper }) private adNode: ADHelper;
  22. private withdrawalID = 0;//提现id
  23. private isWithdrawal = false;//是否正在提现
  24. private itemArray: Node[] = [];//提现档次item
  25. private itemData: any[];
  26. private chooseID: number = -1;//选中提现档次
  27. private withdrawalType: number = -1;//选中提现类型
  28. start() {
  29. this.initUI();
  30. }
  31. private async initUI() {
  32. let avator = DataSystem.getData(UserData).avator;
  33. if (ISJSB() && avator) {
  34. let img = await await this.getComponent(ResourceLoader).loadRemote<ImageAsset>(avator, { ext: ".png" });
  35. const spriteFrame = new SpriteFrame();
  36. const texture = new Texture2D();
  37. texture.image = img;
  38. spriteFrame.texture = texture;
  39. this.headIcon.spriteFrame = spriteFrame;
  40. }
  41. this.initBonus();
  42. //获取提现列表
  43. let result = await this.getComponent(Http).send("/api/user/withdrawList");
  44. if (result && result.code == 0) {
  45. this.initItemList(result.data);
  46. }
  47. }
  48. private chooseChange(chooseID: number, message: string, withdrawalType: number, withdrawalID: number) {
  49. // if (chooseID == this.chooseID) {
  50. // return;
  51. // }
  52. this.chooseID > -1 && (this.itemArray[this.chooseID].getComponent(WithdrawalItem).chooseImg.active = false);
  53. this.chooseID = chooseID;
  54. this.withdrawalID = withdrawalID;
  55. this.withdrawalType = withdrawalType;
  56. this.itemArray[this.chooseID].getComponent(WithdrawalItem).chooseImg.active = true;
  57. if (message) {
  58. this.withdrawalTipTxt.color = color(214, 51, 51, 255);
  59. this.withdrawalTipTxt.string = message;
  60. this.withdrawlBtn.getComponent(Button).interactable = false;
  61. this.withdrawlBtn.getComponent(Sprite).grayscale = true;
  62. } else {
  63. this.withdrawalTipTxt.string = "当前可提现";
  64. this.withdrawalTipTxt.color = color(71, 148, 40, 255);
  65. this.withdrawlBtn.getComponent(Button).interactable = true;
  66. this.withdrawlBtn.getComponent(Sprite).grayscale = false;
  67. }
  68. }
  69. public async withdrawal() {
  70. if (this.isWithdrawal) {
  71. return;
  72. }
  73. this.isWithdrawal = true;
  74. let result = await this.getComponent(Http).send("/api/user/withdraw", { id: this.withdrawalID, type: this.withdrawalType });
  75. if (result && result.data) {//提现成功
  76. WindowSystem.showTips(result.data.message);
  77. let userData = DataSystem.getData(UserData);
  78. ReportThinking.currency_decrease('bonus', userData.bonus, result.data.bonus, userData.bonus - result.data.bonus, 'withdraw');
  79. result.data.bonus && (DataSystem.getData(UserData).bonus -= result.data.bonus);
  80. result.data.bonus && this.initBonus();
  81. // result.data.list && this.initItemList(result.data.list);
  82. let _result = await this.getComponent(Http).send("/api/user/withdrawList");
  83. if (_result && _result.code == 0) {
  84. this.initItemList(_result.data);
  85. }
  86. (Math.random() * 100) <= 35 && this.adNode.show();
  87. }
  88. this.isWithdrawal = false;
  89. }
  90. //更新item
  91. public async initItemList(list: Array<any>) {
  92. //#region 只显示一个0.3
  93. let isDelete = false;
  94. for (let k = 0; k < list.length; k++) {
  95. if (list[k].type == WithDrawType.Frist) {
  96. isDelete = true;
  97. break;
  98. }
  99. }
  100. if (isDelete) {
  101. for (let k = 0; k < list.length; k++) {
  102. if (list[k].type != WithDrawType.Frist && list[k].money == 0.3) {
  103. list.splice(k, 1);
  104. break;
  105. }
  106. }
  107. }
  108. this.itemData = list;
  109. //#endregion
  110. let indexMax = 0;
  111. for (let i = 0; i < list.length; i++) {
  112. if (i > 7) {
  113. break;//最多显示八个档次
  114. }
  115. indexMax = i;
  116. if (i >= this.itemArray.length) {
  117. let prefab = await this.node.getComponent(ResourceLoader).load<Prefab>("prefabs/ui/myWallet/withdrawalItem", Prefab);
  118. let item = instantiate(prefab);
  119. item.parent = this.itemParent;
  120. this.itemArray.push(item);
  121. item.getComponent(WithdrawalItem).chooseChangeBack = this.chooseChangeBack;
  122. item.getComponent(WithdrawalItem).setData(list[i], i);
  123. } else {
  124. this.itemArray[i].getComponent(WithdrawalItem).setData(list[i], i);
  125. }
  126. }
  127. if ((indexMax + 1) < this.itemArray.length) {//删除多余的item
  128. for (let j = 0; j < this.itemArray.length - indexMax - 1; j++) {
  129. this.itemArray[indexMax + 1].destroy();
  130. this.itemArray.splice(indexMax + 1, 1);
  131. }
  132. }
  133. let index = this.chooseID == -1 ? 0 : this.chooseID;
  134. index = (index + 1) > this.itemArray.length ? (this.itemArray.length - 1) : index;
  135. this.chooseChange(index, this.itemData[index].message, this.itemData[index].type, this.itemData[index].id);
  136. }
  137. //更新红包币显示
  138. public initBonus() {
  139. let bonus = DataSystem.getData(UserData).bonus;
  140. if (bonus >= 1000) {
  141. this.assetsTxt.string = bonus + "≈" + (Math.round(bonus / 10000 * 100) / 100) + "元";
  142. } else {
  143. this.assetsTxt.string = bonus + "≈" + (bonus / 10000) + "元";
  144. }
  145. }
  146. }
  147. export enum WithDrawType {
  148. /**
  149. * 裂变提现
  150. */
  151. Share = 1,
  152. /**
  153. * 看视频提现
  154. */
  155. Video,
  156. /**
  157. * 红包兑换提现
  158. */
  159. Bonus,
  160. /**
  161. * 首次提现
  162. */
  163. Frist,
  164. /**
  165. * 彩票提现
  166. */
  167. Lottery
  168. }