|
@@ -1,12 +1,3 @@
|
|
|
-// import detectEthereumProvider from '@metamask/detect-provider'
|
|
|
-import {
|
|
|
- login
|
|
|
-} from '@/api/login.js';
|
|
|
-import {
|
|
|
- getUserInfo
|
|
|
-} from '@/api/user.js';
|
|
|
-import store from '../store'
|
|
|
-
|
|
|
export function getActionPage() {
|
|
|
let pages = getCurrentPages();
|
|
|
return pages[pages.length - 1]
|
|
@@ -19,13 +10,10 @@ export function interceptor() {
|
|
|
let url = '/pages/public/login'
|
|
|
// #ifdef H5
|
|
|
// 判断是否为公众号调用微信授权登录
|
|
|
- // let weichatBrowser = uni.getStorageSync('weichatBrowser');
|
|
|
- // if (weichatBrowser) {
|
|
|
- // url = '/pages/public/wxLogin';
|
|
|
- // }
|
|
|
- setPeovider();
|
|
|
- return
|
|
|
-
|
|
|
+ let weichatBrowser = uni.getStorageSync('weichatBrowser');
|
|
|
+ if (weichatBrowser) {
|
|
|
+ url = '/pages/public/wxLogin';
|
|
|
+ }
|
|
|
// #endif
|
|
|
// #ifdef MP-WEIXIN
|
|
|
url = '/pages/public/wxLogin';
|
|
@@ -44,7 +32,7 @@ export function saveUrl() {
|
|
|
}
|
|
|
|
|
|
// 处理分享链接地址
|
|
|
-export function getPageUrl(showSpeard = false) {
|
|
|
+export function getPageUrl(showSpeard=false) {
|
|
|
let page = getActionPage();
|
|
|
let path = '/' + page.route;
|
|
|
let url = '';
|
|
@@ -57,9 +45,9 @@ export function getPageUrl(showSpeard = false) {
|
|
|
// 判断有无邀请人
|
|
|
if (a != 'speard') {
|
|
|
url += a + "=" + page.options[a] + "&"
|
|
|
- } else {
|
|
|
+ }else{
|
|
|
// 判断是否需要存储邀请人
|
|
|
- if (showSpeard) {
|
|
|
+ if(showSpeard){
|
|
|
url += a + "=" + page.options[a] + "&"
|
|
|
}
|
|
|
}
|
|
@@ -69,143 +57,3 @@ export function getPageUrl(showSpeard = false) {
|
|
|
// 返回处理邀请人后的邀请地址
|
|
|
return path
|
|
|
}
|
|
|
-
|
|
|
-async function setPeovider() { // 检测提供者
|
|
|
- uni.showLoading({
|
|
|
- title: '检测授权中...',
|
|
|
- mask: true
|
|
|
- });
|
|
|
- const provider = await detectEthereumProvider();
|
|
|
- if (provider) {
|
|
|
- startApp(provider); // Initialize your app
|
|
|
- } else {
|
|
|
- uni.showToast({
|
|
|
- title: '请先安装MetaMask插件',
|
|
|
- icon: 'none',
|
|
|
- duration: 3500
|
|
|
- });
|
|
|
- return false;
|
|
|
- }
|
|
|
-};
|
|
|
-// 检测插件
|
|
|
-async function startApp(provider) {
|
|
|
- if (provider !== window.ethereum) {
|
|
|
- uni.showToast({
|
|
|
- title: '安装了多个钱包,加载失败',
|
|
|
- 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()
|
|
|
- console.log(src, 'src');
|
|
|
- } catch (addError) {
|
|
|
- console.log(addError);
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- } else {
|
|
|
- login_metamask()
|
|
|
- }
|
|
|
-};
|
|
|
-async function login_metamask() {
|
|
|
- eth_requestAccounts(); // 拿到账户对应的账号
|
|
|
- ethereum.on('accountsChanged', (accounts) => { // 地址修改时收到通知
|
|
|
- store.commit('user/logout');
|
|
|
- console.log('234');
|
|
|
- setPeovider(); // 重新登录
|
|
|
- });
|
|
|
- // 监听
|
|
|
- // ethereum.on('chainChanged', (res)=>{
|
|
|
- // console.log('235')
|
|
|
- // 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)
|
|
|
- // })
|
|
|
-};
|
|
|
-async function eth_requestAccounts() {
|
|
|
- // 链接到MetaMask
|
|
|
- ethereum.request({
|
|
|
- method: 'eth_requestAccounts'
|
|
|
- }).then((account) => {
|
|
|
- console.log(account, 'account');
|
|
|
- // 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('token', e.data.token);
|
|
|
- getUserInfo({}).then(e => {
|
|
|
- console.log(store);
|
|
|
- store.commit('user/login')
|
|
|
- // 保存返回用户数据
|
|
|
- // obj.setUserInfo();
|
|
|
- store.commit('user/setUserInfo', e.data);
|
|
|
- uni.hideLoading()
|
|
|
- });
|
|
|
- })
|
|
|
- .catch(function(e) {
|
|
|
- console.log(e);
|
|
|
- });
|
|
|
-};
|