StoreSmallItem.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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. // const ADVideo = require('../utils/ADVideo');
  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. _adState: 0
  25. },
  26. // LIFE-CYCLE CALLBACKS:
  27. // onLoad () {},
  28. start () {
  29. },
  30. // buyType 商品购买类型(1人民币购买,2砖石购买,3看广告获得) 是 [int] 查看
  31. // cdTime 剩余的CD时间,单位毫秒(为0就是没有CD 可以继续购买) 是 [long] 查看
  32. // desc 商品描述 是 [string] 查看
  33. // isBuy 是否还可以继续购买(1为已经购买过,不能在购买了),某些商品只能购买一次 是 [int] 查看
  34. // minuteTime 购买的商品有效时间,单位分钟(为0就是永久性商品) 是 [long] 查看
  35. // name 商品名字 是 [string] 查看
  36. // picId 图片ID 是 [int] 查看
  37. // price 商品的价格(人民币或者砖石) 是 [int] 查看
  38. // shopId 商品ID 是 [string] 查看
  39. // type 商品类型(1推荐,2砖石,3金币,4礼包)
  40. /// tabIndex为种类
  41. init(data, tabIndex, index) {
  42. this._tabIndex = tabIndex;
  43. this._index = index;
  44. this._shopData = data;
  45. this.adNode.active = index == 0;
  46. this.coinNode.active = tabIndex == 3 && index !== 0;
  47. this.diamondNode.active = tabIndex == 2 && index !==0;
  48. /// 说明是广告的样式
  49. if (index == 0) {
  50. /// 是砖石
  51. if (tabIndex == 2) {
  52. /// 是金币
  53. } else {
  54. let path = './textures/quest/altas/quest_coin';
  55. DWTool.loadResSpriteFrame(path).then((spriteFrame) => {
  56. this.adSprite.spriteFrame = spriteFrame;
  57. });
  58. let add = TapTapTool.multiple(GameModule.userInfo.rateGold, GameModule.skill.multiple);
  59. add = TapTapTool.multiple({'n': GameModule.shop.multiple, 'e': 0}, add);
  60. let gold = TapTapTool.multiple(TapTapTool.multiple(add, GameModule.userInfo.perpetualMt), {'e': 0, 'n': 36});
  61. this._getGold = gold;
  62. }
  63. GameEvent.on(GameNotificationKey.AdUpdateStateNotification, this, (adState, callBack) => {
  64. /// 说明是技能的关闭状态
  65. if (adState === 3 && callBack === 'store' + this._tabIndex) {
  66. this.shareActionCallback();
  67. }
  68. if (adState === 0 || adState === 1) {
  69. this.initStoreAd();
  70. }
  71. });
  72. this.initStoreAd();
  73. } else {
  74. /// 如果是砖石
  75. if (tabIndex == 2) {
  76. let path = './textures/store/800' + tabIndex + index + '0';
  77. DWTool.loadResSpriteFrame(path).then((spriteFrame) => {
  78. this.diamondSprite.spriteFrame= spriteFrame;
  79. });
  80. if (this._shopData.isBuyDiamond == 1) {
  81. this.diamondDoubleSprite.node.active = false;
  82. } else {
  83. this.diamondDoubleSprite.node.active = index > 1;
  84. }
  85. } else {
  86. let index = this._shopData.shopId - 11;
  87. let path = './textures/store/800' + tabIndex + index + '0';
  88. DWTool.loadResSpriteFrame(path).then((spriteFrame) => {
  89. this.coinSprite.spriteFrame= spriteFrame;
  90. });
  91. }
  92. }
  93. this.updateTime();
  94. },
  95. initStoreAd() {
  96. if (!CC_WECHATGAME) {
  97. return;
  98. }
  99. // this.adId = this._tabIndex === 2 ? ADVideo.storeAdDiamand : ADVideo.storeAdCoinId;
  100. //// 说明有广告
  101. if (Global._adVideoState == 0) {
  102. this._adState = 1;
  103. } else if (Global._adVideoState === 1) {
  104. this._adState = 0;
  105. }
  106. this.updateNormalTitle();
  107. if (this.buyBtn.interactable) {
  108. this.updateBuyTitle();
  109. }
  110. },
  111. updateNormalTitle() {
  112. if (this._tabIndex == 2) {
  113. if ( this._adState === 0) {
  114. this.adLabel.string = '分享到群\n' + '获得钻石10个';
  115. } else {
  116. this.adLabel.string = '播放视频\n' + '获得钻石10个';
  117. }
  118. } else {
  119. if ( this._adState === 0) {
  120. this.adLabel.string = '分享到群\n' + '获得金币' + TapTapTool.parseToString(this._getGold);
  121. } else {
  122. this.adLabel.string = '播放视频\n' + '获得金币' + TapTapTool.parseToString(this._getGold);
  123. }
  124. }
  125. },
  126. updateBuyTitle() {
  127. if (this._adState === 0) {
  128. this.buyRichText.string = `<color=#ffffff>分享</c>`;
  129. } else {
  130. this.buyRichText.string = `<color=#ffffff>播放视频</c>`;
  131. }
  132. },
  133. updateTime() {
  134. /// 设置购买样式和文字
  135. /// 说明没有时间限制
  136. let data = this._shopData;
  137. let index = this._index;
  138. let tabIndex = this._tabIndex;
  139. if (data.minuteTime == 0 || data.cdTime == 0) {
  140. if (index != 0) {
  141. if (data.buyType == 1) {
  142. this.buyRichText.string = `<color=#ffffff>¥ ${data.price}</c>`;
  143. } else {
  144. this.buyRichText.string = `<img src='skill_diamond'/><color=#ffffff>${data.price}</c>`;
  145. }
  146. }
  147. //// 钻石购买金币
  148. if (tabIndex == 3 && index == 1) {
  149. this.setupBuyBtn(GameModule.userInfo.diamond >= data.price);
  150. } else {
  151. this.buyBtn.interactable = true;
  152. }
  153. } else {
  154. this.setupTime();
  155. }
  156. },
  157. setupTime() {
  158. this._cdTime = this._shopData.cdTime / 1000;
  159. let timeStr = DWTool.calculateTime(this._cdTime);
  160. this.buyRichText.string = timeStr;
  161. this.setupBuyBtn(false);
  162. this.schedule(this.timeAction, 1);
  163. },
  164. setupBuyBtn(isActive) {
  165. let path = isActive ? './textures/store/store_blue_btn' : './textures/store/store_btn_gray';
  166. DWTool.loadResSpriteFrame(path)
  167. .then((spriteFrame) => {
  168. this.buySprite.spriteFrame = spriteFrame;
  169. });
  170. this.buyBtn.interactable = isActive;
  171. },
  172. timeAction() {
  173. this._cdTime -= 1;
  174. this.buyRichText.string = DWTool.calculateTime(this._cdTime);
  175. if (this._cdTime <= 0) {
  176. /// 如果不是广告样式
  177. if (this._index !== 0) {
  178. if (this._shopData.buyType == 1) {
  179. this.buyRichText.string = `<img src='skill_diamond'/><color=#ffffff>${this._shopData.price}</c>`;
  180. } else {
  181. this.buyRichText.string = `<color=#ffffff>¥ ${this._shopData.price}</c>`;
  182. }
  183. } else {
  184. this.updateBuyTitle();
  185. }
  186. this.setupBuyBtn(true);
  187. this.unschedule(this.timeAction, this);
  188. }
  189. },
  190. buyAction() {
  191. GameModule.audioMng.playClickButton();
  192. /// 是钻石 并且没有买过 显示后面的礼包特惠
  193. if (this._tabIndex === 2 && this._index > 1 && this._shopData.isBuyDiamond == 0) {
  194. //传入选择的index为购买优惠的顺序
  195. let selectIndex = this._index - 2;
  196. AlertManager.showStoreDiamondAlert(selectIndex);
  197. /// 分享广告购买
  198. } else if (this._index == 0) {
  199. if (this._adState === 0) {
  200. this.buyBtn.interactable = false;
  201. GameEvent.on(GameNotificationKey.ShowShareAction, this, (type, isOk) => {
  202. if (type != WechatShareType.StoreGetGift) { return; }
  203. if (isOk) {
  204. this.shareActionCallback();
  205. } else {
  206. this.buyBtn.interactable = true;
  207. }
  208. GameEvent.off(GameNotificationKey.ShowShareAction, this);
  209. });
  210. WeChat.shareAction(WechatShareType.StoreGetGift, () => {
  211. }, () => {
  212. console.log('分享取消或失败');
  213. this.buyBtn.interactable = true;
  214. });
  215. } else {
  216. Global._adVideo.showVideo('store' + this._tabIndex);
  217. }
  218. } else if (this._tabIndex == 3 && this._index == 1) {
  219. this.buyBtn.interactable = false;
  220. this.buyShop().then(() => {
  221. this.buyBtn.interactable = true;
  222. GameModule.userInfo.diamond -= this._shopData.price;
  223. GameEvent.fire('store_buy_coin_updateDiamond', this._shopData.infoDesc);
  224. let isActive = GameModule.userInfo.diamond >= this._shopData.price;
  225. if (isActive == false) {
  226. this.setupBuyBtn(false);
  227. }
  228. let iconPath = './textures/store/' + this._shopData.picId;
  229. AlertManager.showCommonAlert(iconPath, this._shopData.desc, this._shopData.name);
  230. }).catch( (err, code) => {
  231. console.log(err, code);
  232. this.buyBtn.interactable = false;
  233. })
  234. } else {
  235. WeChat.jumpCustomerServices();
  236. }
  237. },
  238. shareActionCallback() {
  239. let self = this;
  240. this.buyShop().then( () => {
  241. self._shopData.cdTime = self._shopData.minuteTime * 60 * 1000;
  242. self.setupTime();
  243. /// 如果是钻石
  244. let iconPath = '';
  245. let name = '';
  246. let desc = '';
  247. if (this._tabIndex == 2) {
  248. GameModule.userInfo.diamond += 10;
  249. iconPath = './textures/quest/altas/quest_diamond';
  250. name = '获得钻石';
  251. if (this._adState === 0) {
  252. desc = '分享获得钻石10个';
  253. } else {
  254. desc = '观看视频获得钻石10个';
  255. }
  256. TapTapTool.removeRedDot(GameRedDot.storeDiamond);
  257. } else {
  258. GameModule.userInfo.gold = TapTapTool.add(GameModule.userInfo.gold, this._getGold);
  259. iconPath = './textures/quest/altas/quest_coin';
  260. name = '获得金币';
  261. if (this._adState === 0) {
  262. desc = '分享获得金币' + TapTapTool.parseToString(this._getGold);
  263. } else {
  264. desc = '观看视频获得金币' + TapTapTool.parseToString(this._getGold);
  265. }
  266. TapTapTool.removeRedDot(GameRedDot.storeCoin);
  267. }
  268. AlertManager.showCommonAlert(iconPath, desc, name);
  269. }).catch( (code, msg) => {
  270. this.buyBtn.interactable = true;
  271. console.log(err, code);
  272. })
  273. if (this._adState === 0) {
  274. GameEvent.off(GameNotificationKey.ShowShareAction, this);
  275. }
  276. },
  277. onDestroy() {
  278. GameEvent.off(GameNotificationKey.ShowShareAction, this);
  279. GameEvent.off(GameNotificationKey.AdUpdateStateNotification, this);
  280. },
  281. buyShop() {
  282. return new Promise((resolve, reject) => {
  283. StoreApi.buyShop(this._shopData.shopId, (respondData) => {
  284. resolve(respondData);
  285. }, (code, msg) => {
  286. reject({code, msg});
  287. });
  288. });
  289. },
  290. // update (dt) {},
  291. });