Wish.ts 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. import ViewObject from "../../../main/ViewObject";
  2. import FqPay, { UM_EVENT_ID } from "../../../pay/FqPay";
  3. import { HttpStateType, ReveData } from "../../../util/CHttp";
  4. import { __FirstPayRewardData, __PayData, __WishData } from "../../data/sdata/SManage";
  5. const { ccclass, property } = cc._decorator;
  6. interface WishData {
  7. playerPay: Map<string, number>,
  8. hdPayData: Array<__WishData>
  9. }
  10. @ccclass
  11. export default class Wish extends ViewObject {
  12. @property([cc.Node])
  13. itemList: cc.Node[] = [];
  14. @property(cc.Prefab)
  15. goodItem: cc.Prefab = null;
  16. wishData: WishData = null;
  17. curHdPayData = null;
  18. target: cc.Node = null;
  19. /**
  20. *
  21. * @param prev 父界面
  22. */
  23. public show(prev?: ViewObject) {
  24. if (prev) {
  25. this.prev = prev;
  26. this.prev.__close();
  27. }
  28. this.main.viewManage.popView1(this.node);
  29. if (this.main && this.main.gameHttp) {
  30. this.main.gameHttp.pushEvent(this);
  31. }
  32. }
  33. getWishData() {
  34. let msg = {};
  35. this.main.gameHttp.sendJson(`hdpay/v1/hdData`, msg, (state, reve: ReveData) => {
  36. this.main.stopLoad();
  37. if (state == HttpStateType.SUCCESS) {
  38. if (reve.retCode == 0) {
  39. console.log("==reve=getWishData===", reve)
  40. this.wishData = reve.data;
  41. this.initItem();
  42. this.show();
  43. } else {
  44. this.main.showTips(reve.message);
  45. }
  46. } else {
  47. this.main.showTips('网络异常');
  48. }
  49. });
  50. }
  51. playVideo(callback: (result: number) => void) {
  52. let eventId1 = UM_EVENT_ID.ad_wish_0;
  53. let eventId2 = UM_EVENT_ID.ad_wish_1;
  54. let fqPay: FqPay = new FqPay(this.main);
  55. fqPay.adVideo(callback, eventId1, eventId2);
  56. }
  57. videoFree(id: number) {
  58. let msg = {
  59. hdId: id
  60. };
  61. this.main.gameHttp.sendJson(`hdpay/v1/freeHd`, msg, (state, reve: ReveData) => {
  62. this.main.stopLoad();
  63. if (state == HttpStateType.SUCCESS) {
  64. if (reve.retCode == 0) {
  65. this.refresh();
  66. this.main.showReward(reve);
  67. } else {
  68. this.main.showTips(reve.message);
  69. }
  70. } else {
  71. this.main.showTips('网络异常');
  72. }
  73. });
  74. }
  75. initItem() {
  76. this.wishData.hdPayData.forEach((data, index) => {
  77. let item = this.itemList[index];
  78. let parent = item.getChildByName("goodList");
  79. let lbtimes = item.getChildByName("lbtimes");
  80. let btnPay = item.getChildByName("btnPay");
  81. let btnGeted = item.getChildByName("btnGeted");
  82. let lbpay = btnPay.getChildByName("lbpay");
  83. let times = !this.wishData.playerPay[data.id] ? 0 : this.wishData.playerPay[data.id];
  84. if (lbpay) {
  85. let payData = this.getPayDataById(this.wishData.hdPayData[index].payId);
  86. lbpay.getComponent(cc.Label).string = `$ ${payData.usd}`;
  87. }
  88. lbtimes.getComponent(cc.Label).string = `限购:${times}/${data.maxCount}`;
  89. if (times == data.maxCount) {
  90. btnGeted.active = true;
  91. btnPay.active = false;
  92. }
  93. JSON.parse(data.reward).forEach(rd => {
  94. this.addGood(parent, rd);
  95. });
  96. })
  97. }
  98. addGood(parent: cc.Node, reward: __FirstPayRewardData) {
  99. let good = cc.instantiate(this.goodItem);
  100. parent.addChild(good);
  101. this.initGoodItem(good, reward);
  102. }
  103. initGoodItem(item: cc.Node, reward: __FirstPayRewardData) {
  104. let node = item.getChildByName("infoNode");
  105. let icon = node.getChildByName("icon").getComponent(cc.Sprite);
  106. let frame = item.getChildByName("frame").getComponent(cc.Sprite);
  107. this.initIcon(icon, reward.icon);
  108. this.initFrameIcon(frame, reward.frame);
  109. node.getChildByName("countNode").getChildByName("count").getComponent(cc.Label).string = reward.count + "";
  110. }
  111. public initIcon(icon: cc.Sprite, path: string) {
  112. cc.resources.load(`icon/${path}`, cc.SpriteFrame, (err, spriteFrame: cc.SpriteFrame) => {
  113. if (err) {
  114. cc.error(err);
  115. } else {
  116. icon.spriteFrame = spriteFrame;
  117. }
  118. });
  119. }
  120. public initFrameIcon(icon: cc.Sprite, path: string | number) {
  121. cc.resources.load(`icon/frame/${path}`, cc.SpriteFrame, (err, spriteFrame: cc.SpriteFrame) => {
  122. if (err) {
  123. cc.error(err);
  124. } else {
  125. icon.spriteFrame = spriteFrame;
  126. }
  127. });
  128. }
  129. onClick(eventTouch: cc.Event.EventTouch, data: string) {
  130. this.target = eventTouch.target;
  131. this.curHdPayData = this.wishData.hdPayData[data];
  132. if (data == "0") {
  133. this.playVideo((result) => {
  134. if (result == 1) {
  135. this.videoFree(this.curHdPayData.id);
  136. } else {
  137. this.main.showTips("广告还未准备好");
  138. }
  139. });
  140. } else if (data == "1") {
  141. this.pay(this.curHdPayData.payId);
  142. } else if (data == "2") {
  143. this.pay(this.curHdPayData.payId);
  144. }
  145. }
  146. pay(payId: string) {
  147. let fqPay: FqPay = new FqPay(this.main);
  148. let payData = this.getPayDataById(payId);
  149. if (payData == null) {
  150. console.log("===payData=null===", payId)
  151. return
  152. }
  153. fqPay.pay(payData, () => {
  154. this.refresh();
  155. })
  156. }
  157. getPayDataById(payId: string) {
  158. let payData = null;
  159. this.main.sManage.payDatas.forEach(data => {
  160. if (data.id == Number(payId)) {
  161. payData = data;
  162. }
  163. })
  164. return payData;
  165. }
  166. refresh() {
  167. let item = this.target.parent;
  168. let lbtimes = item.getChildByName("lbtimes");
  169. let btnPay = item.getChildByName("btnPay");
  170. let btnGeted = item.getChildByName("btnGeted");
  171. let times = !this.wishData.playerPay[this.curHdPayData.id] ? 0 : this.wishData.playerPay[this.curHdPayData.id];
  172. times++;
  173. lbtimes.getComponent(cc.Label).string = `限购:${times}/${this.curHdPayData.maxCount}`;
  174. if (times == this.curHdPayData.maxCount) {
  175. btnPay.active = false;
  176. btnGeted.active = true;
  177. }
  178. }
  179. }