OTPButton.ts 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. import { AppKernel } from '@scripts/app/AppKernel';
  2. import { MailOtpType, OtpType } from '@scripts/store';
  3. import { checkIsEmail } from '@scripts/utils/Helper';
  4. import { addBtnClick } from '@scripts/utils/UIHelper';
  5. import { _decorator, Button, Component, EditBox, Enum, Label, Node } from 'cc';
  6. const { ccclass, property } = _decorator;
  7. export enum OTPSendType {
  8. Register, // 注册
  9. ResetPsd, // 重置密码
  10. BindEmail, // 绑定邮箱
  11. ChangeEmailOld, // 修改邮箱(旧邮箱)
  12. ChangeEmailNew, // 修改邮箱(新邮箱)
  13. BindPhone, // 绑定手机
  14. ChangePhoneOld, // 修改手机(旧手机)
  15. ChangePhoneNew, // 修改手机(新手机)
  16. Unbind3rdLogin, // 解绑第三方登录
  17. }
  18. @ccclass('OTPButton')
  19. export class OTPButton extends Component {
  20. @property(Label)
  21. label: Label;
  22. @property(Button)
  23. button: Button;
  24. @property(EditBox)
  25. account: EditBox;
  26. @property({ type: Enum(OTPSendType) })
  27. sendType: OTPSendType;
  28. private _sending = false;
  29. private _cdTimer;
  30. private _account = '';
  31. protected onLoad(): void {
  32. addBtnClick(this.node, this.onSendCode, this);
  33. }
  34. protected onEnable(): void {
  35. this.startTimer();
  36. }
  37. protected onDisable(): void {
  38. }
  39. private async onSendCode() {
  40. if (this._sending) return;
  41. this._sending = true;
  42. const { mainStore, mailStore } = AppKernel.stores;
  43. switch (this.sendType) {
  44. case OTPSendType.Register: {
  45. const account = this.account.string;
  46. if (!account) break;
  47. const isEmail = checkIsEmail(account);
  48. isEmail
  49. ? await mailStore.reqSendCode(account, "register")
  50. : await mainStore.reqSendCode(account);
  51. this._account = account;
  52. this.startTimer();
  53. break;
  54. }
  55. case OTPSendType.ResetPsd: {
  56. const account = this.account.string;
  57. if (!account) break;
  58. const isEmail = checkIsEmail(account);
  59. isEmail
  60. ? await mailStore.reqSendCode(account, "reset_password")
  61. : await mainStore.reqSendCode(account);
  62. this._account = account;
  63. this.startTimer();
  64. break;
  65. }
  66. case OTPSendType.BindEmail: {
  67. const account = this.account.string;
  68. if (!account) break;
  69. await mailStore.reqBindEmailCode({ account });
  70. this._account = account;
  71. this.startTimer();
  72. break;
  73. }
  74. case OTPSendType.ChangeEmailOld: {
  75. const { Email: account } = AppKernel.stores.mainStore.userInfo;
  76. if (!account) break;
  77. await mailStore.reqChangeEmailCode({
  78. account,
  79. target: "old"
  80. });
  81. this._account = account;
  82. this.startTimer();
  83. break;
  84. }
  85. case OTPSendType.ChangeEmailNew: {
  86. const account = this.account.string;
  87. if (!account) break;
  88. await mailStore.reqChangeEmailCode({
  89. account,
  90. target: "new"
  91. });
  92. this._account = account;
  93. this.startTimer();
  94. break;
  95. }
  96. case OTPSendType.BindPhone: {
  97. const account = this.account.string;
  98. if (!account) break;
  99. await mainStore.reqSendCode(account);
  100. this._account = account;
  101. this.startTimer();
  102. break;
  103. }
  104. case OTPSendType.ChangePhoneOld: {
  105. const { Phone: account } = AppKernel.stores.mainStore.userInfo;
  106. if (!account) break;
  107. await mainStore.reqSendCode(account, true);
  108. this._account = account;
  109. this.startTimer();
  110. break;
  111. }
  112. case OTPSendType.ChangePhoneNew: {
  113. const account = this.account.string;
  114. if (!account) break;
  115. await mainStore.reqSendCode(account);
  116. this._account = account;
  117. this.startTimer();
  118. break;
  119. }
  120. case OTPSendType.Unbind3rdLogin: {
  121. const account = this.account.string;
  122. if (!account) break;
  123. const isEmail = checkIsEmail(account);
  124. await mainStore.reqUnBind3rdCode({
  125. verify_type: isEmail ? "email" : "phone"
  126. });
  127. this._account = account;
  128. this.startTimer();
  129. break;
  130. }
  131. default:
  132. break;
  133. }
  134. this._sending = false;
  135. }
  136. private stopTimer() {
  137. if (this._cdTimer) {
  138. this.unschedule(this._cdTimer);
  139. this._cdTimer = null;
  140. }
  141. }
  142. private startTimer() {
  143. this.stopTimer();
  144. const cd = this.getCurrentOtpCD();
  145. this.button.interactable = cd <= 0;
  146. if (cd <= 0) {
  147. this.label.string = this.getOptLabel("send");
  148. return;
  149. }
  150. this._cdTimer = this.onUpdateCD.bind(this);
  151. this.schedule(this._cdTimer, 1, cd)
  152. this.onUpdateCD();
  153. }
  154. private onUpdateCD() {
  155. const cd = this.getCurrentOtpCD();
  156. this.label.string = `${cd}s`;
  157. console.log('otp cd: ', cd);
  158. if (cd <= 0) {
  159. this.stopTimer();
  160. this.label.string = this.getOptLabel("resend");
  161. this.button.interactable = true;
  162. }
  163. }
  164. private getCurrentOtpCD() {
  165. const account = this._account;
  166. if (!account) return 0;
  167. const isEmail = checkIsEmail(account);
  168. const { mainStore, mailStore } = AppKernel.stores;
  169. switch (this.sendType) {
  170. case OTPSendType.Register:
  171. return isEmail
  172. ? mailStore.getOtpCD(MailOtpType.Register)
  173. : mainStore.getOtpCD(OtpType.Main);
  174. case OTPSendType.ResetPsd:
  175. return isEmail
  176. ? mailStore.getOtpCD(MailOtpType.Reset)
  177. : mainStore.getOtpCD(OtpType.Main);
  178. case OTPSendType.BindEmail:
  179. return mailStore.getOtpCD(MailOtpType.Bind);
  180. case OTPSendType.ChangeEmailOld:
  181. return mailStore.getOtpCD(MailOtpType.ChangeOld);
  182. case OTPSendType.ChangeEmailNew:
  183. return mailStore.getOtpCD(MailOtpType.ChangeNew);
  184. case OTPSendType.BindPhone:
  185. case OTPSendType.ChangePhoneNew:
  186. return mainStore.getOtpCD(OtpType.Main);
  187. case OTPSendType.ChangePhoneOld:
  188. return mainStore.getOtpCD(OtpType.ChangeOld);
  189. case OTPSendType.Unbind3rdLogin:
  190. return mainStore.getOtpCD(OtpType.Unbind3rd);
  191. default:
  192. break;
  193. }
  194. }
  195. private getOptLabel(state: string) {
  196. if (state === "send") {
  197. switch (this.sendType) {
  198. case OTPSendType.BindEmail:
  199. case OTPSendType.ChangeEmailNew:
  200. case OTPSendType.BindPhone:
  201. case OTPSendType.ChangePhoneNew:
  202. case OTPSendType.Unbind3rdLogin:
  203. return "Send Code"
  204. case OTPSendType.ChangeEmailOld:
  205. case OTPSendType.ChangePhoneOld:
  206. return "Send to Current"
  207. default:
  208. return "Send"
  209. }
  210. } else if (state === "resend") {
  211. switch (this.sendType) {
  212. case OTPSendType.BindEmail:
  213. case OTPSendType.ChangeEmailOld:
  214. case OTPSendType.ChangeEmailNew:
  215. case OTPSendType.BindPhone:
  216. case OTPSendType.ChangePhoneOld:
  217. case OTPSendType.ChangePhoneNew:
  218. case OTPSendType.Unbind3rdLogin:
  219. return "Resend Code"
  220. default:
  221. return "Resend"
  222. }
  223. }
  224. }
  225. }