123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367 |
- import NP from 'number-precision'
- import API from '@/api'
- import { showError, Cookie, getUserOpt, setUserOpt } from '@/util/util.js'
- import { getNetwork } from '@/util/contract.js'
- import Eos from 'eosjs'
- export const actions = {
- setScatter: ({ commit }, scatter) => commit('setScatter', scatter),
- setEos: ({ commit }, eosjs) => commit('setEos', eosjs),
- setAccount: ({ commit }, account) => commit('setAccount', account),
-
- async doScatterLogin ({ dispatch, commit, state }, h5Router) {
- if (!state.scatter) {
- showError('Please install the scatter', 'Scatter')
- } else {
- if (getUserOpt('isChangeIndentity') == 1) {
- await actions.doScatterLogout(...arguments)
- localStorage.setItem('accounts', '')
- setUserOpt('isChangeIndentity', 0)
- }
-
- let networkConfig = await getNetwork()
-
- try {
- var identity = await state.scatter.login({
- accounts: [networkConfig]
- })
- } catch (error) {
- if (error.type !== 'identity_rejected') {
- showError(error.message)
- }
- return Promise.reject(error)
- }
- let accounts = identity.accounts.find(x => x.blockchain === 'eos')
- let lastAccounts = localStorage.getItem('accounts')
- if (lastAccounts && JSON.parse(lastAccounts).name !== accounts.name) {
- await actions.doScatterLogout(...arguments)
- h5Router.push({ path: '/login' })
- commit('setUserInfo', null)
- commit('initChatData')
- commit('initGroupData')
- commit('chatAppLogin', false)
- commit('toApp', false)
- }
- commit('setAccount', accounts)
- localStorage.setItem('accounts', JSON.stringify(accounts))
-
-
-
-
-
-
-
- window.EOS = state.scatter.eos(networkConfig, Eos, {})
-
-
-
- await dispatch('getAccount')
-
-
-
-
- return Promise.resolve(true)
- }
- },
-
- async doScatterBind ({ dispatch, commit, state }) {
- let eosType = getUserOpt('eosType') || 'eos'
- let eosExtInfo = {
- contract: eosType == 'meetone' ? 'meechat.m' : 'meechatadmin',
- net_id: eosType == 'meetone' ? 2 : 1
- }
- if (!state.scatter) {
- showError('Please install the scatter', 'Scatter')
- } else {
-
- let networkConfig = await getNetwork()
- if (state.scatter.identity) {
- await (state.scatter && state.scatter.logout && state.scatter.logout())
- }
- setUserOpt('isChangeIndentity', 1)
-
- try {
- var identity = await state.scatter.login({
- accounts: [networkConfig]
- })
- } catch (error) {
- if (error.type !== 'identity_rejected') {
- showError(error.message)
- }
- return Promise.reject(error)
- }
- let accounts = identity.accounts.find(x => x.blockchain === 'eos')
-
- let { data } = await API.user.getRandom2({ account: accounts.name })
- let random = data.data.random
- let accessToken = data.data.accessToken
- let trx = null
- let eos = state.scatter.eos(networkConfig, Eos, {})
- let contract = await eos.contract(eosExtInfo.contract)
- trx = await contract.login(accounts.name, random, { authorization: [`${accounts.name}@${accounts.authority}`] })
- let param = {
- account: accounts.name,
- access_token: accessToken,
- trx_id: trx.transaction_id || trx.transaction.transaction_id,
- net_id: eosExtInfo.net_id
- }
- return new Promise((resolve, reject) => {
- API.user.eosBind2(param).then(() => {
- resolve(param)
- }).catch((e) => {
- reject(e)
- })
- })
- }
- },
-
- doScatterLogout ({ commit, state }, params) {
- Cookie.delCookie('user_id')
- Cookie.delCookie('token')
- localStorage.removeItem('user_id')
- localStorage.removeItem('token')
- localStorage.removeItem('accounts')
- commit('setUserId', null)
- commit('setToken', null)
-
- commit('setAccount', '')
- setUserOpt('isChangeIndentity', 0)
-
- return state.scatter && state.scatter.logout && state.scatter.logout()
- },
-
- getUserEOS ({ dispatch, commit, state }, params) {
- return new Promise((resolve, reject) => {
- window.EOS.getCurrencyBalance('eosio.token', state.account.name, 'EOS').then(([ balance ]) => {
- let balanceNum = Number(balance.replace(/\sEOS/, '')).toFixed(4)
- balanceNum = NP.times(balanceNum, 10000)
- commit('setBalance', balanceNum)
- commit('setEOSError', 0)
- resolve()
- }).catch(error => {
-
- commit('setEOSError', state.eosErrorCount + 1)
- if (state.eosErrorCount <= 5) {
- setTimeout(() => {
- dispatch('getUserEOS')
- }, 1000 * state.eosErrorCount)
- }
- reject(error)
- })
- })
- },
-
- getAccount ({ commit, state }, params) {
- return new Promise((resolve, reject) => {
- window.EOS.getAccount(state.account.name).then(data => {
-
- const perms = JSON.parse(JSON.stringify(data.permissions))
- perms.forEach(v => {
- if (v.perm_name === 'active') {
- let publicKey = v.required_auth.keys[0].key
- commit('setPublicKey', publicKey)
- }
- })
- resolve(data)
- }).catch(error => {
- reject(error)
- })
- })
- },
-
- async doGameLogin ({ dispatch, commit, state }, params) {
- let userId = localStorage.getItem('user_id')
- let token = localStorage.getItem('token')
- if (userId && token) {
- commit('setUserId', userId)
- commit('setToken', token)
- return Promise.resolve({
- user_id: userId,
- token: token
- })
- } else {
- let rData = await API.user.getRandom({ account: state.account.name })
- let random = rData.data.data
- let pubkey = state.publicKey
- return new Promise((resolve, reject) => {
- state.scatter.getArbitrarySignature(pubkey, random, 'Need Login').then(sign => {
- let param = {
- account: state.account.name,
- sign,
- pubkey,
- data: random
- }
- API.user.eosLogin(param).then(({ data }) => {
- if (data.result === 1) {
- let userId = data.data.user_id
- let token = data.data.token
- localStorage.setItem('user_id', userId)
- localStorage.setItem('token', token)
- commit('setUserId', userId)
- commit('setToken', token)
- resolve(data)
- } else {
- reject(new Error(data.msg))
- }
- })
- }).catch(error => {
- reject(error)
- })
- })
- }
- },
-
- async resetGameLogin ({ dispatch, commit, state }, params) {
- localStorage.removeItem('user_id')
- localStorage.removeItem('token')
- await dispatch('doContractLogin')
- if (self !== top) {
- location.replace(location.href.replace('show=false', 'show=true'))
- } else {
- location.reload()
- }
- },
-
- async doContractLogin ({ commit, state }, params) {
- let eosType = getUserOpt('eosType') || 'eos'
- let eosExtInfo = {
- contract: eosType == 'meetone' ? 'meechat.m' : 'meechatadmin',
- net_id: eosType == 'meetone' ? 2 : 1
- }
- let userId = localStorage.getItem('user_id')
- let token = localStorage.getItem('token')
- if (userId && token) {
- commit('setUserId', userId)
- commit('setToken', token)
- return Promise.resolve({
- user_id: userId,
- token: token
- })
- } else {
- try {
-
- let { data } = await API.user.getRandom2({ account: state.account.name })
- let random = data.data.random
- let accessToken = data.data.accessToken
- let identity = { authorization: [`${state.account.name}@${state.account.authority}`] }
- let trx = null
- let networkConfig = await getNetwork()
- if (self !== top) {
- trx = await window.postMessager.send({
- action: 'meechat:eosAction',
- data: {
- contract: eosExtInfo.contract,
- action: 'login',
- param: [state.account.name, random, identity],
- network: networkConfig
- }
- })
- } else {
- let eos = window.EOS || state.scatter.eos
- let contract = await eos.contract(eosExtInfo.contract)
- trx = await contract.login(state.account.name, random, identity)
- }
- let param = {
- account: state.account.name,
- access_token: accessToken,
- trx_id: trx.transaction_id || trx.transaction.transaction_id,
- group_id: state.group.groupId || null,
- net_id: eosExtInfo.net_id
- }
- let res = await API.user.eosLogin2(param)
- if (res.data.result === 1) {
- return Promise.resolve(res.data)
- }
- } catch (error) {
- return Promise.reject(error)
- }
- }
- },
-
- getHotList ({ state }, params) {
- if (state.hotList.length > 0) return
- API.group.getHotList().then(({ data }) => {
- state.hotList = data.data
- })
- },
-
- getDiscovery ({ state }, params) {
- if (state.hotGame.length > 0 && state.hotList.length > 0) return
- API.base.discovery().then(({ data }) => {
- state.hotGame = data.data.hotGame
- state.hotList = data.data.hotGroup
- })
- }
- }
|