liming 1 mēnesi atpakaļ
vecāks
revīzija
b5576b7b56

+ 2 - 0
src/i18n/zhHk/login.js

@@ -18,4 +18,6 @@ export default {
 
   BackupComplete: "備份完成",
   CopyMnemonic: "複製助記詞",
+
+  CopiedToClipboard: "已複製到剪貼板",
 };

+ 55 - 53
src/stores/modules/systemStore.js

@@ -4,20 +4,22 @@ import { setLocalStorage, getLocalStorage } from "@/utils";
 
 const INITIALIZE = {
   local_loading: false,
-  layout: "menu",
-  menus: "",
   routeSate: "",
   user_login_information: "",
   stsClientInfo: {},
+  wallet: {
+    address: "",
+    privateKey: "",
+  },
 };
 try {
-  const SYSTEM_STORE = getLocalStorage("SYSTEM_STORE")
+  const SYSTEM_STORE = getLocalStorage("SYSTEM_STORE");
   for (const key in SYSTEM_STORE) {
-    const value = getLocalStorage(key)
-    if (value) INITIALIZE[key] = value
+    const value = getLocalStorage(key);
+    if (value) INITIALIZE[key] = value;
   }
 } catch (err) {
-  window.console.log('无存储数据', err)
+  window.console.log("无存储数据", err);
 }
 
 export const useSystemStore = defineStore("useSystemStore", {
@@ -26,87 +28,87 @@ export const useSystemStore = defineStore("useSystemStore", {
   }),
   getters: {
     getLocalLoading() {
-      return this.local_loading
+      return this.local_loading;
     },
     getToken() {
       if (this.token) return this.token;
       this.token = window.localStorage?.token || "";
       return this.token;
     },
-    getLayout() {
-      return this.layout;
+    getWallet() {
+      if (this.wallet.address) return this.wallet;
+      this.wallet = window.localStorage?.wallet || "";
+      return this.wallet;
     },
     getRouteSate() {
-      return this.routeSate || getLocalStorage("routeSate")
-    },
-    getMenus() {
-      if (this.menus) return this.menus;
-      this.menus = window.localStorage?.menus || "";
-      return this.menus;
+      return this.routeSate || getLocalStorage("routeSate");
     },
     getSTSClient() {
       const stsFn = async () => {
-        const windowVar = window
-        let longTime = 0
-        if (windowVar.stsOSSClient && windowVar.stsOSSClient.stsTokenFreshTime) {
-          const oldTime = windowVar.stsOSSClient.stsTokenFreshTime.getTime()
-          const newTime = new Date().getTime()
-          longTime = newTime - oldTime
+        const windowVar = window;
+        let longTime = 0;
+        if (
+          windowVar.stsOSSClient &&
+          windowVar.stsOSSClient.stsTokenFreshTime
+        ) {
+          const oldTime = windowVar.stsOSSClient.stsTokenFreshTime.getTime();
+          const newTime = new Date().getTime();
+          longTime = newTime - oldTime;
         }
 
         // STS信息
         if (!windowVar.stsOSSClient || longTime > 10 * 60 * 1000) {
-          const res = await getSTSInfo({})
+          const res = await getSTSInfo({});
 
-          const info = res.data
-          const systemStore = useSystemStore()
-          const getInfoData = info => {
+          const info = res.data;
+          const systemStore = useSystemStore();
+          const getInfoData = (info) => {
             const infoData = {
-              endpoint: 'https://flexi.oss-cn-hongkong.aliyuncs.com',
-              region: 'oss-cn-hongkong',
+              endpoint: "https://flexi.oss-cn-hongkong.aliyuncs.com",
+              region: "oss-cn-hongkong",
               accessKeyId: info.AccessKeyId,
               accessKeySecret: info.AccessKeySecret,
               stsToken: info.SecurityToken,
-              bucket: 'flexi',
-              cname: true
-            }
-            this.stsClientInfo = infoData
-            systemStore.setStateValue('stsClientInfo', infoData)
-            return infoData
-          }
-          getInfoData(info)
+              bucket: "flexi",
+              cname: true,
+            };
+            this.stsClientInfo = infoData;
+            systemStore.setStateValue("stsClientInfo", infoData);
+            return infoData;
+          };
+          getInfoData(info);
 
           const client = new windowVar.OSS({
             ...this.stsClientInfo,
             refreshSTSToken: async () => {
-              const res = await getSTSInfo({})
-              getInfoData(res.data)
+              const res = await getSTSInfo({});
+              getInfoData(res.data);
               return {
                 accessKeyId: info.AccessKeyId,
                 accessKeySecret: info.AccessKeySecret,
-                stsToken: info.SecurityToken
-              }
+                stsToken: info.SecurityToken,
+              };
             },
-            refreshSTSTokenInterval: 30 * 60 * 1000
-          })
+            refreshSTSTokenInterval: 30 * 60 * 1000,
+          });
 
-          client.putObject = async data => {
-            const putRes = await client.put(data.key, data.body)
-            putRes.statusCode = 200
-            return putRes
-          }
-          windowVar.stsOSSClient = client
+          client.putObject = async (data) => {
+            const putRes = await client.put(data.key, data.body);
+            putRes.statusCode = 200;
+            return putRes;
+          };
+          windowVar.stsOSSClient = client;
 
-          return client
+          return client;
         }
-        return windowVar.stsOSSClient
+        return windowVar.stsOSSClient;
       };
-      return stsFn
-    }
+      return stsFn;
+    },
   },
   actions: {
     localLoading(value = false) {
-      this.local_loading = value
+      this.local_loading = value;
     },
     // 储存
     setStateValue(res) {
@@ -117,6 +119,6 @@ export const useSystemStore = defineStore("useSystemStore", {
         // 此方法只处理了简单数据类型 如字符串(token)、布尔之类的
         window.localStorage.setItem(res.key, value);
       }
-    }
+    },
   },
 });

