ChangeName.ts 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import { _decorator, Button, Color, EditBox, instantiate, Label, Node, RichText, Sprite, SpriteFrame, UITransform } from 'cc';
  2. import { UIViewID } from '@scripts/define/UIDefine';
  3. import { UICloseReason } from '@scripts/framework/ui/UILayer';
  4. import { addBtnClick } from '@scripts/utils/UIHelper';
  5. import { AppKernel } from '@scripts/app/AppKernel';
  6. import { SelectBox } from '@scripts/components/selectBox/SelectBox';
  7. import { UIPopup } from '@scripts/framework/ui/UIPopup';
  8. const { ccclass, property } = _decorator;
  9. @ccclass('ChangeName')
  10. export class ChangeName extends UIPopup {
  11. readonly viewId = UIViewID.ChangeName;
  12. @property(Node)
  13. private btn_close: Node = null;
  14. @property(Node)
  15. private btn_confirm: Node = null;
  16. @property(EditBox)
  17. private nickname: EditBox = null;
  18. private _userInfo: any = null;
  19. protected onCreate(): void | Promise<void> {
  20. addBtnClick(this.btn_close, this.onBtnCloseClick, this);
  21. addBtnClick(this.btn_confirm, this.onBtnConfirmClick, this);
  22. this._userInfo = AppKernel.stores.mainStore.userInfo;
  23. const { NickName } = this._userInfo;
  24. this.nickname.placeholder = NickName;
  25. }
  26. private onBtnCloseClick() {
  27. AppKernel.ui.close(UIViewID.ChangeName);
  28. }
  29. private async onBtnConfirmClick() {
  30. const newName = this.nickname.string;
  31. if (this._userInfo.NickName === newName || !newName) {
  32. AppKernel.ui.toast("Cannot use the same nickname");
  33. } else if (!this.validateNickname(newName)) {
  34. AppKernel.ui.toast("Invalid format")
  35. } else {
  36. const res = await AppKernel.stores.mainStore.reqChangeUserInfo({ NickName: newName });
  37. if (res) {
  38. await AppKernel.ui.close(UIViewID.ChangeName);
  39. AppKernel.ui.toast("Change success")
  40. }
  41. }
  42. }
  43. private validateNickname(name: string): boolean {
  44. const str = name.trim();
  45. const reg = /^[a-zA-Z][a-zA-Z0-9_]{2,19}$/;
  46. return reg.test(str);
  47. }
  48. }