cwg-page-wrapper.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  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. let res = await customApi.PushMessageList({ type: null });
  209. if (res.code == 200) {
  210. if (res.data == null) {
  211. pushManager.value = {};
  212. } else {
  213. pushManager.value = res.data;
  214. }
  215. } else {
  216. uni.showToast({ title: res.msg, icon: 'none' });
  217. }
  218. }
  219. //获取原因列表
  220. const searchReasons = async () => {
  221. let res = await customApi.reasonsRefusalList({ type: null });
  222. if (res.code == 200) {
  223. if (res.data == null) {
  224. reasons.value = {};
  225. } else {
  226. reasons.value = res.data;
  227. }
  228. } else {
  229. uni.showToast({ title: res.msg, icon: 'none' });
  230. }
  231. }
  232. const pushToSingle = (newsType: number) => {
  233. switch (newsType) {
  234. case 3:
  235. router
  236. .push({ path: "/pages/customer/recording-history", query: { type: 4 } })
  237. .catch((arr) => arr);
  238. break;
  239. }
  240. }
  241. onMounted(() => {
  242. // 只在组件挂载时注册事件监听器
  243. uni.$once('open-ib', handleOpenIb)
  244. uni.$on('open-right-drawer', handleOpenRightDrawer)
  245. searchPush()
  246. searchReasons()
  247. initWebSocket()
  248. })
  249. onUnmounted(() => {
  250. // 在组件销毁时移除事件监听器
  251. uni.$off('open-ib', handleOpenIb)
  252. uni.$off('open-right-drawer', handleOpenRightDrawer)
  253. if (websock) {
  254. websock.close()
  255. websock = null
  256. }
  257. clearTimeout(reconnectTimer)
  258. })
  259. function openRightDrawer() {
  260. rightDrawerRef.value?.open()
  261. }
  262. const sidebarVisible = ref(false)
  263. const maskVisible = ref(false)
  264. function openLeftDrawer() {
  265. // 真正显示时 → 关闭
  266. if (maskVisible.value) {
  267. maskVisible.value = false
  268. setTimeout(() => {
  269. sidebarVisible.value = false
  270. }, 200)
  271. }
  272. // 隐藏时 → 打开
  273. else {
  274. sidebarVisible.value = true
  275. setTimeout(() => {
  276. maskVisible.value = true
  277. }, 200)
  278. }
  279. }
  280. function handleDrawerNavigate(path: string) {
  281. router.push(path)
  282. }
  283. async function handleDrawerLogout() {
  284. try {
  285. const res = await userApi.logout()
  286. if (res.code === 200) {
  287. userStore.clearUserInfo()
  288. router.push('/pages/login/index')
  289. }
  290. } catch (error) {
  291. userStore.clearUserInfo()
  292. router.push('/pages/login/index')
  293. }
  294. }
  295. onLoad(() => {
  296. updateRoute()
  297. })
  298. onShow(() => {
  299. updateRoute()
  300. })
  301. </script>
  302. <style lang="scss" scoped>
  303. @import "@/uni.scss";
  304. .page-wrapper {
  305. height: calc(100vh - (56px + var(--status-bar-height)));
  306. }
  307. .header-box {
  308. width: 100%;
  309. height: calc(56px + var(--status-bar-height));
  310. }
  311. .page-content {
  312. width: 100%;
  313. height: calc(100vh - (56px + var(--status-bar-height)));
  314. overflow: hidden;
  315. display: flex;
  316. flex-direction: column;
  317. position: relative;
  318. box-sizing: border-box;
  319. .content-info {
  320. height: calc(100vh - (56px + var(--status-bar-height)));
  321. overflow: auto;
  322. }
  323. }
  324. .left-sidebar {
  325. width: 0;
  326. overflow: hidden;
  327. position: absolute;
  328. left: 0;
  329. top: 0;
  330. z-index: 1000;
  331. background-color: #fff;
  332. transition: width 81ms cubic-bezier(0.4, 0, 0.2, 1);
  333. }
  334. .sidebar-mask {
  335. position: absolute;
  336. left: 0;
  337. top: 0;
  338. width: 100vw;
  339. height: calc(100vh - (56px + var(--status-bar-height)));
  340. background-color: rgba(0, 0, 0, 0.2);
  341. z-index: 101;
  342. }
  343. .mask-visible {
  344. background-color: rgba(0, 0, 0, 0);
  345. width: 100vw;
  346. height: calc(56px + var(--status-bar-height));
  347. }
  348. .sidebar-visible {
  349. width: px2rpx(280);
  350. }
  351. .content-wrapper {
  352. max-width: px2rpx(1224);
  353. width: 100%;
  354. margin: 0 auto;
  355. padding-top: px2rpx(20);
  356. box-sizing: border-box;
  357. display: flex;
  358. flex-direction: column;
  359. justify-content: space-between;
  360. min-height: calc(100vh - (56px + var(--status-bar-height)));
  361. }
  362. .chat-icon {
  363. width: px2rpx(50);
  364. height: px2rpx(50);
  365. border-radius: 50%;
  366. background-color: #cf1322;
  367. display: flex;
  368. align-items: center;
  369. justify-content: center;
  370. position: fixed;
  371. bottom: px2rpx(25);
  372. right: px2rpx(-25);
  373. z-index: 999;
  374. cursor: pointer;
  375. transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  376. box-shadow: 0 px2rpx(8) px2rpx(20) rgba(0, 0, 0, 0.15);
  377. will-change: transform;
  378. }
  379. .chat-icon:hover {
  380. transform: scale(1.1);
  381. }
  382. .chat-icon-expanded {
  383. bottom: px2rpx(20);
  384. right: px2rpx(20);
  385. transform: scale(1.1);
  386. box-shadow: 0 px2rpx(12) px2rpx(30) rgba(0, 0, 0, 0.2);
  387. }
  388. .chat-icon-hidden {
  389. display: none;
  390. }
  391. .fixed {
  392. // position: fixed;
  393. // top: 0;
  394. // left: 0;
  395. width: 100%;
  396. height: calc(56px + var(--status-bar-height));
  397. background-color: var(--color-white);
  398. z-index: 9;
  399. }
  400. .custom-header {
  401. background-color: var(--color-white);
  402. padding: 0 px2rpx(15);
  403. position: fixed;
  404. top: calc(var(--status-bar-height));
  405. left: 0;
  406. }
  407. @media screen and (max-width: 1504px) {
  408. .content-wrapper-padding {
  409. padding: px2rpx(16) px2rpx(16) 0 px2rpx(16);
  410. }
  411. }
  412. @media screen and (max-width: 991px) {
  413. .content-wrapper-padding {
  414. padding: px2rpx(16) px2rpx(16) 0 px2rpx(16);
  415. }
  416. .page-wrapper {
  417. height: 100vh;
  418. }
  419. }
  420. .mobile-menu-btn {
  421. width: px2rpx(64);
  422. height: px2rpx(64);
  423. display: flex;
  424. align-items: center;
  425. justify-content: center;
  426. }
  427. /* 页面切换动画 */
  428. .fade-enter-active,
  429. .fade-leave-active {
  430. transition: opacity 0.2s ease;
  431. }
  432. .fade-enter-from,
  433. .fade-leave-to {
  434. opacity: 0;
  435. }
  436. /* 侧边栏动画优化 */
  437. .left-sidebar {
  438. transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  439. }
  440. .sidebar-mask {
  441. transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  442. }
  443. /* 内容区域动画 */
  444. .content-wrapper {
  445. animation: slideIn 0.2s ease-out;
  446. }
  447. @keyframes slideIn {
  448. from {
  449. transform: translateY(20px);
  450. opacity: 0;
  451. }
  452. to {
  453. transform: translateY(0);
  454. opacity: 1;
  455. }
  456. }
  457. </style>