cwg-page-wrapper.vue 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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. <view class="sidebar-mask mask-visible" v-if="sidebarVisible" @click="openLeftDrawer">
  7. </view>
  8. <view class="fixed"></view>
  9. <cwg-header v-if=pageTitle class="custom-header" :title="pageTitle" />
  10. </cwg-match-media>
  11. <cwg-language style="width: 0;display: none;" />
  12. <cwg-progress />
  13. <cwg-confirm-popup />
  14. <view class="page-content" :style="{ backgroundColor: bgColor }">
  15. <cwg-match-media :max-width="991" v-if="!isLoginPage">
  16. <view class="left-sidebar" :class="{ 'sidebar-visible': sidebarVisible }">
  17. <cwg-sidebar />
  18. </view>
  19. <view class="sidebar-mask" v-if="sidebarVisible" @click="openLeftDrawer" :style="{
  20. opacity: maskVisible ? 1 : 0,
  21. transition: 'opacity 0.3s cubic-bezier(0.4, 0, 0.2, 1)'
  22. }">
  23. </view>
  24. </cwg-match-media>
  25. <view class="content-info">
  26. <!-- 完善信息提示,-->
  27. <PrefectInfo v-if="!isLoginPage" />
  28. <!-- 申请成为ib,-->
  29. <IbInfo ref="ibRef" v-if="!isLoginPage" />
  30. <view class="content-wrapper" :class="{ 'content-wrapper-padding': isContentPadding }">
  31. <!-- <cwg-header /> -->
  32. <transition name="fade" mode="out-in">
  33. <view :key="$route.path">
  34. <slot />
  35. </view>
  36. </transition>
  37. <cwg-custom-footer />
  38. </view>
  39. </view>
  40. <view :style="{ height: isTabBarPage ? '60px' : '0px' }" />
  41. </view>
  42. <cwg-right-drawer v-if="!isLoginPage" ref="rightDrawerRef" @navigate="handleDrawerNavigate"
  43. @logout="handleDrawerLogout" />
  44. </view>
  45. </template>
  46. <script setup lang="ts">
  47. import { ref, computed, onMounted, onUnmounted } from 'vue'
  48. import { onLoad, onShow } from '@dcloudio/uni-app'
  49. import useRoute, { updateRoute } from '@/hooks/useRoute'
  50. import useRouter from '@/hooks/useRouter'
  51. import useUserStore from '@/stores/use-user-store'
  52. import { userApi } from '@/api/user'
  53. import useGlobalStore from '@/stores/use-global-store'
  54. import PrefectInfo from '@/components/PrefectInfo.vue'
  55. import IbInfo from '@/components/IbInfo.vue'
  56. const globalStore = useGlobalStore()
  57. const router = useRouter()
  58. const userStore = useUserStore()
  59. const props = defineProps({
  60. // 是否固定顶部导航栏
  61. isHeaderFixed: {
  62. type: Boolean,
  63. default: false,
  64. },
  65. // 是否登录页,登录页不显示侧边栏和顶部导航,注册忘记密码等页面
  66. isLoginPage: {
  67. type: Boolean,
  68. default: false,
  69. },
  70. // 是否含有padding 默认有
  71. isContentPadding: {
  72. type: Boolean,
  73. default: true,
  74. },
  75. // 主内容背景颜色
  76. bgColor: {
  77. type: String,
  78. default: '',
  79. },
  80. // 顶部导航栏标题
  81. pageTitle: {
  82. type: String,
  83. default: '',
  84. },
  85. })
  86. const isDark = computed(() => globalStore.theme === 'dark')
  87. const isTabBarPage = ref(false)
  88. const rightDrawerRef = ref<any>(null)
  89. const noticeDrawerRef = ref<any>(null)
  90. const ibRef = ref<any>(null)
  91. // 事件处理函数
  92. const handleOpenIb = () => {
  93. if (ibRef.value) ibRef.value?.getInfo()
  94. }
  95. const handleOpenNoticeDrawer = () => {
  96. noticeDrawerRef.value?.open()
  97. }
  98. const handleOpenRightDrawer = () => {
  99. openRightDrawer()
  100. }
  101. onMounted(() => {
  102. // 只在组件挂载时注册事件监听器
  103. uni.$once('open-ib', handleOpenIb)
  104. uni.$on('open-notice-drawer', handleOpenNoticeDrawer)
  105. uni.$on('open-right-drawer', handleOpenRightDrawer)
  106. })
  107. onUnmounted(() => {
  108. // 在组件销毁时移除事件监听器
  109. uni.$off('open-ib', handleOpenIb)
  110. uni.$off('open-notice-drawer', handleOpenNoticeDrawer)
  111. uni.$off('open-right-drawer', handleOpenRightDrawer)
  112. })
  113. function openRightDrawer() {
  114. rightDrawerRef.value?.open()
  115. }
  116. const sidebarVisible = ref(false)
  117. const maskVisible = ref(false)
  118. function openLeftDrawer() {
  119. if (sidebarVisible.value) {
  120. // 关闭时先隐藏遮罩层
  121. maskVisible.value = false
  122. // 等待遮罩层动画结束后再隐藏侧边栏
  123. setTimeout(() => {
  124. sidebarVisible.value = false
  125. }, 200)
  126. } else {
  127. // 打开时先显示侧边栏
  128. sidebarVisible.value = true
  129. // 等待侧边栏动画结束后再显示遮罩层
  130. setTimeout(() => {
  131. maskVisible.value = true
  132. }, 200)
  133. }
  134. }
  135. function handleDrawerNavigate(path: string) {
  136. router.push(path)
  137. }
  138. async function handleDrawerLogout() {
  139. try {
  140. const res = await userApi.logout()
  141. if (res.code === 200) {
  142. userStore.clearUserInfo()
  143. router.push('/pages/login/index')
  144. }
  145. } catch (error) {
  146. userStore.clearUserInfo()
  147. router.push('/pages/login/index')
  148. }
  149. }
  150. onLoad(() => {
  151. updateRoute()
  152. })
  153. onShow(() => {
  154. updateRoute()
  155. })
  156. </script>
  157. <style lang="scss" scoped>
  158. @import "@/uni.scss";
  159. .page-wrapper {
  160. height: calc(100vh - 56px);
  161. }
  162. .header-box {
  163. width: 100%;
  164. height: 56px;
  165. }
  166. .page-content {
  167. width: 100%;
  168. height: calc(100vh - 56px);
  169. overflow: hidden;
  170. display: flex;
  171. flex-direction: column;
  172. position: relative;
  173. box-sizing: border-box;
  174. .content-info {
  175. height: calc(100vh - 56px);
  176. overflow: auto;
  177. }
  178. }
  179. .left-sidebar {
  180. width: 0;
  181. overflow: hidden;
  182. position: absolute;
  183. left: 0;
  184. top: 0;
  185. z-index: 1000;
  186. background-color: #fff;
  187. transition: width 81ms cubic-bezier(0.4, 0, 0.2, 1);
  188. }
  189. .sidebar-mask {
  190. position: absolute;
  191. left: 0;
  192. top: 0;
  193. width: 100vw;
  194. height: calc(100vh - 56px);
  195. background-color: rgba(0, 0, 0, 0.2);
  196. z-index: 101;
  197. }
  198. .mask-visible {
  199. background-color: rgba(0, 0, 0, 0);
  200. width: 100vw;
  201. height: 56px;
  202. }
  203. .sidebar-visible {
  204. width: px2rpx(280);
  205. }
  206. .content-wrapper {
  207. max-width: px2rpx(1224);
  208. width: 100%;
  209. margin: 0 auto;
  210. padding-top: px2rpx(20);
  211. box-sizing: border-box;
  212. display: flex;
  213. flex-direction: column;
  214. justify-content: space-between;
  215. min-height: calc(100vh - 56px);
  216. }
  217. .fixed {
  218. // position: fixed;
  219. // top: 0;
  220. // left: 0;
  221. width: 100%;
  222. height: 56px;
  223. background-color: var(--color-white);
  224. z-index: 9;
  225. }
  226. .custom-header {
  227. background-color: var(--color-white);
  228. padding: 0 px2rpx(15);
  229. position: fixed;
  230. top: 56px;
  231. left: 0;
  232. }
  233. @media screen and (max-width: 1504px) {
  234. .content-wrapper-padding {
  235. padding: px2rpx(16) px2rpx(16) 0 px2rpx(16);
  236. }
  237. }
  238. @media screen and (max-width: 991px) {
  239. .content-wrapper-padding {
  240. padding: px2rpx(16) px2rpx(16) 0 px2rpx(16);
  241. }
  242. .page-wrapper {
  243. height: 100vh;
  244. }
  245. }
  246. .mobile-menu-btn {
  247. width: px2rpx(64);
  248. height: px2rpx(64);
  249. display: flex;
  250. align-items: center;
  251. justify-content: center;
  252. }
  253. /* 页面切换动画 */
  254. .fade-enter-active,
  255. .fade-leave-active {
  256. transition: opacity 0.3s ease;
  257. }
  258. .fade-enter-from,
  259. .fade-leave-to {
  260. opacity: 0;
  261. }
  262. /* 侧边栏动画优化 */
  263. .left-sidebar {
  264. transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  265. }
  266. .sidebar-mask {
  267. transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  268. }
  269. /* 内容区域动画 */
  270. .content-wrapper {
  271. animation: slideIn 0.3s ease-out;
  272. }
  273. @keyframes slideIn {
  274. from {
  275. transform: translateY(20px);
  276. opacity: 0;
  277. }
  278. to {
  279. transform: translateY(0);
  280. opacity: 1;
  281. }
  282. }
  283. </style>