OTPButton.ts 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. import { AppKernel } from '@scripts/app/AppKernel';
  2. import { addBtnClick } from '@scripts/utils/UIHelper';
  3. import { _decorator, Button, Component, EditBox, Label, Node } from 'cc';
  4. const { ccclass, property } = _decorator;
  5. @ccclass('OTPButton')
  6. export class OTPButton extends Component {
  7. @property(Label)
  8. label: Label;
  9. @property(Button)
  10. button: Button;
  11. @property(EditBox)
  12. account: EditBox;
  13. private _sending = false;
  14. private _cdTimer;
  15. protected onLoad(): void {
  16. addBtnClick(this.node, this.onSendCode, this);
  17. }
  18. protected onEnable(): void {
  19. this.startTimer();
  20. }
  21. protected onDisable(): void {
  22. }
  23. private async onSendCode() {
  24. const account = this.account.string;
  25. if (!account) return;
  26. if (this._sending) return;
  27. this._sending = true;
  28. const mainStore = AppKernel.stores.mainStore;
  29. if (await mainStore.reqSendCode(account)) {
  30. this.startTimer();
  31. }
  32. this._sending = false;
  33. }
  34. private stopTimer() {
  35. if (this._cdTimer) {
  36. this.unschedule(this._cdTimer);
  37. this._cdTimer = null;
  38. }
  39. }
  40. private startTimer() {
  41. this.stopTimer();
  42. const cd = AppKernel.stores.mainStore.otpCD;
  43. this.button.interactable = cd <= 0;
  44. if (cd <= 0) {
  45. this.label.string = "Send Code";
  46. return;
  47. }
  48. this._cdTimer = this.onUpdateCD.bind(this);
  49. this.schedule(this._cdTimer, 1, cd)
  50. this.onUpdateCD();
  51. }
  52. private onUpdateCD() {
  53. const cd = AppKernel.stores.mainStore.otpCD;
  54. this.label.string = `${cd} s`;
  55. console.log('otp cd: ', cd);
  56. if (cd <= 0) {
  57. this.stopTimer();
  58. this.label.string = "Resend Code";
  59. this.button.interactable = true;
  60. }
  61. }
  62. }