| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151 |
- import { ref, computed } from 'vue';
- import { fetchEnterpriseInfo, fetchEnterpriseLogin, type EnterpriseInfo } from '@/network/api/enterprise';
- const TEMP_HISTORY_KEY = 'ba_temp_chat_history';
- const AUTH_HISTORY_KEY = 'ba_chat_history';
- const AUTH_CURRENT_SESSION_KEY = 'ba_current_session_id';
- const ENTERPRISE_INFO_KEY = 'ba_enterprise_info';
- const AUTH_TOKEN_KEY = 'ba_auth_token';
- export const enterpriseInfo = ref<EnterpriseInfo | null>(null);
- const authLoading = ref(false);
- const authError = ref(false);
- // Global fetch interceptor: any 401 response → clear enterprise info (logout)
- (function setupFetchInterceptor() {
- if (typeof window === 'undefined') return;
- const originalFetch = window.fetch.bind(window);
- window.fetch = async (...args: Parameters<typeof fetch>) => {
- const response = await originalFetch(...args);
- if (response.status === 401 && enterpriseInfo.value) {
- enterpriseInfo.value = null;
- globalThis.authToken = undefined;
- globalThis.token = undefined;
- localStorage.removeItem(ENTERPRISE_INFO_KEY);
- localStorage.removeItem(AUTH_TOKEN_KEY);
- }
- return response;
- };
- })();
- export function getQueryParamUniversal(key: string): string | null {
- const searchParams = new URLSearchParams(window.location.search);
- if (searchParams.has(key)) {
- return searchParams.get(key);
- }
- const hash = window.location.hash;
- const queryIndex = hash.indexOf('?');
- if (queryIndex !== -1) {
- const hashQuery = hash.substring(queryIndex + 1);
- const hashParams = new URLSearchParams(hashQuery);
- return hashParams.get(key);
- }
- return null;
- }
- export function getChatHistoryStorageKey(): string {
- return enterpriseInfo.value ? AUTH_HISTORY_KEY : TEMP_HISTORY_KEY;
- }
- export function getCurrentSessionStorageKey(): string {
- return AUTH_CURRENT_SESSION_KEY;
- }
- export function useEnterpriseAuth() {
- const isLoggedIn = computed(() => !!enterpriseInfo.value);
- const enterpriseName = computed(() => enterpriseInfo.value?.name || '');
- const creditCode = computed(() => enterpriseInfo.value?.credit_code || '');
- const initAuth = async () => {
- const creditCodeParam = getQueryParamUniversal('credit_code');
- const accessToken = getQueryParamUniversal('access_token');
- // 跟随 querystring 的 policyMode:A/B 测试变体(如 zhaoshang_staging_1)也需体现在 source 中
- globalThis.source = globalThis.policyMode || "zhaoshang";
- // 1. access_token 存在: 换取 credit_code + authToken, 再拉企业信息
- // (要企业态就显式带 ?access_token=<token> 或 ?credit_code=<代码>)
- if (accessToken && !creditCodeParam) {
- authLoading.value = true;
- authError.value = false;
- try {
- const loginResult = await fetchEnterpriseLogin(accessToken);
- if (loginResult) {
- globalThis.authToken = loginResult.authToken;
- globalThis.token = loginResult.authToken;
- localStorage.setItem(AUTH_TOKEN_KEY, loginResult.authToken);
- authLoading.value = false;
- return await initAuthWithCreditCode(loginResult.creditCode);
- }
- } catch {
- authError.value = true;
- } finally {
- authLoading.value = false;
- }
- return;
- }
- // 2. credit_code 存在: 直接拉企业信息
- if (creditCodeParam) {
- return await initAuthWithCreditCode(creditCodeParam);
- }
- // 3. 无参数: 保持未登录状态
- };
- const initAuthWithCreditCode = async (creditCodeParam: string) => {
- authLoading.value = true;
- authError.value = false;
- try {
- const info = await fetchEnterpriseInfo(creditCodeParam);
- if (info) {
- enterpriseInfo.value = info;
- const cc = info.credit_code || creditCodeParam;
- (globalThis as any).credit_code = cc;
- (globalThis as any).transmission = {
- ...((globalThis as any).transmission || {}),
- credit_code: cc,
- };
- localStorage.setItem(ENTERPRISE_INFO_KEY, JSON.stringify(info));
- }
- } catch {
- authError.value = true;
- } finally {
- authLoading.value = false;
- }
- };
- const logout = () => {
- enterpriseInfo.value = null;
- globalThis.authToken = undefined;
- globalThis.token = undefined;
- localStorage.removeItem(ENTERPRISE_INFO_KEY);
- localStorage.removeItem(AUTH_TOKEN_KEY);
- };
- const redirectToLogin = () => {
- // 回跳到本页;一网通办 redirect_uri 白名单后续自行对接
- const mode = globalThis.policyMode || "zhaoshang";
- const currentUrl = `${window.location.origin}${window.location.pathname}#/?mode=${mode}`;
- const sourceLink = `https://zwdtuser.sh.gov.cn/uc/login/login.jsp?sendPost=false&type=2&redirect_uri=${encodeURIComponent(currentUrl)}`;
- window.location.href = sourceLink;
- };
- return {
- isLoggedIn,
- enterpriseName,
- creditCode,
- enterpriseInfo,
- authLoading,
- authError,
- initAuth,
- logout,
- redirectToLogin,
- };
- }
|