DrawContent.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. const AlertManager = require('../utils/AlertManager');
  2. const DrawApi = require("../net/DrawApi");
  3. const DWTool = require("../utils/DWTool");
  4. const GameModule = require("../utils/GameModule");
  5. const WeChat = require('../net/WeChat');
  6. const GameNotificationKey = require('../utils/GameEnum').GameNotificationKey;
  7. const GameRedDot = require('../utils/GameEnum').GameRedDot;
  8. const TapTapTool = require("../utils/TapTapTool");
  9. cc.Class({
  10. extends: cc.Component,
  11. properties: {
  12. moreRichText: cc.RichText,
  13. bestRichText: cc.RichText,
  14. normalRichText: cc.RichText,
  15. normalDrawButton: cc.Button,
  16. bestChanceButton: cc.Button,
  17. moreChanceButton: cc.Button,
  18. normalDrawButtonSprite: cc.Sprite,
  19. bestChanceButtonSprite: cc.Sprite,
  20. moreChanceButtonSprite: cc.Sprite,
  21. content: cc.Node,
  22. shareActionNode: cc.Node,
  23. },
  24. // LIFE-CYCLE CALLBACKS:
  25. onLoad () {
  26. this.setUpUIIsShow(false);
  27. if (Global.isCheck) {
  28. this.shareActionNode.active = false;
  29. }
  30. this.getDrawState().then((respondData) => {
  31. this.setUpUIIsShow(true);
  32. this.respondData = respondData;
  33. this.setUpUI();
  34. }).catch(({code, msg}) => {
  35. console.log(code, msg);
  36. });
  37. GameEvent.on(GameNotificationKey.GameShowNotificationKey, this, this.updatelData);
  38. },
  39. onDestroy() {
  40. GameEvent.off(GameNotificationKey.GameShowNotificationKey, this);
  41. },
  42. updatelData() {
  43. // cdTime 已经用了的CD时间 如果 如果为0或者大于和等于总时间 说明可以免费抽奖
  44. // cdTotalTime cd总时间 ,目前是60分钟
  45. this.getDrawState().then((respondData) => {
  46. this.respondData = respondData;
  47. this.updateUI();
  48. }).catch(({code, msg}) => {
  49. console.log(code, msg);
  50. });
  51. },
  52. start () {
  53. },
  54. setUpUIIsShow(isShow) {
  55. this.normalDrawButton.node.active = isShow;
  56. this.bestChanceButton.node.active = isShow;
  57. this.moreChanceButton.node.active = isShow;
  58. },
  59. updateUI() {
  60. /// 抽奖冷却结束
  61. if (this.respondData.cdTime >= this.respondData.cdTotalTime) {
  62. this.setUpNormalButton();
  63. } else {
  64. this.setUpUseBtnBg(false, 1);
  65. let text = DWTool.calculateTime((this.respondData.cdTotalTime - this.respondData.cdTime) / 1000);
  66. this.normalRichText.string = '<b><color=#ffffff>' + text + '</c></b>';
  67. }
  68. },
  69. setUpUI() {
  70. /// 抽奖冷却结束
  71. if (this.respondData.cdTime >= this.respondData.cdTotalTime) {
  72. this.setUpNormalButton();
  73. } else {
  74. this.setUpUseBtnBg(false, 1);
  75. let text = DWTool.calculateTime((this.respondData.cdTotalTime - this.respondData.cdTime) / 1000);
  76. this.normalRichText.string = '<b><color=#ffffff>' + text + '</c></b>';
  77. this.schedule(this.updateCD, 1);
  78. }
  79. this.moreRichText.string = `<img src='skill_diamond'/><b><color=#ffffff> ${this.respondData.diamond2}</c></b>`;
  80. this.bestRichText.string = `<img src='skill_diamond'/><b><color=#ffffff> ${this.respondData.diamond3}</c></b>`;
  81. let hasDiamod = GameModule.userInfo.diamond;
  82. if (hasDiamod < this.respondData.diamond2) {
  83. this.setUpUseBtnBg(false, 2);
  84. }
  85. if (hasDiamod < this.respondData.diamond3) {
  86. this.setUpUseBtnBg(false, 3);
  87. }
  88. },
  89. setUpNormalButton() {
  90. this.normalDrawButton.interactable = true;
  91. this.normalRichText.string = '<b><color=#ffffff>分享\n免费获得</c></b>';
  92. },
  93. /// 网络请求
  94. getDrawState() {
  95. return new Promise((resolve, reject) => {
  96. DrawApi.getLotteryInfo((respondData) => {
  97. resolve(respondData);
  98. }, (code, msg) => {
  99. reject({code, msg});
  100. });
  101. })
  102. },
  103. startDrawRequest(typeId, diamond = 0) {
  104. return new Promise((resolve, reject) => {
  105. ///开始抽奖
  106. DrawApi.startLottery(typeId, diamond, (respondData) => {
  107. resolve(respondData);
  108. }, (code, msg) => {
  109. reject({code, msg});
  110. });
  111. })
  112. },
  113. startDraw(typeId, completion) {
  114. this.startDrawRequest(typeId, 0).then((respondData) => {
  115. /// 把抽奖状态 以及抽奖成功之后的数据加上去
  116. this.respondData.propId = respondData.propId;
  117. this.respondData.propName = respondData.propName;
  118. this.respondData.roomId = respondData.roomId;
  119. this.closeNodeAction();
  120. completion(true);
  121. AlertManager.showDrawScrollAlert(this.respondData, typeId);
  122. }).catch(({code, msg}) => {
  123. console.log(code, msg);
  124. completion(false);
  125. Global.commonAlert.showCommonErrorAlert(msg);
  126. });
  127. },
  128. updateCD() {
  129. this.respondData.cdTime += 1000;
  130. if (this.respondData.cdTime >= this.respondData.cdTotalTime) {
  131. this.setUpNormalButton();
  132. this.setUpUseBtnBg(true, 1);
  133. this.unschedule(this.updateCD, this);
  134. }
  135. let text = DWTool.calculateTime((this.respondData.cdTotalTime - this.respondData.cdTime) / 1000);
  136. this.normalRichText.string = '<b><color=#ffffff>' + text + '</c></b>';
  137. },
  138. /// 看广告抽奖
  139. adNodeAction() {
  140. GameModule.audioMng.playClickButton();
  141. if (CC_WECHATGAME) {
  142. this.normalDrawButton.interactable = false;
  143. WeChat.shareAction( () => {
  144. this.startDraw(1, () => {
  145. this.normalDrawButton.interactable = true;
  146. TapTapTool.removeRedDot(GameRedDot.draw);
  147. });
  148. }, () => {
  149. this.normalDrawButton.interactable = true;
  150. console.log('分享失败或取消');
  151. });
  152. }
  153. },
  154. ///更多砖石抽奖
  155. moreChanceNodeAction() {
  156. GameModule.audioMng.playClickButton();
  157. this.moreChanceButton.interactable = false;
  158. this.startDraw(2, (isSuccess) => {
  159. this.moreChanceButton.interactable = true;
  160. if (isSuccess) {
  161. GameModule.userInfo.diamond -= this.respondData.diamond2;
  162. }
  163. });
  164. },
  165. ///最大概率抽奖
  166. bestChanceNodeAction () {
  167. GameModule.audioMng.playClickButton();
  168. this.bestChanceButton.interactable = false;
  169. this.startDraw(3, (isSuccess) => {
  170. this.bestChanceButton.interactable = true;
  171. if (isSuccess) {
  172. GameModule.userInfo.diamond -= this.respondData.diamond3;
  173. }
  174. });
  175. },
  176. closeNodeAction() {
  177. // let finish = cc.callFunc(() => {
  178. // this.node.destroy();
  179. // }, this);
  180. // let sequence = cc.sequence(cc.scaleTo(0.2, 0, 0).easing(cc.easeBackIn()), finish)
  181. // this.content.runAction(sequence);
  182. this.node.destroy();
  183. GameModule.audioMng.playClickButton();
  184. },
  185. /// 设置button是否能点击 type 123
  186. setUpUseBtnBg(isActive, type) {
  187. let path = isActive ? './textures/draw/draw_green_btn_bg' : './textures/draw/draw_gray_btn_bg';
  188. DWTool.loadResSpriteFrame(path)
  189. .then((spriteFrame) => {
  190. if (type == 1) {
  191. this.normalDrawButtonSprite.spriteFrame = spriteFrame;
  192. this.normalDrawButton.interactable = isActive;
  193. } else if (type == 2) {
  194. this.moreChanceButtonSprite.spriteFrame = spriteFrame;
  195. this.moreChanceButton.interactable = isActive;
  196. } else {
  197. this.bestChanceButtonSprite.spriteFrame = spriteFrame;
  198. this.bestChanceButton.interactable = isActive;
  199. }
  200. });
  201. },
  202. // update (dt) {},
  203. });