| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321 |
- import { AppKernel } from '@scripts/app/AppKernel';
- import { MailOtpType, OtpType } from '@scripts/store';
- import { checkIsEmail } from '@scripts/utils/Helper';
- import { addBtnClick, removeBtnClick } from '@scripts/utils/UIHelper';
- import { _decorator, Button, Color, Component, EditBox, Enum, Label, Node, Sprite } from 'cc';
- const { ccclass, property } = _decorator;
- export enum OTPSendType {
- Register, // 注册
- ResetPsd, // 重置密码
- BindEmail, // 绑定邮箱
- ChangeEmailOld, // 修改邮箱(旧邮箱)
- ChangeEmailNew, // 修改邮箱(新邮箱)
- BindPhone, // 绑定手机
- ChangePhoneOld, // 修改手机(旧手机)
- ChangePhoneNew, // 修改手机(新手机)
- Unbind3rdLogin, // 解绑第三方登录
- }
- @ccclass('OTPButton')
- export class OTPButton extends Component {
- @property(Label)
- label: Label;
- @property(Button)
- button: Button;
- @property(EditBox)
- account: EditBox = null;
- @property({ type: Enum(OTPSendType) })
- sendType: OTPSendType;
- private _sending = false;
- private _cdTimer: () => void = null;
- // 0A5312
- private readonly _active_border_color = new Color('#0A5312');
- private readonly _inactive_border_color = new Color('#000000');
- // 使用时直接赋值
- protected onLoad(): void {
- addBtnClick(this.button, this.onSendCode, this);
- if (this.account) this.account.node.on(EditBox.EventType.TEXT_CHANGED, this.handleTextChanged, this);
- }
- protected onDestroy() {
- this.stopTimer(); // 新增清理定时器
- }
- protected onEnable(): void {
- this.updateBtnStatus();
- this.handleTextChanged();
- }
- private async onSendCode() {
- if (this._sending) return;
- this._sending = true;
- AppKernel.ui.loading(true);
- try {
- const cd = this.getCurCD();
- if (cd > 0) return;
- const { mainStore, mailStore } = AppKernel.stores;
- switch (this.sendType) {
- case OTPSendType.Register: {
- const account = this.getInputAccount();
- if (!account) break;
- const isEmail = checkIsEmail(account);
- isEmail
- ? await mailStore.reqSendCode(account, "register")
- : await mainStore.reqSendCode(account);
- this.startTimer();
- break;
- }
- case OTPSendType.ResetPsd: {
- const account = this.getInputAccount();
- if (!account) break;
- const isEmail = checkIsEmail(account);
- isEmail
- ? await mailStore.reqSendCode(account, "reset_password")
- : await mainStore.reqSendCode(account);
- this.startTimer();
- break;
- }
- case OTPSendType.BindEmail: {
- const account = this.getInputAccount();
- if (!account) break;
- await mailStore.reqBindEmailCode({ account });
- this.startTimer();
- break;
- }
- case OTPSendType.ChangeEmailOld: {
- const { Email: account } = AppKernel.stores.mainStore.userInfo;
- if (!account) break;
- await mailStore.reqChangeEmailCode({
- account,
- target: "old"
- });
- this.startTimer();
- break;
- }
- case OTPSendType.ChangeEmailNew: {
- const account = this.getInputAccount();
- if (!account) break;
- await mailStore.reqChangeEmailCode({
- account,
- target: "new"
- });
- this.startTimer();
- break;
- }
- case OTPSendType.BindPhone: {
- const account = this.getInputAccount();
- if (!account) break;
- await mainStore.reqSendCode(account);
- this.startTimer();
- break;
- }
- case OTPSendType.ChangePhoneOld: {
- const { Phone: account } = AppKernel.stores.mainStore.userInfo;
- if (!account) break;
- await mainStore.reqSendCode(account, true);
- this.startTimer();
- break;
- }
- case OTPSendType.ChangePhoneNew: {
- const account = this.getInputAccount();
- if (!account) break;
- await mainStore.reqSendCode(account);
- this.startTimer();
- break;
- }
- case OTPSendType.Unbind3rdLogin: {
- const account = this.getInputAccount();
- if (!account) break;
- const isEmail = checkIsEmail(account);
- await mainStore.reqUnBind3rdCode({
- verify_type: isEmail ? "email" : "phone"
- });
- this.startTimer();
- break;
- }
- default:
- break;
- }
- } finally {
- AppKernel.ui.loading(false);
- this._sending = false;
- }
- }
- private stopTimer() {
- if (this._cdTimer) {
- this.unschedule(this._cdTimer);
- this._cdTimer = null;
- }
- }
- private startTimer() {
- this.stopTimer();
- this.updateBtnStatus();
- const cd = this.getCurCD();
- if (cd <= 0) {
- this.label.string = this.getLabel("send");
- return;
- }
- this._cdTimer = this.onUpdateCD.bind(this);
- this.schedule(this._cdTimer, 1, cd - 1);
- this.onUpdateCD();
- }
- private onUpdateCD() {
- const cd = this.getCurCD();
- this.label.string = `${cd}s`;
- console.log('otp cd: ', cd);
- if (cd <= 0) {
- this.stopTimer();
- this.label.string = this.getLabel("resend");
- this.updateBtnStatus();
- }
- }
- private handleTextChanged() {
- const cd = this.getCurCD();
- if (cd > 0 && this._cdTimer == null) {//存在cd 但没倒计时
- this.startTimer();
- } else if (cd <= 0 && this._cdTimer) {//不存在cd 但有倒计时
- this.stopTimer();
- this.label.string = this.getLabel("send");
- this.updateBtnStatus();
- } else {
- this.updateBtnStatus();
- }
- }
- private updateBtnStatus() {
- const cd = this.getCurCD();
- const flag = !this.account || !!this.getInputAccount();
- const isActive = cd <= 0 && flag;
- this.button.interactable = isActive;
- const sprite = this.button.getComponent(Sprite);
- if (sprite) sprite.grayscale = !isActive;
- this.label.outlineColor = isActive
- ? this._active_border_color
- : this._inactive_border_color;
- }
- private getCurCD() {
- const { mainStore, mailStore } = AppKernel.stores;
- switch (this.sendType) {
- case OTPSendType.Register: {
- if (this.account == null) return 0;
- const account = this.getInputAccount();
- if (!account) return 0;
- const isEmail = checkIsEmail(account);
- return isEmail
- ? mailStore.getOtpCD(MailOtpType.Register)
- : mainStore.getOtpCD(OtpType.Main);
- }
- case OTPSendType.ResetPsd: {
- if (this.account == null) return 0;
- const account = this.getInputAccount();
- if (!account) return 0;
- const isEmail = checkIsEmail(account);
- return isEmail
- ? mailStore.getOtpCD(MailOtpType.Reset)
- : mainStore.getOtpCD(OtpType.Main);
- }
- case OTPSendType.BindEmail:
- return mailStore.getOtpCD(MailOtpType.Bind);
- case OTPSendType.ChangeEmailOld:
- return mailStore.getOtpCD(MailOtpType.ChangeOld);
- case OTPSendType.ChangeEmailNew:
- return mailStore.getOtpCD(MailOtpType.ChangeNew);
- case OTPSendType.BindPhone:
- case OTPSendType.ChangePhoneNew:
- return mainStore.getOtpCD(OtpType.Main);
- case OTPSendType.ChangePhoneOld:
- return mainStore.getOtpCD(OtpType.ChangeOld);
- case OTPSendType.Unbind3rdLogin:
- return mainStore.getOtpCD(OtpType.Unbind3rd);
- default:
- return 0;
- }
- }
- private getLabel(state: string) {
- if (state === "send") {
- switch (this.sendType) {
- case OTPSendType.BindEmail:
- case OTPSendType.ChangeEmailNew:
- case OTPSendType.BindPhone:
- case OTPSendType.ChangePhoneNew:
- case OTPSendType.Unbind3rdLogin:
- return "Send Code"
- case OTPSendType.ChangeEmailOld:
- case OTPSendType.ChangePhoneOld:
- return "Send to Current"
- default:
- return "Send"
- }
- } else if (state === "resend") {
- switch (this.sendType) {
- case OTPSendType.BindEmail:
- case OTPSendType.ChangeEmailOld:
- case OTPSendType.ChangeEmailNew:
- case OTPSendType.BindPhone:
- case OTPSendType.ChangePhoneOld:
- case OTPSendType.ChangePhoneNew:
- case OTPSendType.Unbind3rdLogin:
- return "Resend Code"
- default:
- return "Resend"
- }
- }
- return "Send";
- }
- private getInputAccount(): string {
- if (!this.account) return '';
- return this.account.string.trim();
- }
- }
|