|
|
|
import { Api } from './api';
|
|
|
|
import { Connection, TestRequest, ConnectionPoolType, SocketResult, ConnectionLicInfo } from './crud.typings';
|
|
|
|
import { requestGet, requestDelete, requestPost, requestPut } from './crud.service';
|
|
|
|
import { editStatusEvent, errorCode } from '@constants/env';
|
|
|
|
|
|
|
|
export class DecisionApi implements Api {
|
|
|
|
isDec = true;
|
|
|
|
|
|
|
|
getConnectionList(): Promise<{ data?: Connection[] }> {
|
|
|
|
return requestGet('list', {});
|
|
|
|
}
|
|
|
|
|
|
|
|
getConnectionLicInfo(): Promise<{ data?: ConnectionLicInfo }> {
|
|
|
|
return requestGet('lic/info', {});
|
|
|
|
}
|
|
|
|
|
|
|
|
deleteConnection(connectionName: string) {
|
|
|
|
return requestDelete('', {
|
|
|
|
connectionName,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
addConnection(data: Connection) {
|
|
|
|
const form = {
|
|
|
|
...data,
|
|
|
|
connectionId: data.connectionId || data.connectionName,
|
|
|
|
connectionData: JSON.stringify(data.connectionData),
|
|
|
|
};
|
|
|
|
|
|
|
|
return requestPost('', form);
|
|
|
|
}
|
|
|
|
|
|
|
|
updateConnection(data: Connection) {
|
|
|
|
const form = {
|
|
|
|
...data,
|
|
|
|
connectionData: JSON.stringify(data.connectionData),
|
|
|
|
};
|
|
|
|
|
|
|
|
return requestPut('', form);
|
|
|
|
}
|
|
|
|
|
|
|
|
testConnection(data: Connection): Promise<TestRequest> {
|
|
|
|
const form = {
|
|
|
|
...data,
|
|
|
|
connectionData: JSON.stringify(data.connectionData),
|
|
|
|
};
|
|
|
|
|
|
|
|
return requestPost('test', form);
|
|
|
|
}
|
|
|
|
|
|
|
|
getConnectionPool(name: string): Promise<{ data?: ConnectionPoolType }> {
|
|
|
|
return requestGet(`pool/info?connectionName=${encodeURIComponent(name)}`, {});
|
|
|
|
}
|
|
|
|
|
|
|
|
getSimpleDriverList(): Promise<{ data: any[] }> {
|
|
|
|
return new Promise(resolve => {
|
|
|
|
Dec.reqGet('/v10/drivers/simple/list', '', re => {
|
|
|
|
resolve(re);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
getConnectionStatus(name: string): Promise<SocketResult> {
|
|
|
|
// TODO: 当前emit同步回调已无法执行
|
|
|
|
this.sendEditStatusEvent(name, editStatusEvent.OPEN);
|
|
|
|
|
|
|
|
return new Promise((resolve, rejected) => {
|
|
|
|
this.addEventListener(editStatusEvent.OPEN, res => {
|
|
|
|
const data = JSON.parse(res.data);
|
|
|
|
|
|
|
|
if (data.errorCode) {
|
|
|
|
let errorMessage = '';
|
|
|
|
switch (data.errorCode) {
|
|
|
|
case errorCode.CONNECTION_DELETED:
|
|
|
|
errorMessage = 'Dec-Dcm_Connection_Deleted';
|
|
|
|
break;
|
|
|
|
case errorCode.CONNECTION_UNDER_EDIT:
|
|
|
|
errorMessage = 'Dec-Dcm_Connection_Is_Using';
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
errorMessage = data.errorMessage;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
BI.Msg.toast(BI.i18nText(errorMessage, data.errorMessage), {
|
|
|
|
level: 'error',
|
|
|
|
});
|
|
|
|
rejected(data);
|
|
|
|
} else {
|
|
|
|
resolve(res);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
setEditedConnectionStatus(name: string): Promise<SocketResult> {
|
|
|
|
return this.sendEditStatusEvent(name, editStatusEvent.CONNECTION_EDITED);
|
|
|
|
}
|
|
|
|
|
|
|
|
shutdownConnectionStatus(name: string): Promise<SocketResult> {
|
|
|
|
return this.sendEditStatusEvent(name, editStatusEvent.CONNECTION_RELEASED);
|
|
|
|
}
|
|
|
|
|
|
|
|
getSocketStatus(): boolean {
|
|
|
|
return !!Dec?.socket?.connected;
|
|
|
|
}
|
|
|
|
|
|
|
|
isDriverError(errorCode: string) {
|
|
|
|
if (Dec) {
|
|
|
|
return DecCst.ErrorCode.LACK_DRIVER === errorCode;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
getCipher(password: string) {
|
|
|
|
return BI.Providers.getProvider('dec.provider.cipher')
|
|
|
|
.getCipher(password);
|
|
|
|
}
|
|
|
|
|
|
|
|
getPlain(cipher: string) {
|
|
|
|
return BI.Providers.getProvider('dec.provider.cipher')
|
|
|
|
.getPlain(cipher);
|
|
|
|
}
|
|
|
|
|
|
|
|
getHyperlink(name: string) {
|
|
|
|
return Dec.system[DecCst.Hyperlink.DECISION_HYPERLINK_CONFIG][name];
|
|
|
|
}
|
|
|
|
|
|
|
|
private sendEditStatusEvent(name: string, type: string): Promise<SocketResult> {
|
|
|
|
return new Promise(resolve => {
|
|
|
|
if (Dec?.socket?.connected) {
|
|
|
|
Dec.socket.emit(type, BI.encode(name), (re: any) => {
|
|
|
|
resolve(re);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
resolve({ data: 'success' });
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
private addEventListener(name: string, callback: Function) {
|
|
|
|
if (!Dec.socket) return;
|
|
|
|
|
|
|
|
if (Dec.socket.hasListeners(name)) {
|
|
|
|
Dec.socket.removeListener(name);
|
|
|
|
}
|
|
|
|
|
|
|
|
Dec.socket.originOn(name, callback);
|
|
|
|
}
|
|
|
|
}
|