actions.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. import NP from 'number-precision'
  2. import API from '@/api'
  3. import { showError, Cookie } from '@/util/util.js'
  4. import { getNetwork } from '@/util/contract.js'
  5. import Eos from 'eosjs'
  6. export const actions = {
  7. setScatter: ({ commit }, scatter) => commit('setScatter', scatter),
  8. setEos: ({ commit }, eosjs) => commit('setEos', eosjs),
  9. setAccount: ({ commit }, account) => commit('setAccount', account),
  10. /**
  11. * 调起合约转账
  12. * @param {Object} store
  13. * @param {Object} params
  14. */
  15. async doDeposit ({ commit, state }, params) {
  16. var memo
  17. if (Cookie.getCookie('account') && Cookie.getCookie('token')) {
  18. memo = 'deposit'
  19. } else {
  20. let randomCode = await API.user.getRandom({ account: state.account.name })
  21. memo = 'deposit|' + randomCode.data.data
  22. }
  23. let player = state.account.name
  24. let toAccount = state.toAccount
  25. let chargeAmount = params.amount.toFixed(4) + ' EOS'
  26. let identity = { authorization: [`${player}@${state.account.authority}`] }
  27. // 发出合约调用请求
  28. window.EOS.transfer(player, toAccount, chargeAmount, memo, identity).then(trx => {
  29. let param = {
  30. transaction_id: trx.transaction_id,
  31. block_num: trx.processed.block_num ? trx.processed.block_num : '',
  32. player: player,
  33. amount: NP.times(params.amount, 10000),
  34. appid: state.appid // 暂时写死
  35. }
  36. API.eos.deposit(param).then(({ data }) => {
  37. if (data.code === 0) {
  38. let amount = NP.times(params.amount, 10000)
  39. commit('updateBalance', -amount)
  40. commit('updateMoney', amount)
  41. params.callback && params.callback()
  42. } else {
  43. showError(data.msg)
  44. }
  45. }).catch((err) => {
  46. showError(err)
  47. params.cancel && params.cancel()
  48. })
  49. }).catch(msg => {
  50. params.cancel && params.cancel()
  51. if (msg.type === 'signature_rejected') {
  52. return
  53. }
  54. let json = JSON.parse(msg)
  55. let details = json.error.details
  56. showError(details[0].message)
  57. })
  58. },
  59. /**
  60. * 调起接口转账
  61. * @param {Object} store
  62. * @param {Object} params
  63. */
  64. async doWithDraw ({ commit, state }, params) {
  65. },
  66. /**
  67. * 调起授权登录
  68. * @param {Object} store
  69. * @param {Object} params
  70. */
  71. async doScatterLogin ({ dispatch, commit, state }, params) {
  72. if (!state.scatter) {
  73. showError('Please install the scatter', 'Scatter')
  74. } else {
  75. // 获取最快主网
  76. let networkConfig = await getNetwork()
  77. commit('setMainnet', EOS_HOST)
  78. return new Promise((resolve, reject) => {
  79. state.scatter.getIdentity({
  80. accounts: [networkConfig]
  81. }).then(identity => {
  82. let accounts = identity.accounts.find(x => x.blockchain === 'eos')
  83. if (Cookie.getCookie('account') !== accounts.name) {
  84. Cookie.delCookie('account')
  85. }
  86. commit('setAccount', accounts)
  87. window.EOS = state.scatter.eos(networkConfig, Eos, {})
  88. // 调用scatter登录成功后获取用户EOS、GT
  89. dispatch('getUserEOS')
  90. dispatch('getUserGT')
  91. dispatch('getAccount')
  92. // 每分钟刷新一次
  93. this.limitTimer = setInterval(() => {
  94. dispatch('getAccount')
  95. }, 60000)
  96. resolve(accounts)
  97. }).catch(error => {
  98. commit('setAccount', 'NO_LOGINING')
  99. reject(error)
  100. })
  101. })
  102. }
  103. },
  104. /**
  105. * 注销当前scatter账号
  106. * @param {Object} store
  107. * @param {Object} params
  108. */
  109. doScatterLoginOut ({ commit, state }, params) {
  110. Cookie.delCookie('account')
  111. Cookie.delCookie('token')
  112. state.scatter.forgetIdentity()
  113. clearInterval(this.limitTimer)
  114. commit('setAccount', 'NO_LOGINING')
  115. commit('setBalance', 0)
  116. commit('setMoney', 0)
  117. commit('setGt', 0)
  118. },
  119. /**
  120. * 更新用户EOS币数量
  121. * @param {Object} store
  122. * @param {Object} params
  123. */
  124. getUserEOS ({ dispatch, commit, state }, params) {
  125. return new Promise((resolve, reject) => {
  126. window.EOS.getCurrencyBalance('eosio.token', state.account.name, 'EOS').then(([ balance ]) => {
  127. let balanceNum = Number(balance.replace(/\sEOS/, '')).toFixed(4)
  128. balanceNum = NP.times(balanceNum, 10000)
  129. commit('setBalance', balanceNum)
  130. commit('setEOSError', 0)
  131. resolve()
  132. }).catch(error => {
  133. // 失败重新尝试,最大尝试5次
  134. commit('setEOSError', state.eosErrorCount + 1)
  135. if (state.eosErrorCount <= 5) {
  136. setTimeout(() => {
  137. dispatch('getUserEOS')
  138. }, 1000 * state.eosErrorCount)
  139. }
  140. reject(error)
  141. })
  142. })
  143. },
  144. /**
  145. * 更新用户GT币数量
  146. * @param {Object} store
  147. * @param {Object} params
  148. */
  149. getUserGT ({ dispatch, commit, state }, params) {
  150. return new Promise((resolve, reject) => {
  151. window.EOS.getCurrencyBalance('eosgetgtoken', state.account.name, 'GT').then(([ balance ]) => {
  152. let balanceNum = 0
  153. if (balance) {
  154. balanceNum = Number(balance.replace(/\sGT/, ''))
  155. }
  156. commit('setGt', balanceNum)
  157. commit('setGTError', 0)
  158. resolve()
  159. }).catch(error => {
  160. // 失败重新尝试,最大尝试5次
  161. commit('setGTError', state.gtErrorCount + 1)
  162. if (state.gtErrorCount <= 5) {
  163. setTimeout(() => {
  164. dispatch('getUserGT')
  165. }, 1000 * state.gtErrorCount)
  166. }
  167. reject(error)
  168. })
  169. })
  170. },
  171. /**
  172. * 获取用户信息
  173. * @param {Object} store
  174. * @param {Object} params
  175. */
  176. getAccount ({ commit, state }, params) {
  177. return new Promise((resolve, reject) => {
  178. window.EOS.getAccount(state.account.name).then(data => {
  179. // 设置CPU、NET使用率显示
  180. if (data.cpu_limit && data.net_limit) {
  181. commit('setLimit', {
  182. cpu: data.cpu_limit.used / data.cpu_limit.max,
  183. net: data.net_limit.used / data.net_limit.max
  184. })
  185. }
  186. // 设置公钥
  187. const perms = JSON.parse(JSON.stringify(data.permissions))
  188. perms.forEach(v => {
  189. if (v.perm_name === 'active') {
  190. let publicKey = v.required_auth.keys[0].key
  191. commit('setPublicKey', publicKey)
  192. }
  193. })
  194. resolve(data)
  195. }).catch(error => {
  196. reject(error)
  197. })
  198. })
  199. },
  200. /**
  201. * 进行验证身份登录,生成业务登录态token
  202. * @param {Object} store
  203. * @param {Object} params
  204. */
  205. async doGameLogin ({ dispatch, commit, state }, params) {
  206. if (!state.account || state.account === 'NO_LOGINING') {
  207. await dispatch('doScatterLogin')
  208. } else if (Cookie.getCookie('account') && Cookie.getCookie('token')) {
  209. return Promise.resolve()
  210. } else {
  211. let rData = await API.user.getRandom({ account: state.account.name })
  212. let random = rData.data.data
  213. let pubkey = state.publicKey
  214. return new Promise((resolve, reject) => {
  215. state.scatter.getArbitrarySignature(pubkey, random, 'Need Login').then(sign => {
  216. let param = {
  217. account: state.account.name,
  218. sign,
  219. pubkey,
  220. data: random
  221. }
  222. API.user.login(param).then(({ data }) => {
  223. if (data.result === 1) {
  224. let account = data.data.account
  225. let token = data.data.token
  226. Cookie.setCookie('account', account)
  227. Cookie.setCookie('token', token)
  228. resolve(data)
  229. } else {
  230. reject(new Error(data.msg))
  231. }
  232. })
  233. }).catch(error => {
  234. reject(error)
  235. })
  236. })
  237. }
  238. }
  239. }