OTPButton.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. import { AppKernel } from '@scripts/app/AppKernel';
  2. import { MailOtpType, OtpType } from '@scripts/store';
  3. import { checkIsEmail } from '@scripts/utils/Helper';
  4. import { addBtnClick, removeBtnClick } from '@scripts/utils/UIHelper';
  5. import { _decorator, Button, Color, Component, EditBox, Enum, Label, Node, Sprite } 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 = null;
  26. @property({ type: Enum(OTPSendType) })
  27. sendType: OTPSendType;
  28. private _sending = false;
  29. private _cdTimer: () => void = null;
  30. // 0A5312
  31. private readonly _active_border_color = new Color('#0A5312');
  32. private readonly _inactive_border_color = new Color('#000000');
  33. // 使用时直接赋值
  34. protected onLoad(): void {
  35. addBtnClick(this.button, this.onSendCode, this);
  36. if (this.account) this.account.node.on(EditBox.EventType.TEXT_CHANGED, this.handleTextChanged, this);
  37. }
  38. protected onDestroy() {
  39. this.stopTimer(); // 新增清理定时器
  40. }
  41. protected onEnable(): void {
  42. this.updateBtnStatus();
  43. this.handleTextChanged();
  44. }
  45. private async onSendCode() {
  46. if (this._sending) return;
  47. this._sending = true;
  48. AppKernel.ui.loading(true);
  49. try {
  50. const cd = this.getCurCD();
  51. if (cd > 0) return;
  52. const { mainStore, mailStore } = AppKernel.stores;
  53. switch (this.sendType) {
  54. case OTPSendType.Register: {
  55. const account = this.getInputAccount();
  56. if (!account) break;
  57. const isEmail = checkIsEmail(account);
  58. isEmail
  59. ? await mailStore.reqSendCode(account, "register")
  60. : await mainStore.reqSendCode(account);
  61. this.startTimer();
  62. break;
  63. }
  64. case OTPSendType.ResetPsd: {
  65. const account = this.getInputAccount();
  66. if (!account) break;
  67. const isEmail = checkIsEmail(account);
  68. isEmail
  69. ? await mailStore.reqSendCode(account, "reset_password")
  70. : await mainStore.reqSendCode(account);
  71. this.startTimer();
  72. break;
  73. }
  74. case OTPSendType.BindEmail: {
  75. const account = this.getInputAccount();
  76. if (!account) break;
  77. await mailStore.reqBindEmailCode({ account });
  78. this.startTimer();
  79. break;
  80. }
  81. case OTPSendType.ChangeEmailOld: {
  82. const { Email: account } = AppKernel.stores.mainStore.userInfo;
  83. if (!account) break;
  84. await mailStore.reqChangeEmailCode({
  85. account,
  86. target: "old"
  87. });
  88. this.startTimer();
  89. break;
  90. }
  91. case OTPSendType.ChangeEmailNew: {
  92. const account = this.getInputAccount();
  93. if (!account) break;
  94. await mailStore.reqChangeEmailCode({
  95. account,
  96. target: "new"
  97. });
  98. this.startTimer();
  99. break;
  100. }
  101. case OTPSendType.BindPhone: {
  102. const account = this.getInputAccount();
  103. if (!account) break;
  104. await mainStore.reqSendCode(account);
  105. this.startTimer();
  106. break;
  107. }
  108. case OTPSendType.ChangePhoneOld: {
  109. const { Phone: account } = AppKernel.stores.mainStore.userInfo;
  110. if (!account) break;
  111. await mainStore.reqSendCode(account, true);
  112. this.startTimer();
  113. break;
  114. }
  115. case OTPSendType.ChangePhoneNew: {
  116. const account = this.getInputAccount();
  117. if (!account) break;
  118. await mainStore.reqSendCode(account);
  119. this.startTimer();
  120. break;
  121. }
  122. case OTPSendType.Unbind3rdLogin: {
  123. const account = this.getInputAccount();
  124. if (!account) break;
  125. const isEmail = checkIsEmail(account);
  126. await mainStore.reqUnBind3rdCode({
  127. verify_type: isEmail ? "email" : "phone"
  128. });
  129. this.startTimer();
  130. break;
  131. }
  132. default:
  133. break;
  134. }
  135. } finally {
  136. AppKernel.ui.loading(false);
  137. this._sending = false;
  138. }
  139. }
  140. private stopTimer() {
  141. if (this._cdTimer) {
  142. this.unschedule(this._cdTimer);
  143. this._cdTimer = null;
  144. }
  145. }
  146. private startTimer() {
  147. this.stopTimer();
  148. this.updateBtnStatus();
  149. const cd = this.getCurCD();
  150. if (cd <= 0) {
  151. this.label.string = this.getLabel("send");
  152. return;
  153. }
  154. this._cdTimer = this.onUpdateCD.bind(this);
  155. this.schedule(this._cdTimer, 1, cd - 1);
  156. this.onUpdateCD();
  157. }
  158. private onUpdateCD() {
  159. const cd = this.getCurCD();
  160. this.label.string = `${cd}s`;
  161. console.log('otp cd: ', cd);
  162. if (cd <= 0) {
  163. this.stopTimer();
  164. this.label.string = this.getLabel("resend");
  165. this.updateBtnStatus();
  166. }
  167. }
  168. private handleTextChanged() {
  169. const cd = this.getCurCD();
  170. if (cd > 0 && this._cdTimer == null) {//存在cd 但没倒计时
  171. this.startTimer();
  172. } else if (cd <= 0 && this._cdTimer) {//不存在cd 但有倒计时
  173. this.stopTimer();
  174. this.label.string = this.getLabel("send");
  175. this.updateBtnStatus();
  176. } else {
  177. this.updateBtnStatus();
  178. }
  179. }
  180. private updateBtnStatus() {
  181. const cd = this.getCurCD();
  182. const flag = !this.account || !!this.getInputAccount();
  183. const isActive = cd <= 0 && flag;
  184. this.button.interactable = isActive;
  185. const sprite = this.button.getComponent(Sprite);
  186. if (sprite) sprite.grayscale = !isActive;
  187. this.label.outlineColor = isActive
  188. ? this._active_border_color
  189. : this._inactive_border_color;
  190. }
  191. private getCurCD() {
  192. const { mainStore, mailStore } = AppKernel.stores;
  193. switch (this.sendType) {
  194. case OTPSendType.Register: {
  195. if (this.account == null) return 0;
  196. const account = this.getInputAccount();
  197. if (!account) return 0;
  198. const isEmail = checkIsEmail(account);
  199. return isEmail
  200. ? mailStore.getOtpCD(MailOtpType.Register)
  201. : mainStore.getOtpCD(OtpType.Main);
  202. }
  203. case OTPSendType.ResetPsd: {
  204. if (this.account == null) return 0;
  205. const account = this.getInputAccount();
  206. if (!account) return 0;
  207. const isEmail = checkIsEmail(account);
  208. return isEmail
  209. ? mailStore.getOtpCD(MailOtpType.Reset)
  210. : mainStore.getOtpCD(OtpType.Main);
  211. }
  212. case OTPSendType.BindEmail:
  213. return mailStore.getOtpCD(MailOtpType.Bind);
  214. case OTPSendType.ChangeEmailOld:
  215. return mailStore.getOtpCD(MailOtpType.ChangeOld);
  216. case OTPSendType.ChangeEmailNew:
  217. return mailStore.getOtpCD(MailOtpType.ChangeNew);
  218. case OTPSendType.BindPhone:
  219. case OTPSendType.ChangePhoneNew:
  220. return mainStore.getOtpCD(OtpType.Main);
  221. case OTPSendType.ChangePhoneOld:
  222. return mainStore.getOtpCD(OtpType.ChangeOld);
  223. case OTPSendType.Unbind3rdLogin:
  224. return mainStore.getOtpCD(OtpType.Unbind3rd);
  225. default:
  226. return 0;
  227. }
  228. }
  229. private getLabel(state: string) {
  230. if (state === "send") {
  231. switch (this.sendType) {
  232. case OTPSendType.BindEmail:
  233. case OTPSendType.ChangeEmailNew:
  234. case OTPSendType.BindPhone:
  235. case OTPSendType.ChangePhoneNew:
  236. case OTPSendType.Unbind3rdLogin:
  237. return "Send Code"
  238. case OTPSendType.ChangeEmailOld:
  239. case OTPSendType.ChangePhoneOld:
  240. return "Send to Current"
  241. default:
  242. return "Send"
  243. }
  244. } else if (state === "resend") {
  245. switch (this.sendType) {
  246. case OTPSendType.BindEmail:
  247. case OTPSendType.ChangeEmailOld:
  248. case OTPSendType.ChangeEmailNew:
  249. case OTPSendType.BindPhone:
  250. case OTPSendType.ChangePhoneOld:
  251. case OTPSendType.ChangePhoneNew:
  252. case OTPSendType.Unbind3rdLogin:
  253. return "Resend Code"
  254. default:
  255. return "Resend"
  256. }
  257. }
  258. return "Send";
  259. }
  260. private getInputAccount(): string {
  261. if (!this.account) return '';
  262. return this.account.string.trim();
  263. }
  264. }