reown/appkit/vue钱包登录

引入:

复制代码
<script setup>
import { createAppKit,useAppKitEvents } from '@reown/appkit/vue'
import { arbitrum, mainnet, polygon, base } from '@reown/appkit/networks'
import { WagmiAdapter } from '@reown/appkit-adapter-wagmi'
import { watch } from 'vue'


// 1. Get projectId from https://dashboard.reown.com
// 注意设置domain
 const projectId = 'xxxxxx'

// 2. Create a metadata object
const metadata = {
  name: 'AppKit',
  description: 'AppKit Example',
  url: window.location.origin, // 自动跟当前域名保持一致
  icons: ['https://avatars.githubusercontent.com/u/179229932']
}



// 3. Set the networks
const networks = [mainnet, polygon, base]

// 4. Create Wagmi Adapter
const wagmiAdapter = new WagmiAdapter({
  networks,
  projectId
})

// 5. Create the modal
const modal = createAppKit({
  adapters: [wagmiAdapter], // 只保留钱包适配器
  networks,
  projectId,
  metadata,
  features: {
   analytics: true,      
   socials: [],             // 清空社交登录
   email: false
  },
  themeMode: "light",
   uiPreferences: {
      view: "compact"  
    },
});



</script>

公共头部显示钱包登录按钮

复制代码
import {
		ref,
		onMounted,
		watch
	} from "vue";
import { useAppKit,useAppKitAccount,useDisconnect,useWalletInfo,useAppKitEvents } from '@reown/appkit/vue'
import { useUserStore } from '@/stores/user'
const userStore = useUserStore()

// 调用 hook
	const { open, close, account, network, status } = useAppKit()
	const accountData = useAppKitAccount(); 
	const { disconnect } = useDisconnect()
	//登录按钮
	const connectFunc = async()=>{
		// await disconnect()
		await open() // 打开连接弹窗,等待用户操作
	}
	//钱包内的disconnect
	useAppKitEvents({
	  disconnect: () => {
		console.log('钱包已断开')
		userStore.clearProfile()
	  },
	  connect: (account) => {
		addressText.value = account.address.slice(0, 4) + '...' + account.address.slice(-6)
	  }
	})
	
	//监听钱包地址
	watch(
	  () => accountData.value?.address,
	  (adr,old) => {
		
		if (adr){
			addressText.value = truncateAddress(accountData.value.address)
			if (!userStore.token){
				//获取到钱包地址以后,未登录状态下去登录
			  loginFunc(accountData.value.address)
			}
		}else{
			//获取不到钱包地址后退出
			userStore.clearProfile()
			uni.reLaunch({
				url: '/pages/index/index'
			});
			//这两个clear非常重要,否则会导致明明退出成功了,但是accountData依然是connect状态
			uni.clearStorageSync(); // 清理 localStorage
			uni.clearStorage();     // 异步清理
			
		}
	  }
	)
	//手动disconnect按钮
	const disConnectFunc = async()=>{
		await disconnect()
		userStore.clearProfile()
		addressText.value = ''
		uni.reLaunch({
			url: '/pages/index/index'
		});
		//这两个clear非常重要,否则会导致明明退出成功了,但是accountData依然是connect状态
		uni.clearStorageSync(); // 清理 localStorage
		uni.clearStorage();     // 异步清理

		// 短暂延迟后刷新页面
		setTimeout(() => {
			window.location.reload();
		}, 100);
	}

1.本地测试也需要设置下domain

2.本地测试可手动清理localStorage去测试

3.如本地一直退出不成功,可部署到线上调试,线上不行的话就是代码有问题。

相关推荐
尽意啊21 小时前
echarts树图动态添加子节点
前端·javascript·echarts
吃面必吃蒜21 小时前
echarts 极坐标柱状图 如何定义柱子颜色
前端·javascript·echarts
O_oStayPositive21 小时前
Vue3使用ECharts
前端·javascript·echarts
竹秋…21 小时前
echarts自定义tooltip中的内容
前端·javascript·echarts
宝贝露.21 小时前
Axure引入Echarts图无法正常显示问题
前端·javascript·echarts
shmily麻瓜小菜鸡21 小时前
前端文字转语音
前端
人良爱编程21 小时前
Hugo的Stack主题配置记录03-背景虚化-导航栏-Apache ECharts创建地图
前端·javascript·apache·echarts·css3·html5
来颗仙人掌吃吃21 小时前
解决Echarts设置宽度为100%发现宽度变为100px的问题(Echarts图标宽度自适应问题)
前端·javascript·echarts
余子桃21 小时前
Echarts图表官网打开太慢怎么办?echarts.apache.org
前端·javascript·echarts
pas13621 小时前
41-parse的实现原理&有限状态机
开发语言·前端·javascript