index.vue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  1. <template>
  2. <el-container
  3. v-loading="isAppLoadings" class="hc-layout-box"
  4. :class="{ 'yn-theme': !isYunNanProject, 'is-no-layout': !isNullES(isLayout) && isLayout === 'no' }"
  5. >
  6. <div v-if="appTheme === 'dark'" class="hc-app-bg-box">
  7. <img :src="appViewBg" alt="">
  8. </div>
  9. <el-header class="hc-layout-header">
  10. <div v-if="!isYunNanProject" class="hc-layout-header-logo" :style="`width: ${isCollapse ? '0px' : '200px'};`" @click="logoClick">
  11. <!-- <img id="logo-icon" :src="appLogoIcon" alt=""> -->
  12. <img v-show="!isCollapse" id="logo-name" :src="appLogoName" alt="">
  13. </div>
  14. <div v-else class="hc-layout-header-logo" @click="logoClick">
  15. <img id="logo-icon" :src="appLogoIcon" alt="">
  16. <!-- <img v-show="!isCollapse" id="logo-name" :src="appLogoName" alt=""> -->
  17. <div class="ml-2">
  18. <div class="text-18px font-900">工程项目档案资料管理系统</div>
  19. <div class="text-12px">Engineering Project File Manegement Platform</div>
  20. </div>
  21. </div>
  22. <div class="header-top-collapse-bar" @click="collapseChange">
  23. <HcIcon v-if="isCollapse" name="menu-unfold" />
  24. <HcIcon v-else name="menu-fold" />
  25. </div>
  26. <div class="header-top-menu-bar">
  27. <HcTopMenuBar v-if="!isYunNanProject" @load="topMenuLoad" @change="topMenuChange" />
  28. </div>
  29. <div class="header-content-bar">
  30. <HcCascader v-if="userRoleId !== website.role_id" @send="cascaderSend" @change="cascaderChange" />
  31. <div v-else class="hc-header-project-name-box" @click="userProjectClick">
  32. <HcIcon name="arrow-right" class="project-icon" />
  33. <div class="truncate">{{ projectInfoData?.projectName ?? '请先选择项目' }}</div>
  34. </div>
  35. <hc-upload-bar v-if="!isYunNanProject" />
  36. <HelpInfoBar v-if="!isYunNanProject" />
  37. <ConfigBar v-if="!isYunNanProject" />
  38. <UserInfoBar @load="userInfoLoad" />
  39. </div>
  40. </el-header>
  41. <el-container class="hc-layout-container">
  42. <el-aside v-if="isAsideMenu" class="hc-layout-aside" :class="[isCollapse ? 'is-collapse' : '']" :width="isCollapse ? '90px' : '200px'">
  43. <MenuBar :collapse="isCollapse" :cur="menuBarKey" :datas="menuBarData" :msg-count="msgCount" @change="menuBarChange" />
  44. </el-aside>
  45. <el-main class="hc-layout-main">
  46. <div class="hc-router-menu-bar">
  47. <RouterMenu @load="routerMenuLoad" />
  48. </div>
  49. <div id="hc-main-box" class="hc-main-page">
  50. <div class="hc-main-body">
  51. <router-view v-if="reloadRouter" v-slot="{ Component }">
  52. <transition name="fade-transform">
  53. <keep-alive :max="10" exclude="HcDataV">
  54. <component :is="Component" :msg-count="msgCount" :msg-change="msgChange" />
  55. </keep-alive>
  56. </transition>
  57. </router-view>
  58. </div>
  59. </div>
  60. </el-main>
  61. </el-container>
  62. <hc-reminder v-model="isReminderShow" :text="isReminderText" />
  63. </el-container>
  64. </template>
  65. <script setup>
  66. import { computed, nextTick, onMounted, onUnmounted, ref, watch } from 'vue'
  67. import { getArrValue, getObjValue, isNullES, useClick } from 'js-fast-way'
  68. import { HcSocket } from '~src/plugins/HcSocket'
  69. import { useRoute, useRouter } from 'vue-router'
  70. import { useAppStore } from '~src/store'
  71. import { initButtons } from '~sto/app'
  72. import { HcAnnouncement } from 'hc-vue3-ui'
  73. import { useProject } from '~sto/useProject'
  74. import website from '~src/config'
  75. import appLogoIcon from '~src/assets/logo/yunnan.png'
  76. //初始组合式
  77. const router = useRouter()
  78. const useRoutes = useRoute()
  79. const store = useAppStore()
  80. const reloadRouter = ref(false)
  81. const userRoleId = ref(store.getRoleId)
  82. const projectInfoData = ref(store.getProjectInfo)
  83. const projectId = ref(store.getProjectId)
  84. // 判断是否为云南项目
  85. const isYunNanProject = ref(projectId.value === '1904814720589430785')
  86. // 添加watch来更新isYunNanProject
  87. watch(() => projectId.value, (newProjectId) => {
  88. isYunNanProject.value = newProjectId === '1904814720589430785'
  89. })
  90. //子组件
  91. import HcTopMenuBar from './modules/HcTopMenu.vue'
  92. import HcCascader from './modules/Cascader.vue'
  93. import UserInfoBar from './modules/UserInfoBar.vue'
  94. import HelpInfoBar from './modules/HelpInfoBar.vue'
  95. import ConfigBar from './modules/ConfigBar.vue'
  96. import RouterMenu from './modules/RouterMenu.vue'
  97. import MenuBar from '~src/layout/modules/MenuBar.vue'
  98. import appViewBg from '~src/assets/view/bg.png'
  99. // logo
  100. const appLogoName = ref(store.getLogoName)
  101. const appTheme = ref(store.getTheme)
  102. //菜单数据
  103. const menuBarKey = ref('')
  104. const menuBarData = ref([])
  105. const isLayout = ref('')
  106. //获取项目信息
  107. const { isAppLoading } = useProject()
  108. // 在 setup 中添加
  109. //渲染完成
  110. onMounted(async () => {
  111. const layout = useRoutes?.query?.layout, layout2 = store.isLayout
  112. isLayout.value = layout ?? layout2
  113. annRefs.value = []
  114. initButtons()
  115. // 确保初始化时加载样式
  116. currentStyle = await loadStyles()
  117. })
  118. //监听layout
  119. watch(() => [useRoutes?.query?.layout, store.isLayout], ([layout, layout2]) => {
  120. isLayout.value = layout ?? layout2
  121. }, { deep: true })
  122. //监听
  123. watch(() => store.getTheme, (theme) => {
  124. appTheme.value = theme
  125. })
  126. //监听项目信息变化
  127. const isAppLoadings = ref(true)
  128. watch(() => isAppLoading.value, (res) => {
  129. if (!website.localModel) {
  130. reloadRouter.value = res
  131. isAppLoadings.value = !res
  132. } else {
  133. if (res) {
  134. setTimeout(() => {
  135. isAppLoadings.value = false
  136. }, 1000)
  137. } else {
  138. isAppLoadings.value = true
  139. }
  140. }
  141. }, { immediate:true })
  142. //路由信息
  143. const routerMenuLoad = ({ key }) => {
  144. menuBarKey.value = key
  145. }
  146. // 是否折叠
  147. const isCollapse = ref(false)
  148. const collapseChange = () => {
  149. const bool = !isCollapse.value
  150. isCollapse.value = bool
  151. store.setCollapse(bool)
  152. }
  153. //顶部菜单导航
  154. const isAsideMenu = ref(true)
  155. const topMenuLoad = () => {
  156. isAsideMenu.value = false
  157. }
  158. // 修改菜单数据的监听逻辑
  159. watch(() => store.getMenus, (val) => {
  160. if (isYunNanProject.value) {
  161. const filterAndRenameMenus = (menus) => {
  162. return menus.map(menu => {
  163. // 创建新对象,而不是修改原对象
  164. const newMenu = { ...menu }
  165. // 过滤掉个人中心和系统设置
  166. if (newMenu.name === '个人中心' || newMenu.name === '系统设置') {
  167. return null
  168. }
  169. // 重命名档案收集为文件管理
  170. if (newMenu.name === '档案收集') {
  171. newMenu.name = '文件管理'
  172. }
  173. // 递归处理子菜单
  174. if (newMenu.children && newMenu.children.length > 0) {
  175. newMenu.children = filterAndRenameMenus(newMenu.children)
  176. }
  177. return newMenu
  178. }).filter(Boolean) // 过滤掉null值
  179. }
  180. menuBarData.value = filterAndRenameMenus(getArrValue(val))
  181. }
  182. }, { immediate: true })
  183. //顶部菜单导航被点击// 修改顶部菜单切换方法
  184. const topMenuChange = (data) => {
  185. if (!isYunNanProject.value && !isNullES(data)) {
  186. menuBarData.value = data
  187. isAsideMenu.value = true
  188. }
  189. }
  190. //菜单被点击
  191. const menuBarChange = ({ code }) => {
  192. menuBarKey.value = code
  193. if (code === 'statistics-datav') {
  194. // 如果跳转到数据看板,记录当前路由作为来源路径
  195. const currentRoute = router.currentRoute.value
  196. router.push({
  197. name: code,
  198. query: {
  199. from: currentRoute.fullPath,
  200. },
  201. })
  202. } else {
  203. router.push({ name: code })
  204. }
  205. }
  206. //消息数量
  207. const msgCount = ref({
  208. allCount: 0,
  209. taskCount: 0,
  210. messageCount: 0,
  211. messageCount_1: 0,
  212. messageCount_2: 0,
  213. messageCount_3: 0,
  214. messageCount_4: 0,
  215. messageCount_5: 0,
  216. })
  217. const msgChange = ref(0)
  218. //用户信息
  219. const userId = ref('')
  220. const userInfoLoad = ({ user_id }) => {
  221. userId.value = user_id
  222. }
  223. //项目合同段的ID
  224. let socket
  225. const cascaderSend = async ({ projectId, contractId }) => {
  226. await useClick()
  227. if (isNullES(contractId)) {
  228. //本地模式
  229. if (website.localModel) {
  230. window.$message?.error('项目信息不存在,请联系管理员')
  231. reloadRouter.value = false
  232. }
  233. return
  234. }
  235. //本地模式
  236. if (website.localModel) {
  237. setTimeout(() => {
  238. reloadRouter.value = true
  239. }, 1000)
  240. } else {
  241. reloadRouter.value = true
  242. }
  243. //链接webSocket
  244. if (!isNullES(socket)) socket.close()
  245. socket = new HcSocket({ projectId, contractId, userId: userId.value }, (res) => {
  246. socketData(res?.data)
  247. })
  248. }
  249. //长链接消息
  250. let annUpdateRef
  251. const annRefs = ref([])
  252. const isReminderShow = ref(false)
  253. const isReminderText = ref('')
  254. const socketData = async (res) => {
  255. console.log('socket:', res)
  256. const { type, data } = getObjValue(res)
  257. if (type === 'msgUpdateMsg') {
  258. closeAnnUpdate()
  259. //内容为空时,代表公告已经取消,由于前面已经关闭,所以不再创建
  260. if (isNullES(data)) return
  261. await nextTick()
  262. //系统更新公告,直接替换
  263. annUpdateRef = await HcAnnouncement({ type: 'update', data: data })
  264. } else if (type === 'msgSystemMsg') {
  265. //内容为空时,代表公告已经取消,由于前面已经关闭,所以不再创建
  266. if (isNullES(data)) {
  267. closeAnnFun()
  268. return
  269. }
  270. await nextTick()
  271. //普通公告,追加公告
  272. const ref = await HcAnnouncement({ type: 'system', data: data })
  273. annRefs.value.push(ref)
  274. } else if (type === 'msgLink') {
  275. if (store.isLogin) {
  276. socket.send('getMsg')
  277. store.setIsLogin(false)
  278. }
  279. } else if (type === 'msgCountDown') {
  280. //倒计时更新
  281. if (isNullES(data) || data <= 0) {
  282. closeReminder()
  283. return
  284. }
  285. isReminderText.value = `系统将在${data}秒后,进行更新`
  286. setReminderText(data - 1)
  287. isReminderShow.value = true
  288. }
  289. }
  290. //倒计时
  291. let timeRef
  292. let startTime
  293. const setReminderText = (totalTime) => {
  294. startTime = performance.now()
  295. const step = () => {
  296. const elapsedTime = Math.floor((performance.now() - startTime) / 1000)
  297. const remainingTime = totalTime - elapsedTime
  298. if (remainingTime < 0) {
  299. closeReminder()
  300. return
  301. }
  302. isReminderText.value = `系统将在${remainingTime}秒后,进行更新`
  303. requestAnimationFrame(step)
  304. }
  305. requestAnimationFrame(step)
  306. }
  307. //关闭倒计时
  308. const closeReminder = () => {
  309. isReminderShow.value = false
  310. if (!isNullES(timeRef)) {
  311. clearTimeout(timeRef)
  312. timeRef = null
  313. }
  314. }
  315. // 项目切换
  316. const cascaderChange = () => {
  317. reloadRouter.value = false
  318. }
  319. //首页
  320. const logoClick = () => {
  321. router.push({ name: 'statistics-datav' })
  322. }
  323. const userProjectClick = () => {
  324. router.push({ path: '/user/project' })
  325. }
  326. //关闭普通公告
  327. const closeAnnFun = () => {
  328. const refs = annRefs.value
  329. for (let i = 0; i < refs.length; i++) {
  330. if (!isNullES(refs[i])) {
  331. refs[i]?.close()
  332. }
  333. }
  334. annRefs.value = []
  335. }
  336. //关闭系统更新公告
  337. const closeAnnUpdate = () => {
  338. if (!isNullES(annUpdateRef)) {
  339. annUpdateRef.close()
  340. annUpdateRef = null
  341. }
  342. }
  343. //页面卸载
  344. onUnmounted(() => {
  345. if (!isNullES(socket)) socket.close()
  346. closeAnnFun()
  347. closeAnnUpdate()
  348. return () => {
  349. if (currentStyle) {
  350. document.head.removeChild(currentStyle)
  351. }
  352. }
  353. })
  354. // 动态加载样式
  355. const loadStyles = async () => {
  356. try {
  357. // 1. 首先移除所有已存在的样式
  358. const existingStyles = document.head.querySelectorAll('style[data-theme]')
  359. existingStyles.forEach(style => {
  360. document.head.removeChild(style)
  361. })
  362. // 2. 创建新的样式元素
  363. const styleElement = document.createElement('style')
  364. styleElement.type = 'text/css'
  365. // 添加标识,方便后续清理
  366. styleElement.setAttribute('data-theme', isYunNanProject.value ? 'yunnan' : 'default')
  367. // 3. 加载对应的样式模块
  368. const module = isYunNanProject.value
  369. ? await import('./test-yn/index-yn.scss?inline')
  370. : await import('./index.scss?inline')
  371. styleElement.textContent = module.default
  372. document.head.appendChild(styleElement)
  373. currentStyle = styleElement
  374. // 4. 强制重新计算样式
  375. document.documentElement.style.display = 'none'
  376. document.documentElement.offsetHeight // 触发重排
  377. document.documentElement.style.display = ''
  378. return styleElement
  379. } catch (error) {
  380. console.error('加载样式失败:', error)
  381. return null
  382. }
  383. }
  384. let currentStyle = null
  385. // 监听项目类型变化,动态切换样式
  386. watch(() => isYunNanProject.value, async (newVal) => {
  387. await loadStyles()
  388. console.log('样式已更新:', newVal ? 'yunnan' : 'default')
  389. }, { immediate: true })
  390. // 添加watch来更新isYunNanProject
  391. watch(() =>store.getProjectId, (newProjectId) => {
  392. isYunNanProject.value = newProjectId === '1904814720589430785'
  393. //重新加载菜单
  394. let val = store.getMenus
  395. if (!isYunNanProject.value) {
  396. return
  397. }
  398. const filterAndRenameMenus = (menus) => {
  399. return menus.map(menu => {
  400. // 创建新对象,而不是修改原对象
  401. const newMenu = { ...menu }
  402. // 过滤掉个人中心和系统设置
  403. if (newMenu.name === '个人中心' || newMenu.name === '系统设置') {
  404. return null
  405. }
  406. // 重命名档案收集为文件管理
  407. if (newMenu.name === '档案收集') {
  408. newMenu.name = '文件管理'
  409. }
  410. // 递归处理子菜单
  411. if (newMenu.children && newMenu.children.length > 0) {
  412. newMenu.children = filterAndRenameMenus(newMenu.children)
  413. }
  414. return newMenu
  415. }).filter(Boolean) // 过滤掉null值
  416. }
  417. menuBarData.value = filterAndRenameMenus(getArrValue(val))
  418. isAsideMenu.value = true
  419. })
  420. // 组件卸载时清理样式
  421. </script>