You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

131 lines
3.2 KiB

1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
  1. import { makeAutoObservable } from "mobx";
  2. import { accountInfo, coin_list, market_list, personal_nft } from "~/api";
  3. import { StoreLocalStorageKey } from "~/types";
  4. import { CoinList, MarketNFTData, UserInfo } from "~/types/store";
  5. interface Store {
  6. state: object;
  7. }
  8. class AppStore implements Store {
  9. state = {
  10. token: "",
  11. walletAddress: "",
  12. coinList: [] as CoinList[],
  13. userInfo: {} as UserInfo,
  14. visibleUnLogin: false,
  15. likeNft: [] as MarketNFTData[], //喜歡的NFT
  16. myNft: [] as MarketNFTData[], //我的NFT
  17. auctionNft: [] as MarketNFTData[], //拍賣NFT
  18. sellNft: [] as MarketNFTData[], //售賣NFT
  19. issueNft: [] as MarketNFTData[], //發行搶購NFT
  20. rmdNft: [] as MarketNFTData[], //推薦NFT
  21. };
  22. constructor() {
  23. makeAutoObservable(this);
  24. this.initState();
  25. }
  26. /**
  27. * @description
  28. */
  29. initState() {
  30. let addr = window.localStorage.getItem(StoreLocalStorageKey.ADDRESS) || "";
  31. let token = window.localStorage.getItem(StoreLocalStorageKey.TOKEN) || "";
  32. this.state.walletAddress = addr;
  33. this.state.token = token;
  34. }
  35. /**
  36. * @description token
  37. */
  38. setToken(token: string): void {
  39. this.state.token = token;
  40. window.localStorage.setItem(StoreLocalStorageKey.TOKEN, token);
  41. }
  42. /**
  43. * @description token
  44. */
  45. removeToken(): void {
  46. this.state.token = "";
  47. window.localStorage.removeItem(StoreLocalStorageKey.TOKEN);
  48. }
  49. async getUserInfo(): Promise<UserInfo> {
  50. const res: any = await accountInfo();
  51. if (res && res.code === 0) {
  52. this.state.userInfo = res.data;
  53. }
  54. return res && res.data;
  55. }
  56. resetUserInfo() {
  57. this.state.userInfo = {} as UserInfo;
  58. }
  59. /**
  60. * @description
  61. */
  62. setAddress(addr: string): void {
  63. this.state.walletAddress = addr;
  64. window.localStorage.setItem(StoreLocalStorageKey.ADDRESS, addr);
  65. }
  66. /**
  67. * @description
  68. */
  69. removeAddr(): void {
  70. this.state.walletAddress = "";
  71. window.localStorage.removeItem(StoreLocalStorageKey.ADDRESS);
  72. }
  73. async getCoinList(): Promise<void> {
  74. const res: any = await coin_list();
  75. if (res && res.code === 0 && res.data) {
  76. this.state.coinList = res.data;
  77. }
  78. }
  79. resetCoinList(): void {
  80. this.state.coinList = [];
  81. }
  82. setVisibleUnLogin(bool: boolean): void {
  83. this.state.visibleUnLogin = bool;
  84. }
  85. /**
  86. * @param key
  87. * @param type 1. 2.
  88. */
  89. async getMyNft(key: "myNft" | "likeNft") {
  90. let type = key === "myNft" ? 1 : 2;
  91. const res: any = await personal_nft(type);
  92. if (res && res.code === 0) {
  93. this.state[key] = res.data;
  94. }
  95. }
  96. resetNft(key: "myNft" | "likeNft") {
  97. this.state[key] = [];
  98. }
  99. async getMarketNft(key: "auctionNft" | "sellNft" | "rmdNft" | "issueNft") {
  100. const types = {
  101. auctionNft: 1,
  102. sellNft: 2,
  103. issueNft: 3,
  104. rmdNft: 4,
  105. };
  106. const res: any = await market_list(types[key]);
  107. if (res && res.code === 0 && res.data) {
  108. this.state[key] = res.data;
  109. }
  110. }
  111. }
  112. const store = new AppStore();
  113. export default store;