LoginView.ts 7.8 KB

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