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.

182 lines
5.3 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. 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 useNotify from '~/hooks/useNotify';
  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 [visible, setVisible] = useState(false)
  18. const messageWs = useNotify('getMessage', visible)
  19. const { token, walletAddress, userInfo, visibleUnLogin } = store.state
  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. token={token}
  92. messages={messageWs.messages}
  93. readAllMsg={messageWs.readAllMsg}
  94. />
  95. <div className='pages'>
  96. <RenderRouter />
  97. {
  98. tabbarData.includes(location.pathname) && location.pathname !== '/personal' && <div className='tabbar-block'></div>
  99. }
  100. </div>
  101. {
  102. tabbarData.includes(location.pathname) && (
  103. <Tabbar
  104. tabbarData={tabbarData}
  105. push={push}
  106. pathname={location.pathname}
  107. />
  108. )
  109. }
  110. <Notify
  111. visible={visible}
  112. setVisible={setVisible}
  113. messages={messageWs.messages}
  114. deleteAllMsg={messageWs.deleteAllMsg}
  115. />
  116. <div className="ui">
  117. {/* 無效鏈接 */}
  118. {visibleVaildLink && <VaildLink visible={visibleVaildLink} setVisible={setVisibleVaildLink} />}
  119. {/* 已經綁定 */}
  120. {visibleAlreadyBind && (
  121. <AlreadyBind
  122. onClick={() => {
  123. push('/', null, true)
  124. setVisibleAlreadyBind(false)
  125. push('/team')
  126. }}
  127. visible={visibleAlreadyBind}
  128. setVisible={setVisibleAlreadyBind}
  129. />
  130. )}
  131. {/* 綁定推薦人 */}
  132. {visibleBindRmd && (
  133. <BindRmd
  134. onClick={bindRecommend}
  135. address={shareAddress}
  136. visible={visibleBindRmd}
  137. setVisible={setVisibleBindRmd}
  138. />
  139. )}
  140. {
  141. visibleUnLogin && (
  142. <UnLogin
  143. visible={visibleUnLogin}
  144. setVisible={() => store.setVisibleUnLogin(false)}
  145. />
  146. )
  147. }
  148. {visibleDefault && (
  149. <DefaultBind
  150. visible={visibleDefault}
  151. setVisible={() => setVisibleDefault(false)}
  152. onClick={bindRecommend}
  153. />
  154. )}
  155. {visibleBindSuccess && (
  156. <BindSuccess
  157. visible={visibleBindSuccess}
  158. setVisible={() => setVisibleBindSuccess(false)}
  159. address={shareAddress}
  160. />
  161. )}
  162. </div>
  163. </div>
  164. );
  165. }
  166. export default observer(LayoutRouter)