Store.ts 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. import CommonUtility from "../../../Script/CustomClass/CommonUtility";
  2. const {ccclass, property} = cc._decorator;
  3. const ViewType = {
  4. Diamond: 0,
  5. Gift: 1,
  6. Exchange: 2,
  7. };
  8. @ccclass
  9. export default class Store extends cc.Component {
  10. private typeNode: cc.Node = null;
  11. private coinLab: cc.Label = null;
  12. private contentNode: cc.Node = null;
  13. private descNode: cc.Node = null;
  14. private tipNode: cc.Node = null;
  15. private itemNode: cc.Node = null;
  16. private gotNode: cc.Node = null;
  17. private exchangeNode: cc.Node = null;
  18. private hook: any = null;
  19. private platformId: number = 0;
  20. private selfUserID: number = -1;
  21. //数据
  22. private allData: any = null;
  23. //默认0 钻石0 礼物1 兑换2
  24. private type: number = -1;
  25. private paying: boolean = false;
  26. private buyIndex: number = 0;
  27. private buyOrExchangeCount: number = 1;
  28. private curGoodInfo: any = null;
  29. protected onLoad(): void {
  30. this.initProp();
  31. }
  32. private initProp() {
  33. this.typeNode = this.node.getChildByName("typeTitle");
  34. if (this.typeNode) {
  35. let idx = 0;
  36. for (let node of this.typeNode.children) {
  37. CommonUtility.getInstance().bindNodeHandler(this, node, "onclickType", false, `${idx++}`);
  38. }
  39. }
  40. this.coinLab = cc.find("coinInfo/coinLab", this.node)?.getComponent(cc.Label);
  41. this.contentNode = cc.find("itemView/view/content", this.node);
  42. this.descNode = cc.find("itemView/view/descLab", this.node);
  43. this.tipNode = cc.find("itemView/view/tipLab", this.node);
  44. this.itemNode = this.contentNode?.getChildByName("item");
  45. this.itemNode.parent = null;
  46. this.gotNode = this.node.getChildByName("gotNode");
  47. this.gotNode.active = false;
  48. this.exchangeNode = this.node.getChildByName("exchangeNode");
  49. this.exchangeNode.active = false;
  50. }
  51. SetHook(hook: any) {
  52. this.hook = hook;
  53. }
  54. ShowView() {
  55. this.node.active = true;
  56. this.buyIndex = 0;
  57. this.buyOrExchangeCount = 1;
  58. this.updateCoin();
  59. }
  60. protected onEnable(): void {
  61. }
  62. protected start(): void {
  63. //noraml IOS1 Android2
  64. if (cc.sys.isNative) {
  65. if (cc.sys.OS_IOS == cc.sys.os) {
  66. this.platformId = 1;
  67. } else if (cc.sys.OS_ANDROID == cc.sys.os) {
  68. this.platformId = 2;
  69. }
  70. } else {
  71. this.platformId = 1;
  72. }
  73. this.updateCoin();
  74. this.allData = {};
  75. this.type = 0;
  76. this.descNode.active = false;
  77. this.tipNode.active = false;
  78. let curTypeNode = this.typeNode.getChildByName(`type${this.type}`);
  79. curTypeNode.getComponent(cc.Toggle).isChecked = true;
  80. this.getData();
  81. }
  82. private getData() {
  83. //[{"shopId":1,"platformId":1,"price":2290,"num":2000},{"shopId":2,"platformId":1,"price":120,"num":100}]
  84. let self = this;
  85. let webUrl = `${window["PHP_HOME"]}/shop.php?GetMark=100&platformId=${this.platformId}`;
  86. window["WebCenter"].GetData(webUrl, null, function (data: any) {
  87. if (data == null) return;
  88. let Res = JSON.parse(data);
  89. cc.log("get good list: ", Res);
  90. if (Res && Res.length) {
  91. Res.sort(function(d1: any, d2: any) {
  92. return d2.num - d1.num;
  93. });
  94. self.allData[ViewType.Diamond] = JSON.parse(JSON.stringify(Res));
  95. self.updateView();
  96. }
  97. });
  98. return;
  99. webUrl = `${window["PHP_HOME"]}/shop.php?GetMark=100&platformId=${this.platformId}`;
  100. window["WebCenter"].GetData(webUrl, null, function (data: any) {
  101. if (data == null) return;
  102. let Res = JSON.parse(data);
  103. cc.log("get good list: ", Res);
  104. if (Res && Res.length) {
  105. Res.sort(function(d1: any, d2: any) {
  106. return d2.num - d1.num;
  107. });
  108. self.allData[ViewType.Gift] = JSON.parse(JSON.stringify(Res));
  109. }
  110. });
  111. }
  112. private getConvertibleGoodsdata() {
  113. let self = this;
  114. let webUrl = `${window["PHP_HOME"]}/shop.php?GetMark=100&platformId=${this.platformId}`;
  115. window["WebCenter"].GetData(webUrl, null, function (data: any) {
  116. if (data == null) return;
  117. let Res = JSON.parse(data);
  118. cc.log("get good list: ", Res);
  119. if (Res && Res.length) {
  120. Res.sort(function(d1: any, d2: any) {
  121. return d2.num - d1.num;
  122. });
  123. self.allData[ViewType.Exchange] = JSON.parse(JSON.stringify(Res));
  124. self.updateView();
  125. }
  126. });
  127. }
  128. private updateView() {
  129. let curListData = this.allData[this.type];
  130. if (null == curListData || !curListData.length) {
  131. this.contentNode.active = false;
  132. this.tipNode.active = true;
  133. return;
  134. }
  135. this.contentNode.active = true;
  136. let showBuyDiamond = ViewType.Diamond == this.type;
  137. let showExchange = ViewType.Exchange == this.type;
  138. this.contentNode.getComponent(cc.Widget).top = showExchange ? 60 : 0;
  139. this.descNode.active = showExchange;
  140. this.tipNode.active = false;
  141. let addCount = curListData.length - this.contentNode.childrenCount;
  142. if (addCount > 0) {
  143. for (let idx = 0; idx < addCount; ++idx) {
  144. let itemNode = cc.instantiate(this.itemNode);
  145. itemNode.parent = this.contentNode;
  146. }
  147. }
  148. for (let idx = 0; idx < this.contentNode.childrenCount; ++idx) {
  149. let curData = curListData[idx];
  150. let itemNode = this.contentNode.children[idx];
  151. if (false == !!curData) {
  152. itemNode.active = false;
  153. continue;
  154. }
  155. //字
  156. let countLab = cc.find("labs/countLab", itemNode);
  157. let nameLab = cc.find("labs/nameLab", itemNode);
  158. let priceLab = cc.find("labs/priceLab", itemNode);
  159. let numLab = cc.find("labs/numLab", itemNode);
  160. countLab.active = showBuyDiamond;
  161. nameLab.active = !showBuyDiamond;
  162. priceLab.active = showExchange;
  163. numLab.active = showExchange;
  164. if (showBuyDiamond) {
  165. countLab.getComponent(cc.Label).string = "" + curData.num;
  166. } else {
  167. nameLab.getComponent(cc.Label).string = curData.name;
  168. if (showExchange) {
  169. priceLab.getComponent(cc.Label).string = `單價:${curData.price}鑽石`
  170. numLab.getComponent(cc.Label).string = `x${curData.count}`;
  171. }
  172. }
  173. //按鈕
  174. let btnBuy = itemNode.getChildByName("btnBuy");
  175. let btnBuyGift = itemNode.getChildByName("btnBuyGift");
  176. let btnExchange = itemNode.getChildByName("btnExchange");
  177. let giftContent = itemNode.getChildByName("content");
  178. btnBuy.active = showBuyDiamond;
  179. btnBuyGift.active = !showBuyDiamond && !showExchange;
  180. giftContent.active = !showBuyDiamond && !showExchange;
  181. btnExchange.active = showExchange;
  182. let itemNodeHandlerName = "";
  183. if (showBuyDiamond) {
  184. itemNodeHandlerName = "onclickBuy";
  185. CommonUtility.getInstance().bindNodeHandler(this, btnBuy, "onclickBuy", true, `${idx}`, true);
  186. btnBuy.getChildByName("lab").getComponent(cc.Label).string = window["PAY_CURRENCY_SYMBOL"] + curData.price;
  187. } else if (showExchange) {
  188. itemNodeHandlerName = "onclickExchangeGift";
  189. CommonUtility.getInstance().bindNodeHandler(this, btnExchange, "onclickExchangeGift", true, `${idx}`, true);
  190. } else {
  191. itemNodeHandlerName = "onclickBuyGift";
  192. CommonUtility.getInstance().bindNodeHandler(this, btnExchange, "onclickBuyGift", true, `${idx}`, true);
  193. giftContent.getChildByName("lab").getComponent(cc.Label).string = `${curData.price}`;
  194. }
  195. CommonUtility.getInstance().bindNodeHandler(this, itemNode, itemNodeHandlerName, true, `${idx}`, true);
  196. //图
  197. let descBgNode = cc.find("bg/desc-bg", itemNode);
  198. descBgNode.width = showBuyDiamond ? 150 : 120;
  199. let iconNode = cc.find("bg/icon", itemNode);
  200. let iconIndex = idx + 1 > 6 ? 6 : idx + 1;
  201. let pathKey = showBuyDiamond ? "item" : "gift";
  202. cc["gPreLoader"].LoadRes(`Image_${pathKey}${iconIndex}`, "Store", (sprFrame: cc.SpriteFrame) => {
  203. iconNode.getComponent(cc.Sprite).spriteFrame = sprFrame;
  204. });
  205. }
  206. }
  207. private updateCoin() {
  208. let coin = 0;
  209. if (-1 == this.selfUserID) {
  210. let pGlobalUserData = window["g_GlobalUserInfo"].GetGlobalUserData();
  211. this.selfUserID = pGlobalUserData.dwUserID;
  212. coin = pGlobalUserData.llUserIngot;
  213. }
  214. let self = this;
  215. let webUrl = window["PHP_HOME"] + '/UserFunc.php?&GetMark=5&dwUserID=' + this.selfUserID;
  216. window["WebCenter"].GetData(webUrl, 3, function (data: any) {
  217. if (data == null) return;
  218. let Res = JSON.parse(data);
  219. if (Res.UserMedal != null) {
  220. coin = Res.UserMedal;
  221. }
  222. self.coinLab.string = "" + coin;
  223. });
  224. }
  225. onclickType(event: any, index: string) {
  226. let curType = Number(index);
  227. if (this.type == curType) {
  228. return;
  229. }
  230. this.type = curType;
  231. // if (ViewType.Exchange == curType) {
  232. // this.getConvertibleGoodsdata();
  233. // } else {
  234. // this.updateView();
  235. // }
  236. this.updateView();
  237. }
  238. onclickBuy(event: any, index: string) {
  239. if (!this.allData[this.type]) {
  240. return;
  241. }
  242. let goodInfo = this.allData[this.type][index];
  243. let sendData = {
  244. userId: this.selfUserID,
  245. shopId: goodInfo.shopId,
  246. platformId: this.platformId
  247. };
  248. //info { return, data, timestamp }
  249. let self = this;
  250. let webUrl = `${window["PHP_HOME"]}/pay.php`;
  251. window["WebCenter"].httpPOST(webUrl, sendData, function (info: any) {
  252. cc.log("post buy good, ", info);
  253. if ("0" != info.return && 0 != info.return) {
  254. return cc.error("post buy good Error!");
  255. }
  256. self.buyIndex = Number(index);
  257. self.hook.setOrderId(info.orderId, self.buyGoodReturn.bind(self));
  258. ThirdPartyBuyGood(JSON.stringify({
  259. itemID: goodInfo.buyId
  260. }));
  261. });
  262. }
  263. onclickBuyGift(event: any, index: string) {
  264. if (this.exchangeNode.active) {
  265. return;
  266. }
  267. this.onOpenExchangeView(Number(index));
  268. }
  269. onclickExchangeGift(event: any, index: string) {
  270. if (this.exchangeNode.active) {
  271. return;
  272. }
  273. this.onOpenExchangeView(Number(index));
  274. }
  275. onOpenExchangeView(index: number) {
  276. this.exchangeNode.active = true;
  277. this.buyIndex = index;
  278. this.showExchangeView();
  279. }
  280. onclickConfirmToBuyOrExchange() {
  281. let isBuy = ViewType.Gift == this.type;
  282. this.buyOrExchangeCount
  283. }
  284. onclickClose() {
  285. this.node.active = false;
  286. }
  287. onclickCloseGotView() {
  288. this.gotNode.active = false;
  289. }
  290. onclickCloseExchangeView() {
  291. this.exchangeNode.active = false;
  292. }
  293. onclickAddGoodsCount() {
  294. if (this.buyOrExchangeCount <= 1) {
  295. return;
  296. }
  297. this.buyOrExchangeCount -= 1;
  298. this.updateCountPriceAndButton();
  299. }
  300. onclickSubtractGoodsCount() {
  301. if (this.buyOrExchangeCount >= this.curGoodInfo.count) {
  302. return;
  303. }
  304. this.buyOrExchangeCount += 1;
  305. this.updateCountPriceAndButton();
  306. }
  307. onInputGoodsCountEnd(event: any) {
  308. let editBox = event.target;
  309. let curCount = Number(editBox.string);
  310. if (curCount > this.curGoodInfo.count) {
  311. curCount = this.curGoodInfo.count;
  312. }
  313. if (curCount < 1) {
  314. curCount = 1;
  315. }
  316. this.buyOrExchangeCount = curCount;
  317. this.updateCountPriceAndButton();
  318. }
  319. private buyGoodReturn(newCoin: number = 0) {
  320. if (!!newCoin) {
  321. this.coinLab.string = "" + newCoin;
  322. } else {
  323. this.updateCoin();
  324. }
  325. this.showBoughtCoin(this.buyIndex);
  326. this.buyIndex = 0;
  327. }
  328. private showBoughtCoin(index: number) {
  329. let buyInfo = this.allData[this.type][index];
  330. let isExchange = ViewType.Exchange == this.type;
  331. let isDiamond = ViewType.Diamond == this.type;
  332. this.gotNode.active = true;
  333. cc.find("bg/buyTitle", this.gotNode).active = !isExchange;
  334. cc.find("bg/exchangeTitle", this.gotNode).active = isExchange;
  335. let descLab = this.gotNode.getChildByName("descLab");
  336. let descStr = "";
  337. if (isDiamond || isExchange) {
  338. let count = isDiamond ? buyInfo.num : this.buyOrExchangeCount * buyInfo.price
  339. descStr = `<color=#721e10>恭喜你已獲得<color=#008d3b>${count}</c>鑽石!</c>`;
  340. } else {
  341. descStr = `<color=#721e10>恭喜你已獲得<color=#008d3b>${this.curGoodInfo.name}x${this.buyOrExchangeCount}</c>!</c>`;
  342. }
  343. descLab.getComponent(cc.RichText).string = descStr;
  344. let iconNode = this.gotNode.getChildByName("icon");
  345. let iconIndex = index + 1 > 6 ? 6 : index + 1;
  346. let pathKey = isDiamond ? "item" : "gift";
  347. cc["gPreLoader"].LoadRes(`Image_${pathKey}${iconIndex}`, 'Store', (sprFrame: cc.SpriteFrame) => {
  348. iconNode.getComponent(cc.Sprite).spriteFrame = sprFrame;
  349. });
  350. }
  351. private showExchangeView() {
  352. let isBuy = ViewType.Gift == this.type;
  353. this.curGoodInfo = this.allData[this.type][this.buyIndex];
  354. let titleLab = cc.find("labs/titleLab", this.exchangeNode);
  355. let nameLab = cc.find("labs/nameLab", this.exchangeNode);
  356. let numLab = cc.find("labs/numLab", this.exchangeNode);
  357. titleLab.getComponent(cc.Label).string = isBuy ? "購買禮物" : "兌換鑽石";
  358. nameLab.getComponent(cc.Label).string = this.curGoodInfo.name;
  359. numLab.active = !isBuy;
  360. if (!isBuy) {
  361. numLab.getComponent(cc.Label).string = `擁有 ${this.curGoodInfo.count}`;
  362. }
  363. this.updateCountPriceAndButton();
  364. }
  365. private updateCountPriceAndButton() {
  366. //数量价格
  367. let editBox = cc.find("exchange/editBox", this.exchangeNode);
  368. let priceLab = cc.find("labs/priceLab", this.exchangeNode);
  369. editBox.getComponent(cc.EditBox).string = `${this.buyOrExchangeCount}`;
  370. priceLab.getComponent(cc.Label).string = `${this.curGoodInfo.price * this.buyOrExchangeCount}`;
  371. //按钮状态
  372. let btnSub = cc.find("exchange/btnSub", this.exchangeNode);
  373. let btnAdd = cc.find("exchange/btnAdd", this.exchangeNode);
  374. let canSub = this.buyOrExchangeCount > 1;
  375. let canAdd = this.buyOrExchangeCount < this.curGoodInfo.count;
  376. btnSub.getComponent(cc.Button).interactable = canSub
  377. btnAdd.getComponent(cc.Button).interactable = canAdd;
  378. btnSub.getChildByName("top").active = canSub;
  379. btnAdd.getChildByName("top").active = canAdd;
  380. }
  381. }