LoginView.ts 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. policy_container: Node;
  34. private _labelLayout: Node | null = null;
  35. private _labelTemplate: Node | null = null;
  36. private _policyAgree = true;
  37. private _googleWebBtn;
  38. protected onCreate(): void | Promise<void> {
  39. addBtnClick(this.btnGuest.node, this.onBtnGuestClick, this);
  40. addBtnClick(this.btnFacebook.node, this.onBtnFBClick, this);
  41. addBtnClick(this.btnGoogle.node, this.onBtnGoogleClick, this);
  42. this._googleWebBtn = AppKernel.google.createLoginBtn(
  43. this.btnGoogle.node,
  44. res => this.onGoogleWebLogin(res),
  45. );
  46. addBtnClick(this.btnRegister, this.onBtnRegisterClick, this);
  47. addBtnClick(this.btnLogin, this.onBtnLoginClick, this);
  48. addBtnClick(this.btnResetPsd, this.onBtnResetPsdClick, this);
  49. addBtnClick(this.btnSupport, this.onBtnSupportClick, this);
  50. this.initPsdInput();
  51. this.initPolicyDesc();
  52. }
  53. private initPolicyDesc(): void {
  54. if (!this.policy_container) return;
  55. this._labelTemplate = this.policy_container.getChildByName("template") ?? null;
  56. this._labelLayout = this.policy_container.getChildByName("layout") ?? null;
  57. if (!this._labelTemplate || !this._labelLayout) return;
  58. this._labelLayout.removeAllChildren();
  59. const check_bg = this.policy_container.getChildByName("check_bg") ?? null;
  60. addBtnClick(check_bg, this.onTogglePolicyAgree, this);
  61. this.createText("I am 18+, in an eligible state, and agree to the ", true);
  62. this.createLink("Terms", () => this.onClickTerm());
  63. this.createText(" & ", true);
  64. this.createLink("Privacy Policy", () => this.onClickPrivacy());
  65. }
  66. private initPsdInput(): void {
  67. addBtnClick(this.psd_visible_control, this.onTogglePsdVisible, this);
  68. const eye_open = this.psd_visible_control.getChildByName("open");
  69. const eye_close = this.psd_visible_control.getChildByName("close");
  70. eye_close.active = true;
  71. eye_open.active = false;
  72. }
  73. // 创建普通文本
  74. private createText(str: string, bold: boolean) {
  75. const node = instantiate(this._labelTemplate!);
  76. node.active = true;
  77. const label = node.getComponent(Label)!;
  78. label.string = str;
  79. label.isBold = bold;
  80. label.color = Color.WHITE;
  81. label.isUnderline = false; // 关闭下划线
  82. node.setParent(this._labelLayout);
  83. return node;
  84. }
  85. // 创建链接文字
  86. private createLink(str: string, clickCb: () => void) {
  87. const node = instantiate(this._labelTemplate!);
  88. node.active = true;
  89. const label = node.getComponent(Label)!;
  90. label.string = str;
  91. label.isBold = true;
  92. label.color = new Color(0xff, 0xd2, 0x00);
  93. label.isUnderline = true;
  94. addBtnClick(node, clickCb, this);
  95. node.setParent(this._labelLayout);
  96. return node;
  97. }
  98. private onClickTerm() {
  99. console.log("打开用户协议");
  100. }
  101. private onClickPrivacy() {
  102. console.log("打开隐私政策");
  103. }
  104. protected onOpen(_params: unknown): void | Promise<void> {
  105. /// 播放完打开动画后 再显示h5 google login btn dom
  106. this._googleWebBtn?.setActive(true);
  107. }
  108. protected onClose(_reason: UICloseReason): void | Promise<void> {
  109. this._googleWebBtn?.setActive(false);
  110. }
  111. protected onDestroy(): void {
  112. this._googleWebBtn?.clear();
  113. super.onDestroy();
  114. }
  115. private async onBtnGuestClick() {
  116. AppKernel.ui.loading(true);
  117. await AppKernel.services.loginService.guestLogin();
  118. AppKernel.ui.loading(false);
  119. }
  120. private async onBtnFBClick() {
  121. AppKernel.ui.loading(true);
  122. await AppKernel.services.loginService.fbLogin();
  123. AppKernel.ui.loading(false);
  124. }
  125. private async onGoogleWebLogin(res) {
  126. AppKernel.ui.loading(true);
  127. await AppKernel.services.loginService.googleLogin(res);
  128. AppKernel.ui.loading(false);
  129. }
  130. private async onBtnGoogleClick() {
  131. AppKernel.ui.loading(true);
  132. await AppKernel.services.loginService.googleLogin();
  133. AppKernel.ui.loading(false);
  134. }
  135. private onBtnRegisterClick() {
  136. this._googleWebBtn?.setActive(false);
  137. AppKernel.ui.open(UIViewID.Register);
  138. }
  139. private async onBtnLoginClick() {
  140. const account = this.loginAccount.string;
  141. const password = this.loginPassword.string;
  142. const res = await AppKernel.services.loginService.loginAccount(account, password);
  143. if (res) await AppKernel.appLobby.onLoginSuccess();
  144. }
  145. private onBtnResetPsdClick() {
  146. this._googleWebBtn?.setActive(false);
  147. AppKernel.ui.open(UIViewID.ResetPsd);
  148. }
  149. private async onBtnSupportClick() {
  150. this._googleWebBtn?.setActive(false);
  151. AppKernel.ui.open(UIViewID.Support);
  152. }
  153. private onTogglePsdVisible() {
  154. const eye_open = this.psd_visible_control.getChildByName("open");
  155. const eye_close = this.psd_visible_control.getChildByName("close");
  156. eye_close.active = !eye_close.active;
  157. eye_open.active = !eye_open.active;
  158. if (eye_open.active) {
  159. this.loginPassword.inputFlag = EditBox.InputFlag.DEFAULT;
  160. } else {
  161. this.loginPassword.inputFlag = EditBox.InputFlag.PASSWORD;
  162. }
  163. }
  164. private onTogglePolicyAgree() {
  165. const check_bg = this.policy_container.getChildByName("check_bg") ?? null;
  166. const check = check_bg.getChildByName("check") ?? null;
  167. check.active = !check.active;
  168. this._policyAgree = check.active;
  169. }
  170. }