Files
Situation-Awareness-Platfor…/src/stores/Global/index.ts

131 lines
3.7 KiB
TypeScript
Raw Normal View History

2025-03-12 18:41:20 +08:00
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'); // 网站主题
2025-03-19 19:38:32 +08:00
const headerSearch = ref({
searchType: '软件',
2025-03-19 20:47:53 +08:00
softwareName: ''
2025-03-19 19:38:32 +08:00
})
2025-03-12 18:41:20 +08:00
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
}));
}
}
};
2025-03-19 19:38:32 +08:00
const updateHeaderSearch = (data: any) => {
headerSearch.value = { ...data };
}
2025-03-12 18:41:20 +08:00
/**
* 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,
2025-03-19 19:38:32 +08:00
headerSearch,
2025-03-12 18:41:20 +08:00
setMenuType,
updateMenuNum,
setMenuInfo,
setGlobalMenuInfo,
setIsNotFound,
setShowTools,
setNamespaceType,
2025-03-19 19:38:32 +08:00
setGlobalTheme,
updateHeaderSearch
2025-03-12 18:41:20 +08:00
};
});