Layout.vue 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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 type 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. let islogin = false
  50. if (!islogin && import.meta.env.DEV) {
  51. console.log('开发环境');
  52. islogin = true;
  53. }
  54. console.log({env: import.meta.env});
  55. const showLayout = ref(false)
  56. const showWaline = ref(false)
  57. const { dialog, massage } = createDiscreteApi(["dialog", "massage"])
  58. const casdoorSdk = useCasdoor()
  59. console.log("route: ", route)
  60. function renderLayout(pathname: string) {
  61. if (!islogin && !pathname.startsWith(withBase('/casdoor/'))) {
  62. // 查询登录状态
  63. fetch<string>(withBase('/api/islogin'), {
  64. method: "GET",
  65. credentials: "include"
  66. }).then(async res => {
  67. // 已登录
  68. const token = await res.text()
  69. // 尝试提取用户信息
  70. setWalineUserInfoFromToken(token)
  71. islogin = true
  72. showLayout.value = true
  73. }).catch(err => {
  74. console.error("未登录")
  75. console.error(err)
  76. // 跳转到登录页面
  77. const signinUrl = casdoorSdk.getSigninUrl()
  78. window.location.href = signinUrl.replace(/^https?:\/\/[^/]+/, '')
  79. })
  80. } else if (islogin || pathname.startsWith(withBase('/casdoor/'))) {
  81. showLayout.value = true
  82. } else {
  83. showLayout.value = false
  84. }
  85. }
  86. function renderWaline(pathname: string) {
  87. // Casdoor,不加载waline
  88. if (pathname.startsWith(withBase('/casdoor/'))) {
  89. showWaline.value = false
  90. } else {
  91. if (showWaline.value) {
  92. // 更新waline
  93. walineRef.value?.update()
  94. } else {
  95. showWaline.value = true
  96. }
  97. }
  98. }
  99. onMounted(async () => {
  100. const url = new URL(window.location.href)
  101. const pathname = url.pathname
  102. console.log("url: ", url)
  103. console.log("pathname: ", pathname)
  104. renderLayout(pathname)
  105. renderWaline(pathname)
  106. watch(
  107. () => route.path,
  108. (newPath, oldPath) => {
  109. console.log('📦 路由变化:', oldPath, '➡️', newPath)
  110. renderWaline(pathname)
  111. renderLayout(pathname)
  112. }
  113. )
  114. })
  115. const handlePersonSelect = (key: string | number, option: DropdownOption) => {
  116. if (key === 'logout') {
  117. handleExitClick()
  118. }
  119. }
  120. const handleExitClick = async () => {
  121. dialog.warning({
  122. title: '提示',
  123. content: '确定要退出登录?',
  124. positiveText: '确定',
  125. negativeText: '取消',
  126. draggable: true,
  127. onPositiveClick: async () => {
  128. // 请求登出
  129. await fetch(withBase('/api/exit'), {
  130. method: "POST",
  131. credentials: "include"
  132. }).then(res => {
  133. const text = res.text()
  134. console.log(text)
  135. islogin = false
  136. const signinUrl = casdoorSdk.getSigninUrl()
  137. // 跳转到登录页面
  138. window.location.href = signinUrl.replace(/^https?:\/\/[^/]+/, '')
  139. }).catch(err => {
  140. massage.error("登出失败")
  141. console.error("登出失败")
  142. console.error(err)
  143. })
  144. },
  145. onNegativeClick: () => {
  146. return
  147. }
  148. })
  149. }
  150. </script>
  151. <style lang="css">
  152. .vp-doc h5 {
  153. margin: 24px 0 0;
  154. letter-spacing: -0.01em;
  155. }
  156. /* 禁止选中内容(禁止复制) */
  157. .vp-doc {
  158. user-select: none; /* 禁止选中 */
  159. -webkit-user-select: none;
  160. -moz-user-select: none;
  161. -ms-user-select: none;
  162. }
  163. </style>