| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202 |
- import CommonUtility from "../../../Script/CustomClass/CommonUtility";
- const {ccclass, property} = cc._decorator;
- @ccclass
- export default class Store extends cc.Component {
- private typeNode: cc.Node = null;
- private coinLab: cc.Label = null;
- private contentNode: cc.Node = null;
- private itemNode: cc.Node = null;
- private gotNode: cc.Node = null;
- private hook: any = null;
- private platformId: number = 0;
- private selfUserID: number = -1;
- private storeData: any = null;
- //默认0 钻石0 礼物1
- private type: number = -1;
- private paying: boolean = false;
- private buyIndex: number = 0;
- protected onLoad(): void {
- this.initProp();
- }
- private initProp() {
- this.typeNode = this.node.getChildByName("typeTitle");
- if (this.typeNode) {
- let idx = 0;
- for (let node of this.typeNode.children) {
- CommonUtility.getInstance().bindNodeHandler(this, node, "onclickType", false, `${idx++}`);
- }
- }
- this.coinLab = cc.find("coinInfo/coinLab", this.node)?.getComponent(cc.Label);
- this.contentNode = cc.find("itemView/view/content", this.node);
- this.itemNode = this.contentNode?.getChildByName("item");
- this.itemNode.parent = null;
- this.gotNode = this.node.getChildByName("gotNode");
- this.gotNode.active = false;
- }
- SetHook(hook: any) {
- this.hook = hook;
- }
- ShowView() {
- this.node.active = true;
- }
- protected onEnable(): void {
-
- }
- protected start(): void {
- //noraml IOS1 Android2
- if (cc.sys.isNative) {
- if (cc.sys.OS_IOS == cc.sys.os) {
- this.platformId = 1;
- } else if (cc.sys.OS_ANDROID == cc.sys.os) {
- this.platformId = 2;
- }
- } else {
- this.platformId = 1;
- }
- this.updateCoin();
- let curTypeNode = this.typeNode.getChildByName(`type${this.type}`);
- curTypeNode?.getComponent(cc.Toggle)?.check();
- this.getStoreData();
- }
- private getStoreData() {
- //[{"shopId":1,"platformId":1,"price":2290,"num":2000},{"shopId":2,"platformId":1,"price":120,"num":100}]
- let self = this;
- let webUrl = `${window["PHP_HOME"]}/shop.php?GetMark=100&platformId=${this.platformId}`;
- window["WebCenter"].GetData(webUrl, null, function (data: any) {
- if (data == null) return;
- let Res = JSON.parse(data);
- cc.log("get good list: , ", Res);
- if (Res && Res.length) {
- self.storeData = Res;
- self.storeData.sort(function(d1: any, d2: any) {
- return d2.num - d1.num;
- });
- self.updateView(true);
- }
- });
- }
- private updateView(isInit: boolean = false) {
- if (!this.storeData) {
- return;
- }
- if (isInit) {
- for (let idx = 0; idx < this.storeData.length; ++idx) {
-
- let itemNode = cc.instantiate(this.itemNode);
- itemNode.parent = this.contentNode;
- }
- }
- for (let idx = 0; idx < this.contentNode.childrenCount; ++idx) {
- let curData = this.storeData[idx];
- let itemNode = this.contentNode.children[idx];
- let countLab = itemNode.getChildByName("countLab");
- countLab.getComponent(cc.Label).string = "" + curData.num;
- let btnBuy = itemNode.getChildByName("btnBuy");
- CommonUtility.getInstance().bindNodeHandler(this, btnBuy, "onclickBuy", true, `${idx}`, true);
- btnBuy.getChildByName("lab").getComponent(cc.Label).string = window["PAY_CURRENCY_SYMBOL"] + curData.price;
- let iconNode = cc.find("bg/icon", itemNode);
- let iconIndex = idx + 1 > 6 ? 6 : idx + 1;
- cc["gPreLoader"].LoadRes(`Image_item${iconIndex}`, 'Store', (sprFrame: cc.SpriteFrame) => {
- iconNode.getComponent(cc.Sprite).spriteFrame = sprFrame;
- });
- }
- }
- private updateCoin() {
- let coin = 0;
- if (-1 == this.selfUserID) {
- let pGlobalUserData = window["g_GlobalUserInfo"].GetGlobalUserData();
- this.selfUserID = pGlobalUserData.dwUserID;
- coin = pGlobalUserData.llUserIngot;
- }
- let self = this;
- let webUrl = window["PHP_HOME"] + '/UserFunc.php?&GetMark=5&dwUserID=' + this.selfUserID;
- window["WebCenter"].GetData(webUrl, 3, function (data: any) {
- if (data == null) return;
- let Res = JSON.parse(data);
- if (Res.UserMedal != null) {
- coin = Res.UserMedal;
- }
- self.coinLab.string = "" + coin;
- });
- }
- onclickType(event: any, index: string) {
- let curType = Number(index);
- if (this.type == curType) {
- return;
- }
- this.type = curType;
- this.updateView();
- }
- onclickBuy(event: any, index: string) {
- if (!this.storeData) {
- return;
- }
- if (this.paying) {
- return;
- }
- this.paying = true;
- let goodInfo = this.storeData[index];
- let sendData = {
- userId: this.selfUserID,
- shopId: goodInfo.shopId,
- platformId: this.platformId
- };
- //info { return, data, timestamp }
- let self = this;
- let webUrl = `${window["PHP_HOME"]}/pay.php`;
- window["WebCenter"].httpPOST(webUrl, sendData, function (info: any) {
- cc.log("post buy good, ", info);
- if ("0" != info.return && 0 != info.return) {
- return cc.error("post buy good Error!");
- }
- self.buyIndex = Number(index);
- self.hook.setOrderId(info.orderId, self.buyGoodReturn.bind(self));
- ThirdPartyBuyGood(JSON.stringify({
- itemID: goodInfo.buyId
- }));
- });
-
- }
- onclickClose() {
- this.node.active = false;
- }
- onclickCloseGotView() {
- this.gotNode.active = false;
- }
- private buyGoodReturn() {
- this.paying = false;
- this.updateCoin();
- this.showBoughtCoin(this.buyIndex);
- this.buyIndex = 0;
- }
- private showBoughtCoin(index: number) {
- let buyInfo = this.storeData[index];
- this.gotNode.active = true;
- let descLab = this.gotNode.getChildByName("descLab");
- descLab.getComponent(cc.RichText).string = `<color=#721e10>恭喜你已獲得<color=#008d3b>${buyInfo.num}</c>鑽石!</c>`;
- let iconNode = this.gotNode.getChildByName("icon");
- let iconIndex = index + 1 > 6 ? 6 : index + 1;
- cc["gPreLoader"].LoadRes(`Image_item${iconIndex}`, 'Store', (sprFrame: cc.SpriteFrame) => {
- iconNode.getComponent(cc.Sprite).spriteFrame = sprFrame;
- });
- }
- }
|