FirstPay.ts 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. import { GameViewType } from "../../../main/ViewManage";
  2. import ViewObject from "../../../main/ViewObject";
  3. import { HttpStateType, ReveData } from "../../../util/CHttp";
  4. import { __FirstPayData, __FirstPayRewardData } from "../../data/sdata/SManage";
  5. import ShopView from "../shop/ShopView";
  6. const { ccclass, property } = cc._decorator;
  7. interface FirstPayData {
  8. firstPay: {
  9. open: number,
  10. canGet: boolean,
  11. count: number,
  12. },
  13. data: Array<__FirstPayData>
  14. }
  15. enum Type {
  16. data = "data",
  17. get = "get",
  18. }
  19. @ccclass
  20. export default class FirstPay extends ViewObject {
  21. @property([cc.Node])
  22. itemList: cc.Node[] = [];
  23. @property(cc.Prefab)
  24. goodItem: cc.Prefab = null;
  25. firstPayData: FirstPayData = null;
  26. canGet: boolean = true;
  27. target: cc.Node = null;
  28. /**
  29. *
  30. * @param prev 父界面
  31. */
  32. public show(prev?: ViewObject) {
  33. if (prev) {
  34. this.prev = prev;
  35. this.prev.__close();
  36. }
  37. this.main.viewManage.popView1(this.node);
  38. if (this.main && this.main.gameHttp) {
  39. this.main.gameHttp.pushEvent(this);
  40. }
  41. }
  42. getFirstPayData(type: Type | string) {
  43. let msg = {};
  44. this.main.gameHttp.sendJson(`firstPay/v1/${type}`, msg, (state, reve: ReveData) => {
  45. this.main.stopLoad();
  46. if (state == HttpStateType.SUCCESS) {
  47. if (reve.retCode == 0) {
  48. console.log("==reve=getFirstPayData===", reve)
  49. if (type == Type.data) {
  50. this.firstPayData = reve.data;
  51. this.initItem();
  52. this.show();
  53. } else if (type == Type.get) {
  54. this.refresh(this.target);
  55. this.main.showReward(reve);
  56. }
  57. } else {
  58. this.main.showTips(reve.message);
  59. }
  60. } else {
  61. this.main.showTips('网络异常');
  62. }
  63. });
  64. }
  65. initItem() {
  66. this.canGet = this.firstPayData.firstPay.canGet;
  67. this.firstPayData.data.forEach((data, index) => {
  68. let parent = this.itemList[index].getChildByName("goodList");
  69. JSON.parse(data.reward).forEach(rd => {
  70. this.addGood(parent, rd);
  71. });
  72. })
  73. this.initBtn();
  74. }
  75. initBtn() {
  76. if (this.firstPayData.firstPay.open) {
  77. this.itemList.forEach((item, index) => {
  78. let btnPay = item.getChildByName("btn_pay");
  79. let btnGet = item.getChildByName("btn_get");
  80. let lbGet = btnGet.getChildByName("lbget");
  81. let lbGeted = btnGet.getChildByName("lbgeted");
  82. btnGet.active = true;
  83. btnPay.active = false;
  84. if (index > this.firstPayData.firstPay.count) {
  85. btnGet.getComponent(cc.Button).interactable = false;
  86. lbGet.active = true;
  87. lbGeted.active = false;
  88. } else if (index < this.firstPayData.firstPay.count) {
  89. btnGet.getComponent(cc.Button).interactable = false;
  90. lbGet.active = false;
  91. lbGeted.active = true;
  92. } else if (index == this.firstPayData.firstPay.count) {
  93. lbGet.active = true;
  94. lbGeted.active = false;
  95. }
  96. if (!this.firstPayData.firstPay.canGet) {
  97. btnGet.getComponent(cc.Button).interactable = false;
  98. }
  99. })
  100. } else {
  101. this.itemList.forEach(item => {
  102. item.getChildByName("btn_pay").active = true;
  103. item.getChildByName("btn_get").active = false;
  104. })
  105. }
  106. }
  107. addGood(parent: cc.Node, reward: __FirstPayRewardData) {
  108. let good = cc.instantiate(this.goodItem);
  109. parent.addChild(good);
  110. this.initGoodItem(good, reward);
  111. }
  112. initGoodItem(item: cc.Node, reward: __FirstPayRewardData) {
  113. let node = item.getChildByName("infoNode");
  114. let icon = node.getChildByName("icon").getComponent(cc.Sprite);
  115. let frame = item.getChildByName("frame").getComponent(cc.Sprite);
  116. this.initIcon(icon, reward.icon);
  117. this.initFrameIcon(frame, reward.frame);
  118. node.getChildByName("countNode").getChildByName("count").getComponent(cc.Label).string = reward.count + "";
  119. }
  120. public initIcon(icon: cc.Sprite, path: string) {
  121. cc.resources.load(`icon/${path}`, cc.SpriteFrame, (err, spriteFrame: cc.SpriteFrame) => {
  122. if (err) {
  123. cc.error(err);
  124. } else {
  125. icon.spriteFrame = spriteFrame;
  126. }
  127. });
  128. }
  129. public initFrameIcon(icon: cc.Sprite, path: string | number) {
  130. cc.resources.load(`icon/frame/${path}`, cc.SpriteFrame, (err, spriteFrame: cc.SpriteFrame) => {
  131. if (err) {
  132. cc.error(err);
  133. } else {
  134. icon.spriteFrame = spriteFrame;
  135. }
  136. });
  137. }
  138. onClickPay() {
  139. this.exitDistroy();
  140. this.main.viewManage.loadFunc(GameViewType.tap_shop, (viewObject: ViewObject) => {
  141. let shopView = viewObject as ShopView;
  142. shopView.init(Number(101));
  143. shopView.show();
  144. });
  145. }
  146. onClickGet(eventTouch: cc.Event.EventTouch, data: string) {
  147. if (this.canGet) {
  148. this.target = eventTouch.target;
  149. this.getFirstPayData(Type.get);
  150. }
  151. }
  152. refresh(node: cc.Node) {
  153. this.canGet = false;
  154. node.getComponent(cc.Button).interactable = false;
  155. node.getChildByName("lbgeted").active = true;
  156. node.getChildByName("lbget").active = false;
  157. }
  158. }