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