计算执行定位

This commit is contained in:
wcq 2023-04-13 16:51:25 +08:00
parent e80e6ac44f
commit bbddcac129
3 changed files with 60 additions and 16 deletions

View File

@ -7,7 +7,7 @@
"editor.tabSize": 2,
"editor.formatOnPaste": true,
"editor.guides.bracketPairs": "active",
"files.autoSave": "afterDelay",
"files.autoSave": "off",
"git.confirmSync": false,
"workbench.startupEditor": "newUntitledFile",
"editor.suggestSelection": "first",

View File

@ -3,9 +3,9 @@
<div class="m-2">
<el-button size="small" type="primary" class="ml-2" @click="saveToCache">保存到缓存</el-button>
<el-button size="small" type="primary" class="ml-2" @click="saveToFile">保存到文件</el-button>
<el-button size="small" type="success" class="ml-2" @click="() => run(calModel.data)">运行</el-button>
<el-button size="small" type="success" class="ml-2" @click="runModel">运行</el-button>
</div>
<el-table highlight-current-row ref="table" :data="[calModel.data]" style="width: 100%; margin-bottom: 20px"
<el-table :cell-style="tableCellStyle" ref="table" :data="[calModel.data]" style="width: 100%; margin-bottom: 20px"
row-key="id" align="center" border default-expand-all>
<el-table-column prop="name" label="指标名称" />
<!-- <el-table-column prop="key" label="键值" align="center" /> -->
@ -26,15 +26,13 @@
<el-table-column prop="unit" label="单位" width="120" align="center" />
<el-table-column label="操作" width="230" align="center">
<template #default="scope">
<el-button color="#626aef" :disabled="!(scope.row.children && scope.row.children.length > 0)" size="small"
type="info" @click="showCodeEdit(scope.row, scope)">公式</el-button>
<!-- <el-button size="small" type="primary" @click="showRowAdd(scope.row)">添加</el-button> -->
<!-- <el-button size="small" type="primary" @click="showRowEdit(scope.row)">编辑</el-button> -->
<el-button type="success" size="small" :icon="Plus" circle @click="showRowAdd(scope.row)" />
<el-button type="success" size="small" :icon="CaretRight" circle @click="run(scope.row)" />
<el-button type="primary" size="small" :icon="Edit" circle @click="showRowEdit(scope.row)" />
<el-button type="success" size="small" :icon="Plus" circle @click="showRowAdd(scope.row)" />
<el-button type="danger" size="small" :icon="Delete" circle @click="showRowDelete(scope.row)" />
<!-- <el-button size="small" type="danger" @click="showRowDelete(scope.row)">删除</el-button> -->
<el-button color="#626aef" :disabled="!(scope.row.children && scope.row.children.length > 0)" size="small"
type="info" @click="showCodeEdit(scope.row, scope)">公式</el-button>
</template>
</el-table-column>
</el-table>
@ -104,7 +102,7 @@
</div>
</template>
<script lang="ts" setup>
import { ElMessageBox, ElMessage, ElTable } from "element-plus";
import { ElMessageBox, ElMessage, ElTable, ColumnStyle } from "element-plus";
import { onMounted, ref } from "vue";
import { useDebounceFn } from "@vueuse/core";
import { CalModel, Index } from "@/components/IndexCal/types";
@ -123,6 +121,8 @@ const rowEditVisible = ref(false);
const codeEditVisible = ref(false);
const funcEditerKey = ref(String(new Date()))
const addRowTemp = ref<Index>(null);
const highlightRowIdList = ref<number[]>([]);
const highlightCellList = ref<{ id: number, columnIndex: number }[]>([]);
const editRowTemp = ref<Index>({
id: 0, name: '', inputValue: 1,
calValue: 1,
@ -144,7 +144,22 @@ function copy(obj) {
}
return obj;
}
function tableRowStyle({ row, rowIndex }: { row: Index, rowIndex: number }): ColumnStyle<any> {
if (highlightRowIdList.value.indexOf(row.id) !== -1) {
return { color: 'red', background: "red!important", backgroundColor: "red!important", opacity: 1 }
} else {
return {}
}
}
function tableCellStyle({ row, column, rowIndex, columnIndex }: { row: Index, column: any, rowIndex: number, columnIndex: number }): ColumnStyle<any> {
let strList = highlightCellList.value.map(item => `${item.id}--${item.columnIndex}`)
if (strList.indexOf(`${row.id}--${columnIndex}`) !== -1) {
return { background: 'red' }
} else {
return {}
}
}
function showRowAdd(row: Index) {
// const rs = findParent(row, tableData.value);
// console.log("rrr", rs);
@ -230,6 +245,10 @@ function showCodeEdit(row: Index, scope: any) {
codeEditVisible.value = true;
}
function runModel() {
run(calModel.value.data);
// ElMessage.success('')
}
//
function findParent(findItem, tree: Index) {
let parens = [tree];
@ -303,13 +322,25 @@ function run(data: Index) {
const runner = new IndexCalRunner(data)
try {
runner.run(data, calModel.value.defaultLang)
highlightRowIdList.value = []
highlightCellList.value = []
ElMessage.success('计算成功')
} catch (e) {
if (e instanceof IndexCalError) {
console.error(e)
highlightRowIdList.value = [e.store.index.id]
if (e.type == 'paramValueNotInput') {
highlightCellList.value = [{ id: e.store.index.id, columnIndex: 2 }]
}
if (e.type == "codeConfigNotConfig") {
highlightCellList.value = [{ id: e.store.index.id, columnIndex: 0 }]
}
if (e.type == "evalError") {
highlightCellList.value = [{ id: e.store.index.id, columnIndex: 0 }]
}
ElMessage.warning(e.message)
} else {
throw e;
}
throw e;
}
}

View File

@ -1,7 +1,12 @@
import type { Index, Lang } from "@/components/IndexCal/types";
type IndexCalErrorType = "codeConfigNotConfig" | "paramValueNotInput";
type IndexCalErrorType =
| "codeConfigNotConfig"
| "paramValueNotInput"
| ""
| "evalError";
interface errorStore {
index?: Index;
e?: Error;
}
class IndexCalError extends Error {
store: errorStore;
@ -82,7 +87,7 @@ function calIndex(index: Index, lang: Lang = "js") {
"paramValueNotInput",
`${index.name}-${item.name}值未配置`,
{
index
index: item
}
);
}
@ -98,8 +103,16 @@ function calIndex(index: Index, lang: Lang = "js") {
","
)}}=${JSON.stringify(paramDic)}`;
let evalCode = `()=>{${defineValueCode + "\n" + code}}`;
let res = eval(evalCode)();
return res;
try {
let res = eval(evalCode)();
return res;
} catch (e) {
console.error(e);
throw new IndexCalError("evalError", `${index.name}-公式计算错误`, {
index,
e
});
}
}
class IndexCalRunner {