import {
  errorConfig,
  requestInterceptors,
  responseInterceptors,
} from '@/utils/request';
import type { RequestConfig } from '@umijs/max';
import { Base64 } from 'js-base64';
import services from './services';
import { getUrlQuery } from './utils/comm';
import { setSession } from './utils/session';

//request 配置
export const request: RequestConfig = {
  timeout: 60000,
  errorConfig,
  requestInterceptors,
  responseInterceptors,
};

// 接收登录参数
let token = getUrlQuery('access_token');
if (token) {
  setSession('token', Base64.decode(token));
}

export async function getInitialState(): Promise<any> {
  let areaId: string = '';
  try {
    const userPermDataResp = await services.CommQuery.getUserDataPermission();
    if (userPermDataResp.code === 200) {
      areaId = userPermDataResp?.data?.areaGrid[0];
    }
  } catch (error) {
    console.error('获取初始数据出错：', error);
  }
  return {
    areaId,
  };
}
