8 天以前 2661f1cffa19d2f267561179127dce7c032f7071
feat(layout): 添加AI功能开关控制

- 在布局组件中添加AI聊天侧边栏的条件渲染
- 引入用户状态管理用于获取AI功能启用状态
- 实现计算属性判断AI功能是否启用
- 在权限模块中添加AI功能路由过滤逻辑
- 创建AI特征路由识别函数过滤相关路由
- 在用户状态中添加aiEnabled字段并初始化值
- 登录时设置AI功能启用状态
- 退出登录时重置AI功能启用状态
已修改3个文件
106 ■■■■■ 文件已修改
src/layout/index.vue 5 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/store/modules/permission.js 48 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/store/modules/user.js 53 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/layout/index.vue
@@ -16,7 +16,7 @@
      <app-main />
      <settings ref="settingRef" />
    </div>
    <AIChatSidebar />
    <AIChatSidebar v-if="aiEnabled" />
  </div>
</template>
@@ -28,15 +28,18 @@
  import defaultSettings from "@/settings";
  import useAppStore from "@/store/modules/app";
  import useUserStore from "@/store/modules/user";
  import useSettingsStore from "@/store/modules/settings";
  const settingsStore = useSettingsStore();
  const userStore = useUserStore();
  const theme = computed(() => settingsStore.theme);
  const sideTheme = computed(() => settingsStore.sideTheme);
  const sidebar = computed(() => useAppStore().sidebar);
  const device = computed(() => useAppStore().device);
  const needTagsView = computed(() => settingsStore.tagsView);
  const fixedHeader = computed(() => settingsStore.fixedHeader);
  const aiEnabled = computed(() => Number(userStore.aiEnabled) === 1);
  const classObj = computed(() => ({
    hideSidebar: !sidebar.value.opened,
src/store/modules/permission.js
@@ -1,9 +1,10 @@
import auth from '@/plugins/auth'
import router, { constantRoutes, dynamicRoutes } from '@/router'
import { getRouters } from '@/api/menu'
import Layout from '@/layout/index'
import ParentView from '@/components/ParentView'
import InnerLink from '@/layout/components/InnerLink'
import Layout from '@/layout/index'
import ParentView from '@/components/ParentView'
import InnerLink from '@/layout/components/InnerLink'
import useUserStore from '@/store/modules/user'
// 匹配views里面所有的.vue文件
const modules = import.meta.glob('./../../views/**/*.vue')
@@ -36,9 +37,11 @@
        return new Promise(resolve => {
          // 向后端请求路由数据
          getRouters().then(res => {
            const sdata = JSON.parse(JSON.stringify(res.data))
            const rdata = JSON.parse(JSON.stringify(res.data))
            const defaultData = JSON.parse(JSON.stringify(res.data))
            const aiEnabled = Number(useUserStore().aiEnabled) === 1
            const rawRoutes = filterAiFeatureRoutes(res.data, aiEnabled)
            const sdata = JSON.parse(JSON.stringify(rawRoutes))
            const rdata = JSON.parse(JSON.stringify(rawRoutes))
            const defaultData = JSON.parse(JSON.stringify(rawRoutes))
            const sidebarRoutes = filterAsyncRouter(sdata)
            const rewriteRoutes = filterAsyncRouter(rdata, false, true)
            const defaultRoutes = filterAsyncRouter(defaultData)
@@ -57,7 +60,38 @@
  })
// 遍历后台传来的路由字符串,转换为组件对象
function filterAsyncRouter(asyncRouterMap, lastRouter = false, type = false) {
function filterAiFeatureRoutes(routes = [], aiEnabled = false) {
  if (aiEnabled) {
    return routes
  }
  return routes.reduce((acc, route) => {
    if (!route || isAiFeatureRoute(route)) {
      return acc
    }
    const nextRoute = { ...route }
    if (Array.isArray(nextRoute.children) && nextRoute.children.length > 0) {
      nextRoute.children = filterAiFeatureRoutes(nextRoute.children, aiEnabled)
    }
    acc.push(nextRoute)
    return acc
  }, [])
}
function isAiFeatureRoute(route = {}) {
  const path = String(route.path || '').toLowerCase()
  const component = String(route.component || '').toLowerCase()
  const name = String(route.name || '').toLowerCase()
  const title = String(route?.meta?.title ?? route?.title ?? '')
  return (
    path.includes('chathome') ||
    component.includes('chathome') ||
    name.includes('chathome') ||
    title.includes('AI')
  )
}
function filterAsyncRouter(asyncRouterMap, lastRouter = false, type = false) {
  return asyncRouterMap.filter(route => {
    if (type && route.children) {
      route.children = filterChildren(route.children)
src/store/modules/user.js
@@ -7,13 +7,14 @@
const useUserStore = defineStore(
  'user',
  {
    state: () => ({
      token: getToken(),
      id: '',
      name: '',
      avatar: '',
      roles: [],
      permissions: []
    state: () => ({
      token: getToken(),
      id: '',
      name: '',
      avatar: '',
      roles: [],
      permissions: [],
      aiEnabled: 0
    }),
    actions: {
      // 登录
@@ -58,29 +59,31 @@
            this.id = user.userId
            this.name = user.userName
            this.avatar = avatar
            this.currentFactoryName = user.currentFactoryName
            this.nickName = user.nickName
            this.roleName = user.roles[0].roleName
            this.currentDeptId = user.tenantId
            this.currentLoginTime = this.getCurrentTime()
            resolve(res)
          }).catch(error => {
            reject(error)
          })
        })
            this.currentFactoryName = user.currentFactoryName
            this.nickName = user.nickName
            this.roleName = user.roles[0].roleName
            this.currentDeptId = user.tenantId
            this.currentLoginTime = this.getCurrentTime()
            this.aiEnabled = Number(res.aiEnabled) === 1 ? 1 : 0
            resolve(res)
          }).catch(error => {
            reject(error)
          })
        })
      },
      // 退出系统
      logOut() {
        return new Promise((resolve, reject) => {
          logout(this.token).then(() => {
            this.token = ''
            this.roles = []
            this.permissions = []
            removeToken()
            resolve()
          }).catch(error => {
            reject(error)
          })
            this.token = ''
            this.roles = []
            this.permissions = []
            this.aiEnabled = 0
            removeToken()
            resolve()
          }).catch(error => {
            reject(error)
          })
        })
      },
      // 登录校验