Layout.vue 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <template>
  2. <Layout v-if="showLayout">
  3. <template #doc-after> <!-- 正文插槽 -->
  4. <ClientOnly>
  5. <!-- 页面正文之后插入评论 -->
  6. <Waline v-if="showWaline" ref="walineRef" />
  7. </ClientOnly>
  8. </template>
  9. <template #nav-bar-content-after>
  10. <div style="margin-left: 15px">
  11. <n-dropdown :options="personOptions" @select="handlePersonSelect">
  12. <n-button text style="display: flex; justify-content: center" >
  13. <template #icon>
  14. <n-icon><PersonIcon /></n-icon>
  15. </template>
  16. </n-button>
  17. </n-dropdown>
  18. </div>
  19. </template>
  20. </Layout>
  21. </template>
  22. <script lang="ts" setup>
  23. import { ref, onMounted, watch, Component, h } from 'vue'
  24. import { useRouter, useRoute, withBase } from 'vitepress'
  25. import DefaultTheme from 'vitepress/theme'
  26. import { createDiscreteApi, NButton, NIcon, NDropdown, DropdownOption } from 'naive-ui'
  27. import { Exit as ExitIcon, Person as PersonIcon } from '@vicons/ionicons5'
  28. import { useCasdoor } from 'casdoor-vue-sdk'
  29. import Waline from './components/Waline.vue'
  30. import { setWalineUserInfoFromToken } from '../plugins/waline'
  31. function renderIcon(icon: Component) {
  32. return () => {
  33. return h(NIcon, null, {
  34. default: () => h(icon)
  35. })
  36. }
  37. }
  38. const walineRef = ref<InstanceType<typeof Waline> | undefined>(undefined)
  39. const personOptions = ref([
  40. {
  41. label: '退出登录',
  42. key: 'logout',
  43. icon: renderIcon(ExitIcon)
  44. }
  45. ])
  46. const { Layout } = DefaultTheme
  47. const route = useRoute()
  48. const router = useRouter()
  49. watch(
  50. () => route.path,
  51. (newPath, oldPath) => {
  52. console.log('📦 路由变化:', oldPath, '➡️', newPath)
  53. renderWaline()
  54. renderLayout()
  55. }
  56. )
  57. let islogin = false
  58. const showLayout = ref(false)
  59. const showWaline = ref(false)
  60. const url = new URL(window.location.href)
  61. const pathname = url.pathname
  62. const { dialog, massage } = createDiscreteApi(["dialog", "massage"])
  63. const casdoorSdk = useCasdoor()
  64. console.log("route: ", route)
  65. console.log("url: ", url)
  66. console.log("pathname: ", pathname)
  67. function renderLayout() {
  68. if (!islogin && !pathname.startsWith(withBase('/casdoor/'))) {
  69. // 查询登录状态
  70. fetch<string>(withBase('/api/islogin'), {
  71. method: "GET",
  72. credentials: "include"
  73. }).then(async res => {
  74. // 已登录
  75. const token = await res.text()
  76. // 尝试提取用户信息
  77. setWalineUserInfoFromToken(token)
  78. islogin = true
  79. showLayout.value = true
  80. }).catch(err => {
  81. console.error("未登录")
  82. console.error(err)
  83. // 跳转到登录页面
  84. const signinUrl = casdoorSdk.getSigninUrl()
  85. window.location.href = signinUrl.replace(/^https?:\/\/[^/]+/, '')
  86. })
  87. } else if (islogin || pathname.startsWith(withBase('/casdoor/'))) {
  88. showLayout.value = true
  89. } else {
  90. showLayout.value = false
  91. }
  92. }
  93. function renderWaline() {
  94. // Casdoor,不加载waline
  95. if (pathname.startsWith(withBase('/casdoor/'))) {
  96. showWaline.value = false
  97. } else {
  98. if (showWaline.value) {
  99. // 更新waline
  100. walineRef.value?.update()
  101. } else {
  102. showWaline.value = true
  103. }
  104. }
  105. }
  106. onMounted(async () => {
  107. renderLayout()
  108. renderWaline()
  109. })
  110. const handlePersonSelect = (key: string | number, option: DropdownOption) => {
  111. if (key === 'logout') {
  112. handleExitClick()
  113. }
  114. }
  115. const handleExitClick = async () => {
  116. dialog.warning({
  117. title: '提示',
  118. content: '确定要退出登录?',
  119. positiveText: '确定',
  120. negativeText: '取消',
  121. draggable: true,
  122. onPositiveClick: async () => {
  123. // 请求登出
  124. await fetch(withBase('/api/exit'), {
  125. method: "POST",
  126. credentials: "include"
  127. }).then(res => {
  128. const text = res.text()
  129. console.log(text)
  130. islogin = false
  131. const signinUrl = casdoorSdk.getSigninUrl()
  132. // 跳转到登录页面
  133. window.location.href = signinUrl.replace(/^https?:\/\/[^/]+/, '')
  134. }).catch(err => {
  135. massage.error("登出失败")
  136. console.error("登出失败")
  137. console.error(err)
  138. })
  139. },
  140. onNegativeClick: () => {
  141. return
  142. }
  143. })
  144. }
  145. </script>
  146. <style lang="css">
  147. .vp-doc h5 {
  148. margin: 24px 0 0;
  149. letter-spacing: -0.01em;
  150. }
  151. </style>