cwg-page-wrapper.vue 13 KB

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