PrivateMessageBubble.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  1. <template>
  2. <view class="message-row-wrap">
  3. <view v-if="showDateLabel" class="date-separator">
  4. <view class="date-line-cell">
  5. <view class="date-line" />
  6. </view>
  7. <view class="date-text-wrap">
  8. <text class="date-text">{{ dateLabel }}</text>
  9. </view>
  10. <view class="date-line-cell">
  11. <view class="date-line" />
  12. </view>
  13. </view>
  14. <view
  15. :id="'msg-' + (msg.id || msg.tempId)"
  16. class="message-row"
  17. :class="{ isMe: msg.isMe }"
  18. >
  19. <!-- 左侧:仅对方头像 -->
  20. <view class="side-left">
  21. <view v-if="!msg.isMe" class="avatar-box">
  22. <SystemAvatar
  23. v-if="isAppSession && !senderAvatar"
  24. :name="senderName"
  25. :size="64"
  26. unit="rpx"
  27. />
  28. <UserAvatar
  29. v-else
  30. :name="senderName"
  31. :id="senderId"
  32. :src="senderAvatar"
  33. :size="64"
  34. unit="rpx"
  35. />
  36. </view>
  37. </view>
  38. <!-- 中间:气泡 + 时间 -->
  39. <view class="center" :class="{ isMe: msg.isMe }">
  40. <!-- 用户通知:与系统通知一致的卡片(绿标题条 + 白底正文 + 蓝边框按钮) -->
  41. <view
  42. v-if="isUserNotification"
  43. class="bubble notify-card"
  44. >
  45. <view class="notify-header">
  46. <text class="notify-header-text">{{ msg.title || '通知' }}</text>
  47. </view>
  48. <view class="notify-body">
  49. <text v-if="msg.content" class="notify-content">{{ msg.content }}</text>
  50. </view>
  51. <!-- 接收方:可打开 callback;发送方:再次提醒(不展示 action 文案) -->
  52. <view v-if="!msg.isMe && (msg.actionText || msg.actionUrl)" class="notify-footer">
  53. <view class="notify-btn" @click="onOpenNotificationUrl">
  54. <text class="notify-btn-text">{{ msg.actionText || '打开应用' }}</text>
  55. </view>
  56. </view>
  57. <view v-if="msg.isMe && !isTempMessage" class="notify-footer">
  58. <view
  59. class="notify-btn"
  60. :class="{ 'is-disabled': remindLoading }"
  61. @click="onRemindClick"
  62. >
  63. <text class="notify-btn-text">{{ remindLoading ? '发送中...' : '再次提醒' }}</text>
  64. </view>
  65. </view>
  66. </view>
  67. <view
  68. v-else
  69. class="bubble"
  70. :class="{ 'is-sending': msg.status === 'sending' }"
  71. >
  72. <text v-if="msgType === 'TEXT'" class="bubble-text">{{ msg.content }}</text>
  73. <image
  74. v-else-if="msgType === 'IMAGE'"
  75. class="bubble-img"
  76. :src="msg.content"
  77. mode="widthFix"
  78. @click.stop="onPreviewImage"
  79. />
  80. <view
  81. v-else-if="msgType === 'VIDEO'"
  82. class="bubble-video-card"
  83. @click.stop="onOpenVideo"
  84. >
  85. <image
  86. class="bubble-video-icon"
  87. src="/static/icons/video.svg"
  88. mode="aspectFit"
  89. />
  90. <view class="bubble-video-meta">
  91. <text class="bubble-video-title">{{ msg.title || '视频' }}</text>
  92. <text class="bubble-video-hint">{{ canPlayVideo ? '点击下载并播放' : '暂无法播放' }}</text>
  93. </view>
  94. </view>
  95. <view v-else-if="msgType === 'FILE'" class="bubble-file" @click.stop="onOpenFile">
  96. <text class="file-name">{{ msg.title || '文件' }}</text>
  97. <text v-if="msg.actionUrl" class="action-btn" @click.stop="onOpenNotificationUrl">立即处理</text>
  98. <text v-else-if="canOpenRemoteFile" class="action-btn" @click.stop="onOpenFile">打开</text>
  99. </view>
  100. <view v-else class="bubble-file">
  101. <text class="file-name">{{ msg.title || msg.content || '消息' }}</text>
  102. <text v-if="msg.actionUrl" class="action-btn" @click.stop="onOpenNotificationUrl">立即处理</text>
  103. </view>
  104. <view v-if="msg.isMe && msg.status === 'sending' && uploadProgressText" class="upload-hint">
  105. <text>{{ uploadProgressText }}</text>
  106. </view>
  107. </view>
  108. <view v-if="msg.isMe && msg.status === 'failed'" class="msg-failed" @click.stop="onRetry">
  109. <text>发送失败,点击重试</text>
  110. </view>
  111. <view class="time-row">
  112. <text class="bubble-time">{{ timeText }}</text>
  113. </view>
  114. </view>
  115. <!-- 右侧:自己头像 -->
  116. <view v-if="msg.isMe" class="side-right">
  117. <view class="avatar-box">
  118. <UserAvatar
  119. :name="meName"
  120. :id="meId"
  121. :src="meAvatar"
  122. :size="64"
  123. unit="rpx"
  124. />
  125. </view>
  126. </view>
  127. <view v-else class="side-right placeholder" />
  128. </view>
  129. </view>
  130. </template>
  131. <script setup>
  132. import { computed } from 'vue'
  133. import UserAvatar from '../UserAvatar.vue'
  134. import SystemAvatar from '../SystemAvatar.vue'
  135. import { normalizeMessageContentType } from '../../utils/api'
  136. import { openVideoWithSystemPlayer } from '../../utils/openVideo'
  137. const props = defineProps({
  138. msg: {
  139. type: Object,
  140. required: true
  141. },
  142. senderName: {
  143. type: String,
  144. default: ''
  145. },
  146. senderId: { type: String, default: '' },
  147. senderAvatar: { type: String, default: '' },
  148. /** 应用/系统会话:无自定义头像时用 SystemAvatar,与消息列表、应用中心渐变一致 */
  149. isAppSession: { type: Boolean, default: false },
  150. meName: { type: String, default: '我' },
  151. meId: { type: String, default: '' },
  152. meAvatar: { type: String, default: '' },
  153. showDateLabel: {
  154. type: Boolean,
  155. default: false
  156. },
  157. /** 当前消息是否正在「再次提醒」发送中(用于按钮 loading / 防连点) */
  158. remindLoading: {
  159. type: Boolean,
  160. default: false
  161. }
  162. })
  163. const emit = defineEmits(['preview-image', 'open-notification-url', 'retry', 'remind-user-notification'])
  164. const isUserNotification = computed(
  165. () => normalizeMessageContentType(props.msg.contentType) === 'USER_NOTIFICATION'
  166. )
  167. const msgType = computed(() => normalizeMessageContentType(props.msg.contentType))
  168. const canOpenRemoteFile = computed(() => {
  169. const c = props.msg.content
  170. return !!(c && /^https?:\/\//i.test(String(c)))
  171. })
  172. /** 可下载并系统播放:远程 URL,或发送中/本地的临时路径 */
  173. const canPlayVideo = computed(() => {
  174. const c = props.msg.content
  175. if (!c) return false
  176. const s = String(c)
  177. if (/^https?:\/\//i.test(s)) return true
  178. if (props.msg.tempId || props.msg.status === 'sending') return true
  179. if (s.startsWith('/') || s.startsWith('file:') || /^[a-zA-Z]:\\/.test(s)) return true
  180. return false
  181. })
  182. const uploadProgressText = computed(() => {
  183. const m = props.msg
  184. if (m.status !== 'sending') return ''
  185. if (m.uploadProgress != null && m.uploadProgress > 0 && m.uploadProgress < 1) {
  186. return '上传中 ' + Math.round(m.uploadProgress * 100) + '%'
  187. }
  188. return '发送中…'
  189. })
  190. const isTempMessage = computed(() => {
  191. const m = props.msg
  192. return !!(m.tempId || (m.id != null && String(m.id).startsWith('temp-')))
  193. })
  194. const dateLabel = computed(() => {
  195. const t = props.msg.createdAt
  196. if (!t) return ''
  197. const d = new Date(t)
  198. return (d.getMonth() + 1) + '月' + d.getDate() + '日'
  199. })
  200. const timeText = computed(() => {
  201. const t = props.msg.createdAt
  202. if (!t) return ''
  203. const d = new Date(t)
  204. const h = d.getHours()
  205. const m = String(d.getMinutes()).padStart(2, '0')
  206. const month = d.getMonth() + 1
  207. const day = d.getDate()
  208. return month + '月' + day + '日 ' + h + ':' + m
  209. })
  210. function onPreviewImage() {
  211. if (props.msg.content) emit('preview-image', props.msg.content)
  212. }
  213. function onOpenVideo() {
  214. if (!canPlayVideo.value) {
  215. uni.showToast({ title: '暂无法播放', icon: 'none' })
  216. return
  217. }
  218. openVideoWithSystemPlayer(props.msg.content)
  219. }
  220. function onOpenFile() {
  221. const url = props.msg.content
  222. if (!url) return
  223. if (/^https?:\/\//i.test(String(url))) {
  224. uni.showLoading({ title: '加载中', mask: true })
  225. uni.downloadFile({
  226. url: String(url),
  227. success: (res) => {
  228. if (res.statusCode === 200 && res.tempFilePath) {
  229. uni.openDocument({
  230. filePath: res.tempFilePath,
  231. fail: () => {
  232. uni.showToast({ title: '无法打开此文件', icon: 'none' })
  233. }
  234. })
  235. } else {
  236. uni.showToast({ title: '下载失败', icon: 'none' })
  237. }
  238. },
  239. fail: () => {
  240. uni.showToast({ title: '下载失败', icon: 'none' })
  241. },
  242. complete: () => {
  243. uni.hideLoading()
  244. }
  245. })
  246. } else {
  247. uni.showToast({ title: '暂无法预览', icon: 'none' })
  248. }
  249. }
  250. function onOpenNotificationUrl() {
  251. emit('open-notification-url', props.msg)
  252. }
  253. function onRemindClick() {
  254. if (props.remindLoading) return
  255. emit('remind-user-notification', props.msg)
  256. }
  257. function onRetry() {
  258. if (props.msg.status === 'failed') emit('retry', props.msg)
  259. }
  260. </script>
  261. <style scoped>
  262. .message-row-wrap {
  263. width: 100%;
  264. margin-bottom: 28rpx;
  265. }
  266. .date-separator {
  267. display: flex;
  268. flex-direction: row;
  269. align-items: center;
  270. width: 100%;
  271. box-sizing: border-box;
  272. margin-bottom: 24rpx;
  273. }
  274. .date-line-cell {
  275. flex: 1 1 0%;
  276. min-width: 0;
  277. display: flex;
  278. align-items: center;
  279. }
  280. .date-line {
  281. width: 100%;
  282. height: 1rpx;
  283. background: rgba(148, 163, 184, 0.4);
  284. }
  285. .date-text-wrap {
  286. flex: 0 0 auto;
  287. max-width: 100%;
  288. margin: 0 20rpx;
  289. }
  290. .date-text {
  291. font-size: 24rpx;
  292. color: #9ca3af;
  293. padding: 6rpx 16rpx;
  294. border-radius: 999rpx;
  295. background: rgba(15, 23, 42, 0.04);
  296. }
  297. .message-row {
  298. display: flex;
  299. align-items: flex-start;
  300. max-width: 100%;
  301. overflow: hidden;
  302. }
  303. .message-row.isMe {
  304. justify-content: flex-end;
  305. }
  306. .side-left {
  307. flex-shrink: 0;
  308. width: 72rpx;
  309. display: flex;
  310. justify-content: center;
  311. align-items: flex-start;
  312. margin-right: 12rpx;
  313. }
  314. .message-row.isMe .side-left {
  315. order: 1;
  316. width: 40rpx;
  317. margin-right: 0;
  318. margin-left: 12rpx;
  319. }
  320. .avatar-box {
  321. width: 64rpx;
  322. height: 64rpx;
  323. display: flex;
  324. align-items: center;
  325. justify-content: center;
  326. }
  327. .center {
  328. display: flex;
  329. flex-direction: column;
  330. align-items: flex-start;
  331. max-width: 78%;
  332. }
  333. .center.isMe {
  334. align-items: flex-end;
  335. }
  336. .bubble {
  337. max-width: 100%;
  338. min-width: 88rpx;
  339. padding: 22rpx 26rpx;
  340. border-radius: 22rpx;
  341. background: #e5e7eb;
  342. box-shadow: 0 4rpx 10rpx rgba(15, 23, 42, 0.06);
  343. }
  344. .bubble.notify-card {
  345. width: 100%;
  346. max-width: 560rpx;
  347. min-width: 0;
  348. padding: 0;
  349. border-radius: 18rpx;
  350. background: #ffffff;
  351. overflow: hidden;
  352. }
  353. .message-row.isMe .bubble {
  354. background: #259653;
  355. }
  356. .message-row.isMe .bubble.notify-card {
  357. background: #ffffff;
  358. }
  359. .bubble-text {
  360. font-size: 28rpx;
  361. color: #111827;
  362. word-break: break-all;
  363. line-height: 1.5;
  364. }
  365. .message-row.isMe .bubble-text {
  366. color: #f9fafb;
  367. }
  368. .bubble-img {
  369. max-width: 420rpx;
  370. border-radius: 16rpx;
  371. display: block;
  372. box-shadow: 0 4rpx 10rpx rgba(15, 23, 42, 0.06);
  373. }
  374. .bubble-video-card {
  375. display: flex;
  376. align-items: center;
  377. gap: 20rpx;
  378. max-width: 420rpx;
  379. min-height: 100rpx;
  380. overflow: hidden;
  381. }
  382. .bubble-video-icon {
  383. width: 72rpx;
  384. height: 72rpx;
  385. flex-shrink: 0;
  386. opacity: 0.95;
  387. }
  388. .bubble-video-meta {
  389. flex: 1;
  390. min-width: 0;
  391. display: flex;
  392. flex-direction: column;
  393. gap: 8rpx;
  394. }
  395. .bubble-video-title {
  396. font-size: 28rpx;
  397. color: #111827;
  398. word-break: break-all;
  399. }
  400. .message-row.isMe .bubble-video-title {
  401. color: #f9fafb;
  402. }
  403. .bubble-video-hint {
  404. font-size: 24rpx;
  405. color: #6b7280;
  406. }
  407. .message-row.isMe .bubble-video-hint {
  408. color: rgba(249, 250, 251, 0.75);
  409. }
  410. .bubble.is-sending {
  411. opacity: 0.92;
  412. }
  413. .upload-hint {
  414. margin-top: 12rpx;
  415. font-size: 22rpx;
  416. color: #9ca3af;
  417. }
  418. .message-row.isMe .upload-hint {
  419. color: rgba(249, 250, 251, 0.85);
  420. }
  421. .msg-failed {
  422. margin-top: 6rpx;
  423. font-size: 26rpx;
  424. color: #fecaca;
  425. }
  426. .bubble-file .file-name {
  427. display: block;
  428. font-size: 28rpx;
  429. color: #111827;
  430. }
  431. .message-row.isMe .bubble-file .file-name {
  432. color: #f9fafb;
  433. }
  434. .action-btn {
  435. display: inline-block;
  436. margin-top: 12rpx;
  437. font-size: 26rpx;
  438. color: #259653;
  439. }
  440. .notify-header {
  441. background-color: #bbf7d0;
  442. padding: 16rpx 20rpx;
  443. }
  444. .notify-header-text {
  445. font-size: 26rpx;
  446. color: #166534;
  447. font-weight: 600;
  448. }
  449. .notify-body {
  450. padding: 20rpx 20rpx 8rpx;
  451. }
  452. .notify-content {
  453. font-size: 26rpx;
  454. color: #111827;
  455. line-height: 1.5;
  456. word-break: break-all;
  457. }
  458. .notify-footer {
  459. padding: 12rpx 20rpx 20rpx;
  460. }
  461. .notify-btn {
  462. width: 100%;
  463. height: 72rpx;
  464. border-radius: 12rpx;
  465. border-width: 2rpx;
  466. border-style: solid;
  467. border-color: #1677ff;
  468. background-color: #ffffff;
  469. display: flex;
  470. align-items: center;
  471. justify-content: center;
  472. }
  473. .notify-btn-text {
  474. font-size: 28rpx;
  475. color: #1677ff;
  476. }
  477. .notify-btn.is-disabled {
  478. opacity: 0.65;
  479. pointer-events: none;
  480. }
  481. .side-right {
  482. flex-shrink: 0;
  483. width: 72rpx;
  484. display: flex;
  485. justify-content: center;
  486. align-items: flex-start;
  487. margin-left: 12rpx;
  488. }
  489. .message-row.isMe .side-right {
  490. width: auto;
  491. max-width: 120rpx;
  492. }
  493. .side-right.placeholder {
  494. width: 0;
  495. max-width: 0;
  496. margin-left: 0;
  497. }
  498. .time-row {
  499. margin-top: 6rpx;
  500. max-width: 100%;
  501. }
  502. .bubble-time {
  503. font-size: 22rpx;
  504. color: #9ca3af;
  505. }
  506. </style>