|
@@ -2,10 +2,11 @@ import api from '@/api'
|
|
|
import { showError, getUrlParam, getMeechatType, openBlankWindow, getUserOpt, setUserOpt } from '@/util/util'
|
|
|
import { mapActions, mapState, mapMutations } from 'vuex'
|
|
|
import ScatterJS from 'scatter-js/dist/scatter.esm'
|
|
|
+import Web3 from 'web3'
|
|
|
import { Message } from 'element-ui'
|
|
|
import PostMessager from '@/util/postMessager.js'
|
|
|
|
|
|
-let _timeoutHandler = 0
|
|
|
+let _timeoutHandler = 0 // 轮询定时器
|
|
|
|
|
|
// 账号登录
|
|
|
export const accountLoginMixin = {
|
|
@@ -15,7 +16,9 @@ export const accountLoginMixin = {
|
|
|
winHandler: null, // 新开窗口
|
|
|
winTimer: null, // 检测窗口定时器
|
|
|
meechatType: getMeechatType(),
|
|
|
- from: decodeURIComponent(getUrlParam('from'))
|
|
|
+ from: decodeURIComponent(getUrlParam('from')),
|
|
|
+ isShowEth: true, // 是否显示eth登录
|
|
|
+ isShowTron: true // 是否显示Tron登录
|
|
|
}
|
|
|
},
|
|
|
computed: {
|
|
@@ -24,12 +27,10 @@ export const accountLoginMixin = {
|
|
|
methods: {
|
|
|
...mapMutations(['setLogining']),
|
|
|
...mapActions([
|
|
|
- 'getUserInfo',
|
|
|
'setScatter',
|
|
|
'setAccount',
|
|
|
'doScatterLogin',
|
|
|
'doContractLogin',
|
|
|
- 'doScatterLogout',
|
|
|
'initSocket'
|
|
|
]),
|
|
|
/**
|
|
@@ -58,9 +59,10 @@ export const accountLoginMixin = {
|
|
|
await this.loginEosMeetoneCommon(this.curLoginType)
|
|
|
this.setLogining(false)
|
|
|
} else {
|
|
|
- this.$store.commit('setUserId', userId)
|
|
|
- this.$store.commit('setToken', token)
|
|
|
- this.loginSuccCallBack()
|
|
|
+ this.loginSuccCallBack({
|
|
|
+ userId: userId,
|
|
|
+ token: token
|
|
|
+ })
|
|
|
}
|
|
|
|
|
|
return true
|
|
@@ -160,8 +162,11 @@ export const accountLoginMixin = {
|
|
|
|
|
|
// 签名登录
|
|
|
try {
|
|
|
- await this.doContractLogin(2)
|
|
|
- this.loginSuccCallBack()
|
|
|
+ let ctLoginRes = await this.doContractLogin(2)
|
|
|
+ this.loginSuccCallBack({
|
|
|
+ userId: ctLoginRes.data.user_id,
|
|
|
+ token: ctLoginRes.data.token
|
|
|
+ })
|
|
|
} catch (e) {
|
|
|
reject(e)
|
|
|
let err = e
|
|
@@ -202,6 +207,77 @@ export const accountLoginMixin = {
|
|
|
})
|
|
|
},
|
|
|
/**
|
|
|
+ * @des Eth登录 MetaMask
|
|
|
+ */
|
|
|
+ async loginEth () {
|
|
|
+ this.curLoginType = 'eth'
|
|
|
+ this.setLogining(true)
|
|
|
+ if (window.ethereum) {
|
|
|
+ window.web3 = new Web3(window.ethereum)
|
|
|
+ try {
|
|
|
+ await window.ethereum.enable()
|
|
|
+ this.getEthAccount()
|
|
|
+ } catch (error) {
|
|
|
+ }
|
|
|
+ } else if (window.web3) {
|
|
|
+ window.web3 = new Web3(window.web3.currentProvider)
|
|
|
+ this.getEthAccount()
|
|
|
+ }
|
|
|
+ },
|
|
|
+ /**
|
|
|
+ * @des Eth登录 MetaMask-获取当前账号
|
|
|
+ */
|
|
|
+ async getEthAccount () {
|
|
|
+ let accounts = await window.web3.eth.getAccounts()
|
|
|
+ let account = accounts[0]
|
|
|
+
|
|
|
+ if (account) {
|
|
|
+ let randomRes = await api.user.getRandom2({ account: account })
|
|
|
+ let str = window.web3.utils.sha3(randomRes.data.data.random)
|
|
|
+
|
|
|
+ window.web3.eth.sign(str, account, async (err, rs) => {
|
|
|
+ let resutlSign = rs
|
|
|
+
|
|
|
+ let { data } = await api.user.ethLogin({
|
|
|
+ account: account,
|
|
|
+ sign: resutlSign
|
|
|
+ })
|
|
|
+ this.loginSuccCallBack({
|
|
|
+ userId: data.data.user_id,
|
|
|
+ token: data.data.token
|
|
|
+ })
|
|
|
+ console.log(err)
|
|
|
+ })
|
|
|
+ }
|
|
|
+ },
|
|
|
+ /**
|
|
|
+ * @des Tron登录 TronLink
|
|
|
+ */
|
|
|
+ async loginTron () {
|
|
|
+ this.curLoginType = 'tron'
|
|
|
+ this.setLogining(true)
|
|
|
+ let account = window.tronWeb.defaultAddress.base58
|
|
|
+ let randomRes = await api.user.getRandom2({ account: account })
|
|
|
+ let s = window.tronWeb.sha3(randomRes.data.data.random)
|
|
|
+ let resutlSign = await window.tronWeb.trx.signMessage(s)
|
|
|
+
|
|
|
+ let rs = await window.tronWeb.trx.verifyMessage(s, resutlSign, account)
|
|
|
+
|
|
|
+ if (rs) {
|
|
|
+ let { data } = await api.user.tronLogin({
|
|
|
+ account: account,
|
|
|
+ sign: resutlSign
|
|
|
+ })
|
|
|
+
|
|
|
+ this.loginSuccCallBack({
|
|
|
+ userId: data.data.user_id,
|
|
|
+ token: data.data.token
|
|
|
+ })
|
|
|
+ } else {
|
|
|
+ this.setLogining(false)
|
|
|
+ }
|
|
|
+ },
|
|
|
+ /**
|
|
|
* @des SimpleWallet登录
|
|
|
*/
|
|
|
loginSimpleWallet () {
|
|
@@ -267,15 +343,10 @@ export const accountLoginMixin = {
|
|
|
}
|
|
|
|
|
|
if (isLoginSuccess) {
|
|
|
- let userId = res.data.data.user_id
|
|
|
- let token = res.data.data.token
|
|
|
-
|
|
|
- localStorage.setItem('user_id', userId)
|
|
|
- localStorage.setItem('token', token)
|
|
|
-
|
|
|
- this.$store.commit('setUserId', userId)
|
|
|
- this.$store.commit('setToken', token)
|
|
|
- this.loginSuccCallBack()
|
|
|
+ this.loginSuccCallBack({
|
|
|
+ userId: res.data.data.user_id,
|
|
|
+ token: res.data.data.token
|
|
|
+ })
|
|
|
|
|
|
if (this.winHandler != null) {
|
|
|
this.winHandler.close()
|
|
@@ -289,9 +360,18 @@ export const accountLoginMixin = {
|
|
|
}, 1000)
|
|
|
}
|
|
|
},
|
|
|
- async loginSuccCallBack () {
|
|
|
+ async loginSuccCallBack ({ userId = null, token = null }) {
|
|
|
this.setLogining(false)
|
|
|
+
|
|
|
setUserOpt('loginType', this.curLoginType)
|
|
|
+ if (userId) {
|
|
|
+ this.$store.commit('setUserId', userId)
|
|
|
+ localStorage.setItem('user_id', userId)
|
|
|
+ }
|
|
|
+ if (token) {
|
|
|
+ this.$store.commit('setToken', token)
|
|
|
+ localStorage.setItem('token', token)
|
|
|
+ }
|
|
|
|
|
|
switch (this.meechatType) {
|
|
|
// 内嵌版
|
|
@@ -329,5 +409,15 @@ export const accountLoginMixin = {
|
|
|
this.postMessager = new PostMessager('*', { callback })
|
|
|
window.postMessager = this.postMessager
|
|
|
}
|
|
|
+
|
|
|
+ // eth初始化
|
|
|
+ if (!window.ethereum || !window.web3) {
|
|
|
+ this.isShowEth = false
|
|
|
+ }
|
|
|
+
|
|
|
+ // tron初始化
|
|
|
+ if (!window.tronWeb) {
|
|
|
+ this.isShowTron = false
|
|
|
+ }
|
|
|
}
|
|
|
}
|