cwg-page-wrapper.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607
  1. <template>
  2. <view :class="['page-wrapper bg-body', { dark: isDark }]">
  3. <cwg-match-media :max-width="991" v-if="!isLoginPage">
  4. <cwg-pc-header @open-right-drawer="openRightDrawer" @open-left-drawer="openLeftDrawer"
  5. :sidebarVisible="sidebarVisible" v-if="type != 'webview'" />
  6. <view class="sidebar-mask mask-visible" v-if="sidebarVisible" @click="openLeftDrawer">
  7. </view>
  8. <view class="fixed" v-if=pageTitle></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. <view class="page-content" :style="{ backgroundColor: bgColor }">
  14. <cwg-match-media :max-width="991" v-if="!isLoginPage">
  15. <view class="left-sidebar" :class="{ 'sidebar-visible': sidebarVisible }">
  16. <cwg-sidebar @handle-click="openLeftDrawer" />
  17. </view>
  18. <view class="sidebar-mask" v-if="sidebarVisible" @click="openLeftDrawer" :style="{
  19. opacity: maskVisible ? 1 : 0,
  20. transition: 'opacity 0.3s cubic-bezier(0.4, 0, 0.2, 1)'
  21. }">
  22. </view>
  23. </cwg-match-media>
  24. <view class="content-info">
  25. <!-- 完善信息提示,-->
  26. <PrefectInfo v-if="!isLoginPage" />
  27. <!-- 申请成为ib,-->
  28. <IbInfo ref="ibRef" v-if="!isLoginPage" />
  29. <view class="content-wrapper" :class="{ 'content-wrapper-padding': isContentPadding }">
  30. <!-- <cwg-header /> -->
  31. <transition name="fade" mode="out-in">
  32. <view>
  33. <slot />
  34. </view>
  35. </transition>
  36. <cwg-custom-footer v-if=!pageTitle />
  37. <cwg-match-media :max-width="991" v-if="!isLoginPage">
  38. <cwg-footer-link v-if=!pageTitle />
  39. </cwg-match-media>
  40. </view>
  41. </view>
  42. <view class="chat-icon" :class="{ 'chat-icon-expanded': isChatIconExpanded, 'chat-icon-hidden': type == 'chat' }"
  43. @click="handleChatIconClick">
  44. <cwg-icon name="chat" color="#fff" />
  45. </view>
  46. <view :style="{ height: isTabBarPage ? '60px' : '0px' }" />
  47. </view>
  48. <cwg-match-media :min-width="991" v-if="!isLoginPage">
  49. <cwg-footer-link />
  50. </cwg-match-media>
  51. <cwg-match-media :min-width="992" v-if="!isLoginPage">
  52. <view class="desktop-sidebar-toggle-fixed" :style="toggleFixedStyle">
  53. <cwg-sidebar-toggle />
  54. </view>
  55. </cwg-match-media>
  56. </view>
  57. </template>
  58. <script setup lang="ts">
  59. import { ref, computed, onMounted, onUnmounted } from 'vue'
  60. import { onLoad, onShow } from '@dcloudio/uni-app'
  61. import useRoute, { updateRoute } from '@/hooks/useRoute'
  62. import useRouter from '@/hooks/useRouter'
  63. import useUserStore from '@/stores/use-user-store'
  64. import { userApi } from '@/api/user'
  65. import useGlobalStore from '@/stores/use-global-store'
  66. import PrefectInfo from '@/components/PrefectInfo.vue'
  67. import IbInfo from '@/components/IbInfo.vue'
  68. import { userToken } from "@/composables/config";
  69. import { useI18n } from 'vue-i18n'
  70. import Config from '@/config/index'
  71. import { customApi } from '@/service/custom'
  72. import tool from "@/global/tool"
  73. import LiveChatService from '@/utils/liveChat.js'
  74. import { useWindowWidth } from '@/composables/useWindowWidth'
  75. import { useLeftSidebarCollapse } from '@/composables/useLeftSidebarCollapse'
  76. const windowWidth = useWindowWidth(300)
  77. const isMobile = computed(() => windowWidth.value <= 991)
  78. const { isCollapsed, syncCollapsedFromDom } = useLeftSidebarCollapse()
  79. const SIDEBAR_WIDTH_EXPANDED = 280
  80. const SIDEBAR_WIDTH_COLLAPSED = 64
  81. const toggleFixedStyle = computed(() => {
  82. const width = isCollapsed.value ? SIDEBAR_WIDTH_COLLAPSED : SIDEBAR_WIDTH_EXPANDED
  83. return {
  84. left: `${width}px`,
  85. }
  86. })
  87. const { t, locale } = useI18n()
  88. const globalStore = useGlobalStore()
  89. const router = useRouter()
  90. const userStore = useUserStore()
  91. const props = defineProps({
  92. // 是否固定顶部导航栏
  93. isHeaderFixed: {
  94. type: Boolean,
  95. default: false,
  96. },
  97. // 是否登录页,登录页不显示侧边栏和顶部导航,注册忘记密码等页面
  98. isLoginPage: {
  99. type: Boolean,
  100. default: false,
  101. },
  102. // 是否含有padding 默认有
  103. isContentPadding: {
  104. type: Boolean,
  105. default: true,
  106. },
  107. // 主内容背景颜色
  108. bgColor: {
  109. type: String,
  110. default: '',
  111. },
  112. // 顶部导航栏标题
  113. pageTitle: {
  114. type: String,
  115. default: '',
  116. },
  117. type: {
  118. type: String,
  119. default: '',
  120. },
  121. })
  122. const isDark = computed(() => globalStore.theme === 'dark')
  123. const isTabBarPage = ref(false)
  124. const rightDrawerRef = ref<any>(null)
  125. const noticeDrawerRef = ref<any>(null)
  126. const ibRef = ref<any>(null)
  127. const isChatIconExpanded = ref(false)
  128. // 事件处理函数
  129. const handleOpenIb = () => {
  130. if (ibRef.value) ibRef.value?.getInfo()
  131. }
  132. const handleOpenNoticeDrawer = () => {
  133. noticeDrawerRef.value?.open()
  134. }
  135. // 处理聊天图标点击
  136. const handleChatIconClick = () => {
  137. // 如果还没显示 → 先滑出来
  138. if (!isChatIconExpanded.value) {
  139. isChatIconExpanded.value = true
  140. return
  141. }
  142. if (isMobile.value) {
  143. router.push('/pages/common/chat')
  144. } else {
  145. if (LiveChatService) {
  146. LiveChatService.showChat()
  147. }
  148. }
  149. setTimeout(() => {
  150. isChatIconExpanded.value = false
  151. }, 300)
  152. }
  153. const handleOpenRightDrawer = () => {
  154. openRightDrawer()
  155. }
  156. // ========== WebSocket ==========
  157. let websock: any = null
  158. let reconnectTimer: any = null
  159. const pushManager = ref<any>({})
  160. const reasons = ref<any>({})
  161. const initWebSocket = () => {
  162. let token = userToken.value || uni.getStorageSync('token')
  163. if (!token) return
  164. token = tool.tokenReplace(token);
  165. const wsUrl = `${Config.HostWs}/webSocket?Access-Token=${token}`
  166. websock = uni.connectSocket({
  167. url: wsUrl,
  168. success: () => {
  169. console.log('WebSocket connected successfully')
  170. },
  171. fail: () => {
  172. uni.showToast({ title: t('Msg.socket'), icon: 'none' })
  173. }
  174. })
  175. websock.onOpen(() => {
  176. console.log('WebSocket opened')
  177. })
  178. websock.onError(() => {
  179. clearTimeout(reconnectTimer)
  180. reconnectTimer = setTimeout(() => {
  181. initWebSocket()
  182. }, 3000)
  183. })
  184. websock.onMessage((res: any) => {
  185. console.log('接受道消息', res)
  186. try {
  187. const data = JSON.parse(res.data)
  188. if (data.newsType == 3) {
  189. pushRes(data)
  190. }
  191. if (data.newsType == 1) {
  192. // 认证更新文件信息
  193. uni.$emit('updateImproveFile', data)
  194. }
  195. if (data.newsType == 4) {
  196. // 全局广播未读消息数量更新
  197. uni.$emit('updateUnreadCount', data.count)
  198. }
  199. } catch (e) {
  200. console.error('WebSocket parse error:', e)
  201. }
  202. })
  203. websock.onClose(() => {
  204. // console.log('WebSocket closed')
  205. })
  206. }
  207. const pushRes = (data: any) => {
  208. const isCn = ['cn', 'zhHant'].includes(locale.value)
  209. let part1 = ''
  210. if (data.pushMessageId && pushManager.value[data.pushMessageId]) {
  211. part1 = isCn
  212. ? pushManager.value[data.pushMessageId].content
  213. : pushManager.value[data.pushMessageId].enContent
  214. }
  215. let part2 = ''
  216. if (data.approveDesc && reasons.value[data.approveDesc]) {
  217. part2 = isCn
  218. ? reasons.value[data.approveDesc].content
  219. : reasons.value[data.approveDesc].enContent
  220. }
  221. const msg = `${part1}\n${part2}`.trim()
  222. uni.showModal({
  223. title: t("news_add_field.Label.Tips"),
  224. content: msg || t('news_add_field.Label.Tips'),
  225. showCancel: false,
  226. confirmText: t('Btn.Confirm'),
  227. success: (res) => {
  228. if (res.confirm) {
  229. pushToSingle(data.newsType)
  230. }
  231. }
  232. })
  233. }
  234. //获取推送列表
  235. const searchPush = async () => {
  236. if (!userToken.value) return
  237. let res = await customApi.PushMessageList({ type: null });
  238. if (res.code == 200) {
  239. if (res.data == null) {
  240. pushManager.value = {};
  241. } else {
  242. pushManager.value = res.data;
  243. }
  244. } else {
  245. uni.showToast({ title: res.msg, icon: 'none' });
  246. }
  247. }
  248. //获取原因列表
  249. const searchReasons = async () => {
  250. if (!userToken.value) return
  251. let res = await customApi.reasonsRefusalList({ type: null });
  252. if (res.code == 200) {
  253. if (res.data == null) {
  254. reasons.value = {};
  255. } else {
  256. reasons.value = res.data;
  257. }
  258. } else {
  259. uni.showToast({ title: res.msg, icon: 'none' });
  260. }
  261. }
  262. const pushToSingle = (newsType: number) => {
  263. switch (newsType) {
  264. case 3:
  265. router
  266. .push({ path: "/pages/customer/recording-history", query: { type: 4 } })
  267. .catch((arr) => arr);
  268. break;
  269. }
  270. }
  271. onMounted(() => {
  272. syncCollapsedFromDom()
  273. // 只在组件挂载时注册事件监听器
  274. uni.$once('open-ib', handleOpenIb)
  275. uni.$on('open-right-drawer', handleOpenRightDrawer)
  276. searchPush()
  277. searchReasons()
  278. initWebSocket()
  279. })
  280. onUnmounted(() => {
  281. // 在组件销毁时移除事件监听器
  282. uni.$off('open-ib', handleOpenIb)
  283. uni.$off('open-right-drawer', handleOpenRightDrawer)
  284. if (websock) {
  285. websock.close()
  286. websock = null
  287. }
  288. clearTimeout(reconnectTimer)
  289. })
  290. function openRightDrawer() {
  291. rightDrawerRef.value?.open()
  292. }
  293. const sidebarVisible = ref(false)
  294. const maskVisible = ref(false)
  295. let sidebarTimer: any = null
  296. let isAnimating = ref(false)
  297. function debounce<T extends (...args: any[]) => void>(fn: T, delay: number): T {
  298. let timer: any = null
  299. return ((...args: any[]) => {
  300. if (timer) clearTimeout(timer)
  301. timer = setTimeout(() => fn(...args), delay)
  302. }) as T
  303. }
  304. const toggleSidebar = debounce(() => {
  305. if (isAnimating.value) return
  306. isAnimating.value = true
  307. if (maskVisible.value) {
  308. maskVisible.value = false
  309. sidebarTimer = setTimeout(() => {
  310. sidebarVisible.value = false
  311. isAnimating.value = false
  312. sidebarTimer = null
  313. }, 200)
  314. } else {
  315. sidebarVisible.value = true
  316. sidebarTimer = setTimeout(() => {
  317. maskVisible.value = true
  318. isAnimating.value = false
  319. sidebarTimer = null
  320. }, 200)
  321. }
  322. }, 300)
  323. function openLeftDrawer() {
  324. if (sidebarTimer) {
  325. clearTimeout(sidebarTimer)
  326. sidebarTimer = null
  327. }
  328. toggleSidebar()
  329. }
  330. function handleDrawerNavigate(path: string) {
  331. router.push(path)
  332. }
  333. async function handleDrawerLogout() {
  334. try {
  335. const res = await userApi.logout()
  336. if (res.code === 200) {
  337. userStore.clearUserInfo()
  338. router.push('/pages/login/index')
  339. }
  340. } catch (error) {
  341. userStore.clearUserInfo()
  342. router.push('/pages/login/index')
  343. }
  344. }
  345. onLoad(() => {
  346. updateRoute()
  347. })
  348. onShow(() => {
  349. updateRoute()
  350. })
  351. </script>
  352. <style lang="scss" scoped>
  353. @import "@/uni.scss";
  354. .page-wrapper {
  355. height: calc(100vh - (56px + var(--status-bar-height)));
  356. }
  357. .header-box {
  358. width: 100%;
  359. height: calc(56px + var(--status-bar-height));
  360. }
  361. .page-content {
  362. width: 100%;
  363. height: calc(100vh - (110px + var(--status-bar-height)));
  364. overflow: hidden;
  365. display: flex;
  366. flex-direction: column;
  367. position: relative;
  368. box-sizing: border-box;
  369. @media screen and (max-width: 768px) {
  370. height: calc(100vh - (56px + var(--status-bar-height)));
  371. }
  372. .content-info {
  373. height: calc(100vh - (110px + var(--status-bar-height)));
  374. overflow: auto;
  375. @media screen and (max-width: 768px) {
  376. height: calc(100vh - (56px + var(--status-bar-height)));
  377. }
  378. }
  379. }
  380. .left-sidebar {
  381. width: 0;
  382. overflow: hidden;
  383. position: absolute;
  384. left: 0;
  385. top: 0;
  386. z-index: 1000;
  387. background-color: rgba(var(--bs-body-bg-rgb), var(--bs-bg-opacity)) !important;
  388. transition: width 81ms cubic-bezier(0.4, 0, 0.2, 1);
  389. }
  390. .sidebar-mask {
  391. position: absolute;
  392. left: 0;
  393. top: 0;
  394. width: 100vw;
  395. height: calc(100vh - (56px + var(--status-bar-height)));
  396. background-color: rgba(0, 0, 0, 0.2);
  397. z-index: 101;
  398. }
  399. .mask-visible {
  400. background-color: rgba(0, 0, 0, 0);
  401. width: 100vw;
  402. height: calc(56px + var(--status-bar-height));
  403. }
  404. .sidebar-visible {
  405. width: px2rpx(280);
  406. }
  407. .content-wrapper {
  408. max-width: px2rpx(1320);
  409. width: 100%;
  410. margin: 0 auto;
  411. padding-top: px2rpx(20);
  412. box-sizing: border-box;
  413. display: flex;
  414. flex-direction: column;
  415. justify-content: space-between;
  416. min-height: calc(100vh - (110px + var(--status-bar-height)));
  417. @media screen and (max-width: 768px) {
  418. min-height: calc(100vh - (56px + var(--status-bar-height)));
  419. }
  420. }
  421. .chat-icon {
  422. width: px2rpx(50);
  423. height: px2rpx(50);
  424. border-radius: 50%;
  425. background-color: #cf1322;
  426. display: flex;
  427. align-items: center;
  428. justify-content: center;
  429. position: fixed;
  430. bottom: px2rpx(25);
  431. right: px2rpx(0);
  432. z-index: 999;
  433. cursor: pointer;
  434. transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  435. box-shadow: 0 px2rpx(8) px2rpx(20) rgba(0, 0, 0, 0.15);
  436. will-change: transform;
  437. }
  438. @media (max-width: 991px) {
  439. .chat-icon{
  440. right: px2rpx(-25);
  441. }
  442. }
  443. .chat-icon:hover {
  444. transform: scale(1.1);
  445. }
  446. .chat-icon-expanded {
  447. bottom: px2rpx(20);
  448. right: px2rpx(20);
  449. transform: scale(1.1);
  450. box-shadow: 0 px2rpx(12) px2rpx(30) rgba(0, 0, 0, 0.2);
  451. }
  452. .chat-icon-hidden {
  453. display: none;
  454. }
  455. .fixed {
  456. // position: fixed;
  457. // top: 0;
  458. // left: 0;
  459. width: 100%;
  460. height: calc(56px + var(--status-bar-height));
  461. --bs-bg-opacity: 1;
  462. background-color: rgba(var(--bs-body-bg-rgb), var(--bs-bg-opacity)) !important;
  463. z-index: 9;
  464. }
  465. .custom-header {
  466. --bs-bg-opacity: 1;
  467. background-color: rgba(var(--bs-body-bg-rgb), var(--bs-bg-opacity)) !important;
  468. padding: 0 px2rpx(15);
  469. position: fixed;
  470. top: calc(var(--status-bar-height));
  471. left: 0;
  472. }
  473. .content-wrapper-padding {
  474. padding: px2rpx(16) px2rpx(16) 0 px2rpx(16);
  475. }
  476. @media screen and (max-width: 991px) {
  477. .page-wrapper {
  478. height: 100vh;
  479. }
  480. }
  481. .mobile-menu-btn {
  482. width: px2rpx(64);
  483. height: px2rpx(64);
  484. display: flex;
  485. align-items: center;
  486. justify-content: center;
  487. }
  488. /* 页面切换动画 */
  489. .fade-enter-active,
  490. .fade-leave-active {
  491. transition: opacity 0.2s ease;
  492. }
  493. .fade-enter-from,
  494. .fade-leave-to {
  495. opacity: 0;
  496. }
  497. /* 侧边栏动画优化 */
  498. .left-sidebar {
  499. transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  500. }
  501. .sidebar-mask {
  502. transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  503. }
  504. /* 内容区域动画 */
  505. .content-wrapper {
  506. animation: slideIn 0.2s ease-out;
  507. }
  508. @keyframes slideIn {
  509. from {
  510. transform: translateY(20px);
  511. opacity: 0;
  512. }
  513. to {
  514. transform: translateY(0);
  515. opacity: 1;
  516. }
  517. }
  518. .desktop-sidebar-toggle-fixed {
  519. position: fixed;
  520. top: 56px;
  521. z-index: 120;
  522. display: flex;
  523. align-items: center;
  524. height: 48px;
  525. padding: 0 8px;
  526. box-sizing: border-box;
  527. transition: left 281ms cubic-bezier(0.4, 0, 0.2, 1);
  528. }
  529. </style>