cwg-page-wrapper.vue 12 KB

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