PageWallet.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. import { _decorator, Component, Node } from "cc";
  2. import BaseUI from "../../scripts/base/BaseUI";
  3. import { EM } from "../../scripts/event/EM";
  4. import { E } from "../../scripts/event/E";
  5. import { WalletManager } from "../../scripts/web3/WalletManager";
  6. import { CommonTabsTitle, OnTabClickListener } from "../common/CommonTabsTitle";
  7. import { WalletPageMain } from "../wallet/WalletPageMain";
  8. import { PlatformManager } from "../../scripts/mgr/PlatformManager";
  9. import TgM from "../../scripts/mgr/TgM";
  10. import UserM from "../../scripts/api/UserM";
  11. import { Event } from "cc";
  12. import { ScrollView } from "cc";
  13. import { Widget } from "cc";
  14. import { Layout } from "cc";
  15. const { ccclass, property } = _decorator;
  16. @ccclass("PageWallet")
  17. export class PageWallet extends BaseUI implements OnTabClickListener {
  18. private static readonly TAB_MAIN = 0;
  19. private static readonly TAB_POG = 1;
  20. private static readonly TAB_TOG = 2;
  21. private static readonly TAB_ESTOG = 3;
  22. private tabs: CommonTabsTitle;
  23. private pageMain: Node;
  24. private pagePog: Node;
  25. private pageTog: Node;
  26. private pageEstog: Node;
  27. onTabClick(index: number) {
  28. this.toPage(index);
  29. }
  30. private scrollView: ScrollView;
  31. private pages: Node;
  32. protected onLoad(): void {
  33. super.onLoad();
  34. this.pageMain = this.FindNode("page_main");
  35. this.pagePog = this.FindNode("page_pog");
  36. this.pageTog = this.FindNode("page_tog");
  37. this.pageEstog = this.FindNode("page_estog");
  38. this.pageMain.active = false;
  39. this.pagePog.active = false;
  40. this.pageTog.active = false;
  41. this.pageEstog.active = false;
  42. this.tabs = this.getComponentInChildren(CommonTabsTitle);
  43. this.tabs.init(["WALLET", "POG", "TOG"], this);
  44. // this.tabs.init(["WALLET", "POG", "TOG", "esTOG"], this);
  45. this.scrollView = this.FindAs("ScrollView", ScrollView);
  46. this.pages = this.FindNode("pages");
  47. this.pages.parent = this.scrollView.content;
  48. this.pages.setPosition(0, 0, 0);
  49. this.FindNode("btn_to_wallet_connect").active = false;
  50. this.FindNode("connected_content").active = false;
  51. let self = this;
  52. self.initWallet();
  53. }
  54. async initWallet() {
  55. let self = this;
  56. // let isTg = await TgM.ins.isTG();
  57. // if (!isTg) {
  58. // return;
  59. // }
  60. WalletManager.ins.init({
  61. onLoginSuccess: (address: string, balance: number) => {
  62. self.bindWallet(address).then((result) => {
  63. self.init(result);
  64. });
  65. },
  66. OnWalletDisconnected: () => {
  67. self.init(false);
  68. },
  69. });
  70. EM.ins.on(E.Wallet.DISCONNECTED, this.onWalletDisconnected, this);
  71. }
  72. async bindWallet(address: string): Promise<boolean> {
  73. let isTg = TgM.ins.isTG();
  74. if (!isTg) {
  75. // 非tg平台,直接返回true
  76. return true;
  77. }
  78. if (UserM.ins.isBindWallet()) {
  79. return true;
  80. }
  81. console.log("bindWallet", address, "isTg", isTg);
  82. let result = await UserM.ins.bindWallet(address);
  83. if (result) {
  84. // 绑定成功,返回true
  85. return true;
  86. }
  87. return false;
  88. }
  89. protected onDestroy(): void {
  90. EM.ins.off(E.Wallet.DISCONNECTED, this.onWalletDisconnected, this);
  91. }
  92. private onWalletDisconnected() {
  93. console.error("onWalletDisconnected");
  94. this.init(false);
  95. }
  96. async init(isConnected: boolean) {
  97. this.FindNode("connected_content").active = isConnected;
  98. this.FindNode("btn_to_wallet_connect").active = !isConnected;
  99. if (isConnected) {
  100. this.toPage(PageWallet.TAB_MAIN);
  101. this.pageMain.getComponent(WalletPageMain).init(isConnected);
  102. }
  103. }
  104. toPage(index: number) {
  105. let page = null;
  106. switch (index) {
  107. case PageWallet.TAB_MAIN:
  108. page = this.pageMain;
  109. break;
  110. case PageWallet.TAB_POG:
  111. page = this.pagePog;
  112. break;
  113. case PageWallet.TAB_TOG:
  114. page = this.pageTog;
  115. break;
  116. case PageWallet.TAB_ESTOG:
  117. page = this.pageEstog;
  118. break;
  119. }
  120. this.FindNode("pages").children.forEach((child) => {
  121. if (child.active) {
  122. child.getComponent(BaseUI).onHide();
  123. child.active = false;
  124. }
  125. });
  126. if (page) {
  127. page.active = true;
  128. page.getComponent(BaseUI).onShow();
  129. }
  130. this.tabs.setSelect(index);
  131. }
  132. public onShow(): void {
  133. super.onShow();
  134. WalletManager.ins.isConnected().then((isConnected) => {
  135. this.init(isConnected);
  136. });
  137. }
  138. public onHide(): void {
  139. super.onHide();
  140. }
  141. protected simpleOnBtnClick(name: string): void {
  142. switch (name) {
  143. case "btn_to_wallet_connect":
  144. WalletManager.ins.open();
  145. break;
  146. }
  147. }
  148. }