Store.ts 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. import CommonUtility from "../../../Script/CustomClass/CommonUtility";
  2. const {ccclass, property} = cc._decorator;
  3. @ccclass
  4. export default class Store extends cc.Component {
  5. private typeNode: cc.Node = null;
  6. private coinLab: cc.Label = null;
  7. private contentNode: cc.Node = null;
  8. private itemNode: cc.Node = null;
  9. private gotNode: cc.Node = null;
  10. private hook: any = null;
  11. private platformId: number = 0;
  12. private selfUserID: number = -1;
  13. private storeData: any = null;
  14. //默认0 钻石0 礼物1
  15. private type: number = -1;
  16. private paying: boolean = false;
  17. private buyIndex: number = 0;
  18. protected onLoad(): void {
  19. this.initProp();
  20. }
  21. private initProp() {
  22. this.typeNode = this.node.getChildByName("typeTitle");
  23. if (this.typeNode) {
  24. let idx = 0;
  25. for (let node of this.typeNode.children) {
  26. CommonUtility.getInstance().bindNodeHandler(this, node, "onclickType", false, `${idx++}`);
  27. }
  28. }
  29. this.coinLab = cc.find("coinInfo/coinLab", this.node)?.getComponent(cc.Label);
  30. this.contentNode = cc.find("itemView/view/content", this.node);
  31. this.itemNode = this.contentNode?.getChildByName("item");
  32. this.itemNode.parent = null;
  33. this.gotNode = this.node.getChildByName("gotNode");
  34. this.gotNode.active = false;
  35. }
  36. SetHook(hook: any) {
  37. this.hook = hook;
  38. }
  39. ShowView() {
  40. this.node.active = true;
  41. }
  42. protected onEnable(): void {
  43. }
  44. protected start(): void {
  45. //noraml IOS1 Android2
  46. if (cc.sys.isNative) {
  47. if (cc.sys.OS_IOS == cc.sys.os) {
  48. this.platformId = 1;
  49. } else if (cc.sys.OS_ANDROID == cc.sys.os) {
  50. this.platformId = 2;
  51. }
  52. } else {
  53. this.platformId = 1;
  54. }
  55. this.updateCoin();
  56. let curTypeNode = this.typeNode.getChildByName(`type${this.type}`);
  57. curTypeNode?.getComponent(cc.Toggle)?.check();
  58. this.getStoreData();
  59. }
  60. private getStoreData() {
  61. //[{"shopId":1,"platformId":1,"price":2290,"num":2000},{"shopId":2,"platformId":1,"price":120,"num":100}]
  62. let self = this;
  63. let webUrl = `${window["PHP_HOME"]}/shop.php?GetMark=100&platformId=${this.platformId}`;
  64. window["WebCenter"].GetData(webUrl, null, function (data: any) {
  65. if (data == null) return;
  66. let Res = JSON.parse(data);
  67. cc.log("get good list: , ", Res);
  68. if (Res && Res.length) {
  69. self.storeData = Res;
  70. self.storeData.sort(function(d1: any, d2: any) {
  71. return d2.num - d1.num;
  72. });
  73. self.updateView(true);
  74. }
  75. });
  76. }
  77. private updateView(isInit: boolean = false) {
  78. if (!this.storeData) {
  79. return;
  80. }
  81. if (isInit) {
  82. for (let idx = 0; idx < this.storeData.length; ++idx) {
  83. let itemNode = cc.instantiate(this.itemNode);
  84. itemNode.parent = this.contentNode;
  85. }
  86. }
  87. for (let idx = 0; idx < this.contentNode.childrenCount; ++idx) {
  88. let curData = this.storeData[idx];
  89. let itemNode = this.contentNode.children[idx];
  90. let countLab = itemNode.getChildByName("countLab");
  91. countLab.getComponent(cc.Label).string = "" + curData.num;
  92. let btnBuy = itemNode.getChildByName("btnBuy");
  93. CommonUtility.getInstance().bindNodeHandler(this, itemNode, "onclickBuy", true, `${idx}`, true);
  94. CommonUtility.getInstance().bindNodeHandler(this, btnBuy, "onclickBuy", true, `${idx}`, true);
  95. btnBuy.getChildByName("lab").getComponent(cc.Label).string = window["PAY_CURRENCY_SYMBOL"] + curData.price;
  96. let iconNode = cc.find("bg/icon", itemNode);
  97. let iconIndex = idx + 1 > 6 ? 6 : idx + 1;
  98. cc["gPreLoader"].LoadRes(`Image_item${iconIndex}`, 'Store', (sprFrame: cc.SpriteFrame) => {
  99. iconNode.getComponent(cc.Sprite).spriteFrame = sprFrame;
  100. });
  101. }
  102. }
  103. private updateCoin() {
  104. let coin = 0;
  105. if (-1 == this.selfUserID) {
  106. let pGlobalUserData = window["g_GlobalUserInfo"].GetGlobalUserData();
  107. this.selfUserID = pGlobalUserData.dwUserID;
  108. coin = pGlobalUserData.llUserIngot;
  109. }
  110. let self = this;
  111. let webUrl = window["PHP_HOME"] + '/UserFunc.php?&GetMark=5&dwUserID=' + this.selfUserID;
  112. window["WebCenter"].GetData(webUrl, 3, function (data: any) {
  113. if (data == null) return;
  114. let Res = JSON.parse(data);
  115. if (Res.UserMedal != null) {
  116. coin = Res.UserMedal;
  117. }
  118. self.coinLab.string = "" + coin;
  119. });
  120. }
  121. onclickType(event: any, index: string) {
  122. let curType = Number(index);
  123. if (this.type == curType) {
  124. return;
  125. }
  126. this.type = curType;
  127. this.updateView();
  128. }
  129. onclickBuy(event: any, index: string) {
  130. if (!this.storeData) {
  131. return;
  132. }
  133. // if (this.paying) {
  134. // return;
  135. // }
  136. // this.paying = true;
  137. let goodInfo = this.storeData[index];
  138. let sendData = {
  139. userId: this.selfUserID,
  140. shopId: goodInfo.shopId,
  141. platformId: this.platformId
  142. };
  143. //info { return, data, timestamp }
  144. let self = this;
  145. let webUrl = `${window["PHP_HOME"]}/pay.php`;
  146. window["WebCenter"].httpPOST(webUrl, sendData, function (info: any) {
  147. cc.log("post buy good, ", info);
  148. if ("0" != info.return && 0 != info.return) {
  149. return cc.error("post buy good Error!");
  150. }
  151. self.buyIndex = Number(index);
  152. self.hook.setOrderId(info.orderId, self.buyGoodReturn.bind(self));
  153. ThirdPartyBuyGood(JSON.stringify({
  154. itemID: goodInfo.buyId
  155. }));
  156. });
  157. }
  158. onclickClose() {
  159. this.node.active = false;
  160. }
  161. onclickCloseGotView() {
  162. this.gotNode.active = false;
  163. }
  164. private buyGoodReturn() {
  165. // this.paying = false;
  166. this.updateCoin();
  167. this.showBoughtCoin(this.buyIndex);
  168. this.buyIndex = 0;
  169. }
  170. private showBoughtCoin(index: number) {
  171. let buyInfo = this.storeData[index];
  172. this.gotNode.active = true;
  173. let descLab = this.gotNode.getChildByName("descLab");
  174. descLab.getComponent(cc.RichText).string = `<color=#721e10>恭喜你已獲得<color=#008d3b>${buyInfo.num}</c>鑽石!</c>`;
  175. let iconNode = this.gotNode.getChildByName("icon");
  176. let iconIndex = index + 1 > 6 ? 6 : index + 1;
  177. cc["gPreLoader"].LoadRes(`Image_item${iconIndex}`, 'Store', (sprFrame: cc.SpriteFrame) => {
  178. iconNode.getComponent(cc.Sprite).spriteFrame = sprFrame;
  179. });
  180. }
  181. }