支持钉钉刷新时同步用户信息

This commit is contained in:
kx_guo 2026-09-13 16:21:50 +08:00
parent 1d39ff253a
commit 225f8f758e
2 changed files with 17 additions and 5 deletions

View File

@ -2,6 +2,7 @@
import { onMounted, onUnmounted } from "vue";
import { RouterView } from "vue-router";
import { isDingTalkContainer } from "@/api/dingtalk";
import router from "@/router";
import { useAuthStore } from "@/stores/auth.store";
@ -17,9 +18,20 @@ function handleAuthExpired() {
}
}
onMounted(() =>
window.addEventListener("risk-auth-expired", handleAuthExpired),
);
onMounted(() => {
window.addEventListener("risk-auth-expired", handleAuthExpired);
// A persisted JWT only refreshes /auth/me and cannot update stale DingTalk
// department/name fields. Re-sync the organization identity once per page
// load when the app is opened inside DingTalk, without logging out on error.
if (
isDingTalkContainer() &&
auth.token &&
auth.user?.dingtalkId &&
!auth.user.dingtalkId.startsWith("local-")
) {
void auth.dingTalkLogin(true);
}
});
onUnmounted(() =>
window.removeEventListener("risk-auth-expired", handleAuthExpired),
);

View File

@ -61,7 +61,7 @@ export const useAuthStore = defineStore("auth", () => {
}
}
async function dingTalkLogin() {
async function dingTalkLogin(preserveSessionOnFailure = false) {
loading.value = true;
error.value = null;
try {
@ -69,7 +69,7 @@ export const useAuthStore = defineStore("auth", () => {
saveToken(response.accessToken);
user.value = response.user;
} catch (reason) {
clear();
if (!preserveSessionOnFailure) clear();
error.value = reason instanceof Error ? reason.message : "钉钉免登失败";
throw reason;
} finally {