From 27cd042df9aca0383a49f3514bc21958dd890912 Mon Sep 17 00:00:00 2001
From: gaoluyang <2820782392@qq.com>
Date: 星期一, 29 六月 2026 15:42:23 +0800
Subject: [PATCH] 银川 1.联调产品维护页面 2.添加IM即时通讯模块

---
 src/views/im/home/store/channelStore.ts |  108 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 108 insertions(+), 0 deletions(-)

diff --git a/src/views/im/home/store/channelStore.ts b/src/views/im/home/store/channelStore.ts
new file mode 100644
index 0000000..3f059ca
--- /dev/null
+++ b/src/views/im/home/store/channelStore.ts
@@ -0,0 +1,108 @@
+import type { ImManagerChannelApi } from '#/api/im/manager/channel'
+
+import { acceptHMRUpdate, defineStore } from 'pinia'
+
+import { getSimpleChannelList } from '#/api/im/manager/channel'
+
+import { ImConversationType } from '../../utils/constants'
+import { getDb } from '../../utils/db'
+import { useConversationStore } from './conversationStore'
+
+/**
+ * IM 棰戦亾 Store
+ *
+ * 璐熻矗锛氱紦瀛樺綋鍓嶇敤鎴疯兘鐪嬪埌鐨勯閬撶簿绠�鍒楄〃锛堝惈 id / code / name / avatar锛夛紝
+ * 渚涗細璇濆垪琛� / 鍗$墖娓叉煋鏃跺弽鏌ラ閬撳悕绉� + 澶村儚锛岄伩鍏嶆樉绀恒�岄閬� 1銆嶈繖绉嶅崰浣�
+ */
+export const useChannelStore = defineStore('imChannelStore', {
+  state: () => ({
+    channels: [] as ImManagerChannelApi.Channel[],
+    loaded: false
+  }),
+
+  getters: {
+    getChannel(state): (id: number) => ImManagerChannelApi.Channel | undefined {
+      return (id: number) => state.channels.find((c) => c.id === id)
+    }
+  },
+
+  actions: {
+    // ==================== 鏈湴缂撳瓨 ====================
+
+    /** 浠� IndexedDB 鎭㈠棰戦亾鍒楄〃 */
+    async loadChannelList(): Promise<boolean> {
+      try {
+        const cached = await getDb().getAll<ImManagerChannelApi.Channel>('channels')
+        if (!cached || cached.length === 0) {
+          return false
+        }
+        this.channels = cached
+        return true
+      } catch (error) {
+        console.warn('[IM channelStore] 鏈湴棰戦亾缂撳瓨璇诲彇澶辫触', error)
+        return false
+      }
+    },
+
+    /** 淇濆瓨棰戦亾鍒楄〃 */
+    saveChannelList(): void {
+      void getDb()
+        .transaction(['channels'], 'readwrite', async (tx) => {
+          const db = getDb()
+          await db.clearStore('channels', tx)
+          for (const channel of this.channels) {
+            await db.put('channels', channel, tx)
+          }
+        })
+        .catch((error) => console.warn('[IM channelStore] 鏈湴棰戦亾缂撳瓨鍐欏叆澶辫触', error))
+    },
+
+    // ==================== 杩滅鎷夊彇 ====================
+
+    /** 鎷夊彇鍚敤鐨勯閬撶簿绠�鍒楄〃锛涙垚鍔熷悗鍥炲~浼氳瘽鍒楄〃宸叉湁鐨勯閬� name / avatar锛岃鐩� IDB 鏃у崰浣� */
+    async fetchChannelList(force = false) {
+      if (this.loaded && !force) {
+        return
+      }
+      try {
+        this.channels = (await getSimpleChannelList()) || []
+        this.loaded = true
+        this.syncChannelConversationMetadata()
+        this.saveChannelList()
+      } catch (error) {
+        console.warn('[IM channelStore] fetchChannelList 澶辫触', error)
+      }
+    },
+
+    /** 鐢ㄦ渶鏂扮殑棰戦亾淇℃伅瑕嗙洊宸叉湁 CHANNEL 浼氳瘽鐨� name / avatar */
+    syncChannelConversationMetadata() {
+      const conversationStore = useConversationStore()
+      const indexed = new Map(this.channels.map((c) => [c.id, c]))
+      conversationStore.conversations.forEach((conversation) => {
+        if (conversation.type !== ImConversationType.CHANNEL) {
+          return
+        }
+        const channel = indexed.get(conversation.targetId)
+        if (!channel) {
+          return
+        }
+        conversationStore.updateConversation(ImConversationType.CHANNEL, conversation.targetId, {
+          name: channel.name,
+          avatar: channel.avatar
+        })
+      })
+    },
+
+    /** 娓呯┖棰戦亾鍐呭瓨 */
+    clear() {
+      this.channels = []
+      this.loaded = false
+    }
+  }
+})
+
+if (import.meta.hot) {
+  import.meta.hot.accept(acceptHMRUpdate(useChannelStore, import.meta.hot))
+}
+
+export const useChannelStoreWithOut = () => useChannelStore()

--
Gitblit v1.9.3