LoginM.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. import ConfigM, { Config } from "../api/ConfigM";
  2. import { Loading } from "../Loading";
  3. import PogHttp, { PogLoginResp, PogSignMessageResp } from "../net/PogHttp";
  4. import Utils from "../utils/Utils";
  5. import { WalletListener, WalletManager } from "../web3/WalletManager";
  6. import TgM from "./TgM";
  7. import { Tips } from "./Tips";
  8. export default class LoginM {
  9. private static _ins: LoginM;
  10. public static get ins(): LoginM {
  11. return (LoginM._ins ??= new LoginM());
  12. }
  13. retryLogin() {
  14. WalletManager.ins.open();
  15. }
  16. async login(): Promise<PogLoginResp | null> {
  17. let abcd = Utils.getQueryString("abcd");
  18. if (abcd != null && abcd.length > 0) {
  19. return await PogHttp.ins.loginByWalletAddress(abcd);
  20. }
  21. if (await TgM.ins.isTG()) {
  22. await Loading.ins.U("Telegram Login...", 0.3);
  23. return await TgM.ins.login();
  24. }
  25. setTimeout(async () => {
  26. let isConnected = await WalletManager.ins.isConnected();
  27. console.log("isConnected", isConnected);
  28. if (isConnected) {
  29. return;
  30. }
  31. Loading.ins.showLoginByWalletButton();
  32. WalletManager.ins.open();
  33. }, 10000);
  34. const walletLoginResult: PogLoginResp | null = await new Promise(
  35. (resolve) => {
  36. this.checkWalletConnect((res) => {
  37. resolve(res);
  38. });
  39. }
  40. );
  41. return walletLoginResult;
  42. }
  43. async beginSignMessage(address: string) {
  44. let signResult = await PogHttp.ins.getSignMessage(address);
  45. if (
  46. !signResult ||
  47. !signResult.fullMessage ||
  48. signResult.fullMessage.length <= 0
  49. ) {
  50. console.error("sign message error", "signResult is null");
  51. return;
  52. }
  53. let signature = await WalletManager.ins.signMessage(signResult.fullMessage);
  54. return signature;
  55. }
  56. async beginLogin(address: string): Promise<PogLoginResp | null> {
  57. if (!address) {
  58. return null;
  59. }
  60. if (ConfigM.ins.ignoreSignMessage) {
  61. return PogHttp.ins.loginByWalletAddress(address);
  62. // .then(async (res: PogLoginResp) => {
  63. // cb(res);
  64. // });
  65. }
  66. let signature = await LoginM.ins.beginSignMessage(address);
  67. if (signature) {
  68. let loginResult = await PogHttp.ins.loginByWalletAddressAndSignature(
  69. address,
  70. signature
  71. );
  72. return loginResult;
  73. }
  74. return null;
  75. }
  76. async checkWalletConnect(cb: (res: PogLoginResp) => void) {
  77. let self = this;
  78. await Loading.ins.U("Connecting to wallet...", 0.3);
  79. let walletListener: WalletListener = {
  80. onLoginSuccess: async (address: string, balance: number) => {
  81. await Loading.ins.U("Wallet connected...", 0.4);
  82. await new Promise((resolve) => setTimeout(resolve, 30));
  83. await Loading.ins.U("Logging in...", 0.5);
  84. let loginResult: PogLoginResp = await LoginM.ins.beginLogin(address);
  85. cb(loginResult);
  86. // PogHttp.ins
  87. // .loginByWalletAddress(account)
  88. // .then(async (res: PogLoginResp) => {
  89. // cb(res);
  90. // });
  91. },
  92. OnWalletDisconnected: () => {
  93. //
  94. },
  95. };
  96. WalletManager.ins.init(walletListener);
  97. }
  98. startWalletLogin() {
  99. WalletManager.ins.open();
  100. }
  101. }