cwg-page-wrapper.vue 12 KB

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