Main.vue 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <template>
  2. <div
  3. class="zhihui-main"
  4. :class="{
  5. 'open-sidebar': showNavTag && navTagType === 1 && !collapsed,
  6. 'hide-sidebar': showNavTag && navTagType === 1 && collapsed,
  7. 'top-sidebar': showNavTag && navTagType === 2 && collapsed,
  8. 'no-sidebar': !showNavTag
  9. }"
  10. >
  11. <div v-if="showpage" class="main-wrapper" @click="listenClick">
  12. <router-view />
  13. </div>
  14. </div>
  15. </template>
  16. <script>
  17. import { mapGetters } from 'vuex'
  18. import { needIdList } from '@/router/needIdList'
  19. import { EncryptId, desDecryptId, analysisBizId_id } from '@/utils/crypto-js.js'
  20. import { settingGetBizList, settingUserGetBiz } from '@/api/projectIndex'
  21. export default {
  22. data() {
  23. return {
  24. bizIdList: [], // 业务线列表
  25. showpage: false
  26. }
  27. },
  28. computed: {
  29. ...mapGetters(['bizId', 'navTagType', 'showNavTag', 'collapsed'])
  30. },
  31. watch: {
  32. $route: {
  33. handler(to, from) {
  34. this.handlerRouter(to, from)
  35. },
  36. immediate: true
  37. }
  38. },
  39. // created() {
  40. // this.settingUserGetBiz()
  41. // },
  42. methods: {
  43. // async settingUserGetBiz() { // 刷新页面
  44. // // const bizId_id = analysisBizId_id(this.$route.query.bizId_id)
  45. // let decodeBizId = ''
  46. // const { bizId = null, bizId_id = null } = this.$route.query
  47. // if(this.$route.query.bizId) {
  48. // decodeBizId = desDecryptId(bizId)
  49. // } else if(this.$route.query.bizId_id){
  50. // decodeBizId = analysisBizId_id(bizId_id)[0]
  51. // } else {
  52. // const res = await settingUserGetBiz()
  53. // if (res.code === 200) {
  54. // // this.showpage = true
  55. // // this.$store.dispatch('global/setBizId', res.data.bizId)
  56. // decodeBizId = res.data.bizId
  57. // }
  58. // }
  59. // console.log(decodeBizId)
  60. // this.showpage = true
  61. // this.$store.dispatch('global/setBizId', decodeBizId)
  62. // // console.log(this.$route.query, bizId_id)
  63. // // if (this.$store.state.global.bizId === -1) {
  64. // // } else {
  65. // // this.showpage = true
  66. // // }
  67. // },
  68. // 获取业务线列表
  69. async settingGetBizList() {
  70. const res = await settingGetBizList({})
  71. this.bizIdList = res.data || []
  72. },
  73. // 路由处理
  74. async handlerRouter(to, from) {
  75. // 如果业务线列表不存在,请求获取路由业务线列表
  76. if (this.bizIdList.length < 1) await this.settingGetBizList()
  77. // 如果没有业务线,但是页面又是工作台
  78. if (this.bizId === -1 && to.name.search(/工作台/) > 0) this.getDefaultBizId(true)
  79. // 如果是业务线选择页面,不作处理
  80. if (to.name.search(/工作台|业务线/) >= 0) return
  81. // 如果与原路由相同,不作处理
  82. if (from && to.path === from.path) return
  83. // 如果已存在业务线ID(第一优先级)
  84. if (this.bizId !== -1) this.redirectPage()
  85. // 业务线不存在,但是url上面带bizId或者bizId_id的(第二优先级)
  86. if (this.$route.query.bizId || this.$route.query.bizId_id) await this.handlerPage()
  87. // 如果前往路由中参数中bizId和bizId_id都不存在,去获取默认bizId(第三优先级)
  88. if (!to.query.bizId && !to.query.bizId_id) await this.getDefaultBizId()
  89. this.showpage = true
  90. },
  91. // 页面路由替换(增加bizId参数,只替换非详情页)
  92. redirectPage() {
  93. // 是否是需要bizId_id的详情页
  94. const existBizId_id = needIdList.find(item => item === this.$route.name)
  95. // 非详情页处理,路由替换,增加加密后的bizId参数
  96. if (!existBizId_id) {
  97. this.$router.replace({
  98. path: this.$route.path,
  99. query: { ...this.$route.query, bizId: EncryptId(this.bizId) }
  100. })
  101. }
  102. },
  103. // 页面处理
  104. handlerPage() {
  105. // 是否是需要bizId_id的详情页
  106. const existBizId_id = needIdList.find(item => item === this.$route.name)
  107. let bizId = -1
  108. existBizId_id
  109. ? (bizId = analysisBizId_id(this.$route.query.bizId_id)[0]) // 详情页下
  110. : (bizId = Number(desDecryptId(this.$route.query.bizId))) // 非详情页下
  111. // 判断业务线id是否存在
  112. const isExistBizId = this.bizIdList.find(item => bizId === item.code)
  113. // 业务线存在,设置业务线,不存在,去获取默认业务线
  114. if (isExistBizId) {
  115. this.$store.dispatch('global/setBizId', bizId)
  116. this.$store.dispatch('global/setBizName', isExistBizId.name)
  117. } else {
  118. this.getDefaultBizId()
  119. }
  120. },
  121. // 获取用户默认业务线
  122. async getDefaultBizId(noQuery = false) {
  123. const res = await settingUserGetBiz()
  124. if (!res.data) this.$router.push({ name: '业务线选择' })
  125. const nowBiz = this.bizIdList.find(item => res.data.bizId === item.code)
  126. // 如果当前用户默认业务线存在,设置业务线,并页面路由重定向,否则进入业务线选择页面
  127. if (nowBiz) {
  128. this.$store.dispatch('global/setBizId', nowBiz.code)
  129. this.$store.dispatch('global/setBizName', nowBiz.name)
  130. localStorage.setItem('bizId', nowBiz.code)
  131. if (noQuery) return // 如果是不需要url参数的,直接返回
  132. this.redirectPage()
  133. } else {
  134. this.$router.push({ name: '业务线选择' })
  135. }
  136. },
  137. // 整个页面的点击次数
  138. listenClick(event) {
  139. this.$store.dispatch('app/addClickCount')
  140. },
  141. handleClickOutside() {
  142. this.$store.dispatch('app/closeSideBar', { withoutAnimation: false })
  143. }
  144. }
  145. }
  146. </script>
  147. <style lang="scss" scoped>
  148. .zhihui-main {
  149. height: 100vh;
  150. background-color: #f2f3f6;
  151. }
  152. .hide-sidebar {
  153. width: calc(100% - 80px);
  154. }
  155. .open-sidebar {
  156. width: calc(100% - 215px);
  157. }
  158. .top-sidebar {
  159. width: calc(100% - 80px);
  160. padding-top: 60px;
  161. }
  162. .no-sidebar {
  163. width: calc(100% - 80px);
  164. }
  165. .main-wrapper {
  166. box-sizing: border-box;
  167. height: 100%;
  168. width: 100%;
  169. min-width: 890px;
  170. // padding-left: 10px;
  171. overflow-y: scroll;
  172. background-color: rgb(242, 243, 246);
  173. }
  174. </style>