cwg-page-wrapper.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  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. function openLeftDrawer() {
  267. // 真正显示时 → 关闭
  268. if (maskVisible.value) {
  269. maskVisible.value = false
  270. setTimeout(() => {
  271. sidebarVisible.value = false
  272. }, 200)
  273. }
  274. // 隐藏时 → 打开
  275. else {
  276. sidebarVisible.value = true
  277. setTimeout(() => {
  278. maskVisible.value = true
  279. }, 200)
  280. }
  281. }
  282. function handleDrawerNavigate(path: string) {
  283. router.push(path)
  284. }
  285. async function handleDrawerLogout() {
  286. try {
  287. const res = await userApi.logout()
  288. if (res.code === 200) {
  289. userStore.clearUserInfo()
  290. router.push('/pages/login/index')
  291. }
  292. } catch (error) {
  293. userStore.clearUserInfo()
  294. router.push('/pages/login/index')
  295. }
  296. }
  297. onLoad(() => {
  298. updateRoute()
  299. })
  300. onShow(() => {
  301. updateRoute()
  302. })
  303. </script>
  304. <style lang="scss" scoped>
  305. @import "@/uni.scss";
  306. .page-wrapper {
  307. height: calc(100vh - (56px + var(--status-bar-height)));
  308. }
  309. .header-box {
  310. width: 100%;
  311. height: calc(56px + var(--status-bar-height));
  312. }
  313. .page-content {
  314. width: 100%;
  315. height: calc(100vh - (56px + var(--status-bar-height)));
  316. overflow: hidden;
  317. display: flex;
  318. flex-direction: column;
  319. position: relative;
  320. box-sizing: border-box;
  321. .content-info {
  322. height: calc(100vh - (56px + var(--status-bar-height)));
  323. overflow: auto;
  324. }
  325. }
  326. .left-sidebar {
  327. width: 0;
  328. overflow: hidden;
  329. position: absolute;
  330. left: 0;
  331. top: 0;
  332. z-index: 1000;
  333. background-color: #fff;
  334. transition: width 81ms cubic-bezier(0.4, 0, 0.2, 1);
  335. }
  336. .sidebar-mask {
  337. position: absolute;
  338. left: 0;
  339. top: 0;
  340. width: 100vw;
  341. height: calc(100vh - (56px + var(--status-bar-height)));
  342. background-color: rgba(0, 0, 0, 0.2);
  343. z-index: 101;
  344. }
  345. .mask-visible {
  346. background-color: rgba(0, 0, 0, 0);
  347. width: 100vw;
  348. height: calc(56px + var(--status-bar-height));
  349. }
  350. .sidebar-visible {
  351. width: px2rpx(280);
  352. }
  353. .content-wrapper {
  354. max-width: px2rpx(1224);
  355. width: 100%;
  356. margin: 0 auto;
  357. padding-top: px2rpx(20);
  358. box-sizing: border-box;
  359. display: flex;
  360. flex-direction: column;
  361. justify-content: space-between;
  362. min-height: calc(100vh - (56px + var(--status-bar-height)));
  363. }
  364. .chat-icon {
  365. width: px2rpx(50);
  366. height: px2rpx(50);
  367. border-radius: 50%;
  368. background-color: #cf1322;
  369. display: flex;
  370. align-items: center;
  371. justify-content: center;
  372. position: fixed;
  373. bottom: px2rpx(25);
  374. right: px2rpx(-25);
  375. z-index: 999;
  376. cursor: pointer;
  377. transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  378. box-shadow: 0 px2rpx(8) px2rpx(20) rgba(0, 0, 0, 0.15);
  379. will-change: transform;
  380. }
  381. .chat-icon:hover {
  382. transform: scale(1.1);
  383. }
  384. .chat-icon-expanded {
  385. bottom: px2rpx(20);
  386. right: px2rpx(20);
  387. transform: scale(1.1);
  388. box-shadow: 0 px2rpx(12) px2rpx(30) rgba(0, 0, 0, 0.2);
  389. }
  390. .chat-icon-hidden {
  391. display: none;
  392. }
  393. .fixed {
  394. // position: fixed;
  395. // top: 0;
  396. // left: 0;
  397. width: 100%;
  398. height: calc(56px + var(--status-bar-height));
  399. background-color: var(--color-white);
  400. z-index: 9;
  401. }
  402. .custom-header {
  403. background-color: var(--color-white);
  404. padding: 0 px2rpx(15);
  405. position: fixed;
  406. top: calc(var(--status-bar-height));
  407. left: 0;
  408. }
  409. @media screen and (max-width: 1504px) {
  410. .content-wrapper-padding {
  411. padding: px2rpx(16) px2rpx(16) 0 px2rpx(16);
  412. }
  413. }
  414. @media screen and (max-width: 991px) {
  415. .content-wrapper-padding {
  416. padding: px2rpx(16) px2rpx(16) 0 px2rpx(16);
  417. }
  418. .page-wrapper {
  419. height: 100vh;
  420. }
  421. }
  422. .mobile-menu-btn {
  423. width: px2rpx(64);
  424. height: px2rpx(64);
  425. display: flex;
  426. align-items: center;
  427. justify-content: center;
  428. }
  429. /* 页面切换动画 */
  430. .fade-enter-active,
  431. .fade-leave-active {
  432. transition: opacity 0.2s ease;
  433. }
  434. .fade-enter-from,
  435. .fade-leave-to {
  436. opacity: 0;
  437. }
  438. /* 侧边栏动画优化 */
  439. .left-sidebar {
  440. transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  441. }
  442. .sidebar-mask {
  443. transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  444. }
  445. /* 内容区域动画 */
  446. .content-wrapper {
  447. animation: slideIn 0.2s ease-out;
  448. }
  449. @keyframes slideIn {
  450. from {
  451. transform: translateY(20px);
  452. opacity: 0;
  453. }
  454. to {
  455. transform: translateY(0);
  456. opacity: 1;
  457. }
  458. }
  459. </style>