cwg-page-wrapper.vue 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. <template>
  2. <view :class="['page-wrapper', { dark: isDark }]">
  3. <cwg-match-media :max-width="991" v-if="!isLoginPage">
  4. <cwg-pc-header @open-right-drawer="openRightDrawer" @open-left-drawer="openLeftDrawer" class="header-box"
  5. :sidebarVisible="sidebarVisible" />
  6. <!-- 占位-->
  7. <view class="fixed"></view>
  8. </cwg-match-media>
  9. <LanguageDropdown style="width: 0;display: none;" />
  10. <cwg-progress />
  11. <cwg-confirm-popup />
  12. <view class="page-content" :style="{ backgroundColor: bgColor }">
  13. <cwg-match-media :max-width="991" v-if="!isLoginPage">
  14. <view class="left-sidebar" :class="{ 'sidebar-visible': sidebarVisible }">
  15. <cwg-sidebar />
  16. </view>
  17. <view class="sidebar-mask" v-if="sidebarVisible" @click="openLeftDrawer" :style="{
  18. opacity: sidebarVisible ? 1 : 0,
  19. transition: 'opacity 281ms cubic-bezier(0.4, 0, 0.2, 1)'
  20. }">
  21. </view>
  22. </cwg-match-media>
  23. <view class="content-info">
  24. <!-- 完善信息提示,-->
  25. <PrefectInfo />
  26. <!-- 申请成为ib,-->
  27. <IbInfo ref="ibRef" />
  28. <view class="content-wrapper" :class="{ 'content-wrapper-padding': isContentPadding }">
  29. <!-- <cwg-header /> -->
  30. <view>
  31. <slot />
  32. </view>
  33. <cwg-custom-footer />
  34. </view>
  35. </view>
  36. <view :style="{ height: isTabBarPage ? '60px' : '0px' }" />
  37. </view>
  38. <cwg-right-drawer v-if="!isLoginPage" ref="rightDrawerRef" @navigate="handleDrawerNavigate"
  39. @logout="handleDrawerLogout" />
  40. <cwg-notice-drawer v-if="!isLoginPage" ref="noticeDrawerRef" @navigate="handleDrawerNavigate"
  41. @logout="handleDrawerLogout" />
  42. </view>
  43. </template>
  44. <script setup lang="ts">
  45. import { ref, computed, onMounted, onUnmounted } from 'vue'
  46. import { onLoad, onShow } from '@dcloudio/uni-app'
  47. import useRoute, { updateRoute } from '@/hooks/useRoute'
  48. import useRouter from '@/hooks/useRouter'
  49. import useUserStore from '@/stores/use-user-store'
  50. import { userApi } from '@/api/user'
  51. import useGlobalStore from '@/stores/use-global-store'
  52. import LanguageDropdown from './LanguageDropdown.vue'
  53. import PrefectInfo from '@/components/PrefectInfo.vue'
  54. import IbInfo from '@/components/IbInfo.vue'
  55. const globalStore = useGlobalStore()
  56. const router = useRouter()
  57. const userStore = useUserStore()
  58. const props = defineProps({
  59. // 是否固定顶部导航栏
  60. isHeaderFixed: {
  61. type: Boolean,
  62. default: false,
  63. },
  64. // 是否登录页,登录页不显示侧边栏和顶部导航,注册忘记密码等页面
  65. isLoginPage: {
  66. type: Boolean,
  67. default: false,
  68. },
  69. // 是否含有padding 默认有
  70. isContentPadding: {
  71. type: Boolean,
  72. default: true,
  73. },
  74. // 主内容背景颜色
  75. bgColor: {
  76. type: String,
  77. default: '',
  78. },
  79. })
  80. const isDark = computed(() => globalStore.theme === 'dark')
  81. const isTabBarPage = ref(false)
  82. const rightDrawerRef = ref<any>(null)
  83. const noticeDrawerRef = ref<any>(null)
  84. const ibRef = ref<any>(null)
  85. // 事件处理函数
  86. const handleOpenIb = () => {
  87. if (ibRef.value) ibRef.value?.getInfo()
  88. }
  89. const handleOpenNoticeDrawer = () => {
  90. noticeDrawerRef.value?.open()
  91. }
  92. const handleOpenRightDrawer = () => {
  93. openRightDrawer()
  94. }
  95. onMounted(() => {
  96. // 只在组件挂载时注册事件监听器
  97. uni.$once('open-ib', handleOpenIb)
  98. uni.$on('open-notice-drawer', handleOpenNoticeDrawer)
  99. uni.$on('open-right-drawer', handleOpenRightDrawer)
  100. })
  101. onUnmounted(() => {
  102. // 在组件销毁时移除事件监听器
  103. uni.$off('open-ib', handleOpenIb)
  104. uni.$off('open-notice-drawer', handleOpenNoticeDrawer)
  105. uni.$off('open-right-drawer', handleOpenRightDrawer)
  106. })
  107. function openRightDrawer() {
  108. rightDrawerRef.value?.open()
  109. }
  110. const sidebarVisible = ref(false)
  111. function openLeftDrawer() {
  112. sidebarVisible.value = !sidebarVisible.value
  113. }
  114. function handleDrawerNavigate(path: string) {
  115. router.push(path)
  116. }
  117. async function handleDrawerLogout() {
  118. try {
  119. const res = await userApi.logout()
  120. if (res.code === 200) {
  121. userStore.clearUserInfo()
  122. router.push('/pages/login/index')
  123. }
  124. } catch (error) {
  125. userStore.clearUserInfo()
  126. router.push('/pages/login/index')
  127. }
  128. }
  129. onLoad(() => {
  130. updateRoute()
  131. })
  132. onShow(() => {
  133. updateRoute()
  134. })
  135. onUnmounted(() => {
  136. uni.$off('open-ib')
  137. uni.$off('open-notice-drawer')
  138. uni.$off('open-right-drawer')
  139. })
  140. </script>
  141. <style lang="scss" scoped>
  142. @import "@/uni.scss";
  143. .page-wrapper {
  144. height: calc(100vh - 56px);
  145. }
  146. .header-box {
  147. width: 100%;
  148. height: 56px;
  149. }
  150. .page-content {
  151. width: 100%;
  152. height: calc(100vh - 56px);
  153. overflow: hidden;
  154. display: flex;
  155. flex-direction: column;
  156. position: relative;
  157. box-sizing: border-box;
  158. .content-info {
  159. height: calc(100vh - 56px);
  160. overflow: auto;
  161. }
  162. }
  163. .left-sidebar {
  164. width: 0;
  165. overflow: hidden;
  166. position: absolute;
  167. left: 0;
  168. top: 0;
  169. z-index: 1000;
  170. background-color: #fff;
  171. transition: width 281ms cubic-bezier(0.4, 0, 0.2, 1);
  172. }
  173. .sidebar-mask {
  174. position: absolute;
  175. left: 0;
  176. top: 0;
  177. width: 100vw;
  178. height: calc(100vh - 56px);
  179. background-color: rgba(0, 0, 0, 0.2);
  180. z-index: 999;
  181. }
  182. .sidebar-visible {
  183. width: px2rpx(280);
  184. }
  185. .content-wrapper {
  186. max-width: px2rpx(1224);
  187. width: 100%;
  188. margin: 0 auto;
  189. padding-top: px2rpx(20);
  190. box-sizing: border-box;
  191. display: flex;
  192. flex-direction: column;
  193. justify-content: space-between;
  194. min-height: calc(100vh - 56px);
  195. }
  196. .fixed {
  197. // position: fixed;
  198. // top: 0;
  199. // left: 0;
  200. width: 100%;
  201. height: 56px;
  202. background-color: var(--color-white);
  203. z-index: 9;
  204. }
  205. @media screen and (max-width: 1504px) {
  206. .content-wrapper-padding {
  207. padding: px2rpx(16) px2rpx(16) 0 px2rpx(16);
  208. }
  209. }
  210. @media screen and (max-width: 991px) {
  211. .content-wrapper-padding {
  212. padding: px2rpx(16) px2rpx(16) 0 px2rpx(16);
  213. }
  214. .page-wrapper {
  215. height: 100vh;
  216. }
  217. }
  218. .mobile-menu-btn {
  219. width: px2rpx(64);
  220. height: px2rpx(64);
  221. display: flex;
  222. align-items: center;
  223. justify-content: center;
  224. }
  225. </style>