cwg-page-wrapper.vue 13 KB

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