cwg-page-wrapper.vue 13 KB

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