mirror of
https://github.com/steedos/steedos-platform
synced 2024-11-22 00:01:52 +00:00
#638 新建记录时也应该执行公式计算,允许公式中带return语句
This commit is contained in:
parent
2bd334d4e1
commit
a949e6368b
@ -62,7 +62,11 @@ export const runFieldFormular = function (formula: string, params: Array<Steedos
|
||||
formulaParams[key] = value;
|
||||
formula = formula.replace(`{${key}}`, `__params["${key}"]`);
|
||||
});
|
||||
let formulaFun = `module.exports = function (__params) { return ${formula} }`;
|
||||
if(!/\breturn\b/.test(formula)){
|
||||
// 如果里面没有return语句,则在最前面加上return前缀
|
||||
formula = `return ${formula}`;
|
||||
}
|
||||
let formulaFun = `module.exports = function (__params) { ${formula} }`;
|
||||
// console.log("==runFieldFormular==formulaFun===", formulaFun);
|
||||
// console.log("==runFieldFormular==formulaParams===", formulaParams);
|
||||
let result = _eval(formulaFun)(formulaParams);
|
||||
|
@ -8,10 +8,14 @@ const runFieldFormular = async function (fieldFormulaConfig: SteedosFieldFormula
|
||||
|
||||
export const fieldFormulaTriggers = {
|
||||
beforeUpdate: async function () {
|
||||
// console.log("===fieldFormulaTriggers======beforeUpdate===", this);
|
||||
const configs = getObjectFieldFormulaConfigs(this.object_name);
|
||||
for (const config of configs) {
|
||||
// console.log("====config.formula==", config.formula);
|
||||
await runFieldFormular.bind(this)(config)
|
||||
}
|
||||
},
|
||||
beforeInsert: async function () {
|
||||
const configs = getObjectFieldFormulaConfigs(this.object_name);
|
||||
for (const config of configs) {
|
||||
await runFieldFormular.bind(this)(config)
|
||||
}
|
||||
}
|
||||
|
@ -68,6 +68,10 @@ const fieldFormulaBeforeUpdate = function(userId, doc, fieldNames, modifier, opt
|
||||
wrapAsync(objectql.fieldFormulaTriggers.beforeUpdate, Object.assign({userId: userId, spaceId: doc.space, id: doc._id, doc: modifier.$set, previousDoc: doc, object_name: this.object_name}))
|
||||
}
|
||||
|
||||
const fieldFormulaBeforeInsert = function(userId, doc){
|
||||
wrapAsync(objectql.fieldFormulaTriggers.beforeInsert, Object.assign({userId: userId, spaceId: doc.space, doc: doc, object_name: this.object_name}))
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
extend: 'base',
|
||||
actions: {
|
||||
@ -545,6 +549,13 @@ module.exports = {
|
||||
fieldFormulaBeforeUpdate.bind(this)(userId, doc, fieldNames, modifier, options);
|
||||
}
|
||||
},
|
||||
"before.insert.server.fieldFormula": {
|
||||
on: "server",
|
||||
when: "before.insert",
|
||||
todo: function (userId, doc) {
|
||||
fieldFormulaBeforeInsert.bind(this)(userId, doc);
|
||||
}
|
||||
},
|
||||
}
|
||||
};
|
||||
function setDetailOwner(doc, object_name, userId) {
|
||||
|
Loading…
Reference in New Issue
Block a user