163 lines
5.7 KiB
TypeScript
163 lines
5.7 KiB
TypeScript
import { userClient } from '@/apis/client';
|
|
import type { LoginParams, LoginResult } from '@/apis/models/user';
|
|
import type { Result } from '@/axios';
|
|
import { AesEncryption } from '@/utils/cipher';
|
|
import { getAppEnvConfig } from '@/utils/env';
|
|
import axios, { AxiosError } from 'axios';
|
|
import { defineStore } from 'pinia';
|
|
import { ref } from 'vue';
|
|
import dayjs from 'dayjs';
|
|
import { useStationStore } from './station';
|
|
import { useQueryControlStore } from './query-control';
|
|
|
|
const getHeaders = () => {
|
|
const { lampAuthorization, lampClientId, lampClientSecret } = getAppEnvConfig();
|
|
const newAuthorization = window.btoa(`${lampClientId}:${lampClientSecret}`);
|
|
const authorization = lampAuthorization.trim() !== '' ? lampAuthorization : newAuthorization;
|
|
return {
|
|
'content-type': 'application/json',
|
|
'accept-language': 'zh-CN,zh;q=0.9',
|
|
accept: 'application/json, text/plain, */*',
|
|
ApplicationId: '1',
|
|
TenantId: '1',
|
|
Authorization: authorization,
|
|
};
|
|
};
|
|
|
|
const aesEncryption = new AesEncryption();
|
|
|
|
export const useUserStore = defineStore(
|
|
'ndm-user-store',
|
|
() => {
|
|
const userLoginResult = ref<LoginResult | null>(null);
|
|
const userInfo = ref<any>(null);
|
|
const userResourceList = ref<string[]>([]);
|
|
const lampLoginResultRecord = ref<Record<string, LoginResult> | null>(null);
|
|
|
|
const resetStore = () => {
|
|
userLoginResult.value = null;
|
|
userInfo.value = null;
|
|
userResourceList.value = [];
|
|
lampLoginResultRecord.value = null;
|
|
};
|
|
|
|
const userLogin = async (loginParams: LoginParams) => {
|
|
const { username, password, code, key, grantType } = loginParams;
|
|
const data = {
|
|
username: aesEncryption.encryptByAES(username),
|
|
password: aesEncryption.encryptByAES(password),
|
|
code,
|
|
key,
|
|
grantType,
|
|
};
|
|
const headers = getHeaders();
|
|
const { data: respData } = await axios.post<Result<LoginResult>>(`/api/oauth/anyTenant/login`, data, { headers });
|
|
if (!respData.isSuccess) {
|
|
console.error(respData);
|
|
window.$message.destroyAll();
|
|
window.$dialog.error({
|
|
closable: false,
|
|
maskClosable: false,
|
|
title: '错误提示',
|
|
content: respData.msg,
|
|
positiveText: '确认',
|
|
onPositiveClick: () => {
|
|
window.$message.destroyAll();
|
|
},
|
|
});
|
|
throw new AxiosError(respData.msg, `${respData.code}`);
|
|
} else {
|
|
userLoginResult.value = respData.data;
|
|
}
|
|
};
|
|
|
|
const userLogout = async () => {
|
|
const [err] = await userClient.post(`/api/oauth/anyUser/logout`, { token: userLoginResult.value?.token });
|
|
if (err) throw err;
|
|
resetStore();
|
|
};
|
|
|
|
const userGetInfo = async () => {
|
|
const [err, info] = await userClient.get<any>(`/api/oauth/anyone/getUserInfoById`);
|
|
if (err || !info) {
|
|
throw err;
|
|
}
|
|
userInfo.value = info;
|
|
};
|
|
|
|
const userGetResourceList = async () => {
|
|
interface NdmApplication {
|
|
appKey: string;
|
|
id: string;
|
|
name: string;
|
|
}
|
|
const [e, ndmApplicationList] = await userClient.get<NdmApplication[]>(`/api/system/anyone/findMyApplication?_t=${dayjs().valueOf()}`);
|
|
if (e || !ndmApplicationList) {
|
|
throw e;
|
|
}
|
|
const { ndmAppKey } = getAppEnvConfig();
|
|
const applicationId = ndmApplicationList.find((app) => app.appKey === ndmAppKey)?.id ?? '';
|
|
const [err, ndmAppResource] = await userClient.get<{ resourceList: string[] }>(`/api/oauth/anyone/visible/resource?applicationId=${applicationId}&_t=${dayjs().valueOf()}`);
|
|
if (err || !ndmAppResource) {
|
|
throw err;
|
|
}
|
|
userResourceList.value = ndmAppResource.resourceList;
|
|
};
|
|
|
|
const lampLogin = async (stationCode: string) => {
|
|
const { data: accountRecord } = await axios.get<Record<string, { username: string; password: string }>>(`/minio/ndm/ndm-accounts.json?_t=${dayjs().unix()}`);
|
|
const data = {
|
|
username: aesEncryption.encryptByAES(accountRecord[stationCode].username),
|
|
password: aesEncryption.encryptByAES(accountRecord[stationCode].password),
|
|
grantType: 'PASSWORD',
|
|
};
|
|
const headers = getHeaders();
|
|
const { data: respData } = await axios.post<Result<LoginResult>>(`/${stationCode}/api/oauth/anyTenant/login`, data, { headers });
|
|
// 如果登录返回失败,需要提示用户检查用户名和密码配置,并全局停止轮询
|
|
if (!respData.isSuccess) {
|
|
console.error(respData);
|
|
const stationStore = useStationStore();
|
|
console.log('stationList:', stationStore.stationList);
|
|
const stationName = stationStore.stationList.find((station) => station.code === stationCode)?.name ?? '';
|
|
window.$dialog.destroyAll();
|
|
window.$dialog.error({
|
|
closable: false,
|
|
maskClosable: false,
|
|
draggable: true,
|
|
title: `${stationName}登录失败`,
|
|
content: `请检查该车站的用户名和密码配置,并在确认无误后刷新页面!`,
|
|
positiveText: '刷新',
|
|
onPositiveClick: () => {
|
|
window.location.reload();
|
|
},
|
|
});
|
|
const queryControlStore = useQueryControlStore();
|
|
queryControlStore.disablePolling();
|
|
throw new AxiosError(respData.msg, `${respData.code}`);
|
|
} else {
|
|
if (lampLoginResultRecord.value === null) {
|
|
lampLoginResultRecord.value = {};
|
|
}
|
|
lampLoginResultRecord.value[stationCode] = respData.data;
|
|
}
|
|
};
|
|
|
|
return {
|
|
userLoginResult,
|
|
userInfo,
|
|
userResourceList,
|
|
lampLoginResultRecord,
|
|
|
|
resetStore,
|
|
userLogin,
|
|
userLogout,
|
|
userGetInfo,
|
|
userGetResourceList,
|
|
lampLogin,
|
|
};
|
|
},
|
|
{
|
|
persist: true,
|
|
},
|
|
);
|