index.vue 8.8 KB

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