+ 22 - 2
src/views/login/backupMnemonic/index.vue

@@ -8,23 +8,43 @@
 
      <div class="button-group"  >
 
-      <a class="button-group-mnemonic  a-link" > {{  $t('login.CopyMnemonic')  }}</a>
+      <a class="button-group-mnemonic  a-link" @click="copyText" > {{  $t('login.CopyMnemonic')  }}</a>
       <van-button round block type="primary" native-type="submit">
         {{ $t("login.BackupComplete") }}
       </van-button>
     </div>
+
   </div>
 </template>
 
 <script setup>
+import { Clipboard } from '@capacitor/clipboard';
 import { createAccent } from "@/api/path/login.api";
+import { useSystemStore } from "@/stores/modules/systemStore";
+
+
+const systemStore = useSystemStore();
+
+const copyText = async () => {
+  await Clipboard.write({
+    string: walletData.value.words
+  }); 
+  $msg({type: 'success', message: $t("login.CopiedToClipboard")});
+};
 
 const walletData = ref({})
 
 onMounted(async () => {
   const { data } = await createAccent({});
   walletData.value =   JSON.parse(atob(data.content) ) 
-  console.log(data.value.words)
+  systemStore.setStateValue({
+    key: "wallet",
+    value: {
+      address: walletData.value.address1,
+      privateKey: walletData.value.privateKey,
+    },
+    localStorage: true,
+  });
 })
 </script>
 

+ 101 - 0
src/wallet/login.js

@@ -0,0 +1,101 @@
+import Web3 from "web3";
+
+
+// web3-wallet.js
+class Web3Wallet {
+  constructor() {
+    this.web3 = null;
+    this.account = null;
+    this.chainId = null;
+    
+    // 绑定方法到当前实例
+    this.isWalletInstalled = this.isWalletInstalled.bind(this);
+    this.connectWallet = this.connectWallet.bind(this);
+    this.getAccount = this.getAccount.bind(this);
+    this.getChainId = this.getChainId.bind(this);
+    this.getWeb3 = this.getWeb3.bind(this);
+    this.onAccountsChanged = this.onAccountsChanged.bind(this);
+    this.onChainChanged = this.onChainChanged.bind(this);
+    this.disconnect = this.disconnect.bind(this);
+  }
+
+  // 检查是否安装了钱包
+  isWalletInstalled() {
+    return typeof window.ethereum !== "undefined";
+  }
+
+  // 连接钱包
+  async connectWallet() {
+    if (!this.isWalletInstalled()) {
+      throw new Error("未检测到钱包扩展,请安装MetaMask或其他兼容钱包");
+    }
+
+    try {
+      // 请求账户访问权限
+      const accounts = await window.ethereum.request({
+        method: "eth_requestAccounts",
+      });
+
+      // 获取当前链ID
+      const chainId = await window.ethereum.request({
+        method: "eth_chainId",
+      });
+
+      this.account = accounts[0];
+      this.chainId = parseInt(chainId, 16);
+      this.web3 = new Web3(window.ethereum);
+
+      return {
+        account: this.account,
+        chainId: this.chainId,
+        web3: this.web3,
+      };
+    } catch (error) {
+      console.error("连接钱包失败:", error);
+      throw error;
+    }
+  }
+
+  // 获取当前账户
+  getAccount() {
+    return this.account;
+  }
+
+  // 获取当前链ID
+  getChainId() {
+    return this.chainId;
+  }
+
+  // 获取Web3实例
+  getWeb3() {
+    return this.web3;
+  }
+
+  // 监听账户变化
+  onAccountsChanged(callback) {
+    if (!this.isWalletInstalled()) return;
+
+    window.ethereum.on("accountsChanged", callback);
+  }
+
+  // 监听链变化
+  onChainChanged(callback) {
+    if (!this.isWalletInstalled()) return;
+
+    window.ethereum.on("chainChanged", (chainId) => {
+      callback(parseInt(chainId, 16));
+    });
+  }
+
+  // 断开连接
+  disconnect() {
+    this.account = null;
+    this.chainId = null;
+    this.web3 = null;
+  }
+}
+
+// 创建单例实例
+const web3Wallet = new Web3Wallet();
+
+export default web3Wallet;