OTPButton.ts 10.0 KB

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