<template>
|
<el-dialog
|
:title="!dataForm.id ? '新增' : '修改'"
|
:close-on-click-modal="false"
|
:visible.sync="visible"
|
>
|
<el-form
|
:model="dataForm"
|
:rules="dataRule"
|
ref="dataForm"
|
label-width="100px"
|
class="l-mes"
|
>
|
<el-form-item label="标准编号" prop="standardNo">
|
<el-input v-model="dataForm.standardNo" placeholder=""></el-input>
|
</el-form-item>
|
<el-form-item label="标准名称" prop="standardName">
|
<el-input v-model="dataForm.standardName" placeholder=""></el-input>
|
</el-form-item>
|
<el-form-item label="备注" prop="remark">
|
<el-input v-model="dataForm.remark" placeholder=""></el-input>
|
</el-form-item>
|
<el-form-item v-show="dataForm.id" label="函数">
|
<div style="display:flex;">
|
<div
|
v-for="(item, i) in funcs"
|
:key="i"
|
style="margin-right:18px;color:red;font-size:12px;cursor: pointer;"
|
@click="addFun(item)"
|
>
|
<el-tooltip
|
class="item"
|
effect="dark"
|
:content="item.desc"
|
placement="top-start"
|
>
|
<div>{{ item.label }}</div>
|
</el-tooltip>
|
</div>
|
</div>
|
</el-form-item>
|
<el-form-item v-show="dataForm.id" label="判定条件">
|
<div>
|
<ZttMonacoEditor
|
:items="params"
|
:idSubfix="monacoId"
|
ref="orderTemplateJudgmentCondition"
|
></ZttMonacoEditor>
|
</div>
|
<div style="color:red;">
|
点击输入框后,鼠标右键可选择参数项
|
</div>
|
</el-form-item>
|
</el-form>
|
<span slot="footer" class="dialog-footer">
|
<el-button @click="visible = false">取消</el-button>
|
<el-button type="primary" @click="dataFormSubmit()">确定</el-button>
|
</span>
|
</el-dialog>
|
</template>
|
|
<script>
|
import {
|
addMoTestStandard,
|
updateTestStandard,
|
getMoTestStandardById,
|
getOperationTestStandardParam
|
} from '@/api/plan/moteststandard'
|
import ZttMonacoEditor from '@/views/common/ztt-monaco-editor.vue'
|
import { funcForStandard } from '@/util/testFunctionRule'
|
|
export default {
|
data() {
|
return {
|
monacoId: 'order-standard',
|
visible: false,
|
dataForm: {
|
id: 0,
|
standardName: '',
|
standardNo: '',
|
remark: '',
|
moRoutingOperationId: 0,
|
moId: 0,
|
judgeFormula: ''
|
},
|
dataRule: {
|
standardName: [
|
{ required: true, message: '标准编号不能为空', trigger: 'blur' }
|
],
|
standardNo: [
|
{ required: true, message: '标准名称不能为空', trigger: 'blur' }
|
]
|
},
|
params: [],
|
funcs: []
|
}
|
},
|
components: {
|
ZttMonacoEditor
|
},
|
methods: {
|
init(moId, moRoutingOperationId, id) {
|
this.dataForm.id = id || 0
|
this.visible = true
|
this.funcs = funcForStandard()
|
this.$nextTick(() => {
|
this.$refs.dataForm.resetFields()
|
if (this.dataForm.id) {
|
getMoTestStandardById(this.dataForm.id).then((response) => {
|
this.dataForm = response.data.data
|
this.$refs.orderTemplateJudgmentCondition.setVal(
|
this.dataForm.judgeFormula == null
|
? ''
|
: this.dataForm.judgeFormula
|
)
|
})
|
this.params = []
|
getOperationTestStandardParam(
|
Object.assign({
|
moTestStandardId: this.dataForm.id
|
})
|
).then((response) => {
|
this.params = response.data.data
|
})
|
} else {
|
this.dataForm.moRoutingOperationId = moRoutingOperationId
|
this.dataForm.moId = moId
|
this.$refs.orderTemplateJudgmentCondition.setVal('')
|
}
|
})
|
},
|
addFun(item) {
|
let index = this.$refs.orderTemplateJudgmentCondition.getPosition().column
|
const lineNumber = this.$refs.orderTemplateJudgmentCondition.getPosition()
|
.lineNumber
|
let preIndex = 0
|
if (lineNumber > 1) {
|
for (let i = 1; i <= lineNumber - 1; i++) {
|
preIndex +=
|
this.$refs.orderTemplateJudgmentCondition.getLineLength(i) + 2
|
}
|
}
|
index = index + preIndex
|
const pre = this.$refs.orderTemplateJudgmentCondition
|
.getVal()
|
.substring(0, index - 1)
|
const fix = this.$refs.orderTemplateJudgmentCondition
|
.getVal()
|
.substring(index - 1)
|
this.$refs.orderTemplateJudgmentCondition.setVal(pre + item.label + fix)
|
},
|
// 获取某段字符在字符串中出现的次数
|
getNumCharInStr(wholeSection, fragment, i) {
|
let num = i
|
const existIndex = wholeSection.indexOf(fragment)
|
if (existIndex >= 0) {
|
num = num + 1
|
wholeSection = wholeSection.substring(existIndex + fragment.length)
|
return this.getNumCharInStr(wholeSection, fragment, num)
|
} else {
|
return num
|
}
|
},
|
getRandom() {
|
let num = Math.random()
|
num = num * 100
|
num = Math.floor(num)
|
return num
|
},
|
// 表单提交
|
dataFormSubmit() {
|
// 校验判定公式
|
let judgeFormula = this.$refs.orderTemplateJudgmentCondition.getVal()
|
// 先判定公式中函数格式是否正确,再用默认值计算出函数值,替换进公式,最后将公式中剩余的参数进行默认值替换,利用js eval函数进行公式校验
|
let flag = true
|
let funcEl
|
for (let j = 0; j < this.funcs.length; j++) {
|
funcEl = this.funcs[j]
|
// 计算函数关键在公式中出现的次数
|
const num = this.getNumCharInStr(judgeFormula, funcEl.name, 0)
|
// for循环次数校验替换
|
for (let k = 0; k < num; k++) {
|
const existIndex = judgeFormula.indexOf(funcEl.name)
|
if (existIndex >= 0) {
|
// 函数关键字在公式中存在
|
const prefixStr = judgeFormula.substring(0, existIndex)
|
const startToEndStr = judgeFormula.substring(
|
existIndex + funcEl.name.length
|
)
|
if (startToEndStr.length > 0) {
|
const paramStartIndex = startToEndStr.indexOf('(')
|
const paramEndIndex = startToEndStr.indexOf(')')
|
if (paramStartIndex === 0 && paramEndIndex > 0) {
|
const suffixStr = startToEndStr.substring(paramEndIndex + 1)
|
const paramStr = startToEndStr.substring(
|
paramStartIndex + 1,
|
paramEndIndex
|
)
|
if (paramStr.trim() !== '') {
|
const paramArr = paramStr.split(',')
|
if (paramArr.length > 0) {
|
if (
|
(funcEl.singleParam && paramArr.length === 1) ||
|
!funcEl.singleParam
|
) {
|
let paramFlag = true
|
for (let i = 0; i < paramArr.length; i++) {
|
const param = this.params.find(
|
(item) =>
|
paramArr[i].trim() === 'V[' + item.code + ']'
|
)
|
if (param !== undefined) {
|
} else {
|
flag = false
|
paramFlag = false
|
this.$message.error('函数参数不存在')
|
}
|
}
|
if (paramFlag) {
|
if (funcEl.returnType === 'number') {
|
judgeFormula = prefixStr + '1' + suffixStr
|
} else {
|
judgeFormula = prefixStr + 'true' + suffixStr
|
}
|
} else {
|
break
|
}
|
} else {
|
flag = false
|
this.$message.error('函数参数个数不正确')
|
break
|
}
|
} else {
|
break
|
}
|
} else {
|
flag = false
|
this.$message.error('函数必须要有参数')
|
break
|
}
|
} else {
|
flag = false
|
this.$message.error(
|
'函数需包含左右小括号,且左侧小括号需紧邻函数'
|
)
|
break
|
}
|
} else {
|
flag = false
|
this.$message.error('函数需包含左右小括号')
|
break
|
}
|
}
|
}
|
}
|
if (flag) {
|
// 将公式中剩余的参数进行默认值替换
|
let param
|
for (let x = 0; x < this.params.length; x++) {
|
param = this.params[x]
|
// 计算参数在公式中出现的次数
|
const num = this.getNumCharInStr(
|
judgeFormula,
|
'V[' + param.code + ']',
|
0
|
)
|
// for循环次数校验替换
|
for (let y = 0; y < num; y++) {
|
const existIndex = judgeFormula.indexOf('V[' + param.code + ']')
|
if (existIndex >= 0) {
|
const prefixStr = judgeFormula.substring(0, existIndex)
|
const suffixStr = judgeFormula.substring(
|
existIndex + ('V[' + param.code + ']').length
|
)
|
judgeFormula = prefixStr + this.getRandom() + suffixStr
|
}
|
}
|
}
|
try {
|
eval(judgeFormula)
|
} catch (exception) {
|
flag = false
|
}
|
if (flag) {
|
this.dataForm.judgeFormula = this.$refs.orderTemplateJudgmentCondition.getVal()
|
this.$refs.dataForm.validate((valid) => {
|
if (valid) {
|
if (this.dataForm.id) {
|
updateTestStandard(this.dataForm).then((response) => {
|
this.$message.success('检测标准修改成功')
|
this.visible = false
|
this.$emit('refreshDataList', response.data.data)
|
})
|
} else {
|
addMoTestStandard(this.dataForm).then((response) => {
|
this.$message.success('检测标准添加成功')
|
this.visible = false
|
this.$emit('refreshDataList', response.data.data)
|
})
|
}
|
}
|
})
|
} else {
|
this.$message.error('公式格式错误,请仔细检查')
|
}
|
}
|
}
|
}
|
}
|
</script>
|