index.vue 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <template>
  2. <view class="agreement-detail-page">
  3. <nav-bar :title="title"></nav-bar>
  4. <view class="content-card" v-if="!loading">
  5. <text class="title">{{ agreementData.title || title }}</text>
  6. <rich-text :nodes="contentHtml" class="rich-content"></rich-text>
  7. </view>
  8. <view v-else class="loading-state">
  9. <text>协议内容加载中...</text>
  10. </view>
  11. </view>
  12. </template>
  13. <script setup>
  14. // @Author: Antigravity
  15. import { ref } from 'vue'
  16. import { onLoad } from '@dcloudio/uni-app'
  17. import { getAgreement } from '@/api/system/agreement'
  18. import navBar from '@/components/nav-bar/index.vue'
  19. const title = ref('协议详情')
  20. const agreementData = ref({})
  21. const contentHtml = ref('')
  22. const loading = ref(true)
  23. onLoad(async (options) => {
  24. if (options.title) title.value = decodeURIComponent(options.title)
  25. if (options.id) {
  26. fetchAgreement(options.id)
  27. }
  28. })
  29. const fetchAgreement = async (id) => {
  30. loading.value = true
  31. try {
  32. const res = await getAgreement(id)
  33. if (res) {
  34. agreementData.value = res
  35. // 解码 Base64 内容 (Admin端采用 btoa(unescape(encodeURIComponent(content))) 编码)
  36. if (res.content) {
  37. const cleanStr = String(res.content).trim().replace(/\s+/g, '')
  38. // 检查是否是 base64 格式(仅包含 A-Z, a-z, 0-9, +, /, =)
  39. const isBase64 = /^[A-Za-z0-9+/=]+$/.test(cleanStr)
  40. if (isBase64) {
  41. try {
  42. // 1. Base64 解码为二进制字符串
  43. const atobPolyfill = (str) => {
  44. if (typeof atob === 'function') return atob(str)
  45. const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/='
  46. let output = ''
  47. const clean = str.replace(/=+$/, '')
  48. for (let bc = 0, bs, buffer, idx = 0; idx < clean.length; ) {
  49. const char = clean.charAt(idx++)
  50. const buffer = chars.indexOf(char)
  51. if (buffer === -1) throw new Error('Invalid base64 character')
  52. bs = bc % 4 ? bs * 64 + buffer : buffer
  53. if (bc++ % 4) {
  54. output += String.fromCharCode(255 & bs >> (-2 * bc & 6))
  55. }
  56. }
  57. return output
  58. }
  59. const decodedBinary = atobPolyfill(cleanStr)
  60. // 2. 将二进制字符串解码为 UTF-8 字符串
  61. const decodeUtf8 = (str) => {
  62. let result = ''
  63. let i = 0
  64. while (i < str.length) {
  65. const c1 = str.charCodeAt(i++)
  66. if (c1 < 128) {
  67. result += String.fromCharCode(c1)
  68. } else if (c1 > 191 && c1 < 224) {
  69. const c2 = str.charCodeAt(i++)
  70. result += String.fromCharCode(((c1 & 31) << 6) | (c2 & 63))
  71. } else if (c1 > 223 && c1 < 240) {
  72. const c2 = str.charCodeAt(i++)
  73. const c3 = str.charCodeAt(i++)
  74. result += String.fromCharCode(((c1 & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63))
  75. } else {
  76. const c2 = str.charCodeAt(i++)
  77. const c3 = str.charCodeAt(i++)
  78. const c4 = str.charCodeAt(i++)
  79. const codepoint = ((c1 & 7) << 18) | ((c2 & 63) << 12) | ((c3 & 63) << 6) | (c4 & 63)
  80. if (codepoint > 0xffff) {
  81. const offset = codepoint - 0x10000
  82. result += String.fromCharCode((offset >> 10) + 0xd800, (offset & 0x3ff) + 0xdc00)
  83. } else {
  84. result += String.fromCharCode(codepoint)
  85. }
  86. }
  87. }
  88. return result
  89. }
  90. contentHtml.value = decodeUtf8(decodedBinary)
  91. } catch (e) {
  92. // 解码失败,回显原始内容
  93. contentHtml.value = res.content
  94. }
  95. } else {
  96. // 非 base64 格式,直接回显原始 HTML 页面
  97. contentHtml.value = res.content
  98. }
  99. } else {
  100. contentHtml.value = '暂无协议内容'
  101. }
  102. }
  103. } catch (error) {
  104. console.error('获取协议详情失败', error)
  105. uni.showToast({ title: typeof error === 'string' ? error : '获取协议详情失败', icon: 'none' })
  106. } finally {
  107. loading.value = false
  108. }
  109. }
  110. </script>
  111. <style lang="scss" scoped>
  112. .agreement-detail-page { min-height: 100vh; background: #f7f8fa; padding-bottom: calc(40rpx + env(safe-area-inset-bottom)); }
  113. .content-card { background: #fff; margin: 24rpx; border-radius: 24rpx; padding: 40rpx 32rpx; box-shadow: 0 4rpx 12rpx rgba(0,0,0,0.03); }
  114. .title { display: block; font-size: 36rpx; font-weight: 800; color: #333; margin-bottom: 32rpx; position: relative; padding-bottom: 24rpx; border-bottom: 2rpx solid #EEEEEE; }
  115. .rich-content { display: block; font-size: 28rpx; color: #555; line-height: 1.8; word-break: break-all; }
  116. .loading-state { text-align: center; padding: 100rpx 0; color: #999; font-size: 28rpx; }
  117. </style>