Step F2:项目列表 & 当前项目上下文
This commit is contained in:
@@ -3,6 +3,7 @@ import { ref } from "vue";
|
||||
import { login as apiLogin, fetchMe } from "../api/auth";
|
||||
import { setToken, clearToken, getToken } from "../utils/auth";
|
||||
import type { UserInfo } from "../types/api";
|
||||
import { useStudyStore } from "./study";
|
||||
|
||||
export const useAuthStore = defineStore("auth", () => {
|
||||
const token = ref<string | null>(getToken());
|
||||
@@ -31,6 +32,8 @@ export const useAuthStore = defineStore("auth", () => {
|
||||
token.value = null;
|
||||
user.value = null;
|
||||
clearToken();
|
||||
const studyStore = useStudyStore();
|
||||
studyStore.clearCurrentStudy();
|
||||
};
|
||||
|
||||
return {
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
import { defineStore } from "pinia";
|
||||
import { ref } from "vue";
|
||||
import type { Study } from "../types/api";
|
||||
|
||||
const STUDY_KEY = "ctms_current_study";
|
||||
|
||||
export const useStudyStore = defineStore("study", () => {
|
||||
const currentStudy = ref<Study | null>(null);
|
||||
|
||||
const setCurrentStudy = (study: Study) => {
|
||||
currentStudy.value = study;
|
||||
localStorage.setItem(STUDY_KEY, JSON.stringify(study));
|
||||
};
|
||||
|
||||
const loadCurrentStudy = () => {
|
||||
const raw = localStorage.getItem(STUDY_KEY);
|
||||
if (raw) {
|
||||
try {
|
||||
currentStudy.value = JSON.parse(raw) as Study;
|
||||
} catch {
|
||||
currentStudy.value = null;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
const clearCurrentStudy = () => {
|
||||
currentStudy.value = null;
|
||||
localStorage.removeItem(STUDY_KEY);
|
||||
};
|
||||
|
||||
return {
|
||||
currentStudy,
|
||||
setCurrentStudy,
|
||||
loadCurrentStudy,
|
||||
clearCurrentStudy,
|
||||
};
|
||||
});
|
||||
Reference in New Issue
Block a user