index.vue 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <template>
  2. <view class="detail-page">
  3. <view v-if="detail" class="detail">
  4. <view v-if="title" class="detail-title">
  5. {{ title }}
  6. </view>
  7. <view v-if="createTime || author" class="detail-meta">
  8. <view v-if="createTime">{{ createTime }}</view>
  9. <view v-if="author" class="detail-author">{{ author }}</view>
  10. </view>
  11. <view v-if="drugName" class="detail-drug"> 关联药品:{{ drugName }} </view>
  12. <view v-if="content" class="detail-content">
  13. <MpHtml :content="content" />
  14. </view>
  15. </view>
  16. <view v-else class="empty">暂无内容</view>
  17. </view>
  18. </template>
  19. <script setup lang="ts">
  20. import { computed, ref } from 'vue'
  21. import { onLoad } from '@dcloudio/uni-app'
  22. import MpHtml from 'mp-html/dist/uni-app/components/mp-html/mp-html'
  23. import { getAlbumByIdApi, getArticleByIdApi } from '@/services/modules/information'
  24. import type { AlbumPageNewItem, ArticleItem } from '@/services/modules/information/type'
  25. type DetailType = 'academic' | 'album'
  26. type DetailItem = ArticleItem | AlbumPageNewItem
  27. interface PageQuery {
  28. id?: string
  29. type?: DetailType
  30. }
  31. const detail = ref<DetailItem>()
  32. const detailType = ref<DetailType>('academic')
  33. const isArticle = computed(() => detailType.value === 'academic')
  34. const articleDetail = computed(() => detail.value as ArticleItem | undefined)
  35. const albumDetail = computed(() => detail.value as AlbumPageNewItem | undefined)
  36. const title = computed(() => {
  37. return isArticle.value ? articleDetail.value?.title : albumDetail.value?.xcmc
  38. })
  39. const createTime = computed(() => detail.value?.createTime?.slice(0, 10) || '')
  40. const author = computed(() => {
  41. return isArticle.value ? articleDetail.value?.author : albumDetail.value?.createUser || ''
  42. })
  43. const drugName = computed(() => {
  44. if (isArticle.value) return ''
  45. return albumDetail.value?.glypName || albumDetail.value?.glyp || ''
  46. })
  47. const content = computed(() => {
  48. const html = isArticle.value ? articleDetail.value?.content : albumDetail.value?.wb
  49. return formatRichText(html || '')
  50. })
  51. onLoad((query) => {
  52. const { id, type = 'academic' } = query as PageQuery
  53. if (!id) return
  54. detailType.value = type
  55. void getDetail(id)
  56. })
  57. const getDetail = async (id: string): Promise<void> => {
  58. try {
  59. const { data } =
  60. detailType.value === 'academic' ? await getArticleByIdApi(id) : await getAlbumByIdApi(id)
  61. detail.value = data
  62. } catch (error) {
  63. console.error('get detail failed:', error)
  64. uni.showToast({
  65. title: '详情加载失败',
  66. icon: 'none',
  67. })
  68. }
  69. }
  70. const formatRichText = (html: string): string => {
  71. return html
  72. .replace(
  73. /<img\b(?![^>]*\bstyle=)/gi,
  74. '<img style="max-width:100%;height:auto;display:block;margin:20rpx 0;"'
  75. )
  76. .replace(
  77. /<video\b(?![^>]*\bstyle=)/gi,
  78. '<video style="width:100%;height:auto;display:block;margin:20rpx 0;"'
  79. )
  80. }
  81. </script>
  82. <style lang="scss" scoped>
  83. .detail-page {
  84. min-height: 100vh;
  85. background-color: #ffffff;
  86. }
  87. .detail {
  88. padding: 36rpx 30rpx 60rpx;
  89. }
  90. .detail-title {
  91. color: #222222;
  92. font-size: 42rpx;
  93. font-weight: 600;
  94. line-height: 1.45;
  95. }
  96. .detail-meta {
  97. margin-top: 18rpx;
  98. color: #999999;
  99. font-size: 26rpx;
  100. line-height: 1.5;
  101. }
  102. .detail-author {
  103. margin-top: 24rpx;
  104. }
  105. .detail-drug {
  106. margin-top: 24rpx;
  107. padding: 18rpx 20rpx;
  108. border-left: 6rpx solid #3b82f6;
  109. border-radius: 8rpx;
  110. background-color: #f5f8ff;
  111. color: #4b5563;
  112. font-size: 26rpx;
  113. line-height: 1.5;
  114. }
  115. .detail-content {
  116. margin-top: 34rpx;
  117. color: #333333;
  118. font-size: 31rpx;
  119. line-height: 1.8;
  120. word-break: break-word;
  121. }
  122. .empty {
  123. padding: 120rpx 30rpx;
  124. color: #999999;
  125. font-size: 28rpx;
  126. text-align: center;
  127. }
  128. </style>