register.vue 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. <template>
  2. <view class="content" >
  3. <view class="wrap flex flsb" :style="{paddingTop:statusBarHeight+'rpx'}">
  4. <w3m-network-button size='md' balance='show' />
  5. <w3m-button size='sm' balance='hide' />
  6. </view>
  7. <view class="wrap">
  8. <image src="../../static/logos.png" class="logImg"></image>
  9. <view class="radius">
  10. <view class="title">邀請碼</view>
  11. <input type="text" v-model="inviteId" placeholder="請輸入上級邀請碼" placeholder-style="color:#fff" class="invitInput">
  12. </view>
  13. <view class="tjBtn buttonbj" @click="openbtns()">立即注册</view>
  14. </view>
  15. </view>
  16. </template>
  17. <script>
  18. import {
  19. createWeb3Modal,
  20. defaultWagmiConfig,
  21. useWeb3ModalState,
  22. useWeb3Modal
  23. } from '@web3modal/wagmi/vue'
  24. import {
  25. mainnet,
  26. arbitrum,
  27. optimism,
  28. bsc,
  29. polygon
  30. } from '@wagmi/core/chains'
  31. import {
  32. getAccount, //地址
  33. writeContract,
  34. disconnect, //断开
  35. watchAccount,
  36. createConfig, //创建客户端
  37. configureChains, //配置链
  38. getNetwork, //获取链
  39. getWalletClient,
  40. fetchToken,
  41. connect, //链接
  42. watchContractEvent,
  43. watchNetwork,
  44. sendTransaction,
  45. signMessage,
  46. prepareSendTransaction
  47. } from '@wagmi/core'
  48. import {
  49. publicProvider
  50. } from '@wagmi/core/providers/public'
  51. import {
  52. InjectedConnector
  53. } from '@wagmi/core/connectors/injected'
  54. import {
  55. parseEther
  56. } from 'viem'
  57. // 1. Get projectId YOUR_PROJECT_ID
  58. const projectId = 'c46fe115e62fd8cc283e8db10b3a7fa7'
  59. // 2. Create wagmiConfig
  60. const metadata = {
  61. name: 'Web3Modal',
  62. description: 'Web3Modal Example',
  63. url: 'https://web3modal.com',
  64. icons: ['https://avatars.githubusercontent.com/u/37784886']
  65. }
  66. const chains = [bsc]
  67. const wagmiConfig = defaultWagmiConfig({
  68. chains,
  69. projectId,
  70. metadata
  71. })
  72. // 3. Create modal
  73. const modal = createWeb3Modal({
  74. wagmiConfig,
  75. projectId,
  76. chains,
  77. optionalChains: [{
  78. chainId: 128,
  79. chainName: "HECO",
  80. nativeCurrency: {
  81. name: "HECO",
  82. symbol: "HT",
  83. decimals: 18,
  84. },
  85. rpcUrls: ["https://http-mainnet.hecochain.com/ "]
  86. },
  87. {
  88. chainId: 137,
  89. chainName: "MATIC",
  90. nativeCurrency: {
  91. name: "MATIC",
  92. symbol: "MATIC",
  93. decimals: 18,
  94. },
  95. rpcUrls: ["https://matic.mytokenpocket.vip/ "],
  96. }
  97. ],
  98. defaultChain: bsc,
  99. tokens: {
  100. 1: {
  101. address: '0xdac17f958d2ee523a2206206994597c13d831ec7',
  102. image: 'token_image_url' //optional ETH
  103. },
  104. 56: {
  105. address: '0x55d398326f99059ff775485246999027b3197955',
  106. image: 'token_image_url' //optional BSC
  107. },
  108. 137: {
  109. address: '0xc2132d05d31c914a87c6611c10748aeb04b58e8f',
  110. image: 'token_image_url' //optional MATIC
  111. },
  112. 128: {
  113. address: '0xa71edc38d189767582c38a3145b5873052c3e47a',
  114. image: 'token_image_url' //optional HT
  115. }
  116. },
  117. //主题覆盖
  118. themeVariables: {
  119. '--w3m-accent': '#02cc66',
  120. },
  121. //主题亮,暗
  122. themeMode: 'light'
  123. })
  124. export default {
  125. data() {
  126. return{
  127. statusBarHeight:'',
  128. inviteId:'',
  129. cType:'',
  130. walletAddress:''
  131. }
  132. },
  133. onLoad(option){
  134. if(option){this.inviteId=option.inviteId}
  135. const state = useWeb3ModalState()
  136. //账户信息
  137. const account = getAccount()
  138. //账户信息chain
  139. const { chain} = getNetwork()
  140. const result = new InjectedConnector()
  141. const unwatch = watchNetwork((network) => {
  142. this.getChindnew(network)
  143. })
  144. const unwatchAcc = watchAccount((account) => {
  145. this.watchAccounts(account)
  146. })
  147. if (account) {
  148. this.walletAddress = account.address
  149. uni.setStorageSync('walletAddress', account.address)
  150. }
  151. //获取状态栏+导航栏的高度
  152. let _that = this;
  153. uni.getSystemInfo({
  154. success(e) {
  155. if (e.platform == "ios") {
  156. _that.statusBarHeight = e.statusBarHeight + 45;
  157. } else {
  158. _that.statusBarHeight = e.statusBarHeight + 50;
  159. }
  160. }
  161. })
  162. },
  163. methods:{
  164. getChindnew(obj) {
  165. this.cType = obj.chain.id
  166. if (obj.chain.id == 1) {
  167. this.cType = 'ETH'
  168. uni.setStorageSync('cType', 'ETH')
  169. } else if (obj.chain.id == 56) {
  170. this.cType = 'BSC'
  171. uni.setStorageSync('cType', 'BSC')
  172. } else if (obj.chain.id == 137) {
  173. this.cType = 'MATIC'
  174. uni.setStorageSync('cType', 'MATIC')
  175. } else if (obj.chain.id == 128) {
  176. this.cType = 'HT'
  177. uni.setStorageSync('cType', 'HT')
  178. }
  179. },
  180. watchAccounts(data){
  181. if(data.address!=this.walletAddress){
  182. this.walletAddress = data.address
  183. uni.setStorageSync('walletAddress', data.address)
  184. this.cType = 'BSC'
  185. uni.setStorageSync('cType', 'BSC')
  186. }
  187. },
  188. openbtns(){
  189. const state = useWeb3ModalState()
  190. //账户信息
  191. const account = getAccount()
  192. //账户信息chain
  193. const { chain} = getNetwork()
  194. const result = new InjectedConnector()
  195. if (account) {
  196. this.walletAddress = account.address
  197. uni.setStorageSync('walletAddress', account.address)
  198. }
  199. const unwatchAcc = watchAccount((account) => {
  200. //console.log('ceshi',account)
  201. this.watchAccounts(account)
  202. })
  203. if(this.inviteId==''){
  204. uni.showToast({
  205. title: '請輸入上級邀請碼',
  206. icon: 'none',
  207. })
  208. return false
  209. }
  210. //regUser
  211. let data = {
  212. walletAddress:this.walletAddress,
  213. cType:this.cType,
  214. invitation:this.inviteId
  215. }
  216. this.$http.regUser(data).then(res => {
  217. var datas = res.data
  218. if (datas.code == 200) {
  219. uni.showToast({
  220. title: datas.msg,
  221. icon: 'none',
  222. })
  223. uni.switchTab({
  224. url:'/pages/index/index'
  225. })
  226. } else if(datas.code ==40001){
  227. uni.showToast({
  228. title: datas.msg,
  229. icon: 'none',
  230. })
  231. uni.switchTab({
  232. url:'/pages/index/index'
  233. })
  234. }else{
  235. uni.showToast({
  236. title: datas.msg,
  237. icon: 'none',
  238. })
  239. }
  240. }).catch(err => {
  241. uni.showToast({
  242. title: err
  243. })
  244. })
  245. }
  246. }
  247. }
  248. </script>
  249. <style scoped lang="scss">
  250. .logImg{
  251. width:300rpx;
  252. height: 300rpx;
  253. margin: 0 auto;
  254. display: block;
  255. padding-top: 10%;
  256. }
  257. .radius{
  258. width: 90%;
  259. margin: 120rpx 5% 20rpx 5%;
  260. background-color: #fff;
  261. border-radius: 12rpx;
  262. padding: 20rpx 0;
  263. text-align: center;
  264. .title{
  265. font-size: 40rpx;
  266. color: #000;
  267. }
  268. .invitInput{
  269. width: 88%;
  270. margin-top: 20rpx;
  271. margin: 40rpx 5% 20rpx 5%;
  272. padding:20rpx 1%;
  273. background-color: #484a54;
  274. border-radius: 50rpx;
  275. //padding: 20rpx 0;
  276. color: #fff;
  277. text-align: center;
  278. }
  279. }
  280. .tjBtn {
  281. width: 90%;
  282. margin: 100rpx 5%;
  283. text-align: center;
  284. height: 92rpx;
  285. line-height: 92rpx;
  286. color: #333;
  287. font-weight: 700;
  288. border-radius: 8rpx;
  289. font-size: 36rpx;
  290. }
  291. </style>