StoreSmallItem.js 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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 {GameNotificationKey, WechatShareType, GameRedDot } = require('../utils/GameEnum');
  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. if (tabIndex == 2) {
  64. let path = './textures/store/800' + tabIndex + index + '0';
  65. DWTool.loadResSpriteFrame(path).then((spriteFrame) => {
  66. this.diamondSprite.spriteFrame= spriteFrame;
  67. });
  68. if (this._shopData.isBuyDiamond == 1) {
  69. this.diamondDoubleSprite.node.active = false;
  70. } else {
  71. this.diamondDoubleSprite.node.active = index > 1;
  72. }
  73. } else {
  74. let index = this._shopData.shopId - 11;
  75. let path = './textures/store/800' + tabIndex + index + '0';
  76. DWTool.loadResSpriteFrame(path).then((spriteFrame) => {
  77. this.coinSprite.spriteFrame= spriteFrame;
  78. });
  79. }
  80. }
  81. this.updateTime();
  82. },
  83. updateTime() {
  84. /// 设置购买样式和文字
  85. /// 说明没有时间限制
  86. let data = this._shopData;
  87. let index = this._index;
  88. let tabIndex = this._tabIndex;
  89. if (data.minuteTime == 0 || data.cdTime == 0) {
  90. if (index != 0) {
  91. if (data.buyType == 1) {
  92. this.buyRichText.string = `<color=#ffffff>¥ ${data.price}</c>`;
  93. } else {
  94. this.buyRichText.string = `<img src='skill_diamond'/><color=#ffffff>${data.price}</c>`;
  95. }
  96. }
  97. if (tabIndex == 3 && index == 1) {
  98. this.setupBuyBtn(GameModule.userInfo.diamond >= data.price);
  99. } else {
  100. this.buyBtn.interactable = true;
  101. }
  102. } else {
  103. this.setupTime();
  104. }
  105. },
  106. setupTime() {
  107. this._cdTime = this._shopData.cdTime / 1000;
  108. let timeStr = DWTool.calculateTime(this._cdTime);
  109. this.buyRichText.string = timeStr;
  110. this.setupBuyBtn(false);
  111. this.schedule(this.timeAction, 1);
  112. },
  113. setupBuyBtn(isActive) {
  114. let path = isActive ? './textures/store/store_blue_btn' : './textures/store/store_btn_gray';
  115. DWTool.loadResSpriteFrame(path)
  116. .then((spriteFrame) => {
  117. this.buySprite.spriteFrame = spriteFrame;
  118. });
  119. this.buyBtn.interactable = isActive;
  120. },
  121. timeAction() {
  122. this._cdTime -= 1;
  123. this.buyRichText.string = DWTool.calculateTime(this._cdTime);
  124. if (this._cdTime == 0) {
  125. if (this._shopData.buyType == 1) {
  126. this.buyRichText.string = `<img src='skill_diamond'/><color=#ffffff>${this._shopData.price}</c>`;
  127. } else {
  128. this.buyRichText.string = `<color=#ffffff>¥ ${this._shopData.price}</c>`;
  129. }
  130. this.setupBuyBtn(true);
  131. this.unschedule(this.timeAction, this);
  132. }
  133. },
  134. buyAction() {
  135. GameModule.audioMng.playClickButton();
  136. /// 是钻石 并且没有买过 显示后面的礼包特惠
  137. if (this._tabIndex === 2 && this._index > 1 && this._shopData.isBuyDiamond == 0) {
  138. //传入选择的index为购买优惠的顺序
  139. let selectIndex = this._index - 2;
  140. AlertManager.showStoreDiamondAlert(selectIndex);
  141. /// 分享广告购买
  142. } else if (this._index == 0) {
  143. this.buyBtn.interactable = false;
  144. GameEvent.on(GameNotificationKey.ShowShareAction, this, (type) => {
  145. this.shareActionCallback(type);
  146. });
  147. WeChat.shareAction(WechatShareType.StoreGetGift, () => {
  148. }, () => {
  149. console.log('分享取消或失败');
  150. this.buyBtn.interactable = true;
  151. });
  152. } else if (this._tabIndex == 3 && this._index == 1) {
  153. this.buyBtn.interactable = false;
  154. this.buyShop().then(() => {
  155. this.buyBtn.interactable = true;
  156. GameModule.userInfo.diamond -= this._shopData.price;
  157. GameEvent.fire('store_buy_coin_updateDiamond');
  158. let isActive = GameModule.userInfo.diamond >= this._shopData.price;
  159. if (isActive == false) {
  160. this.setupBuyBtn(false);
  161. }
  162. let iconPath = './textures/store/' + this._shopData.picId;
  163. AlertManager.showCommonAlert(iconPath, this._shopData.desc, this._shopData.name);
  164. }).catch( (err, code) => {
  165. console.log(err, code);
  166. this.buyBtn.interactable = false;
  167. })
  168. } else {
  169. WeChat.jumpCustomerServices();
  170. }
  171. },
  172. shareActionCallback(type) {
  173. if (type != WechatShareType.StoreGetGift) { return; }
  174. let self = this;
  175. this.buyShop().then( () => {
  176. self._shopData.cdTime = self._shopData.minuteTime * 60 * 1000;
  177. self.setupTime();
  178. /// 如果是钻石
  179. let iconPath = '';
  180. let name = '';
  181. let desc = '';
  182. if (this._tabIndex == 2) {
  183. GameModule.userInfo.diamond += 10;
  184. iconPath = './textures/quest/altas/quest_diamond';
  185. name = '获得钻石';
  186. desc = '分享获得钻石10个';
  187. TapTapTool.removeRedDot(GameRedDot.storeDiamond);
  188. } else {
  189. GameModule.userInfo.gold = TapTapTool.add(GameModule.userInfo.gold, this._getGold);
  190. iconPath = './textures/quest/altas/quest_coin';
  191. name = '获得金币';
  192. desc = '分享获得金币' + TapTapTool.parseToString(this._getGold);
  193. TapTapTool.removeRedDot(GameRedDot.storeCoin);
  194. }
  195. AlertManager.showCommonAlert(iconPath, desc, name);
  196. }).catch( (code, msg) => {
  197. this.buyBtn.interactable = true;
  198. console.log(err, code);
  199. })
  200. GameEvent.off(GameNotificationKey.ShowShareAction, this);
  201. },
  202. onDestroy() {
  203. GameEvent.off(GameNotificationKey.ShowShareAction, this);
  204. },
  205. buyShop() {
  206. return new Promise((resolve, reject) => {
  207. StoreApi.buyShop(this._shopData.shopId, (respondData) => {
  208. resolve(respondData);
  209. }, (code, msg) => {
  210. reject({code, msg});
  211. });
  212. });
  213. },
  214. // update (dt) {},
  215. });