ElmWindow.ts 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import { _decorator, Component, Node, Button, Sprite, Animation } from 'cc';
  2. import { Http } from '../core/net/Http';
  3. import { WindowManager } from '../core/ui/window/WindowManager';
  4. import { g } from '../Data/g';
  5. import { platform } from '../Data/platform';
  6. const { ccclass, property } = _decorator;
  7. @ccclass('ElmWindow')
  8. export class ElmWindow extends Component {
  9. @property({ type: Http, tooltip: "http组件" }) http: Http;
  10. @property({ type: Button, tooltip: "兑换按钮" }) exchangeBtn: Button;
  11. private needBonus = 0;
  12. async start() {
  13. let result = await this.http.send("/api/user/getCouponTimes");
  14. if (result.code == 0) {
  15. this.needBonus = result.data.needBonus;
  16. if (result.data.times <= 0) {
  17. this.disableButton();
  18. }
  19. } else {
  20. WindowManager.showTips("数据获取失败,请稍后再试");
  21. this.disableButton();
  22. }
  23. }
  24. public async onExchange() {
  25. if (g.userData.bonus >= this.needBonus) {
  26. platform.umUp("elmClick");//饿了么埋点
  27. let result = await this.http.send("/api/user/getCoupon");
  28. if (result.code == 0) {
  29. g.userData.bonus -= result.data.needBonus;
  30. platform.openWXApplets(result.data.ghid, result.data.path);
  31. this.disableButton();
  32. } else {
  33. WindowManager.showTips("数据获取失败,请稍后再试");
  34. }
  35. } else {
  36. WindowManager.showTips("红包不足");
  37. }
  38. }
  39. private disableButton(): void {
  40. this.exchangeBtn.interactable = false;
  41. this.exchangeBtn.node.getComponent(Sprite).grayscale = true;
  42. this.exchangeBtn.getComponent(Animation).stop();
  43. }
  44. }