112 lines
3.5 KiB
JavaScript
112 lines
3.5 KiB
JavaScript
import { cloneDeep, set } from 'oak-domain/lib/utils/lodash';
|
||
import { generateNewId } from 'oak-domain/lib/utils/uuid';
|
||
import { isEmptyJsonObject } from '../../../utils/strings';
|
||
export default OakComponent({
|
||
isList: false,
|
||
properties: {
|
||
config: {},
|
||
entity: 'application',
|
||
entityId: '',
|
||
name: '',
|
||
},
|
||
data: {
|
||
initialConfig: {},
|
||
dirty: false,
|
||
currentConfig: {},
|
||
selections: [],
|
||
},
|
||
lifetimes: {
|
||
async ready() {
|
||
const { config } = this.props;
|
||
this.setState({
|
||
initialConfig: config,
|
||
dirty: false,
|
||
currentConfig: cloneDeep(config),
|
||
});
|
||
const systemId = this.features.application.getApplication().systemId;
|
||
const { data: [system] } = await this.features.cache.refresh("system", {
|
||
data: {
|
||
config: {
|
||
Cos: {
|
||
qiniu: 1,
|
||
ctyun: 1,
|
||
aliyun: 1,
|
||
tencent: 1,
|
||
local: 1,
|
||
s3: 1,
|
||
},
|
||
},
|
||
},
|
||
filter: {
|
||
id: systemId,
|
||
}
|
||
});
|
||
const cosConfig = system?.config?.Cos;
|
||
// 如果key存在并且不为defaultOrigin并且value的keys长度大于0,则加入选择项
|
||
const selections = [];
|
||
if (cosConfig) {
|
||
for (const [key, value] of Object.entries(cosConfig)) {
|
||
if (key === 'defaultOrigin') {
|
||
continue;
|
||
}
|
||
if (value && !isEmptyJsonObject(value)) {
|
||
selections.push({
|
||
name: key,
|
||
value: key,
|
||
});
|
||
}
|
||
}
|
||
}
|
||
this.setState({
|
||
selections,
|
||
});
|
||
}
|
||
},
|
||
methods: {
|
||
setValue(path, value) {
|
||
const { currentConfig } = this.state;
|
||
const newConfig = cloneDeep(currentConfig || {});
|
||
set(newConfig, path, value);
|
||
this.setState({
|
||
currentConfig: newConfig,
|
||
dirty: true,
|
||
});
|
||
},
|
||
resetConfig() {
|
||
const { initialConfig } = this.state;
|
||
this.setState({
|
||
dirty: false,
|
||
currentConfig: cloneDeep(initialConfig),
|
||
});
|
||
},
|
||
async updateConfig() {
|
||
const { currentConfig } = this.state;
|
||
const { entity, entityId } = this.props;
|
||
if (!entityId) {
|
||
this.setMessage({
|
||
content: '缺少实体ID,无法更新配置',
|
||
type: 'error',
|
||
});
|
||
return;
|
||
}
|
||
await this.features.cache.operate("application", {
|
||
id: generateNewId(),
|
||
action: 'update',
|
||
data: {
|
||
config: currentConfig,
|
||
},
|
||
filter: {
|
||
id: entityId,
|
||
}
|
||
}, {});
|
||
this.setMessage({
|
||
content: '操作成功',
|
||
type: 'success',
|
||
});
|
||
this.setState({
|
||
dirty: false,
|
||
});
|
||
},
|
||
},
|
||
});
|