Explorar o código

feat: 路由权限控制函数抽取

cuiyalong %!s(int64=4) %!d(string=hai) anos
pai
achega
97035016ed
Modificáronse 3 ficheiros con 141 adicións e 114 borrados
  1. 13 114
      src/router/router-interceptors.js
  2. 49 0
      src/utils/bigmember/index.js
  3. 79 0
      src/utils/bigmember/powerMap.js

+ 13 - 114
src/router/router-interceptors.js

@@ -1,127 +1,26 @@
 import router from '@/router/router'
 import store from '@/store/'
+import { powerCheck } from '@/utils/bigmember/'
 
-const PowerMap = {
-  1: {
-    tip: '搜索+订阅',
-    url: 'big_subscribe&bigVip_detail&bigvip_viewpage',
-    anchor: 'zb'
-  },
-  2: {
-    tip: '子账号',
-    url: ''
-  },
-  3: {
-    tip: '附件下载',
-    url: ''
-  },
-  4: {
-    tip: '企业全景分析',
-    url: 'ent_portrait&ent_follow_search&ent_follow',
-    anchor: 'jc'
-  },
-  5: {
-    tip: '采购单位全景分析',
-    url: 'unit_portrayal',
-    anchor: 'jc'
-  },
-  6: {
-    tip: '投标决策分析',
-    url: 'analysis_search&analysis_filter&analysis_result&ai_unit',
-    anchor: 'jc'
-  },
-  7: {
-    tip: '挖掘潜在客户',
-    url: 'potential_cor_list&potential_cor',
-    anchor: 'qz'
-  },
-  8: {
-    tip: '挖掘潜在伙伴/竞争对手',
-    url: 'potential_cor_list&potential_cor',
-    anchor: 'qz'
-  },
-  9: {
-    tip: '挖掘潜在项目',
-    url: 'forecast_list&forecast_detail',
-    anchor: 'fx'
-  },
-  10: {
-    tip: '周报/月报',
-    url: 'bigvip_subreport_week&bigvip_subreport_month&report_detail_week&report_detail_month',
-    anchor: 'fx'
-  },
-  11: {
-    tip: '招标文件解读',
-    url: ''
-  },
-  12: {
-    tip: '企业情报监控',
-    url: 'ent_portrait&ent_portrait_change&ent_follow_search&ent_follow',
-    anchor: 'fx'
-  },
-  13: {
-    tip: '企业中标动态',
-    url: 'ent_follow&ent_portrait&ent_follow_search',
-    anchor: 'fx'
-  },
-  14: {
-    tip: '项目进度监控',
-    url: 'pro_follow_list&pro_follow_detail',
-    anchor: 'zb'
-  },
-  15: {
-    tip: '中标企业预测',
-    url: 'ai_search&ai_add&ai_result&ai_balance&voucher_center&ai_unit',
-    anchor: 'zb'
-  },
-  16: {
-    tip: '企业情报监控+企业中标动态',
-    url: '',
-    anchor: 'fx'
-  }
-}
-
-function createURL2Power () {
-  const URL2Power = new Map()
-  Object.keys(PowerMap).forEach(v => {
-    const nowV = PowerMap[v]
-    const tempUrls = nowV.url.split('&')
-    tempUrls.forEach(u => {
-      const oldPower = URL2Power.get(u) || []
-      URL2Power.set(u, Array.from(new Set(oldPower.concat(Number(v)))))
-    })
-  })
-  return URL2Power
-}
-
-const URL2Power = createURL2Power()
+// 权限控制白名单-组件名
+const powerCheckWhiteList = ['404']
 
