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

138 lines
3.1 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";
export type ProjectItem = {
id: string;
name: string;
permission?: string;
};
export const useProjectStore = defineStore("app.project", () => {
const myProjects = ref([]);
const lastProjectId = LocalStorage.get("currentProjectId");
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("currentProjectId", id);
2026-02-11 18:11:33 +08:00
if (!silent) {
message.success("切换项目成功");
}
}
async function reload() {
const projects = await api.MyProjectList();
myProjects.value = projects;
}
async function init() {
if (!myProjects.value) {
await reload();
}
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,
};
});