index.vue 8.5 KB

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