index.vue 10 KB

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