cwg-page-wrapper.vue 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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-right-drawer', handleOpenRightDrawer)
  105. })
  106. onUnmounted(() => {
  107. // 在组件销毁时移除事件监听器
  108. uni.$off('open-ib', handleOpenIb)
  109. uni.$off('open-right-drawer', handleOpenRightDrawer)
  110. })
  111. function openRightDrawer() {
  112. rightDrawerRef.value?.open()
  113. }
  114. const sidebarVisible = ref(false)
  115. const maskVisible = ref(false)
  116. function openLeftDrawer() {
  117. if (sidebarVisible.value) {
  118. // 关闭时先隐藏遮罩层
  119. maskVisible.value = false
  120. // 等待遮罩层动画结束后再隐藏侧边栏
  121. setTimeout(() => {
  122. sidebarVisible.value = false
  123. }, 200)
  124. } else {
  125. // 打开时先显示侧边栏
  126. sidebarVisible.value = true
  127. // 等待侧边栏动画结束后再显示遮罩层
  128. setTimeout(() => {
  129. maskVisible.value = true
  130. }, 200)
  131. }
  132. }
  133. function handleDrawerNavigate(path: string) {
  134. router.push(path)
  135. }
  136. async function handleDrawerLogout() {
  137. try {
  138. const res = await userApi.logout()
  139. if (res.code === 200) {
  140. userStore.clearUserInfo()
  141. router.push('/pages/login/index')
  142. }
  143. } catch (error) {
  144. userStore.clearUserInfo()
  145. router.push('/pages/login/index')
  146. }
  147. }
  148. onLoad(() => {
  149. updateRoute()
  150. })
  151. onShow(() => {
  152. updateRoute()
  153. })
  154. </script>
  155. <style lang="scss" scoped>
  156. @import "@/uni.scss";
  157. .page-wrapper {
  158. height: calc(100vh - 56px);
  159. }
  160. .header-box {
  161. width: 100%;
  162. height: 56px;
  163. }
  164. .page-content {
  165. width: 100%;
  166. height: calc(100vh - 56px);
  167. overflow: hidden;
  168. display: flex;
  169. flex-direction: column;
  170. position: relative;
  171. box-sizing: border-box;
  172. .content-info {
  173. height: calc(100vh - 56px);
  174. overflow: auto;
  175. }
  176. }
  177. .left-sidebar {
  178. width: 0;
  179. overflow: hidden;
  180. position: absolute;
  181. left: 0;
  182. top: 0;
  183. z-index: 1000;
  184. background-color: #fff;
  185. transition: width 81ms cubic-bezier(0.4, 0, 0.2, 1);
  186. }
  187. .sidebar-mask {
  188. position: absolute;
  189. left: 0;
  190. top: 0;
  191. width: 100vw;
  192. height: calc(100vh - 56px);
  193. background-color: rgba(0, 0, 0, 0.2);
  194. z-index: 101;
  195. }
  196. .mask-visible {
  197. background-color: rgba(0, 0, 0, 0);
  198. width: 100vw;
  199. height: 56px;
  200. }
  201. .sidebar-visible {
  202. width: px2rpx(280);
  203. }
  204. .content-wrapper {
  205. max-width: px2rpx(1224);
  206. width: 100%;
  207. margin: 0 auto;
  208. padding-top: px2rpx(20);
  209. box-sizing: border-box;
  210. display: flex;
  211. flex-direction: column;
  212. justify-content: space-between;
  213. min-height: calc(100vh - 56px);
  214. }
  215. .fixed {
  216. // position: fixed;
  217. // top: 0;
  218. // left: 0;
  219. width: 100%;
  220. height: 56px;
  221. background-color: var(--color-white);
  222. z-index: 9;
  223. }
  224. .custom-header {
  225. background-color: var(--color-white);
  226. padding: 0 px2rpx(15);
  227. position: fixed;
  228. top: 56px;
  229. left: 0;
  230. }
  231. @media screen and (max-width: 1504px) {
  232. .content-wrapper-padding {
  233. padding: px2rpx(16) px2rpx(16) 0 px2rpx(16);
  234. }
  235. }
  236. @media screen and (max-width: 991px) {
  237. .content-wrapper-padding {
  238. padding: px2rpx(16) px2rpx(16) 0 px2rpx(16);
  239. }
  240. .page-wrapper {
  241. height: 100vh;
  242. }
  243. }
  244. .mobile-menu-btn {
  245. width: px2rpx(64);
  246. height: px2rpx(64);
  247. display: flex;
  248. align-items: center;
  249. justify-content: center;
  250. }
  251. /* 页面切换动画 */
  252. .fade-enter-active,
  253. .fade-leave-active {
  254. transition: opacity 0.3s ease;
  255. }
  256. .fade-enter-from,
  257. .fade-leave-to {
  258. opacity: 0;
  259. }
  260. /* 侧边栏动画优化 */
  261. .left-sidebar {
  262. transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  263. }
  264. .sidebar-mask {
  265. transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  266. }
  267. /* 内容区域动画 */
  268. .content-wrapper {
  269. animation: slideIn 0.3s ease-out;
  270. }
  271. @keyframes slideIn {
  272. from {
  273. transform: translateY(20px);
  274. opacity: 0;
  275. }
  276. to {
  277. transform: translateY(0);
  278. opacity: 1;
  279. }
  280. }
  281. </style>