46 lines
1.3 KiB
TypeScript
46 lines
1.3 KiB
TypeScript
import { requestCrudService } from "@/service/request-axios-service";
|
|
|
|
const CONSUMER_URL = import.meta.env.VITE_AIPI_CONSUMER_URL || "";
|
|
|
|
export const TOOLS_TOKEN_STORAGE_KEY = "tools_token";
|
|
export const EXTERNAL_USER_ID_STORAGE_KEY = "userId";
|
|
export const EXTERNAL_USER_INFO_STORAGE_KEY = "userInfo";
|
|
export const GENERAL_QA_ROUTE = "/page/strategy/qa/general";
|
|
|
|
type UserDetailResponse = {
|
|
data?: {
|
|
id?: string;
|
|
[key: string]: unknown;
|
|
};
|
|
};
|
|
|
|
/**
|
|
* External login via yUserId + optional token (same flow as web-code Login.tsx):
|
|
* persist tools_token, resolve user via consumer API, store userId/userInfo.
|
|
*/
|
|
export async function loginByYUserId(
|
|
yUserId: string,
|
|
token?: string | null,
|
|
): Promise<void> {
|
|
if (token) {
|
|
localStorage.setItem(TOOLS_TOKEN_STORAGE_KEY, token);
|
|
}
|
|
|
|
const res = (await requestCrudService.get(
|
|
`${CONSUMER_URL}/cop/userDetailByYuserId`,
|
|
{ yuserId: yUserId },
|
|
)) as UserDetailResponse;
|
|
|
|
const result = res.data;
|
|
if (!result?.id) {
|
|
throw new Error("无法根据 yUserId 获取用户信息。");
|
|
}
|
|
|
|
localStorage.setItem(EXTERNAL_USER_ID_STORAGE_KEY, result.id);
|
|
localStorage.setItem(EXTERNAL_USER_INFO_STORAGE_KEY, JSON.stringify(result));
|
|
}
|
|
|
|
export function persistToolsToken(token: string): void {
|
|
localStorage.setItem(TOOLS_TOKEN_STORAGE_KEY, token);
|
|
}
|