LoginService.ts 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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 iosAuthLogin() {
  42. if (!AppKernel.iosAuth) return false;
  43. const res = await AppKernel.iosAuth.login();
  44. console.log(`ios auth res: ${JSON.stringify(res)}`)
  45. if (res.status === 'connected') {
  46. }
  47. }
  48. async login3rd(type: Login3rdType, token: string) {
  49. const mainStore = AppKernel.stores.mainStore;
  50. const res = await mainStore.reqLogin3rd(type, token);
  51. if (checkResponse(res)) {
  52. await AppKernel.appLobby.onLoginSuccess();
  53. return true;
  54. }
  55. return false;
  56. }
  57. async loginAccount(account: string, password: string) {
  58. const mainStore = AppKernel.stores.mainStore;
  59. const res = await mainStore.reqLogin({
  60. type: APP_LOGIN_TYPE.ID,
  61. password,
  62. account,
  63. })
  64. if (checkResponse(res)) {
  65. // await AppKernel.appLobby.onLoginSuccess();
  66. return true;
  67. }
  68. return false;
  69. }
  70. async logOut() {
  71. }
  72. async register({ account, code, password, act }) {
  73. const mainStore = AppKernel.stores.mainStore;
  74. const res = await mainStore.reqRegister({ account, code, password, act })
  75. return checkResponse(res);
  76. }
  77. }
  78. export const loginService = new LoginService();