StoreSmallItem.js 13 KB

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