StoreSmallItem.js 9.1 KB

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