PrivateMessageBubble.vue 11 KB

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