detail.vue 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. <template>
  2. <view class="notification-detail-page">
  3. <view v-if="loading" class="loading-state">
  4. <wd-loading size="36rpx" />
  5. <text class="loading-text">加载中</text>
  6. </view>
  7. <view v-else-if="!msgDetail" class="empty-state">
  8. <wd-empty icon="no-content" tip="暂无通知内容" />
  9. </view>
  10. <view v-else class="detail-card">
  11. <view class="detail-header">
  12. <view class="icon-box">
  13. <wd-icon name="notification" size="24px" />
  14. </view>
  15. <view class="header-content">
  16. <text class="title">{{ msgDetail.title || '消息通知' }}</text>
  17. <text v-if="msgDetail.sendTime" class="time">{{ msgDetail.sendTime }}</text>
  18. </view>
  19. </view>
  20. <view class="divider" />
  21. <view class="content">
  22. <!-- #ifdef MP-WEIXIN -->
  23. <rich-text v-if="safeContent" class="rich-content" space="nbsp" :nodes="safeContent" />
  24. <!-- #endif -->
  25. <!-- #ifndef MP-WEIXIN -->
  26. <rich-text v-if="safeContent" class="rich-content" :nodes="safeContent" />
  27. <!-- #endif -->
  28. <view v-else class="empty-content">
  29. <text>暂无内容</text>
  30. </view>
  31. </view>
  32. </view>
  33. </view>
  34. </template>
  35. <script setup lang="ts">
  36. import { computed, ref } from 'vue'
  37. import { onLoad } from '@dcloudio/uni-app'
  38. import { internalMsgReadApi } from '@/services/modules/mine/notification'
  39. import type { NotificationRecord } from '@/services/modules/mine/notification/type'
  40. const loading = ref(false)
  41. const msgDetail = ref<NotificationRecord | null>(null)
  42. const safeContent = computed(() => {
  43. return formatRichTextContent(msgDetail.value?.content ?? '')
  44. })
  45. const formatRichTextContent = (html: string): string => {
  46. if (!html) return ''
  47. return html
  48. .replace(/<style[\s\S]*?<\/style>/gi, '')
  49. .replace(/<script[\s\S]*?<\/script>/gi, '')
  50. .replace(/<img\b([^>]*)>/gi, (_match: string, attrs: string) => {
  51. const normalizedAttrs = normalizeImageAttrs(attrs)
  52. return `<img${normalizedAttrs} />`
  53. })
  54. }
  55. const normalizeImageAttrs = (attrs: string): string => {
  56. const attrsWithoutSize = attrs
  57. .replace(/\swidth=(['"]).*?\1/gi, '')
  58. .replace(/\sheight=(['"]).*?\1/gi, '')
  59. .replace(/\sstyle=(['"]).*?\1/gi, '')
  60. const attrsWithClass = appendImageClass(attrsWithoutSize)
  61. return `${attrsWithClass} style="max-width:100%;height:auto;display:block;margin:16px auto;"`
  62. }
  63. const appendImageClass = (attrs: string): string => {
  64. const classMatch = attrs.match(/\sclass=(['"])(.*?)\1/i)
  65. if (!classMatch) {
  66. return `${attrs} class="rich-content__image"`
  67. }
  68. const quote = classMatch[1]
  69. const className = classMatch[2]
  70. if (className.split(/\s+/).includes('rich-content__image')) {
  71. return attrs
  72. }
  73. return attrs.replace(
  74. /\sclass=(['"])(.*?)\1/i,
  75. ` class=${quote}${className} rich-content__image${quote}`
  76. )
  77. }
  78. const getMsgDetail = async (id: number): Promise<void> => {
  79. loading.value = true
  80. try {
  81. const res = await internalMsgReadApi({
  82. id,
  83. sendType: 2,
  84. })
  85. msgDetail.value = res.data
  86. } finally {
  87. loading.value = false
  88. }
  89. }
  90. onLoad((options) => {
  91. const id = Number(options?.id)
  92. if (!Number.isFinite(id) || id <= 0) {
  93. uni.showToast({
  94. title: '消息参数错误',
  95. icon: 'none',
  96. })
  97. return
  98. }
  99. void getMsgDetail(id)
  100. })
  101. </script>
  102. <style lang="scss" scoped>
  103. .notification-detail-page {
  104. min-height: 100vh;
  105. box-sizing: border-box;
  106. padding: 24rpx;
  107. background: #f6f7fb;
  108. }
  109. .detail-card {
  110. box-sizing: border-box;
  111. width: 100%;
  112. padding: 32rpx;
  113. background: #ffffff;
  114. border: 1rpx solid rgba(230, 234, 242, 0.95);
  115. border-radius: 28rpx;
  116. box-shadow: 0 10rpx 30rpx rgba(15, 23, 42, 0.045);
  117. }
  118. .detail-header {
  119. display: flex;
  120. align-items: flex-start;
  121. }
  122. .icon-box {
  123. flex: none;
  124. display: flex;
  125. align-items: center;
  126. justify-content: center;
  127. width: 72rpx;
  128. height: 72rpx;
  129. margin-right: 22rpx;
  130. color: #2f80ed;
  131. background: #eef5ff;
  132. border-radius: 22rpx;
  133. }
  134. .header-content {
  135. flex: 1;
  136. min-width: 0;
  137. }
  138. .title {
  139. display: block;
  140. color: #111827;
  141. font-size: 34rpx;
  142. font-weight: 700;
  143. line-height: 48rpx;
  144. word-break: break-word;
  145. }
  146. .time {
  147. display: block;
  148. margin-top: 10rpx;
  149. color: #98a2b3;
  150. font-size: 24rpx;
  151. line-height: 34rpx;
  152. }
  153. .divider {
  154. width: 100%;
  155. height: 1rpx;
  156. margin: 30rpx 0;
  157. background: #eef0f4;
  158. }
  159. .content {
  160. color: #374151;
  161. font-size: 30rpx;
  162. line-height: 1.8;
  163. word-break: break-word;
  164. }
  165. .rich-content {
  166. display: block;
  167. width: 100%;
  168. color: #374151;
  169. font-size: 30rpx;
  170. line-height: 1.8;
  171. word-break: break-word;
  172. overflow-wrap: break-word;
  173. }
  174. .empty-content {
  175. display: flex;
  176. align-items: center;
  177. justify-content: center;
  178. min-height: 220rpx;
  179. color: #98a2b3;
  180. font-size: 28rpx;
  181. }
  182. .loading-state,
  183. .empty-state {
  184. display: flex;
  185. align-items: center;
  186. justify-content: center;
  187. min-height: 72vh;
  188. }
  189. .loading-state {
  190. gap: 12rpx;
  191. }
  192. .loading-text {
  193. color: #98a2b3;
  194. font-size: 26rpx;
  195. }
  196. :deep(.rich-content__image) {
  197. max-width: 100%;
  198. height: auto;
  199. display: block;
  200. }
  201. </style>