ソースを参照

V1.0.2修复消息列表刷新问题

liuq 2 週間 前
コミット
441c54f682
3 ファイル変更32 行追加13 行削除
  1. 12 5
      composables/useContacts.js
  2. 17 8
      pages/index/index.vue
  3. 3 0
      store/chat.js

+ 12 - 5
composables/useContacts.js

@@ -4,14 +4,19 @@
 import { getContacts, getToken } from '../utils/api'
 import { chatStore } from '../store/chat'
 
-/** 供页面与 WS 等模块直接拉取会话列表(与 useContacts().fetchContacts 一致) */
-export async function fetchContactsList() {
+/**
+ * 拉取会话列表
+ * @param {{ silent?: boolean }} [opts] - silent 为 false 时会置 loadingContacts(默认 true:刷新不闪屏、不挡列表)
+ */
+export async function fetchContactsList(opts = {}) {
+	const silent = opts.silent !== false
 	const token = getToken()
 	if (!token) {
 		chatStore.contacts = []
+		chatStore.contactsFetchFailed = false
 		return
 	}
-	chatStore.loadingContacts = true
+	if (!silent) chatStore.loadingContacts = true
 	try {
 		const data = await getContacts(token)
 		const list = Array.isArray(data) ? data : (data.list || data.items || data.conversations || [])
@@ -35,10 +40,12 @@ export async function fetchContactsList() {
 				}
 			})
 		)
+		chatStore.contactsFetchFailed = false
 	} catch (e) {
-		// 网络或接口失败:不覆盖已有会话列表
+		// 网络或接口失败:不覆盖已有会话列表;空列表时用于展示「请下拉刷新」
+		chatStore.contactsFetchFailed = true
 	} finally {
-		chatStore.loadingContacts = false
+		if (!silent) chatStore.loadingContacts = false
 	}
 }
 

+ 17 - 8
pages/index/index.vue

@@ -29,10 +29,8 @@
 			:refresher-triggered="refresherTriggered"
 			@refresherrefresh="onRefresh"
 		>
-			<view v-if="loadingContacts" class="empty-tip">加载中...</view>
-			<view v-else-if="!messageList || messageList.length === 0" class="empty-tip" @click="goLogin">暂无会话,点击去登录</view>
+			<block v-if="messageList && messageList.length">
 			<view
-				v-else
 				class="message-item"
 				v-for="item in messageList"
 				:key="item.id"
@@ -65,6 +63,10 @@
 					</view>
 				</view>
 			</view>
+			</block>
+			<view v-else-if="!hasToken" class="empty-tip" @click="goLogin">暂无会话,点击去登录</view>
+			<view v-else-if="contactsFetchFailed" class="empty-tip" @click="retryFetch">请下拉刷新</view>
+			<view v-else class="empty-tip">暂无消息</view>
 		</scroll-view>
 	</view>
 </template>
@@ -77,6 +79,7 @@
 	import { useWebSocket } from '../../composables/useWebSocket'
 	import { fetchUnreadCountAndUpdateTabBar } from '../../composables/useUnreadBadge'
 	import { chatStore } from '../../store/chat'
+	import { getToken } from '../../utils/api'
 	import { setupAppNotifications } from '../../utils/notificationSetup'
 
 	const USER_KEY = 'current_user'
@@ -109,7 +112,6 @@
 						currentUser.value = {
 							name: raw.name || raw.nickname || '',
 							id: String(raw.id ?? raw.user_id ?? ''),
-							id: String(raw.id ?? raw.user_id ?? ''),
 							avatar: raw.avatar || raw.avatar_url || '',
 							orgName: raw.org_name || raw.orgName || ''
 						}
@@ -130,11 +132,15 @@
 				uni.navigateTo({ url: '/pages/chat/index?otherUserId=' + encodeURIComponent(id) })
 			}
 
+			async function retryFetch() {
+				await fetchContacts()
+				await fetchUnreadCountAndUpdateTabBar()
+			}
+
 			async function onRefresh() {
 				refresherTriggered.value = true
 				try {
-					await fetchContacts()
-					await fetchUnreadCountAndUpdateTabBar()
+					await retryFetch()
 				} finally {
 					refresherTriggered.value = false
 				}
@@ -150,7 +156,8 @@
 				// #endif
 			})
 
-			const loadingContacts = computed(() => chatStore.loadingContacts)
+			const hasToken = computed(() => !!getToken())
+			const contactsFetchFailed = computed(() => chatStore.contactsFetchFailed)
 
 			function goLogin() {
 				uni.reLaunch({ url: '/pages/login/index' })
@@ -158,7 +165,9 @@
 
 			return {
 				messageList,
-				loadingContacts,
+				hasToken,
+				contactsFetchFailed,
+				retryFetch,
 				fetchContacts,
 				fetchUnreadCountAndUpdateTabBar,
 				connect,

+ 3 - 0
store/chat.js

@@ -7,6 +7,8 @@ export const chatStore = reactive({
 	// 会话列表(来自 getContacts)
 	contacts: [],
 	loadingContacts: false,
+	/** 最近一次拉取会话列表是否因网络/接口失败(用于空列表时区分「请下拉刷新」与「暂无消息」) */
+	contactsFetchFailed: false,
 
 	// 按 contactId 存消息列表
 	messages: {},
@@ -21,6 +23,7 @@ export const chatStore = reactive({
 	reset() {
 		this.contacts = []
 		this.loadingContacts = false
+		this.contactsFetchFailed = false
 		this.messages = {}
 		this.loadedContactIds = {}
 		this.activeContactId = ''