cwg-page-wrapper.vue 8.2 KB

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