From 823c5727df00d3181592201cfbf7fd91d4b20054 Mon Sep 17 00:00:00 2001 From: huangjinming Date: Fri, 10 Mar 2023 09:55:18 +0800 Subject: [PATCH] fix --- postcss.config.cjs | 2 +- src/api/User.js | 1 + src/chain/ChainManager.ts | 3 ++ src/chain/blockchain.ts | 1 + src/components/layout/NavBar.vue | 16 +++--- src/store/app.js | 1 + src/store/user.js | 91 +++++++++++++++++++------------- src/utils/cookies.ts | 5 ++ 8 files changed, 76 insertions(+), 44 deletions(-) diff --git a/postcss.config.cjs b/postcss.config.cjs index c331733..546d6d3 100644 --- a/postcss.config.cjs +++ b/postcss.config.cjs @@ -7,7 +7,7 @@ module.exports = { propList: ['*'], // 能转化为vw的属性列表 viewportUnit: 'vw', // 希望使用的视口单位 fontViewportUnit: 'vw', // 字体使用的视口单位 - selectorBlackList: [], // 需要忽略的CSS选择器,不会转为视口单位,使用原有的px等单位。 + selectorBlackList: ['.bg-middle'], // 需要忽略的CSS选择器,不会转为视口单位,使用原有的px等单位。 minPixelValue: 1, // 设置最小的转换数值,如果为1的话,只有大于1的值会被转换 mediaQuery: false, // 媒体查询里的单位是否需要转换单位 replace: true, // 是否直接更换属性值,而不添加备用属性 diff --git a/src/api/User.js b/src/api/User.js index 15f8abb..88d4510 100644 --- a/src/api/User.js +++ b/src/api/User.js @@ -43,6 +43,7 @@ export const logout = () => // }) export const getNonce = (params) => + request({ url: '/webapp/index.php?c=Market&a=getNonce', method: 'get', diff --git a/src/chain/ChainManager.ts b/src/chain/ChainManager.ts index a44dd82..e08046b 100644 --- a/src/chain/ChainManager.ts +++ b/src/chain/ChainManager.ts @@ -62,6 +62,7 @@ export default class ChainManager { } public async login() { + if (!AppModule.step) { try { await this.bc.connect(true) @@ -75,6 +76,7 @@ export default class ChainManager { public async checkNance() { try { + let nonce = AppModule.nonce if (!nonce) { const preRequest: any = await getNonce({ @@ -101,6 +103,7 @@ export default class ChainManager { public async getNance() { console.log('need get nance') try { + const preRequest: any = await getNonce({ account: AppModule.accountId, net_id: AppModule.chainId diff --git a/src/chain/blockchain.ts b/src/chain/blockchain.ts index b146efc..42c6964 100644 --- a/src/chain/blockchain.ts +++ b/src/chain/blockchain.ts @@ -100,6 +100,7 @@ export class Blockchain { this.subscribeToEvents() const accounts = await this.web3.eth.getAccounts() if (accounts && accounts.length > 0) { + AppModule.updateAccount(accounts[0]) } if (!this.currentChain) this.currentChain = chainId diff --git a/src/components/layout/NavBar.vue b/src/components/layout/NavBar.vue index e1b37bf..e897ec2 100644 --- a/src/components/layout/NavBar.vue +++ b/src/components/layout/NavBar.vue @@ -66,17 +66,21 @@ function click(event) { } const formatAddress = computed(() => { - if (AppModule.accountId.length >= 10) { + console.log(AppModule.accountId.value,'AppModule.accountId'); + if(!AppModule.accountId.value){ + return '-' + } + if (AppModule.accountId.value.length >= 10) { return ( - AppModule.accountId.substring(0, 6) + + AppModule.accountId.value.substring(0, 6) + "......" + - AppModule.accountId.substring(AppModule.accountId.length - 4) + AppModule.accountId.value.substring(AppModule.accountId.value.length - 4) ); } else if ( - AppModule.accountId.length > 0 && - AppModule.accountId.length < 10 + AppModule.accountId.value.length > 0 && + AppModule.accountId.value.length < 10 ) { - return AppModule.accountId; + return AppModule.accountId.value } else { return "-"; } diff --git a/src/store/app.js b/src/store/app.js index ab21253..446df50 100644 --- a/src/store/app.js +++ b/src/store/app.js @@ -27,6 +27,7 @@ export const useAppStore = defineStore('app', () => { } function updateAccount(_accountId) { + accountId.value = _accountId; } function updateChainID(_chainId) { diff --git a/src/store/user.js b/src/store/user.js index cdf5062..0502f01 100644 --- a/src/store/user.js +++ b/src/store/user.js @@ -1,76 +1,93 @@ -import { defineStore } from 'pinia'; -import { ref } from 'vue'; -import { getToken, setToken, removeToken } from '@/utils/cookies'; -import glodata from './user_global'; +import { defineStore } from "pinia"; +import { ref } from "vue"; +import { + getToken, + setToken, + removeToken, + getAccountId, + setAccountId, + removeAccountId +} from "@/utils/cookies"; +import glodata from "./user_global"; -import pinia from '@/store' -import { useAppStore } from '@/store/app' +import pinia from "@/store"; +import { useAppStore } from "@/store/app"; const AppModule = useAppStore(pinia); glodata.token = getToken(); -glodata.accountId = AppModule.accountId; -glodata.chainId = AppModule.chainId; -import { login } from '@/api/User'; - -export const useUserStore = defineStore('user', () => { +glodata.accountId = AppModule.accountId.value; +glodata.chainId = AppModule.chainId.value; +import { login } from "@/api/User"; +export const useUserStore = defineStore("user", () => { const EIP721_DOMAIN_DATA = [ - { name: 'name', type: 'string' }, - { name: 'version', type: 'string' } + { name: "name", type: "string" }, + { name: "version", type: "string" }, ]; const token = ref(getToken()); + const accountId = ref(getAccountId()); + AppModule.updateAccount(accountId); + if(token.value && accountId.value){ + + AppModule.updateStep(1); + } - async function Login({bcInstance, account, chainId, nonce}) { - nonce += '' - const tips = 'This signature is only used for verify your account' + async function Login({ bcInstance, account, chainId, nonce }) { + nonce += ""; + const tips = "This signature is only used for verify your account"; const signMsg = { tips, - nonce - } + nonce, + }; const signObj = { types: { EIP712Domain: EIP721_DOMAIN_DATA, set: [ - { name: 'tips', type: 'string' }, - { name: 'nonce', type: 'string' } - ] + { name: "tips", type: "string" }, + { name: "nonce", type: "string" }, + ], }, - primaryType: 'set', + primaryType: "set", domain: { - name: 'Auth', - version: '1' + name: "Auth", + version: "1", }, - message: signMsg - } - const signature = await bcInstance.signData(signObj, account) + message: signMsg, + }; + const signature = await bcInstance.signData(signObj, account); const authData = { account, nonce, signature, tips, - net_id: chainId - } - console.log('login data: ', authData) - const res = await login(authData) + net_id: chainId, + }; + console.log("login data: ", authData); + const res = await login(authData); if (!res.errcode && res.token) { - setToken(res.token) + setToken(res.token); + console.log(res, "resres"); + setAccountId(account); AppModule.updateToken(res.token); token.value = res.token; } } function Logout() { - if (token==='') { + if (token === "") { return; } + removeAccountId() removeToken(); - token.value = ''; + token.value = ""; + accountId.value='' + AppModule.updateAccount('') } - return { + return { token, Login, Logout, - } -}) + }; +}); diff --git a/src/utils/cookies.ts b/src/utils/cookies.ts index a0104c3..f3f8048 100644 --- a/src/utils/cookies.ts +++ b/src/utils/cookies.ts @@ -14,6 +14,11 @@ export const setMini = (mini: string) => Cookies.set(miniKey, mini) // User const tokenKey = 'vue_typescript_access_token' +const accountIdKey = 'vue_typescript_access_accountId' export const getToken = () => localStorage.getItem(tokenKey) export const setToken = (token: string) => localStorage.setItem(tokenKey, token) export const removeToken = () => localStorage.removeItem(tokenKey) + +export const getAccountId = () => localStorage.getItem(accountIdKey) +export const setAccountId = (accountId: string) => localStorage.setItem(accountIdKey, accountId) +export const removeAccountId = () => localStorage.removeItem(accountIdKey) \ No newline at end of file