cwg-page-wrapper.vue 12 KB

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