default-layout.vue 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. <template>
  2. <a-layout class="layout">
  3. <a-layout-header class="layout-navbar">
  4. <a-button
  5. class="mobile-menu-trigger"
  6. type="text"
  7. shape="circle"
  8. @click="toggleSider"
  9. >
  10. <template #icon>
  11. <icon-menu-unfold v-if="siderCollapsed" />
  12. <icon-menu-fold v-else />
  13. </template>
  14. </a-button>
  15. <Navbar />
  16. </a-layout-header>
  17. <a-layout
  18. class="layout-body"
  19. :class="{
  20. 'layout-body--mobile': isMobile,
  21. 'layout-body--menu-open': isMobile && !siderCollapsed
  22. }"
  23. >
  24. <a-layout-sider
  25. class="layout-sider"
  26. :width="200"
  27. :collapsed-width="0"
  28. :collapsed="siderCollapsed"
  29. :hide-trigger="true"
  30. collapsible
  31. breakpoint="lg"
  32. @collapse="(val) => (siderCollapsed = val)"
  33. >
  34. <Menu class="menu-wrapper" />
  35. </a-layout-sider>
  36. <a-layout-content class="layout-content">
  37. <PageLayout class="page-layout-main" />
  38. <Footer />
  39. </a-layout-content>
  40. <div
  41. v-if="isMobile && !siderCollapsed"
  42. class="mobile-menu-mask"
  43. @click="siderCollapsed = true"
  44. />
  45. </a-layout>
  46. <a-badge v-if="aiAvailable" :count="aiUnread" :dot="aiUnread > 0" class="aiButtonBadge">
  47. <a-avatar class="aiButton" @click="chat = !chat">
  48. <img alt="AI助手" src="@/assets/img/ai.png" />
  49. </a-avatar>
  50. </a-badge>
  51. <AIChat v-if="aiAvailable" :visible="chat" @close="chat = false" @unread-change="aiUnread = $event" />
  52. <a-modal
  53. v-model:visible="popupVisible"
  54. :title="popupData.title || '公告'"
  55. :footer="false"
  56. width="760px"
  57. modal-class="home-popup-modal"
  58. @cancel="handlePopupClose"
  59. >
  60. <div class="popup-html" v-html="popupData.content_html"></div>
  61. </a-modal>
  62. </a-layout>
  63. </template>
  64. <script setup>
  65. import { computed, onMounted, onBeforeUnmount, ref, onUnmounted } from 'vue'
  66. import { eventBus } from '@/utils/eventBus'
  67. import { Notification } from '@arco-design/web-vue'
  68. import { getUnreadPopup, markPopupRead } from '@/api/public'
  69. import { useUserStore } from '@/store'
  70. import PageLayout from './page-layout.vue'
  71. import Menu from '@/components/Menu/index.vue'
  72. import Navbar from '@/components/Navbar/index.vue'
  73. import Footer from '@/components/Footer/index.vue'
  74. import AIChat from '@/components/AIChat/index.vue'
  75. const chat = ref(false)
  76. const aiUnread = ref(0)
  77. const popupVisible = ref(false)
  78. const popupData = ref({})
  79. const siderCollapsed = ref(false)
  80. const isMobile = ref(false)
  81. const userStore = useUserStore()
  82. const aiAvailable = computed(() => !!(userStore.uuid && userStore.session))
  83. // 按钮拖动
  84. let isDragging = false
  85. let offsetX = 0
  86. let offsetY = 0
  87. let button = null
  88. function handleMouseDown(e) {
  89. isDragging = true
  90. const rect = button.getBoundingClientRect()
  91. offsetX = e.clientX - rect.left
  92. offsetY = e.clientY - rect.top
  93. document.addEventListener('mousemove', handleMouseMove)
  94. document.addEventListener('mouseup', handleMouseUp)
  95. }
  96. function handleMouseMove(e) {
  97. if (!isDragging) return
  98. const x = e.clientX - offsetX
  99. const y = e.clientY - offsetY
  100. button.style.left = x + 'px'
  101. button.style.top = y + 'px'
  102. button.style.right = 'auto'
  103. button.style.bottom = 'auto'
  104. }
  105. function handleMouseUp() {
  106. isDragging = false
  107. document.removeEventListener('mousemove', handleMouseMove)
  108. document.removeEventListener('mouseup', handleMouseUp)
  109. }
  110. onMounted(() => {
  111. button = document.querySelector('.aiButtonBadge')
  112. if (button) {
  113. button.addEventListener('mousedown', handleMouseDown)
  114. }
  115. eventBus.on('closeAI', () => { chat.value = false })
  116. const mobile = window.innerWidth <= 992
  117. isMobile.value = mobile
  118. if (mobile) {
  119. siderCollapsed.value = false
  120. }
  121. window.addEventListener('resize', syncSiderCollapsed)
  122. getPopup()
  123. })
  124. onBeforeUnmount(() => {
  125. if (button) {
  126. button.removeEventListener('mousedown', handleMouseDown)
  127. }
  128. window.removeEventListener('resize', syncSiderCollapsed)
  129. })
  130. onUnmounted(() => {
  131. eventBus.off('closeAI', () => { chat.value = false })
  132. })
  133. const getPopup = async () => {
  134. try {
  135. const res = await getUnreadPopup({ limit: 1 })
  136. if (!res || res.code !== 0) return
  137. const list = res.data || []
  138. if (!list.length) return
  139. popupData.value = list[0]
  140. popupVisible.value = true
  141. } catch (error) {
  142. Notification.error({
  143. title: '获取首页公告失败',
  144. content: error.message || '请稍后再试'
  145. })
  146. }
  147. }
  148. const handlePopupClose = async () => {
  149. popupVisible.value = false
  150. if (!popupData.value?.id) return
  151. try {
  152. await markPopupRead({ popup_id: popupData.value.id })
  153. } catch (_) {}
  154. }
  155. const syncSiderCollapsed = () => {
  156. const mobile = window.innerWidth <= 992
  157. isMobile.value = mobile
  158. if (!mobile) {
  159. siderCollapsed.value = false
  160. }
  161. }
  162. const toggleSider = () => {
  163. siderCollapsed.value = !siderCollapsed.value
  164. }
  165. </script>
  166. <style scoped lang="less">
  167. @import '@/styles/store-theme.less';
  168. @nav-size-height: 60px;
  169. .layout {
  170. min-width: 0;
  171. width: 100%;
  172. min-height: 100vh;
  173. }
  174. .layout-body {
  175. margin-top: @nav-size-height;
  176. min-height: calc(100vh - @nav-size-height);
  177. height: calc(100vh - @nav-size-height);
  178. position: relative;
  179. overflow: hidden;
  180. }
  181. .layout-navbar {
  182. position: fixed;
  183. top: 0;
  184. left: 0;
  185. z-index: 999;
  186. width: 100%;
  187. height: @nav-size-height;
  188. }
  189. .mobile-menu-trigger {
  190. display: none;
  191. }
  192. .layout-sider {
  193. position: sticky;
  194. top: @nav-size-height;
  195. z-index: 99;
  196. height: calc(100vh - @nav-size-height);
  197. transition: all 0.2s cubic-bezier(0.34, 0.69, 0.1, 1);
  198. &::after {
  199. position: absolute;
  200. top: 0;
  201. right: -1px;
  202. display: block;
  203. width: 1px;
  204. height: 100%;
  205. background-color: var(--color-border);
  206. content: '';
  207. }
  208. }
  209. .menu-wrapper {
  210. user-select: none;
  211. margin-top: 0;
  212. height: 100%;
  213. overflow: auto;
  214. overflow-x: hidden;
  215. :deep(.arco-menu) {
  216. ::-webkit-scrollbar {
  217. width: 12px;
  218. height: 4px;
  219. }
  220. ::-webkit-scrollbar-thumb {
  221. border: 4px solid transparent;
  222. background-clip: padding-box;
  223. border-radius: 7px;
  224. background-color: var(--color-text-4);
  225. }
  226. ::-webkit-scrollbar-thumb:hover {
  227. background-color: var(--color-text-3);
  228. }
  229. }
  230. }
  231. .layout-content {
  232. display: flex;
  233. flex-direction: column;
  234. flex: 1;
  235. min-width: 0;
  236. min-height: calc(100vh - @nav-size-height);
  237. overflow-x: hidden;
  238. overflow-y: auto;
  239. background-color: @store-bg;
  240. transition: transform 0.22s ease;
  241. &::-webkit-scrollbar {
  242. width: 10px;
  243. height: 10px;
  244. }
  245. &::-webkit-scrollbar-track {
  246. background: transparent;
  247. }
  248. &::-webkit-scrollbar-thumb {
  249. border: 2px solid transparent;
  250. border-radius: 8px;
  251. background-clip: padding-box;
  252. background-color: rgba(22, 93, 255, 0.28);
  253. transition: background-color 0.2s ease;
  254. }
  255. &::-webkit-scrollbar-thumb:hover {
  256. background-color: rgba(22, 93, 255, 0.46);
  257. }
  258. }
  259. @media (min-width: 993px) {
  260. .layout-body {
  261. display: block;
  262. }
  263. .layout-sider {
  264. position: fixed;
  265. left: 0;
  266. top: @nav-size-height;
  267. width: 200px !important;
  268. height: calc(100vh - @nav-size-height);
  269. }
  270. .layout-content {
  271. margin-left: 200px;
  272. height: calc(100vh - @nav-size-height);
  273. }
  274. }
  275. .mobile-menu-mask {
  276. position: fixed;
  277. top: @nav-size-height;
  278. left: 0;
  279. right: 0;
  280. bottom: 0;
  281. z-index: 1000;
  282. background: rgba(0, 0, 0, 0.28);
  283. }
  284. .aiButton {
  285. width: 55px;
  286. height: 55px;
  287. background-color: #fff;
  288. cursor: pointer;
  289. transition: transform 0.3s ease;
  290. &:hover {
  291. transform: scale(1.15);
  292. }
  293. }
  294. .aiButtonBadge {
  295. position: fixed;
  296. bottom: 100px;
  297. right: 60px;
  298. z-index: 200;
  299. }
  300. .popup-html {
  301. margin-top: 0;
  302. padding-right: 4px;
  303. max-height: min(56vh, 520px);
  304. overflow-y: auto;
  305. line-height: 1.7;
  306. color: var(--color-text-1);
  307. &::-webkit-scrollbar {
  308. width: 8px;
  309. height: 6px;
  310. }
  311. &::-webkit-scrollbar-track {
  312. background: transparent;
  313. }
  314. &::-webkit-scrollbar-thumb {
  315. border-radius: 6px;
  316. background-color: rgba(134, 144, 156, 0.35);
  317. }
  318. &::-webkit-scrollbar-thumb:horizontal {
  319. border-radius: 999px;
  320. }
  321. &::-webkit-scrollbar-thumb:hover {
  322. background-color: rgba(134, 144, 156, 0.55);
  323. }
  324. }
  325. :deep(.home-popup-modal) {
  326. border-radius: 14px;
  327. box-shadow: 0 14px 40px rgba(15, 35, 95, 0.16);
  328. overflow: hidden;
  329. .arco-modal-header {
  330. margin-bottom: 0;
  331. padding: 18px 24px 14px;
  332. border-bottom: 1px solid var(--color-border-2);
  333. background: linear-gradient(180deg, rgba(22, 93, 255, 0.06) 0%, rgba(22, 93, 255, 0) 100%);
  334. }
  335. .arco-modal-title {
  336. font-size: 18px;
  337. font-weight: 600;
  338. color: var(--color-text-1);
  339. }
  340. .arco-modal-body {
  341. padding: 18px 24px 22px;
  342. }
  343. .arco-modal-close-btn {
  344. top: 14px;
  345. right: 14px;
  346. width: 28px;
  347. height: 28px;
  348. border-radius: 8px;
  349. color: var(--color-text-2);
  350. background-color: rgba(240, 243, 245, 0.9);
  351. transition: all 0.2s ease;
  352. }
  353. .arco-modal-close-btn:hover {
  354. color: var(--color-text-1);
  355. background-color: rgba(229, 233, 237, 1);
  356. }
  357. }
  358. @media (max-width: 992px) {
  359. .mobile-menu-trigger {
  360. display: inline-flex;
  361. position: absolute;
  362. left: 8px;
  363. top: 50%;
  364. transform: translateY(-50%);
  365. z-index: 1001;
  366. }
  367. :deep(.navbar .left-side) {
  368. padding-left: 48px;
  369. }
  370. .layout-body--mobile {
  371. display: block;
  372. }
  373. .layout-body--mobile .layout-sider {
  374. position: fixed;
  375. left: 0;
  376. top: @nav-size-height;
  377. z-index: 1001;
  378. height: calc(100vh - @nav-size-height);
  379. width: 200px !important;
  380. transform: translateX(-100%);
  381. transition: transform 0.22s ease;
  382. box-shadow: 2px 0 16px rgba(0, 0, 0, 0.14);
  383. }
  384. .layout-body--menu-open .layout-sider {
  385. transform: translateX(0);
  386. }
  387. .layout-body--mobile .layout-content {
  388. min-height: calc(100vh - @nav-size-height);
  389. transform: translateX(0);
  390. }
  391. .layout-body--menu-open .layout-content {
  392. transform: translateX(200px);
  393. }
  394. .aiButtonBadge {
  395. right: 18px;
  396. bottom: 24px;
  397. }
  398. }
  399. </style>