123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223 |
- const DWTool = require("../utils/DWTool");
- const GameModule = require("../utils/GameModule");
- const TapTapTool = require("../utils/TapTapTool");
- const AlertManager = require('../utils/AlertManager');
- const WeChat = require('../net/WeChat');
- const StoreApi = require('../net/StoreApi');
- const GameRedDot = require('../utils/GameEnum').GameRedDot;
- cc.Class({
- extends: cc.Component,
- properties: {
- diamondNode: cc.Node,
- diamondSprite: cc.Sprite,
- diamondDoubleSprite: cc.Sprite,
- coinNode: cc.Node,
- coinLabel: cc.Label,
- coinSprite: cc.Sprite,
- adNode: cc.Node,
- adSprite: cc.Sprite,
- adLabel: cc.Label,
- buyBtn: cc.Button,
- buyRichText: cc.RichText,
- buySprite: cc.Sprite,
- },
- // LIFE-CYCLE CALLBACKS:
- // onLoad () {},
- start () {
- },
- // buyType 商品购买类型(1人民币购买,2砖石购买,3看广告获得) 是 [int] 查看
- // cdTime 剩余的CD时间,单位毫秒(为0就是没有CD 可以继续购买) 是 [long] 查看
- // desc 商品描述 是 [string] 查看
- // isBuy 是否还可以继续购买(1为已经购买过,不能在购买了),某些商品只能购买一次 是 [int] 查看
- // minuteTime 购买的商品有效时间,单位分钟(为0就是永久性商品) 是 [long] 查看
- // name 商品名字 是 [string] 查看
- // picId 图片ID 是 [int] 查看
- // price 商品的价格(人民币或者砖石) 是 [int] 查看
- // shopId 商品ID 是 [string] 查看
- // type 商品类型(1推荐,2砖石,3金币,4礼包)
- /// tabIndex为种类
- init(data, tabIndex, index) {
- this._tabIndex = tabIndex;
- this._index = index;
- this._shopData = data;
- this.adNode.active = index == 0;
- this.coinNode.active = tabIndex == 3 && index !== 0;
- this.diamondNode.active = tabIndex == 2 && index !==0;
-
- /// 说明是广告的样式
- if (index == 0) {
- /// 是砖石
- if (tabIndex == 2) {
- /// 是金币
- } else {
- let path = './textures/quest/altas/quest_coin';
- DWTool.loadResSpriteFrame(path).then((spriteFrame) => {
- this.adSprite.spriteFrame = spriteFrame;
- });
- let add = TapTapTool.multiple(GameModule.userInfo.rateGold, GameModule.skill.multiple);
- let gold = TapTapTool.multiple(TapTapTool.multiple(add, GameModule.userInfo.perpetualMt), {'e': 0, 'n': 36});
- this._getGold = gold;
- this.adLabel.string = '分享到群\n' + '获得金币' + TapTapTool.parseToString(gold);
- }
- } else {
- /// 如果是砖石
- let path = './textures/store/800' + tabIndex + index + '0';
- if (tabIndex == 2) {
- DWTool.loadResSpriteFrame(path).then((spriteFrame) => {
- this.diamondSprite.spriteFrame= spriteFrame;
- });
- this.diamondDoubleSprite.node.active = index > 1;
- } else {
- DWTool.loadResSpriteFrame(path).then((spriteFrame) => {
- this.coinSprite.spriteFrame= spriteFrame;
- });
- }
- }
- this.updateTime();
- },
- updateTime() {
- /// 设置购买样式和文字
- /// 说明没有时间限制
- let data = this._shopData;
- let index = this._index;
- let tabIndex = this._tabIndex;
-
- if (data.minuteTime == 0 || data.cdTime == 0) {
- if (index != 0) {
- if (data.buyType == 1) {
- this.buyRichText.string = `<color=#ffffff>¥ ${data.price}</c>`;
- } else {
- this.buyRichText.string = `<img src='skill_diamond'/><color=#ffffff>${data.price}</c>`;
- }
- }
- if (tabIndex == 3 && index == 1) {
- this.setupBuyBtn(GameModule.userInfo.diamond >= data.price);
- } else {
- this.buyBtn.interactable = true;
- }
- } else {
- this.setupTime();
- }
- },
- setupTime() {
- this._cdTime = this._shopData.cdTime / 1000;
- let timeStr = DWTool.calculateTime(this._cdTime);
- this.buyRichText.string = timeStr;
- this.setupBuyBtn(false);
- this.schedule(this.timeAction, 1);
- },
- setupBuyBtn(isActive) {
- let path = isActive ? './textures/store/store_blue_btn' : './textures/store/store_btn_gray';
- DWTool.loadResSpriteFrame(path)
- .then((spriteFrame) => {
- this.buySprite.spriteFrame = spriteFrame;
- });
- this.buyBtn.interactable = isActive;
- },
- timeAction() {
- this._cdTime -= 1;
- this.buyRichText.string = DWTool.calculateTime(this._cdTime);
- if (this._cdTime == 0) {
- if (this._shopData.buyType == 1) {
- this.buyRichText.string = `<img src='skill_diamond'/><color=#ffffff>${this._shopData.price}</c>`;
- } else {
- this.buyRichText.string = `<color=#ffffff>¥ ${this._shopData.price}</c>`;
- }
- this.setupBuyBtn(true);
- this.unschedule(this.timeAction, this);
- }
- },
- buyAction() {
- GameModule.audioMng.playClickButton();
- /// 是钻石 并且没有买过 显示后面的礼包特惠
- if (this._tabIndex === 2 && this._index > 1 && this._shopData.isBuyDiamond == 0) {
- AlertManager.showStoreDiamondAlert();
- /// 分享广告购买
- } else if (this._index == 0) {
- this.buyBtn.interactable = false;
- WeChat.shareAction(() => {
- this._shopData.cdTime = this._shopData.minuteTime * 60 * 1000;
- /// 如果是钻石
- let iconPath = '';
- let name = '';
- let desc = '';
- if (this._tabIndex == 2) {
- GameModule.userInfo.diamond += 10;
- iconPath = './textures/quest/altas/quest_diamond';
- name = '获得钻石';
- desc = '分享获得钻石10个';
- TapTapTool.removeRedDot(GameRedDot.storeDiamond);
- } else {
- GameModule.userInfo.gold = TapTapTool.add(GameModule.userInfo.gold, this._getGold);
- iconPath = './textures/quest/altas/quest_coin';
- name = '获得金币';
- desc = '分享获得金币' + TapTapTool.parseToString(this._getGold);
- TapTapTool.removeRedDot(GameRedDot.storeCoin);
- }
- AlertManager.showCommonAlert(iconPath, desc, name);
-
- this.buyShop().then( () => {
- this.setupTime();
- }).catch( (err, code) => {
- this.buyBtn.interactable = true;
- console.log(err, code);
- })
- }, () => {
- console.log('分享取消或失败');
- this.buyBtn.interactable = true;
- });
- } else if (this._tabIndex == 3 && this._index == 1) {
- this.buyBtn.interactable = false;
- this.buyShop().then(() => {
- this.buyBtn.interactable = true;
- GameModule.userInfo.diamond -= this._shopData.price;
- GameEvent.fire('store_buy_coin_updateDiamond');
- let isActive = GameModule.userInfo.diamond >= this._shopData.price;
- if (isActive == false) {
- this.setupBuyBtn(false);
- }
- let iconPath = './textures/store/' + this._shopData.picId;
- AlertManager.showCommonAlert(iconPath, this._shopData.desc, this._shopData.name);
- }).catch( (err, code) => {
- console.log(err, code);
- this.buyBtn.interactable = false;
- })
- } else {
- WeChat.jumpCustomerServices();
- }
- },
- buyShop() {
- return new Promise((resolve, reject) => {
- StoreApi.buyShop(this._shopData.shopId, (respondData) => {
- resolve(respondData);
- }, (code, msg) => {
- reject({code, msg});
- });
- });
- },
- // update (dt) {},
- });
|