Files
Situation-Awareness-Platfor…/src/stores/Global/index.ts
2025-03-19 20:47:53 +08:00

131 lines
3.7 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { ref, computed } from 'vue';
import { defineStore } from 'pinia';
import menu from '@/constant/menu';
import { type listType, type itemProps } from '@/components/NavTabs/types';
import { useRoute, useRouter } from 'vue-router';
import { getCustomMenuList } from '@/api/org/devIndex';
export const useGlobalInfoStore = defineStore('globalInfo', () => {
const route = useRoute();
const globalMenuInfo = ref<listType>(menu); // 全部菜单信息
const menuType = ref(route?.meta?.type || 'repo'); // 当前菜单类型
const customMenuInfo = ref([]); // org自定义菜单
const isNotFound = ref(false); // 是否展示404页面
const showTools = ref(true); // 是否展示工具栏
const namespaceType = ref(-1); // 0 - 用户 1 - 组织
const globalTheme = ref('light'); // 网站主题
const headerSearch = ref({
searchType: '软件',
softwareName: ''
})
const menuInfo = computed(() => {
// 当前菜单
return globalMenuInfo.value[menuType.value] || [];
});
// 设置单类菜单信息
const setNamespaceType = (val: number) => {
namespaceType.value = val;
};
// 设置全部菜单信息
const setGlobalMenuInfo = (val: listType) => {
globalMenuInfo.value = {
...globalMenuInfo.value,
...val
};
};
// 设置单类菜单信息
const setMenuInfo = (val: itemProps) => {
globalMenuInfo.value[menuType.value] = val;
};
/**
* 设置菜单类型
* @param val 类型
* @param routeNamespace 路由中的命名空间(某些地方获取不到,需要手动传入)
*/
const setMenuType = async(val: string, routeNamespace?: string) => {
if (val) {
menuType.value = val;
// 组织 需要加载自定义菜单
if (val === 'org') {
const namespace = routeNamespace || route.params.namespace;
const res = await getCustomMenuList({
namespace: Array.isArray(namespace) ? namespace.join('/') : namespace
});
customMenuInfo.value = (res?.data?.data || []).map((item) => ({
...item,
id: item.id || item.name,
label: item.name,
icon: item.link || 'gt-link',
link: item.content
}));
}
}
};
const updateHeaderSearch = (data: any) => {
headerSearch.value = { ...data };
}
/**
* 设置菜单标题旁的数字mr数量成员数量
* @param id 菜单id
* @param val 增加或减少的数量
* @param actionType 类型 add增加 reduce减少 equal等于
*/
const updateMenuNum = (id: string, val: number, actionType: 'add' | 'reduce' | 'equal' = 'equal') => {
const list = [...(globalMenuInfo.value[menuType.value] || []), ...customMenuInfo.value];
const item = list.find((item) => item.id === id) || {};
if (actionType === 'equal') {
item.num = val;
}
if (actionType === 'add') {
item.num = item.num ? item.num + val : val; // 新增前判断num是否存在
}
if (actionType === 'reduce') {
item.num = item.num <= 0 ? 0 : item.num - val; // 防止刷成负数
}
};
const setIsNotFound = (val: boolean) => {
isNotFound.value = val;
};
const setShowTools = (val: boolean) => {
showTools.value = val;
};
/**
* TODO: 设置主题模仿github用于设置仓库项目markdown图片
* 前端暂时固定单一主题,后续可能会提供设置功能
*/
const setGlobalTheme = (val: string) => {
globalTheme.value = val;
};
return {
customMenuInfo,
menuInfo,
menuType,
isNotFound,
showTools,
namespaceType,
globalTheme,
headerSearch,
setMenuType,
updateMenuNum,
setMenuInfo,
setGlobalMenuInfo,
setIsNotFound,
setShowTools,
setNamespaceType,
setGlobalTheme,
updateHeaderSearch
};
});