import detectEthereumProvider from '@/plugin/detect-provider' import { login, getUserInfo } from '@/api/login.js'; import store from '../store'; // 初始化监听事件 initEthere(); async function initEthere () { ethereum.on('accountsChanged', (accounts) => { // 地址修改时收到通知 console.log(accounts,'accounts') store.commit('user/logout'); console.log('234'); setPeovider(); // 重新登录 }); // 监听 // ethereum.on('chainChanged', (res)=>{ // console.log('235',res) // store.commit('user/logout'); // setPeovider(); // 重新登录 // }); // ethereum.on('connect', (connectInfo) => { // // 当 MetaMask 提供者第一次能够向链 提交 RPC 请求时,它会发出此事件。 // uni.clearStorage(); // // Time to reload your interface with accounts[0]! // loginDapp(); // }); ethereum.on('disconnect', (error) => { // 如果 MetaMask 提供者无法向任何链提 交 RPC 请求,它会发出此事件。 console.log(error); // 这里可以打印一下error }); // ethereum.on('message', (message) => { // MetaMask 提供者在收到一些应该通知消费者的消息时发出此事件。 // console.log(message.data.result.hash) // }) } export function getActionPage() { let pages = getCurrentPages(); return pages[pages.length - 1] } //登录拦截 export function interceptor() { let pages = getActionPage(); // 获取当前页面 let pageUrl = '/' + pages.route; let url = '/pages/public/login' // #ifdef H5 // 判断是否为公众号调用微信授权登录 // let weichatBrowser = uni.getStorageSync('weichatBrowser'); // if (weichatBrowser) { // url = '/pages/public/wxLogin'; // } setPeovider(); return // #endif // #ifdef MP-WEIXIN url = '/pages/public/wxLogin'; // #endif if (pageUrl != url) { uni.navigateTo({ url }) } } // 保存页面 export function saveUrl() { let path = getPageUrl(true) uni.setStorageSync('present', path); } // 处理分享链接地址 export function getPageUrl(showSpeard = false) { let page = getActionPage(); let path = '/' + page.route; let url = ''; // 获取对象可枚举键值列表 let objKeys = Object.keys(page.options); // 判断有无传值 if (objKeys.length > 0) { // 循环赋值对象数据 for (let a in page.options) { // 判断有无邀请人 if (a != 'speard') { url += a + "=" + page.options[a] + "&" } else { // 判断是否需要存储邀请人 if (showSpeard) { url += a + "=" + page.options[a] + "&" } } } path += '?' + url.substr(0, url.length - 1); } // 返回处理邀请人后的邀请地址 return path } async function setPeovider() { // 检测提供者 const that = getApp(); uni.showLoading({ title: that.$t('common.检测授权中'), mask: true }); const provider = await detectEthereumProvider(); if (provider) { startApp(provider); // Initialize your app } else { uni.showToast({ title: that.$t('common.插件'), icon: 'none', duration: 3500 }); return false; } }; // 检测插件 async function startApp(provider) { const that = getApp(); if (provider !== window.ethereum) { uni.showToast({ title:that.$t('common.安装了多个钱包'), icon: 'none', duration: 3500 }); return false } else { uni.showLoading({ mask: true }); } const srcid = await ethereum.request({ method: 'eth_chainId' }); if (srcid != '0x38') { try { const src = await ethereum.request({ method: 'wallet_switchEthereumChain', params: [{ chainId: '0x38' }], }); console.log('233') login_metamask() } catch (switchError) { if (switchError.code === 4902) { try { const src = await ethereum.request({ method: 'wallet_addEthereumChain', params: [{ chainId: '0x38', chainName: 'BSC', rpcUrls: ['https://bsc-dataseed2.ninicoin.io'], }, ], }); login_metamask('4') console.log(src, 'src'); } catch (addError) { console.log(addError); } } } } else { login_metamask('3') } }; async function login_metamask(as) { console.log(as); eth_requestAccounts('1'); // 拿到账户对应的账号 }; async function eth_requestAccounts(re) { console.log(re); // 链接到MetaMask ethereum.request({ method: 'eth_requestAccounts' }).then((account) => { console.log(account, 'account111111'); // this.$store.commit('accounts/connect_wallet', account[0]); // localStorage.setItem('accounts', account) const PKR_LOGIN = 'PKR_LOGIN' + (new Date()).getTime(); ethereum.request({ "method": "personal_sign", "params": [ PKR_LOGIN, account[0] ] }).then((res) => { console.log(res, 'res'); console.log(res.length, 'reschangdu'); toLogin(res,account[0], PKR_LOGIN); }); }); }; // 登录 async function toLogin(sign, account, msg) { const that = getApp(); login({ sign, account, msg: msg, spread: uni.getStorageSync('spread') || '' //上级推广人 }) .then(function(e) { uni.setStorageSync('tokens', e.data.userinfo.token); getUserInfo({}).then(e => { store.commit('user/login') // 保存返回用户数据 e.data.addresss = e.data.address.slice(0, 4) + "****" + e.data.address.slice(e.data.address.length - 4); console.log(e.data,'e.data') store.commit('user/setUserInfo', e.data); uni.hideLoading() that.$router.go(0) if(e.data.spread_uid == 0){ uni.navigateTo({ url:'/pages/index/index' }) } }); }) .catch(function(e) { console.log(e); }); };