OfflineGrossIncome.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. const DWTool = require('../utils/DWTool')
  2. const { GameNotificationKey } = require("../utils/GameEnum");
  3. const GameModule = require('../utils/GameModule')
  4. const WeChat = require('../net/WeChat');
  5. const Api = require('../net/Api');
  6. cc.Class({
  7. extends: cc.Component,
  8. properties: {
  9. content: cc.Node,
  10. coinRichText: cc.RichText,
  11. watchVideoRichText: cc.RichText,
  12. shareRichText: cc.RichText,
  13. secretarySprite: cc.Node,
  14. videoBtn: {
  15. tooltip: '观看视频按钮',
  16. default: null,
  17. type: cc.Node
  18. },
  19. shareBtn: {
  20. tooltip: '分享按钮',
  21. default: null,
  22. type: cc.Node
  23. },
  24. normalBtn: {
  25. tooltip: '普通确认按钮',
  26. default: null,
  27. type: cc.Node
  28. },
  29. _grossIncome: 0,
  30. grossIncome: {
  31. get() {
  32. return this._grossIncome;
  33. },
  34. set (value) {
  35. this._grossIncome = value;
  36. this.setCoinRichText(value);
  37. this.setWatchVideoRichText(value);
  38. this.setShareVideoRichText(value);
  39. }
  40. }
  41. },
  42. // LIFE-CYCLE CALLBACKS:
  43. init(grossIncome) {
  44. this.grossIncome = grossIncome;
  45. this._resetBtn();
  46. if(CC_WECHATGAME) {
  47. wx.request({
  48. url: "https://pub.dwstatic.com/wxgame/allstar/sheet/offlineIncome.json",
  49. success: (res) => {
  50. if(res.data.video) {
  51. this.videoBtn.active = true;
  52. }
  53. if(res.data.share) {
  54. this.shareBtn.active = true;
  55. }
  56. if(res.data.normal) {
  57. this.normalBtn.active = true;
  58. }
  59. }
  60. })
  61. } else {
  62. this.videoBtn.active = true;
  63. this.shareBtn.active = true;
  64. }
  65. },
  66. onLoad () {
  67. },
  68. start () {
  69. this.content.scaleX = 0;
  70. this.content.scaleY = 0;
  71. this.content.runAction(cc.scaleTo(0.35, 1, 1).easing(cc.easeBackOut()));
  72. this.secretarySprite.runAction(cc.moveBy(0.3, cc.v2(400, 0)));
  73. },
  74. /**
  75. * 重置按钮状态
  76. */
  77. _resetBtn () {
  78. this.videoBtn.active = false;
  79. this.shareBtn.active = false;
  80. this.normalBtn.active = false;
  81. },
  82. dismiss() {
  83. let finish = cc.callFunc(() => {
  84. this.node.destroy();
  85. }, this);
  86. let sequence = cc.sequence(cc.scaleTo(0.2, 0, 0).easing(cc.easeBackIn()), finish)
  87. this.content.runAction(sequence);
  88. },
  89. /**
  90. * 普通关闭,获得正常收益x1
  91. */
  92. close() {
  93. this.addIncome(this._grossIncome)
  94. this.dismiss()
  95. },
  96. /**
  97. * 增加金币收入
  98. * @param {number} value 数量
  99. */
  100. addIncome(value) {
  101. console.log("addIncome: " + value);
  102. GameModule.userInfo.grossIncome = parseInt(GameModule.userInfo.grossIncome) + parseInt(value)
  103. GameEvent.fire(GameNotificationKey.HandleOfflineIncomeAnim)
  104. },
  105. /**
  106. * 看视频获取更多离线收益
  107. */
  108. watchVideo() {
  109. // Todo: 对接微信广告Api
  110. this.addIncome(this._grossIncome * 2)
  111. this.dismiss();
  112. },
  113. /**
  114. * 调起微信分享及后续分享逻辑
  115. */
  116. share() {
  117. if(CC_WECHATGAME) {
  118. // this.shareFlag = 1
  119. wx.shareAppMessage({
  120. title: '偷偷分享给你一个小程序,福利满满,你懂的',
  121. imageUrl: 'https://pub.dwstatic.com/wxgame/allstar/share/share1.jpg',
  122. // query: 'uid=' + Global.user.uid + '&shareType=' + ShareAction.ADD_FRIEND
  123. success: (res) => {
  124. console.log('分享成功');
  125. this.addIncome(this._grossIncome * 3)
  126. this.dismiss();
  127. // this.shareFlag = 0;
  128. },
  129. fail: (res) => {
  130. console.log('分享失败或取消');
  131. }
  132. });
  133. // 微信分享api无法正确回调时候的备用方案
  134. // wx.onShow(res => {
  135. // if(this.shareFlag == 1) {
  136. // GameModule.userInfo.grossIncome += this.grossIncome * 2;
  137. // this.shareFlag = 0;
  138. // this.dismiss();
  139. // }
  140. // })
  141. } else {
  142. this.addIncome(this._grossIncome * 3)
  143. this.dismiss();
  144. }
  145. },
  146. setCoinRichText(coin) {
  147. this.coinRichText.string = `<img src='alert_coin'/><outline color=#ffffff width=4><b><color=#009227> ${DWTool.coinParse(coin)}</c></b></outline>`;
  148. },
  149. setWatchVideoRichText(coin) {
  150. this.watchVideoRichText.string = `<img src='alert_coin'/><color=#ffffff> ${DWTool.coinParse(coin * 2)}</c>`;
  151. },
  152. setShareVideoRichText(coin) {
  153. this.shareRichText.string = `<img src='alert_coin'/><color=#ffffff> ${DWTool.coinParse(coin * 3)}</c>`;
  154. },
  155. // update (dt) {},
  156. });