messageDetail.vue 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <template>
  2. <view class="messageDetail">
  3. <GlobalLoading />
  4. <h3 class="detailTit">{{ messageDateils.noticeTitle }}</h3>
  5. <view v-if="messageDateils.createTime" class="detailTime">时间:{{ messageDateils.createTime }}</view>
  6. <view class="detailInfo">
  7. <rich-text :nodes="htmlData"></rich-text>
  8. <img :src="messageDateils.image" alt="">
  9. </view>
  10. </view>
  11. </template>
  12. <script>
  13. import parse from 'mini-html-parser2'
  14. import GlobalLoading from '../../components/diyLoading'
  15. const NET = require('../../utils/request')
  16. const API = require('../../config/api')
  17. export default {
  18. name: 'MessageDetail',
  19. components: { GlobalLoading },
  20. data() {
  21. return {
  22. onlyid: 0,
  23. messageDateils: {},
  24. htmlData: []
  25. }
  26. },
  27. onShow() {
  28. this.getNotice()
  29. },
  30. onLoad(options) {
  31. this.onlyid = options.noticeId
  32. this.getNotice()
  33. },
  34. methods: {
  35. formatRichText(html) {
  36. let newContent = html.replace(/<img[^>]*>/gi, function (match, capture) {
  37. match = match.replace(/style="[^"]+"/gi, '').replace(/style='[^']+'/gi, '')
  38. match = match.replace(/width="[^"]+"/gi, '').replace(/width='[^']+'/gi, '')
  39. match = match.replace(/height="[^"]+"/gi, '').replace(/height='[^']+'/gi, '')
  40. return match
  41. })
  42. newContent = newContent.replace(/style="[^"]+"/gi, function (match, capture) {
  43. match = match.replace(/width:[^;]+;/gi, 'max-width:100%;').replace(
  44. /width:[^;]+;/gi,
  45. 'max-width:100%;'
  46. )
  47. return match
  48. })
  49. newContent = newContent.replace(/<br[^>]*\/>/gi, '')
  50. newContent = newContent.replace(
  51. /\<img/gi,
  52. '<img style="max-width:100%;height:auto;display:block;margin-top:0;margin-bottom:0;"'
  53. )
  54. return newContent
  55. },
  56. gitMassageDateils() {
  57. NET.request(API.getMessageDateils, {
  58. noticeId: this.onlyid
  59. }, 'GET').then((res) => {
  60. this.messageDateils = res.data
  61. this.messageDateils.noticeContent = this.formatRichText(this.messageDateils.noticeContent)
  62. parse(this.messageDateils.noticeContent, (err, htmlData) => {
  63. this.htmlData = htmlData
  64. })
  65. uni.hideLoading()
  66. })
  67. .catch((res) => {
  68. uni.hideLoading()
  69. uni.showToast({
  70. title: '失败',
  71. icon: 'none'
  72. })
  73. })
  74. },
  75. getNotice() {
  76. // uni.showLoading({
  77. // title: '加载中...',
  78. // })
  79. NET.request(API.readNotice, {
  80. noticeId: this.onlyid
  81. }, 'POST').then((res) => {
  82. uni.hideLoading()
  83. this.gitMassageDateils()
  84. })
  85. .catch((res) => {
  86. uni.hideLoading()
  87. uni.showToast({
  88. title: '失败',
  89. icon: 'none'
  90. })
  91. })
  92. }
  93. }
  94. }
  95. </script>
  96. <style lang="scss" scoped>
  97. .messageDetail {
  98. padding: 30upx;
  99. h3 {
  100. font-size: 36upx;
  101. color: #333333;
  102. margin-bottom: 40upx;
  103. }
  104. .detailTime {
  105. font-size: 28upx;
  106. color: #666666;
  107. margin-bottom: 50upx;
  108. }
  109. .detailInfo {
  110. word-wrap: break-word;
  111. p {
  112. font-size: 28upx;
  113. color: #333333;
  114. line-height: 48upx;
  115. text-indent: 1em;
  116. margin-bottom: 50upx;
  117. }
  118. img {
  119. width: 100%;
  120. }
  121. }
  122. }
  123. </style>