|
- import { history } from 'umi';
- import { fetchApi } from '@/utils/request';
- import storage from '@/utils/storage';
- import { errorReponse, isReqSuccess } from '@/utils/tool';
- import system from './system';
- import { DATA } from './API';
- import { fileMng } from '@/utils/hooks';
-
- export async function queryCurrent() {
- const accountId = storage.get('accountId');
- if (!accountId) {
- return errorReponse('');
- }
- const res = await fetchApi<DATA.User>('user/queryUserByUserId', {
- id: accountId,
- });
- return res;
- }
-
- export async function login(account: string, password: string) {
- const res = await fetchApi<DATA.User>(
- 'authentication/login',
- { UserName: account, Password: password },
- { silent: true },
- );
- if (!isReqSuccess(res)) {
- // dispatch(logout());
- return res;
- }
- const userData = res.data;
-
- const isSuperAdmin = userData.backgroundPermission === 3;
- if (isSuperAdmin) {
- return errorReponse('该账号没有访问权限');
- }
-
- const companyInfoRes = await fetchApi(
- 'company/queryFrontDeskCompanyById',
- { id: userData.companyId },
- { silent: true },
- );
- if (!isReqSuccess(companyInfoRes)) {
- return companyInfoRes;
- }
- const systemLoginRes = await system.login(
- res.data.id,
- res.data.phone,
- companyInfoRes.data?.company?.IpfsApi || '',
- );
- if (!isReqSuccess(systemLoginRes)) {
- return errorReponse('本地网管通讯失败');
- }
-
- // const { data: { company: { SoftwareVersion } = {} } = {} } = companyInfoRes;
-
- // 角色判断
- // const { app: appStore } = getState();
- // if (appStore.appType && userData.userType !== appStore.appType) { // 登录角色权限不符合,退出登录
- // hint('账号权限不足');
- // dispatch(logout());
- // return false;
- // }
- // const payload = {
- // accountId: userData.id,
- // accountName: userData.cnName,
- // customerId: userData.companyId,
- // avatorUrl: userData.headImgUrl,
- // isMini: SoftwareVersion === 'Mini',
- // };
- storage.set('accountId', userData.id);
- storage.set('companyId', userData.companyId);
- // storage.set('isMini', payload.isMini);
- return res;
- }
-
- export function logout(clearStorage = true) {
- if (clearStorage) {
- storage.clear();
- }
- fileMng.clear();
- system.logout();
- // window.location.href = '/login';
- history.replace('/login');
- // window.location.reload();
- }
|