LoginService.ts 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. if (res.accessToken) {
  19. return await this.login3rd('facebook', res.accessToken);
  20. }
  21. if (res.authenticationToken) {
  22. // The current backend accepts Access Tokens only. Do not send an OIDC JWT
  23. // through that endpoint or report a game login success without a session.
  24. AppKernel.ui.toast('Facebook 授权已完成,但服务器尚未支持此 iOS 登录方式,请暂时使用其他登录方式。');
  25. return false;
  26. }
  27. AppKernel.ui.toast(res.reason || 'Facebook 未返回可用的 Access Token,请使用其他登录方式。');
  28. return false;
  29. }
  30. async googleLogin(res?: IGoogleLoginRes) {
  31. if (!res) {
  32. /// h5 通过dom按钮触发登录
  33. res = await AppKernel.google.login();
  34. }
  35. if (res && res.accessToken) {
  36. await this.login3rd('google', res.accessToken);
  37. }
  38. return true;
  39. }
  40. async login3rd(type: Login3rdType, token: string) {
  41. const mainStore = AppKernel.stores.mainStore;
  42. const res = await mainStore.reqLogin3rd(type, token);
  43. if (checkResponse(res)) {
  44. await AppKernel.appLobby.onLoginSuccess();
  45. return true;
  46. }
  47. return false;
  48. }
  49. async loginAccount(account: string, password: string) {
  50. const mainStore = AppKernel.stores.mainStore;
  51. const res = await mainStore.reqLogin({
  52. type: APP_LOGIN_TYPE.ID,
  53. password,
  54. account,
  55. })
  56. if (checkResponse(res)) {
  57. // await AppKernel.appLobby.onLoginSuccess();
  58. return true;
  59. }
  60. return false;
  61. }
  62. async logOut() {
  63. }
  64. async register({ account, code, password, act }) {
  65. const mainStore = AppKernel.stores.mainStore;
  66. const res = await mainStore.reqRegister({ account, code, password, act })
  67. return checkResponse(res);
  68. }
  69. }
  70. export const loginService = new LoginService();