// import { getStorage, setStorage, getBrowserLang } from "@/utils/utis";
|
import { Toast } from "vant";
|
import { _register, _info, perpetualContracts } from '@/API/user.api'
|
import { SET_USERINFO, GET_USERINFO, SET_OUT,CONNECT_WALLET, SET_STATUS, SET_CONFIG,GET_DAPPUSERINFO } from "@/store/const.store";
|
import { _getBalance } from '@/API/trade.api'
|
import i18n from "@/i18n";
|
export default {
|
namespaced: true,
|
state: {
|
status: 0, // 状态
|
gasObj: {}, //
|
userInfo: {
|
usercode: '', //
|
token: '', // 登录token
|
username: '',
|
perpetual_contracts_status: 0,
|
account: '', // 钱包地址
|
}
|
},
|
getters: {
|
mingStatus: state => state.status,
|
isToken: state => state.token,
|
userInfo: state => state.userInfo
|
},
|
mutations: {
|
// SET_GAS_OBJ(state,gasObj){
|
// state.gasObj=gasObj
|
// },
|
[SET_STATUS](state, status) { // 质押状态
|
state.status = status;
|
},
|
[SET_USERINFO](state, info) {
|
console.log('用户信息', info)
|
state.userInfo = { ...state.userInfo, ...info }
|
},
|
[SET_OUT](state) { // 退出
|
state.userInfo = {}
|
},
|
[SET_CONFIG](state, status) {
|
// console.log('statusstatus',status)
|
state.userInfo.perpetual_contracts_status = status
|
}
|
|
},
|
actions: {
|
// actionsGasObj({commit},data){
|
// commit("SET_GAS_OBJ", data);
|
// },
|
// actionsLogin({ commit }, data) {
|
// commit("SET_UESR_OBJ", data);
|
// },
|
async [GET_USERINFO]({ commit }, accounts) { // 发送请求获取信息
|
commit(SET_USERINFO, accounts) // 登陆
|
let data = await _info()
|
commit(SET_USERINFO, data) // 用户信息
|
data = await _getBalance()
|
commit(SET_USERINFO, { balance: data.money }) // 余额
|
},
|
|
async [SET_CONFIG]({ commit, state, }) {
|
const data = await perpetualContracts().catch(err => Promise.reject((err)))
|
// console.log('data222222222222',data)
|
let status = data.perpetual_contracts
|
commit(SET_CONFIG, status)
|
},
|
|
async [GET_DAPPUSERINFO]({ commit }, accounts) { // 发送请求获取信息
|
// console.log('accounts', accounts)
|
Toast.loading({ duration: 0, forbidClick: true })
|
commit(SET_USERINFO, { account: accounts[0] }) // 钱包地址
|
let data = await _register(accounts[0], localStorage.getItem('usercode'))
|
commit(SET_USERINFO, data) // 登陆
|
data = await _info()
|
commit(SET_USERINFO, data) // 用户信息
|
data = await _getBalance()
|
commit(SET_USERINFO, { balance: data.money }) // 余额
|
},
|
async [CONNECT_WALLET]({ commit, state, dispatch }, type) {
|
Toast.loading({ duration: 0, forbidClick: true })
|
setTimeout(async () => {
|
const eth = window.ethereum
|
if (eth) { // 钱包环境
|
const accounts = await eth.request({ method: "eth_requestAccounts" }).catch(err => {
|
const { code } = err
|
if (code === 4001) {
|
Toast.fail(i18n.t('您拒绝了链接'));
|
return;
|
} if (code === 'ECONNABORTED') {
|
this.$toast(this.$t('网络超时!'));
|
return;
|
}
|
})
|
if (accounts) {
|
if (!state.userInfo.token) {
|
await dispatch(GET_DAPPUSERINFO, accounts)
|
Toast.success(i18n.t('连接钱包成功'))
|
return false
|
} else {
|
if (type == 'connect') {
|
await dispatch(GET_DAPPUSERINFO, accounts)
|
Toast.success(i18n.t('连接钱包成功'))
|
} else {
|
Toast.success(i18n.t('刷新完成'))
|
}
|
}
|
// await dispatch(GET_DAPPUSERINFO, accounts)
|
// Toast.success(i18n.t('连接钱包成功'))
|
eth.on('accountsChanged', async (accounts) => { // 登录变了
|
commit(SET_OUT)
|
if (!accounts.length) {
|
Toast.fail(i18n.t('已断开钱包连接'))
|
} else { // 重新请求数据
|
await dispatch(GET_DAPPUSERINFO, accounts)
|
const data = await _checkStatus()
|
commit(SET_STATUS, data.status)
|
Toast.success(i18n.t('钱包切换成功'))
|
}
|
})
|
} else {
|
// Toast('连接钱包异常')
|
}
|
} else {
|
Toast.fail(i18n.t('请先安装钱包插件'))
|
}
|
}, 300);
|
}
|
},
|
};
|