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

65 lines
3.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 } from 'oak-external-sdk';
/**
* @param systemConfig
* @param service
* @param origin
* @returns
*/
export function getConfig(systemConfig, service, origin) {
let originConfig = systemConfig[service] && systemConfig[service][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,
};
}
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,
};
}
}
}