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.

174 lines
5.1 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. import '~/styles/layout.scss'
  2. import { useEffect, useState } from 'react';
  3. import { useRouter } from '~/hooks/useRouter';
  4. import Notify from './Notify';
  5. import RenderRouter from './RenderRouter';
  6. import routes, { tabbarData } from '../routes';
  7. import Navbar from './Navbar';
  8. import Tabbar from './Tabbar';
  9. import { observer } from 'mobx-react';
  10. import store from '~/store';
  11. import useWs from '~/hooks/useWs';
  12. import { ethers } from 'ethers';
  13. import { bind_rmd } from '~/api';
  14. import { AlreadyBind, BindRmd, BindSuccess, DefaultBind, UnLogin, VaildLink } from './ui';
  15. const LayoutRouter = () => {
  16. const { location, push } = useRouter()
  17. const messageWs = useWs('getMessage')
  18. const { token, walletAddress, userInfo, visibleUnLogin } = store.state
  19. const [visible, setVisible] = useState(false)
  20. const [visibleVaildLink, setVisibleVaildLink] = useState(false) //无效的链接
  21. const [visibleAlreadyBind, setVisibleAlreadyBind] = useState(false) //已绑定
  22. const [visibleBindRmd, setVisibleBindRmd] = useState(false) //直接绑定
  23. const [shareAddress, setShareAddress] = useState('')
  24. const [visibleBindSuccess, setVisibleBindSuccess] = useState(false) //綁定成功
  25. const [visibleDefault, setVisibleDefault] = useState(false) //默認綁定
  26. const bindRecommend = async () => {
  27. const res: any = await bind_rmd(shareAddress)
  28. if (res.code === 0) {
  29. setVisibleBindRmd(false)
  30. store.getUserInfo()
  31. setTimeout(() => {
  32. push('/', null, true)
  33. setVisibleBindSuccess(true)
  34. }, 200)
  35. }
  36. }
  37. useEffect(() => {
  38. if (token) {
  39. store.getUserInfo()
  40. store.getCoinList()
  41. store.getMyNft('myNft')
  42. store.getMyNft('likeNft')
  43. }
  44. if (!token) {
  45. store.resetCoinList()
  46. store.resetUserInfo()
  47. store.resetNft("likeNft")
  48. store.resetNft("myNft")
  49. }
  50. // token && messageWs.connect(token)
  51. // !token && messageWs.disconnect()
  52. }, [token])
  53. useEffect(() => {
  54. const isModal = async () => {
  55. let user = userInfo
  56. let isRouter = routes.find(v => v.path === location.pathname)
  57. const address = location.pathname.substring(1, location.pathname.length)
  58. if (isRouter) return
  59. if (Object.keys(user).length <= 0) {
  60. const res: any = await store.getUserInfo()
  61. if (res) {
  62. user = res
  63. }
  64. }
  65. if (!ethers.utils.isAddress(address)) {
  66. // 无效的分享链接
  67. setVisibleVaildLink(true)
  68. return
  69. }
  70. // 地址相同
  71. if (address.toLocaleLowerCase() === walletAddress.toLocaleLowerCase()) return
  72. setShareAddress(address)
  73. // 绑定推荐人
  74. if (!user.is_bound && ethers.utils.isAddress(address)) {
  75. setVisibleBindRmd(true)
  76. return
  77. }
  78. // 已有推荐人
  79. if (user.is_bound && ethers.utils.isAddress(address)) {
  80. setVisibleAlreadyBind(true)
  81. }
  82. }
  83. token && isModal()
  84. !token && setVisibleBindRmd(false)
  85. }, [token, walletAddress])
  86. return (
  87. <div className='layout'>
  88. <Navbar
  89. pathname={location.pathname}
  90. setVisible={setVisible}
  91. />
  92. <div className='pages'>
  93. <RenderRouter />
  94. {
  95. tabbarData.includes(location.pathname) && location.pathname !== '/personal' && <div className='tabbar-block'></div>
  96. }
  97. </div>
  98. {
  99. tabbarData.includes(location.pathname) && (
  100. <Tabbar
  101. tabbarData={tabbarData}
  102. push={push}
  103. pathname={location.pathname}
  104. />
  105. )
  106. }
  107. <Notify visible={visible} setVisible={setVisible} />
  108. <div className="ui">
  109. {/* 無效鏈接 */}
  110. {visibleVaildLink && <VaildLink visible={visibleVaildLink} setVisible={setVisibleVaildLink} />}
  111. {/* 已經綁定 */}
  112. {visibleAlreadyBind && (
  113. <AlreadyBind
  114. onClick={() => {
  115. push('/', null, true)
  116. setVisibleAlreadyBind(false)
  117. push('/team')
  118. }}
  119. visible={visibleAlreadyBind}
  120. setVisible={setVisibleAlreadyBind}
  121. />
  122. )}
  123. {/* 綁定推薦人 */}
  124. {visibleBindRmd && (
  125. <BindRmd
  126. onClick={bindRecommend}
  127. address={shareAddress}
  128. visible={visibleBindRmd}
  129. setVisible={setVisibleBindRmd}
  130. />
  131. )}
  132. {
  133. visibleUnLogin && (
  134. <UnLogin
  135. visible={visibleUnLogin}
  136. setVisible={() => store.setVisibleUnLogin(false)}
  137. />
  138. )
  139. }
  140. {visibleDefault && (
  141. <DefaultBind
  142. visible={visibleDefault}
  143. setVisible={() => setVisibleDefault(false)}
  144. onClick={bindRecommend}
  145. />
  146. )}
  147. {visibleBindSuccess && (
  148. <BindSuccess
  149. visible={visibleBindSuccess}
  150. setVisible={() => setVisibleBindSuccess(false)}
  151. address={shareAddress}
  152. />
  153. )}
  154. </div>
  155. </div>
  156. );
  157. }
  158. export default observer(LayoutRouter)