detail.vue 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. <template>
  2. <view class="agreement-detail-page">
  3. <!-- 自定义头部 -->
  4. <view class="custom-header" :style="{ paddingTop: statusBarHeight + 'px' }">
  5. <view class="header-content">
  6. <view class="back-btn" @click="handleBack">
  7. <text class="back-icon">‹</text>
  8. </view>
  9. <text class="header-title">{{ pageTitle }}</text>
  10. <view class="placeholder"></view>
  11. </view>
  12. </view>
  13. <!-- 页面内容 -->
  14. <view class="page-body" :style="{ paddingTop: headerHeight + 'px' }">
  15. <view class="content-wrapper">
  16. <scroll-view scroll-y class="content-scroll">
  17. <rich-text :nodes="agreementContent" class="agreement-content"></rich-text>
  18. </scroll-view>
  19. </view>
  20. </view>
  21. </view>
  22. </template>
  23. <script setup>
  24. import { ref, computed, onMounted } from 'vue'
  25. import { useI18n } from 'vue-i18n'
  26. import { getAgreementContent } from '@/apis/setting'
  27. const { t, locale } = useI18n()
  28. // 状态栏高度
  29. const statusBarHeight = ref(0)
  30. // 头部总高度(状态栏 + 导航栏)
  31. const headerHeight = computed(() => {
  32. return statusBarHeight.value + 44 // 44px是导航栏高度
  33. })
  34. // 协议类型
  35. const agreementType = ref('')
  36. // 协议内容
  37. const agreementContent = ref('')
  38. // 加载状态
  39. const loading = ref(false)
  40. // 页面标题
  41. const pageTitle = computed(() => {
  42. return agreementType.value === 'user'
  43. ? t('pagesContent.my.agreement.userAgreement')
  44. : t('pagesContent.my.agreement.privacyPolicy')
  45. })
  46. onMounted(async () => {
  47. // 获取系统信息
  48. const systemInfo = uni.getSystemInfoSync()
  49. statusBarHeight.value = systemInfo.statusBarHeight || 0
  50. // 获取页面参数
  51. const pages = getCurrentPages()
  52. const currentPage = pages[pages.length - 1]
  53. agreementType.value = currentPage.options.type || 'user'
  54. console.log('协议详情页面已加载, 类型:', agreementType.value)
  55. // 获取协议内容
  56. await fetchAgreementContent()
  57. })
  58. // 返回上一页
  59. const handleBack = () => {
  60. const pages = getCurrentPages()
  61. if (pages.length > 1) {
  62. uni.navigateBack()
  63. } else {
  64. uni.reLaunch({
  65. url: '/pages/index'
  66. })
  67. }
  68. }
  69. // 获取协议内容
  70. const fetchAgreementContent = async () => {
  71. try {
  72. loading.value = true
  73. // 显示加载提示
  74. uni.showLoading({
  75. title: t('common.message.loading'),
  76. mask: true
  77. })
  78. // 调用API获取内容
  79. const response = await getAgreementContent(agreementType.value)
  80. if (response && response.code === 200 && response.data && response.data.content) {
  81. // 解析JSON字符串
  82. try {
  83. const contentObj = JSON.parse(response.data.content)
  84. // 根据当前语言环境选择对应的内容
  85. const currentLocale = locale.value // 'zh-CN' 或 'en-US'
  86. const localeKey = currentLocale.replace('-', '_') // 转换为 'zh_CN' 或 'en_US'
  87. // 优先使用当前语言,如果不存在则使用中文,再不存在则使用任意可用语言
  88. agreementContent.value = contentObj[localeKey] ||
  89. contentObj['zh_CN'] ||
  90. contentObj['en_US'] ||
  91. Object.values(contentObj)[0] ||
  92. ''
  93. console.log('已加载协议内容,语言:', localeKey)
  94. } catch (parseError) {
  95. console.error('解析协议内容JSON失败:', parseError)
  96. // 如果解析失败,尝试直接使用原始内容
  97. agreementContent.value = response.data.content
  98. }
  99. } else {
  100. throw new Error(response?.msg || '获取失败')
  101. }
  102. } catch (error) {
  103. console.error('获取协议内容失败:', error)
  104. uni.showToast({
  105. title: error.message || '获取协议内容失败',
  106. icon: 'none',
  107. duration: 2000
  108. })
  109. // 失败时显示默认内容
  110. agreementContent.value = getDefaultContent()
  111. } finally {
  112. loading.value = false
  113. uni.hideLoading()
  114. }
  115. }
  116. // 获取默认内容(API失败时的后备方案)
  117. const getDefaultContent = () => {
  118. if (agreementType.value === 'user') {
  119. return `
  120. <div style="padding: 20px; line-height: 1.8; color: #333;">
  121. <h2 style="text-align: center; color: #667eea; margin-bottom: 20px;">用户服务协议</h2>
  122. <p>协议内容加载失败,请稍后重试。</p>
  123. </div>
  124. `
  125. } else {
  126. return `
  127. <div style="padding: 20px; line-height: 1.8; color: #333;">
  128. <h2 style="text-align: center; color: #667eea; margin-bottom: 20px;">隐私政策</h2>
  129. <p>协议内容加载失败,请稍后重试。</p>
  130. </div>
  131. `
  132. }
  133. }
  134. </script>
  135. <style lang="scss" scoped>
  136. .agreement-detail-page {
  137. width: 100%;
  138. height: 100vh;
  139. display: flex;
  140. flex-direction: column;
  141. background: linear-gradient(180deg, #f0f4ff 0%, #f8f9fa 100%);
  142. // 自定义头部
  143. .custom-header {
  144. position: fixed;
  145. top: 0;
  146. left: 0;
  147. right: 0;
  148. background-color: #ffffff;
  149. border-bottom: 1rpx solid #e5e5e5;
  150. z-index: 100;
  151. .header-content {
  152. height: 88rpx;
  153. display: flex;
  154. align-items: center;
  155. justify-content: space-between;
  156. padding: 0 32rpx;
  157. .back-btn {
  158. width: 60rpx;
  159. height: 60rpx;
  160. display: flex;
  161. align-items: center;
  162. justify-content: center;
  163. .back-icon {
  164. font-size: 56rpx;
  165. color: #333333;
  166. font-weight: 300;
  167. }
  168. }
  169. .header-title {
  170. flex: 1;
  171. text-align: center;
  172. font-size: 32rpx;
  173. font-weight: 500;
  174. color: #000000;
  175. }
  176. .placeholder {
  177. width: 60rpx;
  178. }
  179. }
  180. }
  181. // 页面内容
  182. .page-body {
  183. flex: 1;
  184. display: flex;
  185. flex-direction: column;
  186. overflow: hidden;
  187. .content-wrapper {
  188. flex: 1;
  189. margin: 40rpx;
  190. background-color: #ffffff;
  191. border-radius: 20rpx;
  192. box-shadow: 0 4rpx 20rpx rgba(0, 0, 0, 0.08);
  193. overflow: hidden;
  194. .content-scroll {
  195. width: 100%;
  196. height: 100%;
  197. .agreement-content {
  198. padding: 40rpx;
  199. ::v-deep h2 {
  200. font-size: 36rpx;
  201. font-weight: bold;
  202. margin-bottom: 30rpx;
  203. }
  204. ::v-deep h3 {
  205. font-size: 30rpx;
  206. font-weight: 600;
  207. margin: 30rpx 0 20rpx;
  208. }
  209. ::v-deep p {
  210. font-size: 28rpx;
  211. line-height: 1.8;
  212. margin-bottom: 20rpx;
  213. text-align: justify;
  214. }
  215. ::v-deep ul {
  216. padding-left: 40rpx;
  217. margin-bottom: 20rpx;
  218. li {
  219. font-size: 28rpx;
  220. line-height: 1.8;
  221. margin-bottom: 10rpx;
  222. list-style-type: disc;
  223. }
  224. }
  225. }
  226. }
  227. }
  228. }
  229. }
  230. </style>