SignIn.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. const SignInApi = require("../net/SignInApi");
  2. const GameModule = require("../utils/GameModule");
  3. cc.Class({
  4. extends: cc.Component,
  5. properties: {
  6. layout: cc.Layout,
  7. zIndex: {
  8. default: 0,
  9. notify(oldValue) {
  10. //减少无效赋值
  11. if (oldValue === this.zIndex) {
  12. return;
  13. }
  14. this.node.zIndex = this.zIndex;
  15. }
  16. },
  17. awardSprite: cc.Sprite,
  18. awardRichText: cc.RichText,
  19. signInItem: cc.Prefab
  20. },
  21. // LIFE-CYCLE CALLBACKS:
  22. onLoad () {
  23. this.node.zIndex = this.zIndex;
  24. this.isSigned = false;
  25. },
  26. handleSignIn() {
  27. if (this.isSigned) { return; }
  28. if (this.award) {
  29. this.isSigned = true;
  30. GameModule.audioMng.playClickButton();
  31. SignInApi.postSignAward(this.award.signId, (respondData) => {
  32. this.isSigned = false;
  33. // 更新全局userInfo
  34. GameModule.userInfo.diamond += this.award.diamond;
  35. GameGlobal.isSignAward = true;
  36. this.node.destroy();
  37. }, (errCode, errMsg) => {
  38. this.isSigned = false;
  39. if (errCode == -3) {
  40. this.node.destroy();
  41. } else {
  42. GameGlobal.commonAlert.showCommonErrorAlert(`签到失败 \n${errMsg}`);
  43. }
  44. });
  45. }
  46. },
  47. start () {
  48. this.getAllSign((respondData) => {
  49. this.configData(respondData);
  50. }, (code) => {
  51. console.log(code);
  52. this.node.destroy();
  53. });
  54. },
  55. /// 网络请求
  56. getAllSign(success, fail) {
  57. SignInApi.getUserAllSign(success, fail);
  58. },
  59. configData(respondData) {
  60. this.respondData = respondData;
  61. var hasObject = false;
  62. if (respondData.userSigns.length > 0) {
  63. for (let i = 0; i < respondData.userSigns.length; ++i) {
  64. let model = respondData.userSigns[i];
  65. let item = cc.instantiate(this.signInItem);
  66. item = item.getComponent('SignInItem');
  67. item.configData(model);
  68. this.layout.node.addChild(item.node);
  69. if (model.status == 0 && !hasObject) {
  70. this.award = model;
  71. hasObject = true;
  72. }
  73. }
  74. if (this.award) {
  75. this.awardRichText.string = `<outline color=white width=2><b><color=#7D4025>X${this.award.diamond}</color></b></outline>`;
  76. }
  77. }
  78. }
  79. // update (dt) {},
  80. });