LoginM.ts 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import { Loading } from "../Loading";
  2. import PogHttp, { PogLoginResp } from "../net/PogHttp";
  3. import { WalletListener, WalletManager } from "../web3/WalletManager";
  4. import TgM from "./TgM";
  5. export default class LoginM {
  6. private static _ins: LoginM;
  7. public static get ins(): LoginM {
  8. return LoginM._ins || new LoginM();
  9. }
  10. async login(): Promise<PogLoginResp | null> {
  11. if (await TgM.ins.isTG()) {
  12. await Loading.ins.U("Telegram Login...", 0.3);
  13. return await TgM.ins.login();
  14. }
  15. const walletLoginResult: PogLoginResp | null = await new Promise(
  16. (resolve) => {
  17. this.checkWalletConnect((res) => {
  18. resolve(res);
  19. });
  20. }
  21. );
  22. return walletLoginResult;
  23. }
  24. async checkWalletConnect(cb: (res: PogLoginResp) => void) {
  25. let self = this;
  26. await Loading.ins.U("Connecting to wallet...", 0.3);
  27. let walletListener: WalletListener = {
  28. onLoginSuccess: async (account: string, balance: number) => {
  29. await Loading.ins.U("Wallet connected...", 0.4);
  30. await new Promise((resolve) => setTimeout(resolve, 30));
  31. await Loading.ins.U("Logging in...", 0.5);
  32. PogHttp.ins
  33. .loginByWalletAddress(account)
  34. .then(async (res: PogLoginResp) => {
  35. cb(res);
  36. });
  37. },
  38. OnWalletDisconnected: () => {
  39. //
  40. },
  41. };
  42. WalletManager.ins.init(walletListener);
  43. }
  44. startWalletLogin() {
  45. WalletManager.ins.open();
  46. }
  47. }