LoginView.ts 7.5 KB

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