+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ 取消
+ 添加
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/im-web/src/utils/accountManager.js b/im-web/src/utils/accountManager.js
new file mode 100644
index 0000000..34e6282
--- /dev/null
+++ b/im-web/src/utils/accountManager.js
@@ -0,0 +1,94 @@
+// utils/accountManager.js
+const ACCOUNT_LIST_KEY = 'IM_ACCOUNT_LIST';
+const MAX_ACCOUNT_COUNT = 5;
+
+class AccountManager {
+ // 获取所有保存的账号
+ static getAccountList() {
+ try {
+ const list = localStorage.getItem(ACCOUNT_LIST_KEY);
+ return list ? JSON.parse(list) : [];
+ } catch (e) {
+ console.error('获取账号列表失败', e);
+ return [];
+ }
+ }
+
+ // 保存账号列表
+ static saveAccountList(accounts) {
+ try {
+ localStorage.setItem(ACCOUNT_LIST_KEY, JSON.stringify(accounts));
+ } catch (e) {
+ console.error('保存账号列表失败', e);
+ }
+ }
+
+ // 保存或更新账号信息
+ static saveAccount(accountInfo) {
+ if (!accountInfo || !accountInfo.account) return;
+
+ let accounts = this.getAccountList();
+ const existingIndex = accounts.findIndex(item => item.account === accountInfo.account);
+
+ const newAccount = {
+ account: accountInfo.account,
+ nickName: accountInfo.nickName || accountInfo.account,
+ headImage: accountInfo.headImage || '',
+ headImageThumb: accountInfo.headImageThumb || '',
+ lastLoginTime: new Date().getTime(),
+ userId: accountInfo.userId || accountInfo.id
+ };
+
+ if (existingIndex >= 0) {
+ accounts[existingIndex] = newAccount;
+ } else {
+ accounts.unshift(newAccount);
+ }
+
+ if (accounts.length > MAX_ACCOUNT_COUNT) {
+ accounts = accounts.slice(0, MAX_ACCOUNT_COUNT);
+ }
+
+ this.saveAccountList(accounts);
+ }
+
+ // 删除账号
+ static removeAccount(account) {
+ let accounts = this.getAccountList();
+ accounts = accounts.filter(item => item.account !== account);
+ this.saveAccountList(accounts);
+ }
+
+ // 获取其他账号
+ static getOtherAccounts(currentAccount) {
+ const accounts = this.getAccountList();
+ return accounts.filter(item => item.account !== currentAccount);
+ }
+
+ // 设置要切换的账号
+ static setSwitchAccount(account) {
+ sessionStorage.setItem('switch_account', account);
+ }
+
+ // 清除切换账号标记
+ static clearSwitchAccount() {
+ sessionStorage.removeItem('switch_account');
+ }
+
+ // 格式化时间
+ static formatTime(timestamp) {
+ if (!timestamp) return '';
+ const date = new Date(timestamp);
+ const now = new Date();
+ const diff = now - date;
+
+ if (diff < 60000) return '刚刚';
+ if (diff < 3600000) return Math.floor(diff / 60000) + '分钟前';
+ if (diff < 86400000) return Math.floor(diff / 3600000) + '小时前';
+ if (diff < 604800000) return Math.floor(diff / 86400000) + '天前';
+
+ return `${date.getMonth() + 1}月${date.getDate()}日`;
+ }
+}
+
+export default AccountManager;
\ No newline at end of file