LoginService.ts 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import { Login3rdType, APP_LOGIN_TYPE } from "@scripts/define/Define";
  2. import { AppKernel } from "../../app/AppKernel";
  3. import { IGoogleLoginRes } from "@scripts/platform/Google/GoogleImpl";
  4. import { checkResponse, getHttpMsg } from "@scripts/api/ApiHelper";
  5. class LoginService {
  6. async guestLogin() {
  7. const mainStore = AppKernel.stores.mainStore;
  8. const res = await mainStore.reqLogin({ type: APP_LOGIN_TYPE.GUEST });
  9. if (checkResponse(res)) {
  10. await AppKernel.appLobby.onLoginSuccess();
  11. return true;
  12. }
  13. return false;
  14. }
  15. async fbLogin() {
  16. const res = await AppKernel.fb.login();
  17. if (res.isCancel) return false;
  18. const token = res.accessToken || res.authenticationToken;
  19. if (token) {
  20. return await this.login3rd('facebook', token);
  21. }
  22. // if (res.authenticationToken) {
  23. // // The current backend accepts Access Tokens only. Do not send an OIDC JWT
  24. // // through that endpoint or report a game login success without a session.
  25. // AppKernel.ui.toast('Facebook 授权已完成,但服务器尚未支持此 iOS 登录方式,请暂时使用其他登录方式。');
  26. // return false;
  27. // }
  28. AppKernel.ui.toast(res.reason || 'Facebook 未返回可用的 Access Token,请使用其他登录方式。');
  29. return false;
  30. }
  31. async googleLogin(res?: IGoogleLoginRes) {
  32. if (!res) {
  33. /// h5 通过dom按钮触发登录
  34. res = await AppKernel.google.login();
  35. }
  36. if (res && res.accessToken) {
  37. await this.login3rd('google', res.accessToken);
  38. }
  39. return true;
  40. }
  41. async login3rd(type: Login3rdType, token: string) {
  42. const mainStore = AppKernel.stores.mainStore;
  43. const res = await mainStore.reqLogin3rd(type, token);
  44. if (checkResponse(res)) {
  45. await AppKernel.appLobby.onLoginSuccess();
  46. return true;
  47. }
  48. return false;
  49. }
  50. async loginAccount(account: string, password: string) {
  51. const mainStore = AppKernel.stores.mainStore;
  52. const res = await mainStore.reqLogin({
  53. type: APP_LOGIN_TYPE.ID,
  54. password,
  55. account,
  56. })
  57. if (checkResponse(res)) {
  58. // await AppKernel.appLobby.onLoginSuccess();
  59. return true;
  60. }
  61. return false;
  62. }
  63. async logOut() {
  64. }
  65. async register({ account, code, password, act }) {
  66. const mainStore = AppKernel.stores.mainStore;
  67. const res = await mainStore.reqRegister({ account, code, password, act })
  68. return checkResponse(res);
  69. }
  70. }
  71. export const loginService = new LoginService();