Files
certd/packages/ui/certd-client/src/store/project/index.ts
T

144 lines
3.3 KiB
TypeScript
Raw Normal View History

import { defineStore } from "pinia";
import * as api from "./api";
import { message } from "ant-design-vue";
import { computed, ref } from "vue";
import { useSettingStore } from "../settings";
import { LocalStorage } from "/@/utils/util.storage";
2026-02-28 12:13:31 +08:00
import { useUserStore } from "../user";
export type ProjectItem = {
id: string;
name: string;
permission?: string;
};
export const useProjectStore = defineStore("app.project", () => {
const myProjects = ref([]);
2026-02-27 23:09:50 +08:00
const inited = ref(false);
2026-02-28 12:13:31 +08:00
const userStore = useUserStore();
const userId = userStore.getUserInfo?.id;
const lastProjectIdCacheKey = "currentProjectId:" + userId;
const lastProjectId = LocalStorage.get(lastProjectIdCacheKey);
const currentProjectId = ref(lastProjectId); // 直接调用
const projects = computed(() => {
return myProjects.value;
});
const currentProject = computed(() => {
if (currentProjectId.value) {
const project = projects.value.find(item => item.id === currentProjectId.value);
if (project) {
return project;
}
}
if (projects.value.length > 0) {
return projects.value[0];
}
return null;
});
const settingStore = useSettingStore();
const isEnterprise = computed(() => {
return settingStore.isEnterprise;
});
function getSearchForm() {
if (!currentProjectId.value || !isEnterprise.value) {
return {};
}
return {
projectId: currentProjectId.value,
};
}
async function loadMyProjects(): Promise<ProjectItem[]> {
if (!isEnterprise.value) {
return [];
}
const projects = await api.MyProjectList();
myProjects.value = projects;
2026-02-11 18:11:33 +08:00
if (projects.length > 0 && !currentProjectId.value) {
changeCurrentProject(projects[0].id, true);
}
}
2026-02-11 18:11:33 +08:00
function changeCurrentProject(id: string, silent?: boolean) {
currentProjectId.value = id;
LocalStorage.set(lastProjectIdCacheKey, id);
2026-02-11 18:11:33 +08:00
if (!silent) {
message.success("切换项目成功");
}
}
async function reload() {
2026-02-27 23:09:50 +08:00
inited.value = false;
await init();
}
async function init() {
2026-02-27 23:09:50 +08:00
if (!inited.value) {
await loadMyProjects();
inited.value = true;
}
return myProjects.value;
}
2026-02-21 23:20:26 +08:00
const isRead = computed(() => {
if (!isEnterprise.value) {
return true;
}
return currentProject.value;
});
const isWrite = computed(() => {
if (!isEnterprise.value) {
return true;
}
return currentProject.value?.permission === "write" || currentProject.value?.permission === "admin";
});
const isAdmin = computed(() => {
if (!isEnterprise.value) {
return true;
}
return currentProject.value?.permission === "admin";
});
2026-02-26 00:12:59 +08:00
function hasPermission(value: string) {
if (!isEnterprise.value) {
return true;
}
if (value === "read") {
return isRead.value;
} else if (value === "write") {
return isWrite.value;
} else if (value === "admin") {
return isAdmin.value;
}
return false;
}
2026-02-21 23:20:26 +08:00
function $reset() {
myProjects.value = [];
currentProjectId.value = "";
}
return {
projects,
myProjects,
currentProject,
2026-02-11 00:07:29 +08:00
currentProjectId,
isEnterprise,
2026-02-21 23:20:26 +08:00
isRead,
isWrite,
isAdmin,
getSearchForm,
loadMyProjects,
changeCurrentProject,
reload,
init,
2026-02-21 23:20:26 +08:00
$reset,
2026-02-26 00:12:59 +08:00
hasPermission,
};
});