PrivateMessageBubble.vue 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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="msg.contentType === 'USER_NOTIFICATION'"
  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 v-else class="bubble">
  55. <text v-if="msg.contentType === 'TEXT'" class="bubble-text">{{ msg.content }}</text>
  56. <image
  57. v-else-if="msg.contentType === 'IMAGE'"
  58. class="bubble-img"
  59. :src="msg.content"
  60. mode="widthFix"
  61. @click="onPreviewImage"
  62. />
  63. <view v-else class="bubble-file">
  64. <text class="file-name">{{ msg.title || '文件' }}</text>
  65. <text v-if="msg.actionUrl" class="action-btn" @click="onOpenNotificationUrl">立即处理</text>
  66. </view>
  67. </view>
  68. <view class="time-row">
  69. <text class="bubble-time">{{ timeText }}</text>
  70. </view>
  71. </view>
  72. <!-- 右侧:自己头像 -->
  73. <view v-if="msg.isMe" class="side-right">
  74. <view class="avatar-box">
  75. <UserAvatar
  76. :name="meName"
  77. :id="meId"
  78. :src="meAvatar"
  79. :size="64"
  80. unit="rpx"
  81. />
  82. </view>
  83. </view>
  84. <view v-else class="side-right placeholder" />
  85. </view>
  86. </view>
  87. </template>
  88. <script setup>
  89. import { computed } from 'vue'
  90. import UserAvatar from '../UserAvatar.vue'
  91. const props = defineProps({
  92. msg: {
  93. type: Object,
  94. required: true
  95. },
  96. senderName: {
  97. type: String,
  98. default: ''
  99. },
  100. senderId: { type: String, default: '' },
  101. senderAvatar: { type: String, default: '' },
  102. meName: { type: String, default: '我' },
  103. meId: { type: String, default: '' },
  104. meAvatar: { type: String, default: '' },
  105. showDateLabel: {
  106. type: Boolean,
  107. default: false
  108. },
  109. /** 当前消息是否正在「再次提醒」发送中(用于按钮 loading / 防连点) */
  110. remindLoading: {
  111. type: Boolean,
  112. default: false
  113. }
  114. })
  115. const emit = defineEmits(['preview-image', 'open-notification-url', 'retry', 'remind-user-notification'])
  116. const isTempMessage = computed(() => {
  117. const m = props.msg
  118. return !!(m.tempId || (m.id != null && String(m.id).startsWith('temp-')))
  119. })
  120. const dateLabel = computed(() => {
  121. const t = props.msg.createdAt
  122. if (!t) return ''
  123. const d = new Date(t)
  124. return (d.getMonth() + 1) + '月' + d.getDate() + '日'
  125. })
  126. const timeText = computed(() => {
  127. const t = props.msg.createdAt
  128. if (!t) return ''
  129. const d = new Date(t)
  130. const h = d.getHours()
  131. const m = String(d.getMinutes()).padStart(2, '0')
  132. const month = d.getMonth() + 1
  133. const day = d.getDate()
  134. return month + '月' + day + '日 ' + h + ':' + m
  135. })
  136. function onPreviewImage() {
  137. if (props.msg.content) emit('preview-image', props.msg.content)
  138. }
  139. function onOpenNotificationUrl() {
  140. emit('open-notification-url', props.msg)
  141. }
  142. function onRemindClick() {
  143. if (props.remindLoading) return
  144. emit('remind-user-notification', props.msg)
  145. }
  146. function onRetry() {
  147. if (props.msg.status === 'failed') emit('retry', props.msg)
  148. }
  149. </script>
  150. <style scoped>
  151. .message-row-wrap {
  152. margin-bottom: 28rpx;
  153. }
  154. .date-separator {
  155. display: flex;
  156. align-items: center;
  157. justify-content: center;
  158. gap: 20rpx;
  159. margin-bottom: 24rpx;
  160. }
  161. .date-line {
  162. flex: 1;
  163. height: 1rpx;
  164. background: rgba(148, 163, 184, 0.4);
  165. }
  166. .date-text {
  167. font-size: 24rpx;
  168. color: #9ca3af;
  169. flex-shrink: 0;
  170. padding: 6rpx 16rpx;
  171. border-radius: 999rpx;
  172. background: rgba(15, 23, 42, 0.04);
  173. }
  174. .message-row {
  175. display: flex;
  176. align-items: flex-start;
  177. max-width: 100%;
  178. overflow: hidden;
  179. }
  180. .message-row.isMe {
  181. justify-content: flex-end;
  182. }
  183. .side-left {
  184. flex-shrink: 0;
  185. width: 72rpx;
  186. display: flex;
  187. justify-content: center;
  188. align-items: flex-start;
  189. margin-right: 12rpx;
  190. }
  191. .message-row.isMe .side-left {
  192. order: 1;
  193. width: 40rpx;
  194. margin-right: 0;
  195. margin-left: 12rpx;
  196. }
  197. .avatar-box {
  198. width: 64rpx;
  199. height: 64rpx;
  200. display: flex;
  201. align-items: center;
  202. justify-content: center;
  203. }
  204. .center {
  205. display: flex;
  206. flex-direction: column;
  207. align-items: flex-start;
  208. max-width: 78%;
  209. }
  210. .center.isMe {
  211. align-items: flex-end;
  212. }
  213. .bubble {
  214. max-width: 100%;
  215. min-width: 88rpx;
  216. padding: 22rpx 26rpx;
  217. border-radius: 22rpx;
  218. background: #e5e7eb;
  219. box-shadow: 0 4rpx 10rpx rgba(15, 23, 42, 0.06);
  220. }
  221. .bubble.notify-card {
  222. width: 100%;
  223. max-width: 560rpx;
  224. min-width: 0;
  225. padding: 0;
  226. border-radius: 18rpx;
  227. background: #ffffff;
  228. overflow: hidden;
  229. }
  230. .message-row.isMe .bubble {
  231. background: #259653;
  232. }
  233. .message-row.isMe .bubble.notify-card {
  234. background: #ffffff;
  235. }
  236. .bubble-text {
  237. font-size: 28rpx;
  238. color: #111827;
  239. word-break: break-all;
  240. line-height: 1.5;
  241. }
  242. .message-row.isMe .bubble-text {
  243. color: #f9fafb;
  244. }
  245. .bubble-img {
  246. max-width: 420rpx;
  247. border-radius: 16rpx;
  248. display: block;
  249. box-shadow: 0 4rpx 10rpx rgba(15, 23, 42, 0.06);
  250. }
  251. .bubble-file .file-name {
  252. display: block;
  253. font-size: 28rpx;
  254. color: #111827;
  255. }
  256. .message-row.isMe .bubble-file .file-name {
  257. color: #f9fafb;
  258. }
  259. .action-btn {
  260. display: inline-block;
  261. margin-top: 12rpx;
  262. font-size: 26rpx;
  263. color: #259653;
  264. }
  265. .notify-header {
  266. background-color: #bbf7d0;
  267. padding: 16rpx 20rpx;
  268. }
  269. .notify-header-text {
  270. font-size: 26rpx;
  271. color: #166534;
  272. font-weight: 600;
  273. }
  274. .notify-body {
  275. padding: 20rpx 20rpx 8rpx;
  276. }
  277. .notify-content {
  278. font-size: 26rpx;
  279. color: #111827;
  280. line-height: 1.5;
  281. word-break: break-all;
  282. }
  283. .notify-footer {
  284. padding: 12rpx 20rpx 20rpx;
  285. }
  286. .notify-btn {
  287. width: 100%;
  288. height: 72rpx;
  289. border-radius: 12rpx;
  290. border-width: 2rpx;
  291. border-style: solid;
  292. border-color: #1677ff;
  293. background-color: #ffffff;
  294. display: flex;
  295. align-items: center;
  296. justify-content: center;
  297. }
  298. .notify-btn-text {
  299. font-size: 28rpx;
  300. color: #1677ff;
  301. }
  302. .notify-btn.is-disabled {
  303. opacity: 0.65;
  304. pointer-events: none;
  305. }
  306. .side-right {
  307. flex-shrink: 0;
  308. width: 72rpx;
  309. display: flex;
  310. justify-content: center;
  311. align-items: flex-start;
  312. margin-left: 12rpx;
  313. }
  314. .message-row.isMe .side-right {
  315. width: auto;
  316. max-width: 120rpx;
  317. }
  318. .side-right.placeholder {
  319. width: 0;
  320. max-width: 0;
  321. margin-left: 0;
  322. }
  323. .time-row {
  324. margin-top: 6rpx;
  325. max-width: 100%;
  326. }
  327. .bubble-time {
  328. font-size: 22rpx;
  329. color: #9ca3af;
  330. }
  331. </style>