OTPButton.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. import { AppKernel } from '@scripts/app/AppKernel';
  2. import { checkIsEmail } from '@scripts/utils/Helper';
  3. import { addBtnClick } from '@scripts/utils/UIHelper';
  4. import { _decorator, Button, Component, EditBox, Enum, Label, Node } from 'cc';
  5. const { ccclass, property } = _decorator;
  6. export enum OTPSendType {
  7. Register, // 注册
  8. ResetPsd, // 重置密码
  9. }
  10. @ccclass('OTPButton')
  11. export class OTPButton extends Component {
  12. @property(Label)
  13. label: Label;
  14. @property(Button)
  15. button: Button;
  16. @property(EditBox)
  17. account: EditBox;
  18. @property({ type: Enum(OTPSendType) })
  19. sendType: OTPSendType;
  20. private _sending = false;
  21. private _cdTimer;
  22. private _account = '';
  23. protected onLoad(): void {
  24. addBtnClick(this.node, this.onSendCode, this);
  25. }
  26. protected onEnable(): void {
  27. this.startTimer();
  28. }
  29. protected onDisable(): void {
  30. }
  31. private async onSendCode() {
  32. const account = this.account.string;
  33. if (!account) return;
  34. if (this._sending) return;
  35. this._sending = true;
  36. const isEmail = checkIsEmail(account);
  37. const { mainStore, mailStore } = AppKernel.stores;
  38. switch (this.sendType) {
  39. case OTPSendType.Register:
  40. const res_register = isEmail
  41. ? await mailStore.reqSendCode(account, "register")
  42. : await mainStore.reqSendCode(account);
  43. if (res_register) {
  44. this._account = account;
  45. this.startTimer();
  46. }
  47. break;
  48. case OTPSendType.ResetPsd:
  49. const res_resetPsd = isEmail
  50. ? await mailStore.reqSendCode(account, "reset_password")
  51. : await mainStore.reqSendCode(account);
  52. if (res_resetPsd) {
  53. this._account = account;
  54. this.startTimer();
  55. }
  56. break;
  57. default:
  58. break;
  59. }
  60. this._sending = false;
  61. }
  62. private stopTimer() {
  63. if (this._cdTimer) {
  64. this.unschedule(this._cdTimer);
  65. this._cdTimer = null;
  66. }
  67. }
  68. private startTimer() {
  69. this.stopTimer();
  70. const cd = this.getCurrentOtpCD();
  71. this.button.interactable = cd <= 0;
  72. if (cd <= 0) {
  73. this.label.string = this.getOptLabel("send");
  74. return;
  75. }
  76. this._cdTimer = this.onUpdateCD.bind(this);
  77. this.schedule(this._cdTimer, 1, cd)
  78. this.onUpdateCD();
  79. }
  80. private onUpdateCD() {
  81. const cd = this.getCurrentOtpCD();
  82. this.label.string = `${cd}s`;
  83. console.log('otp cd: ', cd);
  84. if (cd <= 0) {
  85. this.stopTimer();
  86. this.label.string = this.getOptLabel("resend");
  87. this.button.interactable = true;
  88. }
  89. }
  90. private getCurrentOtpCD() {
  91. const account = this._account;
  92. if (!account) return 0;
  93. const isEmail = checkIsEmail(account);
  94. const { mainStore, mailStore } = AppKernel.stores;
  95. switch (this.sendType) {
  96. case OTPSendType.Register:
  97. return isEmail ? mailStore.otpCD : mainStore.otpCD;
  98. case OTPSendType.ResetPsd:
  99. return isEmail ? mailStore.otpCD : mainStore.otpCD;
  100. default:
  101. break;
  102. }
  103. }
  104. private getOptLabel(state: string) {
  105. if (state === "send") {
  106. switch (this.sendType) {
  107. case OTPSendType.Register:
  108. default:
  109. return "Send"
  110. }
  111. } else if (state === "resend") {
  112. switch (this.sendType) {
  113. case OTPSendType.Register:
  114. default:
  115. return "Resend"
  116. }
  117. }
  118. }
  119. }