Layout.vue 4.6 KB

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