index.vue 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. <template>
  2. <view class="message-page">
  3. <!-- 自定义顶栏:头像 + 组织名 + 搜索 -->
  4. <view class="custom-header">
  5. <view class="header-left" @click="onAvatarClick">
  6. <UserAvatar
  7. :name="currentUser.name"
  8. :id="currentUser.id"
  9. :src="currentUser.avatar"
  10. :size="80"
  11. unit="rpx"
  12. />
  13. <view class="org-info">
  14. <text class="user-name">{{ currentUser.name || '未设置' }}</text>
  15. <text class="org-name">{{ currentUser.orgName || '' }}</text>
  16. </view>
  17. </view>
  18. <view class="header-right">
  19. <view class="icon-btn" @click="onSearch">
  20. <image class="icon-img" src="/static/icons/search.svg" mode="aspectFit" />
  21. </view>
  22. </view>
  23. </view>
  24. <!-- 消息列表 -->
  25. <scroll-view
  26. class="message-list"
  27. scroll-y
  28. refresher-enabled
  29. :refresher-triggered="refresherTriggered"
  30. @refresherrefresh="onRefresh"
  31. >
  32. <block v-if="messageList && messageList.length">
  33. <view
  34. class="message-item"
  35. v-for="item in messageList"
  36. :key="item.id"
  37. @click="openChat(item)"
  38. >
  39. <view class="item-left">
  40. <view class="avatar-wrap">
  41. <SystemAvatar
  42. v-if="item.is_system || (item.app_id != null && item.app_id !== '')"
  43. :name="item.app_name || item.title"
  44. :size="96"
  45. unit="rpx"
  46. />
  47. <UserAvatar
  48. v-else
  49. :name="item.title"
  50. :id="item.id"
  51. :src="item.avatar"
  52. :size="96"
  53. unit="rpx"
  54. />
  55. <view v-if="item.unread" class="badge">{{ item.unread > 99 ? '99+' : item.unread }}</view>
  56. </view>
  57. <view class="item-content">
  58. <view class="item-row">
  59. <text class="item-title">{{ item.app_name || item.title }}</text>
  60. <text class="item-time">{{ item.time }}</text>
  61. </view>
  62. <text class="item-desc">{{ item.lastMessage }}</text>
  63. </view>
  64. </view>
  65. </view>
  66. </block>
  67. <view v-else-if="!hasToken" class="empty-tip" @click="goLogin">暂无会话,点击去登录</view>
  68. <view v-else-if="contactsFetchFailed" class="empty-tip" @click="retryFetch">请下拉刷新</view>
  69. <view v-else class="empty-tip">暂无消息</view>
  70. </scroll-view>
  71. </view>
  72. </template>
  73. <script>
  74. import { computed, onMounted, ref } from 'vue'
  75. import UserAvatar from '../../components/UserAvatar.vue'
  76. import SystemAvatar from '../../components/SystemAvatar.vue'
  77. import { useContacts } from '../../composables/useContacts'
  78. import { useWebSocket } from '../../composables/useWebSocket'
  79. import { fetchUnreadCountAndUpdateTabBar } from '../../composables/useUnreadBadge'
  80. import { chatStore } from '../../store/chat'
  81. import { getToken } from '../../utils/api'
  82. import { setupAppNotifications } from '../../utils/notificationSetup'
  83. const USER_KEY = 'current_user'
  84. function formatTime(str) {
  85. if (!str) return ''
  86. const d = new Date(str)
  87. if (isNaN(d.getTime())) return str
  88. const now = new Date()
  89. const isToday = d.toDateString() === now.toDateString()
  90. if (isToday) return d.getHours() + ':' + String(d.getMinutes()).padStart(2, '0')
  91. const yesterday = new Date(now)
  92. yesterday.setDate(yesterday.getDate() - 1)
  93. if (d.toDateString() === yesterday.toDateString()) return '昨天'
  94. return (d.getMonth() + 1) + '月' + d.getDate() + '日'
  95. }
  96. export default {
  97. components: { UserAvatar, SystemAvatar },
  98. setup() {
  99. const { fetchContacts } = useContacts()
  100. const { connect } = useWebSocket()
  101. const currentUser = ref({ name: '', id: '', avatar: '', orgName: '' })
  102. const refresherTriggered = ref(false)
  103. function loadCurrentUser() {
  104. try {
  105. const raw = uni.getStorageSync(USER_KEY)
  106. if (raw && typeof raw === 'object') {
  107. currentUser.value = {
  108. name: raw.name || raw.nickname || '',
  109. id: String(raw.id ?? raw.user_id ?? ''),
  110. avatar: raw.avatar || raw.avatar_url || '',
  111. orgName: raw.org_name || raw.orgName || ''
  112. }
  113. }
  114. } catch (e) {}
  115. }
  116. const messageList = computed(() => {
  117. return (chatStore.contacts || []).map((c) => ({
  118. ...c,
  119. time: formatTime(c.time),
  120. unread: Number(c.unread_count ?? c.unread ?? 0) || 0
  121. }))
  122. })
  123. function openChat(item) {
  124. const id = item.user_id ?? item.id
  125. uni.navigateTo({ url: '/pages/chat/index?otherUserId=' + encodeURIComponent(id) })
  126. }
  127. async function retryFetch() {
  128. await fetchContacts()
  129. await fetchUnreadCountAndUpdateTabBar()
  130. }
  131. async function onRefresh() {
  132. refresherTriggered.value = true
  133. try {
  134. await retryFetch()
  135. } finally {
  136. refresherTriggered.value = false
  137. }
  138. }
  139. onMounted(() => {
  140. loadCurrentUser()
  141. fetchContacts()
  142. fetchUnreadCountAndUpdateTabBar()
  143. connect()
  144. // #ifdef APP-PLUS
  145. setupAppNotifications()
  146. // #endif
  147. })
  148. const hasToken = computed(() => !!getToken())
  149. const contactsFetchFailed = computed(() => chatStore.contactsFetchFailed)
  150. function goLogin() {
  151. uni.reLaunch({ url: '/pages/login/index' })
  152. }
  153. return {
  154. messageList,
  155. hasToken,
  156. contactsFetchFailed,
  157. retryFetch,
  158. fetchContacts,
  159. fetchUnreadCountAndUpdateTabBar,
  160. connect,
  161. openChat,
  162. goLogin,
  163. currentUser,
  164. loadCurrentUser,
  165. refresherTriggered,
  166. onRefresh
  167. }
  168. },
  169. async onShow() {
  170. if (this.loadCurrentUser) this.loadCurrentUser()
  171. if (this.connect) this.connect()
  172. if (this.fetchContacts) await this.fetchContacts()
  173. if (this.fetchUnreadCountAndUpdateTabBar) await this.fetchUnreadCountAndUpdateTabBar()
  174. },
  175. async onTabItemTap() {
  176. if (this.fetchContacts) await this.fetchContacts()
  177. if (this.fetchUnreadCountAndUpdateTabBar) await this.fetchUnreadCountAndUpdateTabBar()
  178. },
  179. methods: {
  180. onAvatarClick() {
  181. uni.navigateTo({ url: '/pages/profile/index' })
  182. },
  183. onSearch() {
  184. uni.navigateTo({ url: '/pages/search-center/index' })
  185. }
  186. }
  187. }
  188. </script>
  189. <style scoped>
  190. .message-page {
  191. /* 高度占满视口,不再手动减 tabBar,高度保持 100vh 以保证 scroll-view 有明确高度 */
  192. height: 100vh;
  193. display: flex;
  194. flex-direction: column;
  195. background: #fff;
  196. }
  197. /* 顶部安全区:88rpx 为无安全区时的最小间距(安卓等),max 保证刘海/状态栏下也足够 */
  198. .custom-header {
  199. display: flex;
  200. align-items: center;
  201. justify-content: space-between;
  202. padding: 24rpx 24rpx 24rpx 32rpx;
  203. padding-top: 88rpx;
  204. padding-top: max(88rpx, calc(24rpx + constant(safe-area-inset-top)));
  205. padding-top: max(88rpx, calc(24rpx + env(safe-area-inset-top)));
  206. background: #fff;
  207. border-bottom: 1rpx solid #eee;
  208. }
  209. .header-left {
  210. display: flex;
  211. align-items: center;
  212. flex: 1;
  213. min-width: 0;
  214. }
  215. .avatar-wrap :deep(.user-avatar) {
  216. flex-shrink: 0;
  217. }
  218. .org-info {
  219. margin-left: 24rpx;
  220. display: flex;
  221. flex-direction: column;
  222. min-width: 0;
  223. }
  224. .user-name {
  225. font-size: 32rpx;
  226. font-weight: 600;
  227. color: #333;
  228. }
  229. .org-name {
  230. font-size: 24rpx;
  231. color: #999;
  232. margin-top: 4rpx;
  233. }
  234. .header-right {
  235. display: flex;
  236. align-items: center;
  237. gap: 24rpx;
  238. }
  239. .icon-btn {
  240. width: 64rpx;
  241. height: 64rpx;
  242. display: flex;
  243. align-items: center;
  244. justify-content: center;
  245. }
  246. .icon-img {
  247. width: 44rpx;
  248. height: 44rpx;
  249. opacity: 0.85;
  250. }
  251. .message-list {
  252. flex: 1;
  253. min-height: 0;
  254. height: 0;
  255. /* 使用框架提供的 tabBar 高度变量,为 H5 端等留出底部空间,避免与 tabBar 重叠 */
  256. padding-bottom: var(--window-bottom, 50px);
  257. box-sizing: border-box;
  258. }
  259. .empty-tip {
  260. padding: 80rpx 32rpx;
  261. text-align: center;
  262. font-size: 28rpx;
  263. color: #999;
  264. }
  265. .message-item {
  266. padding: 28rpx 32rpx;
  267. border-bottom: 1rpx solid #f0f0f0;
  268. }
  269. .item-left {
  270. display: flex;
  271. align-items: flex-start;
  272. }
  273. .avatar-wrap {
  274. position: relative;
  275. flex-shrink: 0;
  276. }
  277. .badge {
  278. position: absolute;
  279. top: -8rpx;
  280. right: -8rpx;
  281. min-width: 32rpx;
  282. height: 32rpx;
  283. line-height: 32rpx;
  284. padding: 0 8rpx;
  285. font-size: 20rpx;
  286. color: #fff;
  287. background: #f5222d;
  288. border-radius: 16rpx;
  289. text-align: center;
  290. }
  291. .item-content {
  292. flex: 1;
  293. margin-left: 24rpx;
  294. min-width: 0;
  295. }
  296. .item-row {
  297. display: flex;
  298. align-items: center;
  299. justify-content: space-between;
  300. margin-bottom: 8rpx;
  301. }
  302. .item-title {
  303. font-size: 30rpx;
  304. color: #333;
  305. flex: 1;
  306. overflow: hidden;
  307. text-overflow: ellipsis;
  308. white-space: nowrap;
  309. }
  310. .item-time {
  311. font-size: 24rpx;
  312. color: #999;
  313. flex-shrink: 0;
  314. margin-left: 16rpx;
  315. }
  316. .item-desc {
  317. font-size: 26rpx;
  318. color: #999;
  319. overflow: hidden;
  320. text-overflow: ellipsis;
  321. white-space: nowrap;
  322. display: block;
  323. }
  324. </style>