StoreSmallItem.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. const DWTool = require("../utils/DWTool");
  2. const GameModule = require("../utils/GameModule");
  3. const TapTapTool = require("../utils/TapTapTool");
  4. const AlertManager = require('../utils/AlertManager');
  5. const WeChat = require('../net/WeChat');
  6. const StoreApi = require('../net/StoreApi');
  7. const GameRedDot = require('../utils/GameEnum').GameRedDot;
  8. cc.Class({
  9. extends: cc.Component,
  10. properties: {
  11. diamondNode: cc.Node,
  12. diamondSprite: cc.Sprite,
  13. diamondDoubleSprite: cc.Sprite,
  14. coinNode: cc.Node,
  15. coinLabel: cc.Label,
  16. coinSprite: cc.Sprite,
  17. adNode: cc.Node,
  18. adSprite: cc.Sprite,
  19. adLabel: cc.Label,
  20. buyBtn: cc.Button,
  21. buyRichText: cc.RichText,
  22. buySprite: cc.Sprite,
  23. },
  24. // LIFE-CYCLE CALLBACKS:
  25. // onLoad () {},
  26. start () {
  27. },
  28. // buyType 商品购买类型(1人民币购买,2砖石购买,3看广告获得) 是 [int] 查看
  29. // cdTime 剩余的CD时间,单位毫秒(为0就是没有CD 可以继续购买) 是 [long] 查看
  30. // desc 商品描述 是 [string] 查看
  31. // isBuy 是否还可以继续购买(1为已经购买过,不能在购买了),某些商品只能购买一次 是 [int] 查看
  32. // minuteTime 购买的商品有效时间,单位分钟(为0就是永久性商品) 是 [long] 查看
  33. // name 商品名字 是 [string] 查看
  34. // picId 图片ID 是 [int] 查看
  35. // price 商品的价格(人民币或者砖石) 是 [int] 查看
  36. // shopId 商品ID 是 [string] 查看
  37. // type 商品类型(1推荐,2砖石,3金币,4礼包)
  38. /// tabIndex为种类
  39. init(data, tabIndex, index) {
  40. this._tabIndex = tabIndex;
  41. this._index = index;
  42. this._shopData = data;
  43. this.adNode.active = index == 0;
  44. this.coinNode.active = tabIndex == 3 && index !== 0;
  45. this.diamondNode.active = tabIndex == 2 && index !==0;
  46. /// 说明是广告的样式
  47. if (index == 0) {
  48. /// 是砖石
  49. if (tabIndex == 2) {
  50. /// 是金币
  51. } else {
  52. let path = './textures/quest/altas/quest_coin';
  53. DWTool.loadResSpriteFrame(path).then((spriteFrame) => {
  54. this.adSprite.spriteFrame = spriteFrame;
  55. });
  56. let add = TapTapTool.multiple(GameModule.userInfo.rateGold, GameModule.skill.multiple);
  57. let gold = TapTapTool.multiple(TapTapTool.multiple(add, GameModule.userInfo.perpetualMt), {'e': 0, 'n': 36});
  58. this._getGold = gold;
  59. this.adLabel.string = '分享到群\n' + '获得金币' + TapTapTool.parseToString(gold);
  60. }
  61. } else {
  62. /// 如果是砖石
  63. let path = './textures/store/800' + tabIndex + index + '0';
  64. if (tabIndex == 2) {
  65. DWTool.loadResSpriteFrame(path).then((spriteFrame) => {
  66. this.diamondSprite.spriteFrame= spriteFrame;
  67. });
  68. this.diamondDoubleSprite.node.active = index > 1;
  69. } else {
  70. DWTool.loadResSpriteFrame(path).then((spriteFrame) => {
  71. this.coinSprite.spriteFrame= spriteFrame;
  72. });
  73. }
  74. }
  75. this.updateTime();
  76. },
  77. updateTime() {
  78. /// 设置购买样式和文字
  79. /// 说明没有时间限制
  80. let data = this._shopData;
  81. let index = this._index;
  82. let tabIndex = this._tabIndex;
  83. if (data.minuteTime == 0 || data.cdTime == 0) {
  84. if (index != 0) {
  85. if (data.buyType == 1) {
  86. this.buyRichText.string = `<color=#ffffff>¥ ${data.price}</c>`;
  87. } else {
  88. this.buyRichText.string = `<img src='skill_diamond'/><color=#ffffff>${data.price}</c>`;
  89. }
  90. }
  91. if (tabIndex == 3 && index == 1) {
  92. this.setupBuyBtn(GameModule.userInfo.diamond >= data.price);
  93. } else {
  94. this.buyBtn.interactable = true;
  95. }
  96. } else {
  97. this.setupTime();
  98. }
  99. },
  100. setupTime() {
  101. this._cdTime = this._shopData.cdTime / 1000;
  102. let timeStr = DWTool.calculateTime(this._cdTime);
  103. this.buyRichText.string = timeStr;
  104. this.setupBuyBtn(false);
  105. this.schedule(this.timeAction, 1);
  106. },
  107. setupBuyBtn(isActive) {
  108. let path = isActive ? './textures/store/store_blue_btn' : './textures/store/store_btn_gray';
  109. DWTool.loadResSpriteFrame(path)
  110. .then((spriteFrame) => {
  111. this.buySprite.spriteFrame = spriteFrame;
  112. });
  113. this.buyBtn.interactable = isActive;
  114. },
  115. timeAction() {
  116. this._cdTime -= 1;
  117. this.buyRichText.string = DWTool.calculateTime(this._cdTime);
  118. if (this._cdTime == 0) {
  119. if (this._shopData.buyType == 1) {
  120. this.buyRichText.string = `<img src='skill_diamond'/><color=#ffffff>${this._shopData.price}</c>`;
  121. } else {
  122. this.buyRichText.string = `<color=#ffffff>¥ ${this._shopData.price}</c>`;
  123. }
  124. this.setupBuyBtn(true);
  125. this.unschedule(this.timeAction, this);
  126. }
  127. },
  128. buyAction() {
  129. GameModule.audioMng.playClickButton();
  130. /// 是钻石 并且没有买过 显示后面的礼包特惠
  131. if (this._tabIndex === 2 && this._index > 1 && this._shopData.isBuyDiamond == 0) {
  132. AlertManager.showStoreDiamondAlert();
  133. /// 分享广告购买
  134. } else if (this._index == 0) {
  135. this.buyBtn.interactable = false;
  136. WeChat.shareAction(() => {
  137. this._shopData.cdTime = this._shopData.minuteTime * 60 * 1000;
  138. /// 如果是钻石
  139. let iconPath = '';
  140. let name = '';
  141. let desc = '';
  142. if (this._tabIndex == 2) {
  143. GameModule.userInfo.diamond += 10;
  144. iconPath = './textures/quest/altas/quest_diamond';
  145. name = '获得钻石';
  146. desc = '分享获得钻石10个';
  147. TapTapTool.removeRedDot(GameRedDot.storeDiamond);
  148. } else {
  149. GameModule.userInfo.gold = TapTapTool.add(GameModule.userInfo.gold, this._getGold);
  150. iconPath = './textures/quest/altas/quest_coin';
  151. name = '获得金币';
  152. desc = '分享获得金币' + TapTapTool.parseToString(this._getGold);
  153. TapTapTool.removeRedDot(GameRedDot.storeCoin);
  154. }
  155. AlertManager.showCommonAlert(iconPath, desc, name);
  156. this.buyShop().then( () => {
  157. this.setupTime();
  158. }).catch( (err, code) => {
  159. this.buyBtn.interactable = true;
  160. console.log(err, code);
  161. })
  162. }, () => {
  163. console.log('分享取消或失败');
  164. this.buyBtn.interactable = true;
  165. });
  166. } else if (this._tabIndex == 3 && this._index == 1) {
  167. this.buyBtn.interactable = false;
  168. this.buyShop().then(() => {
  169. this.buyBtn.interactable = true;
  170. GameModule.userInfo.diamond -= this._shopData.price;
  171. GameEvent.fire('store_buy_coin_updateDiamond');
  172. let isActive = GameModule.userInfo.diamond >= this._shopData.price;
  173. if (isActive == false) {
  174. this.setupBuyBtn(false);
  175. }
  176. let iconPath = './textures/store/' + this._shopData.picId;
  177. AlertManager.showCommonAlert(iconPath, this._shopData.desc, this._shopData.name);
  178. }).catch( (err, code) => {
  179. console.log(err, code);
  180. this.buyBtn.interactable = false;
  181. })
  182. } else {
  183. WeChat.jumpCustomerServices();
  184. }
  185. },
  186. buyShop() {
  187. return new Promise((resolve, reject) => {
  188. StoreApi.buyShop(this._shopData.shopId, (respondData) => {
  189. resolve(respondData);
  190. }, (code, msg) => {
  191. reject({code, msg});
  192. });
  193. });
  194. },
  195. // update (dt) {},
  196. });