LoginService.ts 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. AppKernel.ui.toast(res.reason || 'Facebook 未返回可用的 Access Token,请使用其他登录方式。');
  22. return false;
  23. }
  24. async googleLogin(res?: IGoogleLoginRes) {
  25. if (!res) {
  26. /// h5 通过dom按钮触发登录
  27. res = await AppKernel.google.login();
  28. }
  29. if (res && res.accessToken) {
  30. await this.login3rd('google', res.accessToken);
  31. }
  32. return true;
  33. }
  34. async login3rd(type: Login3rdType, token: string) {
  35. const mainStore = AppKernel.stores.mainStore;
  36. const res = await mainStore.reqLogin3rd(type, token);
  37. if (checkResponse(res)) {
  38. await AppKernel.appLobby.onLoginSuccess();
  39. return true;
  40. }
  41. return false;
  42. }
  43. async loginAccount(account: string, password: string) {
  44. const mainStore = AppKernel.stores.mainStore;
  45. const res = await mainStore.reqLogin({
  46. type: APP_LOGIN_TYPE.ID,
  47. password,
  48. account,
  49. })
  50. if (checkResponse(res)) {
  51. // await AppKernel.appLobby.onLoginSuccess();
  52. return true;
  53. }
  54. return false;
  55. }
  56. async logOut() {
  57. }
  58. async register({ account, code, password, act }) {
  59. const mainStore = AppKernel.stores.mainStore;
  60. const res = await mainStore.reqRegister({ account, code, password, act })
  61. return checkResponse(res);
  62. }
  63. }
  64. export const loginService = new LoginService();