mixin.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. // import Vue from 'vue'
  2. import { deviceEnquire, DEVICE_TYPE } from '@/utils/device'
  3. import { mapState } from 'vuex'
  4. // const mixinsComputed = Vue.config.optionMergeStrategies.computed
  5. // const mixinsMethods = Vue.config.optionMergeStrategies.methods
  6. const mixin = {
  7. computed: {
  8. ...mapState({
  9. layoutMode: state => state.app.layout,
  10. navTheme: state => state.app.theme,
  11. fontSize: state => state.app.fontSize,
  12. primaryColor: state => state.app.color,
  13. colorWeak: state => state.app.weak,
  14. fixedHeader: state => state.app.fixedHeader,
  15. fixSiderbar: state => state.app.fixSiderbar,
  16. fixSidebar: state => state.app.fixSiderbar,
  17. contentWidth: state => state.app.contentWidth,
  18. autoHideHeader: state => state.app.autoHideHeader,
  19. sidebarOpened: state => state.app.sidebar,
  20. multiTab: state => state.app.multiTab
  21. })
  22. },
  23. methods: {
  24. isTopMenu () {
  25. return this.layoutMode === 'topmenu'
  26. },
  27. isSideMenu () {
  28. return !this.isTopMenu()
  29. }
  30. }
  31. }
  32. const mixinDevice = {
  33. computed: {
  34. ...mapState({
  35. device: state => state.app.device
  36. })
  37. },
  38. methods: {
  39. isMobile () {
  40. return this.device === DEVICE_TYPE.MOBILE
  41. },
  42. isDesktop () {
  43. return this.device === DEVICE_TYPE.DESKTOP
  44. },
  45. isTablet () {
  46. return this.device === DEVICE_TYPE.TABLET
  47. }
  48. }
  49. }
  50. const AppDeviceEnquire = {
  51. mounted () {
  52. const { $store } = this
  53. deviceEnquire(deviceType => {
  54. switch (deviceType) {
  55. case DEVICE_TYPE.DESKTOP:
  56. $store.commit('TOGGLE_DEVICE', 'desktop')
  57. $store.dispatch('setSidebar', true)
  58. // 默认收起侧边栏
  59. // $store.dispatch('setSidebar', false)
  60. break
  61. case DEVICE_TYPE.TABLET:
  62. $store.commit('TOGGLE_DEVICE', 'tablet')
  63. $store.dispatch('setSidebar', false)
  64. break
  65. case DEVICE_TYPE.MOBILE:
  66. default:
  67. $store.commit('TOGGLE_DEVICE', 'mobile')
  68. $store.dispatch('setSidebar', true)
  69. break
  70. }
  71. })
  72. }
  73. }
  74. // 所有页
  75. const commonMixin = {
  76. watch: {
  77. '$store.state.app.loadingStatus': function (a, b) {
  78. if (!a) {
  79. if (this.disabled !== undefined) {
  80. this.disabled = a
  81. }
  82. if (this.spinning !== undefined) {
  83. this.spinning = a
  84. }
  85. }
  86. }
  87. },
  88. methods: {
  89. getIsHomeNav () {
  90. const data = sessionStorage.getItem('isHomeNav')
  91. return JSON.parse(data) || {}
  92. },
  93. // 设置this.isHomeNav
  94. setIsHomeNav(name,params){
  95. const data = this.getIsHomeNav()
  96. data[name] = params
  97. sessionStorage.setItem('isHomeNav', JSON.stringify(data))
  98. },
  99. // 清除组件缓存
  100. clearCompCache (cache, keys, key) {
  101. if (cache[key] && cache[key].componentInstance) {
  102. cache[key].componentInstance.$destroy()
  103. if (keys.length) {
  104. var index = keys.indexOf(key)
  105. if (index > -1) {
  106. keys.splice(index, 1)
  107. }
  108. }
  109. delete cache[key]
  110. }
  111. }
  112. },
  113. beforeRouteLeave (to, from, next) {
  114. const newVal = to
  115. const oldVal = from
  116. // 是否时同一个父级菜单下的页面
  117. const mnlen = newVal.matched.length < 3
  118. const newMatched = newVal.matched[mnlen ? 1 : 2]
  119. const molen = oldVal.matched.length < 3
  120. const oldMatched = oldVal.matched[molen ? 1 : 2]
  121. const hasChildren = newMatched.name == oldMatched.name
  122. // 判断是否是新增,编辑,详情页
  123. const oa = oldVal.meta.title.indexOf('详情') >= 0
  124. const ob = oldVal.meta.title.indexOf('新增') >= 0
  125. const oc = oldVal.meta.title.indexOf('编辑') >= 0
  126. const oxt = oldVal.meta.replaceTab
  127. // console.log(hasChildren,oa||ob||oc||oxt)
  128. const key = this.$vnode.key
  129. const parentNode = this.$vnode.parent
  130. const cache = parentNode && parentNode.componentInstance ? parentNode.componentInstance.cache : null
  131. const keys = parentNode && parentNode.componentInstance ? parentNode.componentInstance.keys : null
  132. const closeTabPages = this.$store.state.app.closeTabPages
  133. const multiTab = this.$store.state.app.multiTab
  134. // console.log(closeTabPages,key)
  135. // 清空同级路由的组件缓存
  136. if (hasChildren && (oa || ob || oc || oxt) && cache && keys) {
  137. this.clearCompCache(cache, keys, key)
  138. // 单页签模式
  139. if (!multiTab) {
  140. this.$store.state.app.isNewTab = false
  141. this.$store.state.app.updateList = true
  142. }
  143. }
  144. // 单页签模式,并且不是同级菜单的清空组件
  145. if (!multiTab && !hasChildren) {
  146. this.clearCompCache(cache, keys, key)
  147. }
  148. // 批量清空组件缓存
  149. if (closeTabPages && closeTabPages.length && cache && keys) {
  150. for (let i = 0; i < closeTabPages.length; i++) {
  151. this.clearCompCache(cache, keys, closeTabPages[i])
  152. }
  153. this.$store.state.app.closeTabPages = []
  154. }
  155. // console.log(cache,keys)
  156. next()
  157. }
  158. }
  159. export { mixin, AppDeviceEnquire, mixinDevice, commonMixin }