交易所前端蓝色ui, 4.5 jiem
jhzh
2024-04-08 67357c691325dc3cf743267f1ef0e1f5c93d7528
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// 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);
    }
  },
};