Store.ts 7.1 KB

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