diff --git a/src/metamask/ChainManager.ts b/src/metamask/ChainManager.ts deleted file mode 100644 index 37c2af8..0000000 --- a/src/metamask/ChainManager.ts +++ /dev/null @@ -1,162 +0,0 @@ -import store from "@/views/home/store/index"; -import Web3 from 'web3' -import { Message } from 'element-ui'; -import axios from 'axios'; -import {Http_getNonce,Http_login} from "@/utils/login-request" -// import Cookie from "@u/cookie"; -interface Window { - ethereum: any - web3: any - celo: any -} -declare let window: Window - -export default class ChainManager { - public provider:any - public web3:Web3 - public chainId:number - public account:string - public nonce:string - - private async login( account, chainId, nonce ) { - nonce += '' - // console.log(nonce);return - const tips = 'This signature is only used for verify your account' - const signMsg = { - tips, - nonce - } - const EIP721_DOMAIN_DATA = [ - { name: 'name', type: 'string' }, - { name: 'version', type: 'string' } - ] - - const signObj = { - types: { - EIP712Domain: EIP721_DOMAIN_DATA, - set: [ - { name: 'tips', type: 'string' }, - { name: 'nonce', type: 'string' } - ] - }, - primaryType: 'set', - domain: { - name: 'Auth', - version: '1' - }, - message: signMsg - } - const signature = await this.signData(signObj, account) - const authData = { - account, - nonce, - signature, - tips, - net_id: chainId - } - // console.log('login data: ', authData) - const res = await Http_login(authData) - console.log(res) - // if (!res.errcode && res.token) { - // store.commit('set_token',res.token) - // - // } - } - public async checkNance() { - try { - let nonce = store.getters.nonce - // console.log(store.getters);return - if (!nonce) { - let params = {account:this.account, net_id:this.chainId} - const res = await Http_getNonce(params); - // store.commit('set_nonce',res.nonce) - // this.nonce = res.nonce - } - // console.log(this.account,this.chainId,this.nonce);return - this.login(this.account,this.chainId,this.nonce) - } catch (err) { - console.log(err) - Promise.reject(err) - } - } - public async connect(){ - if (!this.hasMetamask()) { - Message({ - message: '请先安装MetaMask插件', - type: 'error', - duration: 5 * 1000 - }) - return - } - this.provider = await this.connectMetaMask() - if (!this.provider) { - return - } - console.log(this.provider);return - this.web3 = new Web3(this.provider) - this.chainId = await this.web3.eth.getChainId() - const accounts = await this.web3.eth.getAccounts() - if (accounts && accounts.length > 0) { - this.account =accounts[0]; - } - } - private async connectMetaMask() { - let provider = window.ethereum - try { - await provider.request({ method: 'eth_requestAccounts' }) - } catch (error) { - if (error.code === -32002) { - throw new Error('MeatMask not login, Open MeatMask and login first') - } else { - throw new Error('User Rejected') - } - } - return provider - } - private verifyInjectedProvider(check) { - return window.ethereum - ? window.ethereum[check] - : window.web3 && - window.web3.currentProvider && - window.web3.currentProvider[check] - } - private hasMetamask() { - if (typeof window.ethereum !== 'undefined') { - return true; - }else { - return false; - } - } - private async signData(signObj, signer) { - const msgParams = JSON.stringify(signObj) - const from = signer - // console.log('clicked, sending personal sign req', 'from', from, msgParams) - const params = [from, msgParams] - const result = await this.sendCmd({ - method: 'eth_signTypedData_v4', - params, - from - }) - console.log(result) - // return result.result - } - private async sendCmd({ method, params, from }) { - return new Promise((resolve, reject) => { - // eslint-disable-next-line @typescript-eslint/ban-ts-comment - // @ts-ignore - this.web3.currentProvider.sendAsync({ - method, - params, - from - }, async function(err, result) { - if (err) { - reject && reject(err) - return - } - resolve && resolve(result) - }) - }) - } - - -} diff --git a/src/views/home/pages/login/login.ts b/src/views/home/pages/login/login.ts index d461c4b..5ac1ae5 100644 --- a/src/views/home/pages/login/login.ts +++ b/src/views/home/pages/login/login.ts @@ -1,23 +1,12 @@ -import { Component, Mixins, Vue, Watch } from 'vue-property-decorator' -import Url, { AdminUrl } from "@/utils/Url"; -import { - State, - Getter, - Action, - Mutation, -} from "vuex-class"; -import { Form } from "view-design"; +import { Component } from 'vue-property-decorator' import "@/assets/js/wwlogin"; import HomeParent from "@/views/home/Common/HomeParent"; -import Types from "@/views/admin/store/types"; import SvgIcon from '@/components/SvgIcon.vue' import { Message } from 'element-ui'; -import ChainManager from '@/metamask/ChainManager' import Web3 from 'web3' import {Http_getNonce, Http_login} from "@/utils/login-request"; import store from "@/views/home/store"; -import {ActionContext} from "vuex"; -import {UserState} from "@/views/home/store/user"; + interface Window { ethereum: any @@ -33,7 +22,6 @@ declare let window: Window }) export default class Login extends HomeParent { // @Action('Login') Login - public provider:any public web3:Web3 public chainId:number @@ -50,11 +38,8 @@ export default class Login extends HomeParent { } public async doLogin(){ - // let apiHost = process.env.NODE_ENV !== 'production' ? process.env.VUE_APP_API_HOST : document.location.host; - // let scheme = document.location.protocol; - // console.log(scheme + "//" + apiHost);return if (!this.isLogin){ - try{ + try { await this.connect() await this.checkNance() } catch (err) { @@ -113,8 +98,10 @@ export default class Login extends HomeParent { net_id: chainId } const res:any = await Http_login(authData) + if (res.code == 200) { store.commit('set_token',res.data.token) + this.goPage("home"); } } @@ -124,11 +111,7 @@ export default class Login extends HomeParent { let nonce = store.getters.nonce if (!nonce) { let params = {account:this.account, net_id:this.chainId} - // await this.$http.get(Url.GET_NONCE,params).then(res => { - // console.log(res.data) - // }) const res:any = await Http_getNonce(params); - // console.log('@@@',res) store.commit('set_nonce',res.data) this.nonce = res.data }