123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274 |
- import NP from 'number-precision'
- import API from '@/api'
- import { showError, Cookie } 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),
- /**
- * 调起合约转账
- * @param {Object} store
- * @param {Object} params
- */
- async doDeposit ({ commit, state }, params) {
- var memo
- if (Cookie.getCookie('account') && Cookie.getCookie('token')) {
- memo = 'deposit'
- } else {
- let randomCode = await API.user.getRandom({ account: state.account.name })
- memo = 'deposit|' + randomCode.data.data
- }
- let player = state.account.name
- let toAccount = state.toAccount
- let chargeAmount = params.amount.toFixed(4) + ' EOS'
- let identity = { authorization: [`${player}@${state.account.authority}`] }
- // 发出合约调用请求
- window.EOS.transfer(player, toAccount, chargeAmount, memo, identity).then(trx => {
- let param = {
- transaction_id: trx.transaction_id,
- block_num: trx.processed.block_num ? trx.processed.block_num : '',
- player: player,
- amount: NP.times(params.amount, 10000),
- appid: state.appid // 暂时写死
- }
- API.eos.deposit(param).then(({ data }) => {
- if (data.code === 0) {
- let amount = NP.times(params.amount, 10000)
- commit('updateBalance', -amount)
- commit('updateMoney', amount)
- params.callback && params.callback()
- } else {
- showError(data.msg)
- }
- }).catch((err) => {
- showError(err)
- params.cancel && params.cancel()
- })
- }).catch(msg => {
- params.cancel && params.cancel()
- if (msg.type === 'signature_rejected') {
- return
- }
- let json = JSON.parse(msg)
- let details = json.error.details
- showError(details[0].message)
- })
- },
- /**
- * 调起接口转账
- * @param {Object} store
- * @param {Object} params
- */
- async doWithDraw ({ commit, state }, params) {
- },
- /**
- * 调起授权登录
- * @param {Object} store
- * @param {Object} params
- */
- async doScatterLogin ({ dispatch, commit, state }, params) {
- if (!state.scatter) {
- showError('Please install the scatter', 'Scatter')
- } else {
- // 获取最快主网
- let networkConfig = await getNetwork()
- commit('setMainnet', EOS_HOST)
- return new Promise((resolve, reject) => {
- state.scatter.getIdentity({
- accounts: [networkConfig]
- }).then(identity => {
- let accounts = identity.accounts.find(x => x.blockchain === 'eos')
- if (Cookie.getCookie('account') !== accounts.name) {
- Cookie.delCookie('account')
- }
- commit('setAccount', accounts)
- window.EOS = state.scatter.eos(networkConfig, Eos, {})
- // 调用scatter登录成功后获取用户EOS、GT
- dispatch('getUserEOS')
- dispatch('getUserGT')
- dispatch('getAccount')
- // 每分钟刷新一次
- this.limitTimer = setInterval(() => {
- dispatch('getAccount')
- }, 60000)
- resolve(accounts)
- }).catch(error => {
- commit('setAccount', 'NO_LOGINING')
- reject(error)
- })
- })
- }
- },
- /**
- * 注销当前scatter账号
- * @param {Object} store
- * @param {Object} params
- */
- doScatterLoginOut ({ commit, state }, params) {
- Cookie.delCookie('account')
- Cookie.delCookie('token')
- state.scatter.forgetIdentity()
- clearInterval(this.limitTimer)
- commit('setAccount', 'NO_LOGINING')
- commit('setBalance', 0)
- commit('setMoney', 0)
- commit('setGt', 0)
- },
- /**
- * 更新用户EOS币数量
- * @param {Object} store
- * @param {Object} params
- */
- 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 => {
- // 失败重新尝试,最大尝试5次
- commit('setEOSError', state.eosErrorCount + 1)
- if (state.eosErrorCount <= 5) {
- setTimeout(() => {
- dispatch('getUserEOS')
- }, 1000 * state.eosErrorCount)
- }
- reject(error)
- })
- })
- },
- /**
- * 更新用户GT币数量
- * @param {Object} store
- * @param {Object} params
- */
- getUserGT ({ dispatch, commit, state }, params) {
- return new Promise((resolve, reject) => {
- window.EOS.getCurrencyBalance('eosgetgtoken', state.account.name, 'GT').then(([ balance ]) => {
- let balanceNum = 0
- if (balance) {
- balanceNum = Number(balance.replace(/\sGT/, ''))
- }
- commit('setGt', balanceNum)
- commit('setGTError', 0)
- resolve()
- }).catch(error => {
- // 失败重新尝试,最大尝试5次
- commit('setGTError', state.gtErrorCount + 1)
- if (state.gtErrorCount <= 5) {
- setTimeout(() => {
- dispatch('getUserGT')
- }, 1000 * state.gtErrorCount)
- }
- reject(error)
- })
- })
- },
- /**
- * 获取用户信息
- * @param {Object} store
- * @param {Object} params
- */
- getAccount ({ commit, state }, params) {
- return new Promise((resolve, reject) => {
- window.EOS.getAccount(state.account.name).then(data => {
- // 设置CPU、NET使用率显示
- if (data.cpu_limit && data.net_limit) {
- commit('setLimit', {
- cpu: data.cpu_limit.used / data.cpu_limit.max,
- net: data.net_limit.used / data.net_limit.max
- })
- }
- // 设置公钥
- 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)
- })
- })
- },
- /**
- * 进行验证身份登录,生成业务登录态token
- * @param {Object} store
- * @param {Object} params
- */
- async doGameLogin ({ dispatch, commit, state }, params) {
- if (!state.account || state.account === 'NO_LOGINING') {
- await dispatch('doScatterLogin')
- } else if (Cookie.getCookie('account') && Cookie.getCookie('token')) {
- return Promise.resolve()
- } 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.login(param).then(({ data }) => {
- if (data.result === 1) {
- let account = data.data.account
- let token = data.data.token
- Cookie.setCookie('account', account)
- Cookie.setCookie('token', token)
- resolve(data)
- } else {
- reject(new Error(data.msg))
- }
- })
- }).catch(error => {
- reject(error)
- })
- })
- }
- }
- }
|