helpCenterDetail.vue 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <route lang="json5" type="page">
  2. {
  3. layout: 'default',
  4. style: {
  5. navigationBarTitleText: '%helpCenter.detail.title%',
  6. navigationBarBackgroundColor: '#fff',
  7. },
  8. }
  9. </route>
  10. <script lang="ts" setup>
  11. import { onLoad } from '@dcloudio/uni-app'
  12. import { qaInfo } from '@/api/qa'
  13. import i18n from '@/locale/index'
  14. defineOptions({
  15. name: 'HelpCenterDetail',
  16. })
  17. interface QaItem {
  18. id: number
  19. bnTitle?: string
  20. enTitle?: string
  21. bnContent?: string
  22. enContent?: string
  23. groupId: number
  24. }
  25. const locale = computed(() => i18n.global.locale)
  26. const shouldUseEn = computed(() => locale.value === 'en' || locale.value === 'zh-Hans')
  27. const detail = ref<QaItem | null>(null)
  28. const contentHtml = ref<string>('')
  29. function formatTextToHtml(text: string) {
  30. if (!text)
  31. return ''
  32. return text.replace(/\n/g, '<br>')
  33. }
  34. function getQaTitle(item: QaItem | null) {
  35. if (!item)
  36. return ''
  37. return shouldUseEn.value ? (item.enTitle || item.bnTitle || '') : (item.bnTitle || item.enTitle || '')
  38. }
  39. async function loadDetail(id: number) {
  40. const res = await qaInfo(id)
  41. if (res.code === '200') {
  42. detail.value = res.data as QaItem
  43. const content = shouldUseEn.value
  44. ? (detail.value.enContent || detail.value.bnContent || '')
  45. : (detail.value.bnContent || detail.value.enContent || '')
  46. contentHtml.value = formatTextToHtml(content)
  47. }
  48. }
  49. watch(locale, () => {
  50. if (!detail.value)
  51. return
  52. const content = shouldUseEn.value
  53. ? (detail.value.enContent || detail.value.bnContent || '')
  54. : (detail.value.bnContent || detail.value.enContent || '')
  55. contentHtml.value = formatTextToHtml(content)
  56. })
  57. onLoad(async (options: any) => {
  58. const id = Number(options?.id)
  59. if (!Number.isFinite(id) || id <= 0)
  60. return
  61. await loadDetail(id)
  62. })
  63. </script>
  64. <template>
  65. <view class="rounded-12rpx bg-white px-22rpx py-40rpx">
  66. <view class="mb-18rpx text-30rpx font-bold">
  67. {{ getQaTitle(detail) }}
  68. </view>
  69. <rich-text :nodes="contentHtml" />
  70. </view>
  71. </template>
  72. <style lang="scss" scoped>
  73. page {
  74. background-color: #fff;
  75. }
  76. </style>