-// 从vue实例中获取store对象
-// const store = router.app.$store
 router.beforeEach(async (to, from, next) => {
   const { power, info } = store.state.user
   if (!Object.keys(info).length) {
     await store.dispatch('user/getUserPower')
   }
-  let href = '/big/page/index'
-  if (info?.memberStatus > 0) {
-    const result = URL2Power.get(to.path) || URL2Power.get(to.name)
-    if (result) {
-      const intersectionPower = result.filter(v => power.includes(v))
-      if (intersectionPower.length) {
-        next()
-      } else {
-        const anchor = PowerMap[result[0]].anchor
-        if (anchor) {
-          href += '#' + anchor
-        }
-        location.href = href
-      }
-    } else {
-      next()
-    }
+  if (powerCheckWhiteList.includes(to.name)) {
+    next()
   } else {
-    if (to.name !== '404') {
+    let href = '/big/page/index'
+    const { pass, anchor } = powerCheck(info, power, to, from)
+    if (pass) {
+      next()
+    } else {
+      if (anchor) {
+        href = `${href}#${anchor}`
+      }
       location.href = href
     }
   }

+ 49 - 0
src/utils/bigmember/index.js

@@ -0,0 +1,49 @@
+import { powerMap } from './powerMap'
+
+// 生成路由对应权限的map
+const createURL2Power = () => {
+  const URL2Power = new Map()
+  Object.keys(powerMap).forEach(v => {
+    const nowV = powerMap[v]
+    const tempUrls = nowV.url.split('&')
+    tempUrls.forEach(u => {
+      const oldPower = URL2Power.get(u) || []
+      URL2Power.set(u, Array.from(new Set(oldPower.concat(Number(v)))))
+    })
+  })
+  return URL2Power
+}
+
+// 路由权限控制判断
+export const powerCheck = (info, power, to, from) => {
+  const URL2Power = createURL2Power()
+  const passInfo = {
+    pass: true,
+    anchor: ''
+  }
+  if (info?.memberStatus > 0) {
+    // 开通了大会员
+    // 当前url对应的power列表
+    const urlPowerList = URL2Power.get(to.path) || URL2Power.get(to.name)
+    if (urlPowerList) {
+      // 当前页面需要权限
+      const intersectionPower = urlPowerList.filter(v => power.includes(v))
+      // 判断当前url是否有权限
+      if (intersectionPower.length) {
+        // 当前路由有权限
+        passInfo.pass = true
+      } else {
+        // 当前路由无权限
+        passInfo.anchor = powerMap[urlPowerList[0]].anchor
+        passInfo.pass = false
+      }
+    } else {
+      // 当前页面不需要权限
+    }
+    return passInfo
+  } else {
+    // 未开通大会员
+    passInfo.pass = false
+    return passInfo
+  }
+}

+ 79 - 0
src/utils/bigmember/powerMap.js

@@ -0,0 +1,79 @@
+export const powerMap = {
+  1: {
+    tip: '搜索+订阅',
+    url: 'big_subscribe&bigVip_detail&bigvip_viewpage',
+    anchor: 'zb'
+  },
+  2: {
+    tip: '子账号',
+    url: ''
+  },
+  3: {
+    tip: '附件下载',
+    url: ''
+  },
+  4: {
+    tip: '企业全景分析',
+    url: 'ent_portrait&ent_follow_search&ent_follow',
+    anchor: 'jc'
+  },
+  5: {
+    tip: '采购单位全景分析',
+    url: 'unit_portrayal',
+    anchor: 'jc'
+  },
+  6: {
+    tip: '投标决策分析',
+    url: 'analysis_search&analysis_filter&analysis_result&ai_unit',
+    anchor: 'jc'
+  },
+  7: {
+    tip: '挖掘潜在客户',
+    url: 'potential_cor_list&potential_cor',
+    anchor: 'qz'
+  },
+  8: {
+    tip: '挖掘潜在伙伴/竞争对手',
+    url: 'potential_cor_list&potential_cor',
+    anchor: 'qz'
+  },
+  9: {
+    tip: '挖掘潜在项目',
+    url: 'forecast_list&forecast_detail',
+    anchor: 'fx'
+  },
+  10: {
+    tip: '周报/月报',
+    url: 'bigvip_subreport_week&bigvip_subreport_month&report_detail_week&report_detail_month',
+    anchor: 'fx'
+  },
+  11: {
+    tip: '招标文件解读',
+    url: ''
+  },
+  12: {
+    tip: '企业情报监控',
+    url: 'ent_portrait&ent_portrait_change&ent_follow_search&ent_follow',
+    anchor: 'fx'
+  },
+  13: {
+    tip: '企业中标动态',
+    url: 'ent_follow&ent_portrait&ent_follow_search',
+    anchor: 'fx'
+  },
+  14: {
+    tip: '项目进度监控',
+    url: 'pro_follow_list&pro_follow_detail',
+    anchor: 'zb'
+  },
+  15: {
+    tip: '中标企业预测',
+    url: 'ai_search&ai_add&ai_result&ai_balance&voucher_center&ai_unit',
+    anchor: 'zb'
+  },
+  16: {
+    tip: '企业情报监控+企业中标动态',
+    url: '',
+    anchor: 'fx'
+  }
+}