LoginView.ts 7.0 KB

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