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