LoginView.ts 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. import { _decorator, Button, Color, EditBox, instantiate, Label, Node, RichText, Sprite, SpriteFrame, UITransform } from 'cc';
  2. import { UIView } from '@scripts/framework/ui/UIView';
  3. import { AppKernel } from '@scripts/app/AppKernel';
  4. import { UIViewID } from '@scripts/define/UIDefine';
  5. import { UICloseReason } from '@scripts/framework/ui/UILayer';
  6. import { formatCurrency } from '@scripts/utils/Helper';
  7. import { addBtnClick } from '@scripts/utils/UIHelper';
  8. const { ccclass, property } = _decorator;
  9. @ccclass('LoginView')
  10. export class LoginView extends UIView {
  11. readonly viewId = UIViewID.LoginView;
  12. @property(Button)
  13. btnGuest: Button;
  14. @property(Button)
  15. btnFacebook: Button;
  16. @property(Button)
  17. btnGoogle: Button;
  18. @property(EditBox)
  19. loginAccount: EditBox;
  20. @property(EditBox)
  21. loginPassword: EditBox;
  22. @property(Node)
  23. psd_visible_control: Node;
  24. @property(Node)
  25. btnLogin: Node;
  26. @property(Node)
  27. btnRegister: Node;
  28. @property(Node)
  29. btnResetPsd: Node;
  30. @property(Node)
  31. btnSupport: Node;
  32. @property(Node)
  33. policyContainer: Node;
  34. private _labelLayout: Node | null = null;
  35. private _labelTemplate: Node | null = null;
  36. private _googleWebBtn;
  37. protected onCreate(): void | Promise<void> {
  38. addBtnClick(this.btnGuest.node, this.onBtnGuestClick, this);
  39. addBtnClick(this.btnFacebook.node, this.onBtnFBClick, this);
  40. addBtnClick(this.btnGoogle.node, this.onBtnGoogleClick, this);
  41. this._googleWebBtn = AppKernel.google.createLoginBtn(
  42. this.btnGoogle.node,
  43. res => this.onGoogleWebLogin(res),
  44. );
  45. addBtnClick(this.btnRegister, this.onBtnRegisterClick, this);
  46. addBtnClick(this.btnLogin, this.onBtnLoginClick, this);
  47. addBtnClick(this.btnResetPsd, this.onBtnResetPsdClick, this);
  48. addBtnClick(this.btnSupport, this.onBtnSupportClick, this);
  49. this.initPsdInput();
  50. this.initPolicyDesc();
  51. }
  52. private initPolicyDesc(): void {
  53. if (!this.policyContainer) return;
  54. this._labelTemplate = this.policyContainer.getChildByName("template") ?? null;
  55. this._labelLayout = this.policyContainer.getChildByName("layout") ?? null;
  56. if (!this._labelTemplate || !this._labelLayout) return;
  57. this._labelLayout.removeAllChildren();
  58. this.createText("I am 18+, in an eligible state, and agree to the ", true);
  59. this.createLink("Terms", () => this.onClickTerm());
  60. this.createText(" & ", true);
  61. this.createLink("Privacy Policy", () => this.onClickPrivacy());
  62. }
  63. private initPsdInput(): void {
  64. addBtnClick(this.psd_visible_control, this.onTogglePsdVisible, this);
  65. const eye_open = this.psd_visible_control.getChildByName("open");
  66. const eye_close = this.psd_visible_control.getChildByName("close");
  67. eye_close.active = true;
  68. eye_open.active = false;
  69. }
  70. // 创建普通文本
  71. private createText(str: string, bold: boolean) {
  72. const node = instantiate(this._labelTemplate!);
  73. node.active = true;
  74. const label = node.getComponent(Label)!;
  75. label.string = str;
  76. label.isBold = bold;
  77. label.color = Color.WHITE;
  78. label.isUnderline = false; // 关闭下划线
  79. node.setParent(this._labelLayout);
  80. return node;
  81. }
  82. // 创建链接文字
  83. private createLink(str: string, clickCb: () => void) {
  84. const node = instantiate(this._labelTemplate!);
  85. node.active = true;
  86. const label = node.getComponent(Label)!;
  87. label.string = str;
  88. label.isBold = true;
  89. label.color = new Color(0xff, 0xd2, 0x00);
  90. label.isUnderline = true;
  91. addBtnClick(node, clickCb, this);
  92. node.setParent(this._labelLayout);
  93. return node;
  94. }
  95. private onClickTerm() {
  96. console.log("打开用户协议");
  97. }
  98. private onClickPrivacy() {
  99. console.log("打开隐私政策");
  100. }
  101. protected onOpen(_params: unknown): void | Promise<void> {
  102. /// 播放完打开动画后 再显示h5 google login btn dom
  103. this._googleWebBtn?.setActive(true);
  104. }
  105. protected onClose(_reason: UICloseReason): void | Promise<void> {
  106. this._googleWebBtn?.setActive(false);
  107. }
  108. protected onDestroy(): void {
  109. this._googleWebBtn?.clear();
  110. super.onDestroy();
  111. }
  112. private async onBtnGuestClick() {
  113. AppKernel.ui.loading(true);
  114. await AppKernel.services.loginService.guestLogin();
  115. AppKernel.ui.loading(false);
  116. }
  117. private async onBtnFBClick() {
  118. AppKernel.ui.loading(true);
  119. await AppKernel.services.loginService.fbLogin();
  120. AppKernel.ui.loading(false);
  121. }
  122. private async onGoogleWebLogin(res) {
  123. AppKernel.ui.loading(true);
  124. await AppKernel.services.loginService.googleLogin(res);
  125. AppKernel.ui.loading(false);
  126. }
  127. private async onBtnGoogleClick() {
  128. AppKernel.ui.loading(true);
  129. await AppKernel.services.loginService.googleLogin();
  130. AppKernel.ui.loading(false);
  131. }
  132. private onBtnRegisterClick() {
  133. this._googleWebBtn?.setActive(false);
  134. AppKernel.ui.open(UIViewID.Register);
  135. }
  136. private onBtnLoginClick() {
  137. const account = this.loginAccount.string;
  138. const password = this.loginPassword.string;
  139. AppKernel.services.loginService.loginAccount(account, password);
  140. }
  141. private onBtnResetPsdClick() {
  142. this._googleWebBtn?.setActive(false);
  143. AppKernel.ui.open(UIViewID.ResetPsd);
  144. }
  145. private async onBtnSupportClick() {
  146. this._googleWebBtn?.setActive(false);
  147. AppKernel.ui.open(UIViewID.Support);
  148. }
  149. private onTogglePsdVisible() {
  150. const eye_open = this.psd_visible_control.getChildByName("open");
  151. const eye_close = this.psd_visible_control.getChildByName("close");
  152. eye_close.active = !eye_close.active;
  153. eye_open.active = !eye_open.active;
  154. if (eye_open.active) {
  155. this.loginPassword.inputFlag = EditBox.InputFlag.DEFAULT;
  156. } else {
  157. this.loginPassword.inputFlag = EditBox.InputFlag.PASSWORD;
  158. }
  159. }
  160. }