oak-general-business/es/utils/getContextConfig.js

84 lines
4.1 KiB
JavaScript

import { assert } from 'oak-domain/lib/utils/assert';
import { OakDataException } from 'oak-domain/lib/types/Exception';
import { AmapSDK, QiniuSDK, CTYunSDK, ALiYunSDK, TencentYunSDK, LocalSDK, S3SDK } from 'oak-external-sdk';
/**
* @param systemConfig
* @param service
* @param origin
* @returns
*/
export function getConfig(systemConfig, service, origin) {
const serviceConfig = systemConfig[service];
let originConfig = serviceConfig && serviceConfig[origin];
let originCloudAccounts = originConfig && systemConfig.Account && systemConfig.Account[origin];
if (!originConfig) {
throw new OakDataException(`调用的服务${service}${origin}找不到相应的配置,请联系管理员`);
}
switch (origin) {
case 'ali': {
const aliAccount = originCloudAccounts.find((ele) => ele.accessKeyId === originConfig.accessKeyId);
assert(aliAccount, `调用的服务${service}${origin}找不到相应的平台帐号,请联系管理员`);
const aliyunInstance = ALiYunSDK.getInstance(aliAccount.accessKeyId, aliAccount.accessKeySecret);
return {
instance: aliyunInstance,
config: originConfig,
};
}
case 'tencent': {
const tencentAccount = originCloudAccounts.find((ele) => ele.secretId === originConfig.secretId);
assert(tencentAccount, `调用的服务${service}${origin}找不到相应的平台帐号,请联系管理员`);
const tencentInstance = TencentYunSDK.getInstance(tencentAccount.secretId, tencentAccount.secretKey);
return {
instance: tencentInstance,
config: originConfig,
};
}
case 'qiniu': {
const qiniuAccount = originCloudAccounts.find((ele) => ele.accessKey === originConfig.accessKey);
assert(qiniuAccount, `调用的服务${service}${origin}找不到相应的平台帐号,请联系管理员`);
const qiniuInstance = QiniuSDK.getInstance(qiniuAccount.accessKey, qiniuAccount.secretKey);
return {
instance: qiniuInstance,
config: originConfig,
};
}
case 'ctyun': {
const ctyunAccount = originCloudAccounts.find((ele) => ele.accessKey === originConfig.accessKey);
assert(ctyunAccount, `调用的服务${service}${origin}找不到相应的平台帐号,请联系管理员`);
const ctyunInstance = CTYunSDK.getInstance(ctyunAccount.accessKey, ctyunAccount.securityKey);
return {
instance: ctyunInstance,
config: originConfig,
};
}
case 'local': {
const localAccount = originCloudAccounts.find((ele) => ele.accessKey === originConfig.accessKey);
assert(localAccount, `调用的服务${service}${origin}找不到相应的平台帐号,请联系管理员`);
const localInstance = LocalSDK.getInstance(localAccount.accessKey, localAccount.secretKey);
return {
instance: localInstance,
config: originConfig,
};
}
case 's3': {
const s3Account = originCloudAccounts.find((ele) => ele.accessKey === originConfig.accessKey);
assert(s3Account, `调用的服务${service}${origin}找不到相应的平台帐号,请联系管理员`);
const s3Instance = S3SDK.getInstance(s3Account.accessKey, s3Account.secretKey);
return {
instance: s3Instance,
config: originConfig,
};
}
default: {
assert(origin === 'amap');
const amapAccount = originCloudAccounts.find((ele) => ele.webApiKey === originConfig.webApiKey);
assert(amapAccount, `调用的服务${service}${origin}找不到相应的平台帐号,请联系管理员`);
const amapInstance = AmapSDK.getInstance(amapAccount.webApiKey);
return {
instance: amapInstance,
config: originConfig,
};
}
}
}