| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414 |
- import CommonUtility from "../../../Script/CustomClass/CommonUtility";
- const {ccclass, property} = cc._decorator;
- const ViewType = {
- Diamond: 0,
- Gift: 1,
- Exchange: 2,
- };
- @ccclass
- export default class Store extends cc.Component {
- private typeNode: cc.Node = null;
- private coinLab: cc.Label = null;
- private contentNode: cc.Node = null;
- private descNode: cc.Node = null;
- private tipNode: cc.Node = null;
- private itemNode: cc.Node = null;
- private gotNode: cc.Node = null;
- private exchangeNode: cc.Node = null;
- private hook: any = null;
- private platformId: number = 0;
- private selfUserID: number = -1;
- //数据
- private allData: any = null;
- //默认0 钻石0 礼物1 兑换2
- private type: number = -1;
- private paying: boolean = false;
- private buyIndex: number = 0;
- private buyOrExchangeCount: number = 1;
- private curGoodInfo: any = null;
- 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.descNode = cc.find("itemView/view/descLab", this.node);
- this.tipNode = cc.find("itemView/view/tipLab", this.node);
- this.itemNode = this.contentNode?.getChildByName("item");
- this.itemNode.parent = null;
- this.gotNode = this.node.getChildByName("gotNode");
- this.gotNode.active = false;
- this.exchangeNode = this.node.getChildByName("exchangeNode");
- this.exchangeNode.active = false;
- }
- SetHook(hook: any) {
- this.hook = hook;
- }
- ShowView() {
- this.node.active = true;
- this.buyIndex = 0;
- this.buyOrExchangeCount = 1;
- this.updateCoin();
- }
- 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();
- this.allData = {};
- this.type = 0;
- this.descNode.active = false;
- this.tipNode.active = false;
- let curTypeNode = this.typeNode.getChildByName(`type${this.type}`);
- curTypeNode.getComponent(cc.Toggle).isChecked = true;
- this.getData();
- }
- private getData() {
- //[{"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) {
- Res.sort(function(d1: any, d2: any) {
- return d2.num - d1.num;
- });
- self.allData[ViewType.Diamond] = JSON.parse(JSON.stringify(Res));
- self.updateView();
- }
- });
- return;
- 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) {
- Res.sort(function(d1: any, d2: any) {
- return d2.num - d1.num;
- });
- self.allData[ViewType.Gift] = JSON.parse(JSON.stringify(Res));
- }
- });
- }
- private getConvertibleGoodsdata() {
- 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) {
- Res.sort(function(d1: any, d2: any) {
- return d2.num - d1.num;
- });
- self.allData[ViewType.Exchange] = JSON.parse(JSON.stringify(Res));
- self.updateView();
- }
- });
- }
- private updateView() {
- let curListData = this.allData[this.type];
- if (null == curListData || !curListData.length) {
- this.contentNode.active = false;
- this.tipNode.active = true;
- return;
- }
- this.contentNode.active = true;
- let showBuyDiamond = ViewType.Diamond == this.type;
- let showExchange = ViewType.Exchange == this.type;
- this.contentNode.getComponent(cc.Widget).top = showExchange ? 60 : 0;
- this.descNode.active = showExchange;
- this.tipNode.active = false;
- let addCount = curListData.length - this.contentNode.childrenCount;
- if (addCount > 0) {
- for (let idx = 0; idx < addCount; ++idx) {
- let itemNode = cc.instantiate(this.itemNode);
- itemNode.parent = this.contentNode;
- }
- }
- for (let idx = 0; idx < this.contentNode.childrenCount; ++idx) {
- let curData = curListData[idx];
- let itemNode = this.contentNode.children[idx];
- if (false == !!curData) {
- itemNode.active = false;
- continue;
- }
- //字
- let countLab = cc.find("labs/countLab", itemNode);
- let nameLab = cc.find("labs/nameLab", itemNode);
- let priceLab = cc.find("labs/priceLab", itemNode);
- let numLab = cc.find("labs/numLab", itemNode);
- countLab.active = showBuyDiamond;
- nameLab.active = !showBuyDiamond;
- priceLab.active = showExchange;
- numLab.active = showExchange;
- if (showBuyDiamond) {
- countLab.getComponent(cc.Label).string = "" + curData.num;
- } else {
- nameLab.getComponent(cc.Label).string = curData.name;
- if (showExchange) {
- priceLab.getComponent(cc.Label).string = `單價:${curData.price}鑽石`
- numLab.getComponent(cc.Label).string = `x${curData.count}`;
- }
- }
- //按鈕
- let btnBuy = itemNode.getChildByName("btnBuy");
- let btnBuyGift = itemNode.getChildByName("btnBuyGift");
- let btnExchange = itemNode.getChildByName("btnExchange");
- let giftContent = itemNode.getChildByName("content");
- btnBuy.active = showBuyDiamond;
- btnBuyGift.active = !showBuyDiamond && !showExchange;
- giftContent.active = !showBuyDiamond && !showExchange;
- btnExchange.active = showExchange;
- let itemNodeHandlerName = "";
- if (showBuyDiamond) {
- itemNodeHandlerName = "onclickBuy";
- CommonUtility.getInstance().bindNodeHandler(this, btnBuy, "onclickBuy", true, `${idx}`, true);
- btnBuy.getChildByName("lab").getComponent(cc.Label).string = window["PAY_CURRENCY_SYMBOL"] + curData.price;
- } else if (showExchange) {
- itemNodeHandlerName = "onclickExchangeGift";
- CommonUtility.getInstance().bindNodeHandler(this, btnExchange, "onclickExchangeGift", true, `${idx}`, true);
- } else {
- itemNodeHandlerName = "onclickBuyGift";
- CommonUtility.getInstance().bindNodeHandler(this, btnExchange, "onclickBuyGift", true, `${idx}`, true);
- giftContent.getChildByName("lab").getComponent(cc.Label).string = `${curData.price}`;
- }
- CommonUtility.getInstance().bindNodeHandler(this, itemNode, itemNodeHandlerName, true, `${idx}`, true);
- //图
- let descBgNode = cc.find("bg/desc-bg", itemNode);
- descBgNode.width = showBuyDiamond ? 150 : 120;
- let iconNode = cc.find("bg/icon", itemNode);
- let iconIndex = idx + 1 > 6 ? 6 : idx + 1;
- let pathKey = showBuyDiamond ? "item" : "gift";
- cc["gPreLoader"].LoadRes(`Image_${pathKey}${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;
- // if (ViewType.Exchange == curType) {
- // this.getConvertibleGoodsdata();
- // } else {
- // this.updateView();
- // }
- this.updateView();
- }
- onclickBuy(event: any, index: string) {
- if (!this.allData[this.type]) {
- return;
- }
- let goodInfo = this.allData[this.type][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
- }));
- });
- }
- onclickBuyGift(event: any, index: string) {
- if (this.exchangeNode.active) {
- return;
- }
- this.onOpenExchangeView(Number(index));
- }
- onclickExchangeGift(event: any, index: string) {
- if (this.exchangeNode.active) {
- return;
- }
- this.onOpenExchangeView(Number(index));
- }
- onOpenExchangeView(index: number) {
- this.exchangeNode.active = true;
- this.buyIndex = index;
- this.showExchangeView();
- }
- onclickConfirmToBuyOrExchange() {
- let isBuy = ViewType.Gift == this.type;
- this.buyOrExchangeCount
- }
- onclickClose() {
- this.node.active = false;
- }
- onclickCloseGotView() {
- this.gotNode.active = false;
- }
- onclickCloseExchangeView() {
- this.exchangeNode.active = false;
- }
- onclickAddGoodsCount() {
- if (this.buyOrExchangeCount <= 1) {
- return;
- }
- this.buyOrExchangeCount -= 1;
- this.updateCountPriceAndButton();
- }
- onclickSubtractGoodsCount() {
- if (this.buyOrExchangeCount >= this.curGoodInfo.count) {
- return;
- }
- this.buyOrExchangeCount += 1;
- this.updateCountPriceAndButton();
- }
- onInputGoodsCountEnd(event: any) {
- let editBox = event.target;
- let curCount = Number(editBox.string);
- if (curCount > this.curGoodInfo.count) {
- curCount = this.curGoodInfo.count;
- }
- if (curCount < 1) {
- curCount = 1;
- }
- this.buyOrExchangeCount = curCount;
- this.updateCountPriceAndButton();
- }
- private buyGoodReturn(newCoin: number = 0) {
- if (!!newCoin) {
- this.coinLab.string = "" + newCoin;
- } else {
- this.updateCoin();
- }
- this.showBoughtCoin(this.buyIndex);
- this.buyIndex = 0;
- }
- private showBoughtCoin(index: number) {
- let buyInfo = this.allData[this.type][index];
- let isExchange = ViewType.Exchange == this.type;
- let isDiamond = ViewType.Diamond == this.type;
- this.gotNode.active = true;
- cc.find("bg/buyTitle", this.gotNode).active = !isExchange;
- cc.find("bg/exchangeTitle", this.gotNode).active = isExchange;
- let descLab = this.gotNode.getChildByName("descLab");
- let descStr = "";
- if (isDiamond || isExchange) {
- let count = isDiamond ? buyInfo.num : this.buyOrExchangeCount * buyInfo.price
- descStr = `<color=#721e10>恭喜你已獲得<color=#008d3b>${count}</c>鑽石!</c>`;
- } else {
- descStr = `<color=#721e10>恭喜你已獲得<color=#008d3b>${this.curGoodInfo.name}x${this.buyOrExchangeCount}</c>!</c>`;
- }
- descLab.getComponent(cc.RichText).string = descStr;
- let iconNode = this.gotNode.getChildByName("icon");
- let iconIndex = index + 1 > 6 ? 6 : index + 1;
- let pathKey = isDiamond ? "item" : "gift";
- cc["gPreLoader"].LoadRes(`Image_${pathKey}${iconIndex}`, 'Store', (sprFrame: cc.SpriteFrame) => {
- iconNode.getComponent(cc.Sprite).spriteFrame = sprFrame;
- });
- }
- private showExchangeView() {
- let isBuy = ViewType.Gift == this.type;
- this.curGoodInfo = this.allData[this.type][this.buyIndex];
- let titleLab = cc.find("labs/titleLab", this.exchangeNode);
- let nameLab = cc.find("labs/nameLab", this.exchangeNode);
- let numLab = cc.find("labs/numLab", this.exchangeNode);
- titleLab.getComponent(cc.Label).string = isBuy ? "購買禮物" : "兌換鑽石";
- nameLab.getComponent(cc.Label).string = this.curGoodInfo.name;
- numLab.active = !isBuy;
- if (!isBuy) {
- numLab.getComponent(cc.Label).string = `擁有 ${this.curGoodInfo.count}`;
- }
- this.updateCountPriceAndButton();
- }
- private updateCountPriceAndButton() {
- //数量价格
- let editBox = cc.find("exchange/editBox", this.exchangeNode);
- let priceLab = cc.find("labs/priceLab", this.exchangeNode);
- editBox.getComponent(cc.EditBox).string = `${this.buyOrExchangeCount}`;
- priceLab.getComponent(cc.Label).string = `${this.curGoodInfo.price * this.buyOrExchangeCount}`;
- //按钮状态
- let btnSub = cc.find("exchange/btnSub", this.exchangeNode);
- let btnAdd = cc.find("exchange/btnAdd", this.exchangeNode);
- let canSub = this.buyOrExchangeCount > 1;
- let canAdd = this.buyOrExchangeCount < this.curGoodInfo.count;
- btnSub.getComponent(cc.Button).interactable = canSub
- btnAdd.getComponent(cc.Button).interactable = canAdd;
- btnSub.getChildByName("top").active = canSub;
- btnAdd.getChildByName("top").active = canAdd;
- }
- }
|