fix(接口测试): 修复推荐提取数字显示精确度错误问题

--bug=1009465 --user=赵勇 [github#9090]JSONPath提取器数据精度丢失 https://www.tapd.cn/55049933/s/1090126
This commit is contained in:
fit2-zhao 2022-01-10 18:29:53 +08:00 committed by 刘瑞斌
parent 09a4129dd6
commit 9d085402f1
2 changed files with 108 additions and 106 deletions

View File

@ -125,6 +125,7 @@ export default {
this.fullTreeNodes = [];
this.failsTreeNodes = [];
this.isRequestResult = false;
this.activeName = "total";
},
handleClick(tab, event) {
this.isRequestResult = false;

View File

@ -2,7 +2,7 @@
<ms-drawer class="json-path-picker" :visible="visible" :size="30" @close="close" direction="right" v-clickoutside="close">
<template v-slot:header>
<ms-instructions-icon :content="tip"/>
{{tip}}
{{ tip }}
</template>
<jsonpath-picker :code="data" v-on:path="pathChangeHandler" ref="jsonpathPicker"/>
</ms-drawer>
@ -16,8 +16,8 @@ let dotReplace = "#DOT_MASK#";
const clickoutside = {
//
bind (el, binding, vnode) {
function documentHandler (e) {
bind(el, binding, vnode) {
function documentHandler(e) {
//
if (el.contains(e.target)) {
return false
@ -28,12 +28,14 @@ const clickoutside = {
binding.value(e)
}
}
// 便unbind
el.__vueClickOutside__ = documentHandler
document.addEventListener('click', documentHandler)
},
update () { },
unbind (el, binding) {
update() {
},
unbind(el, binding) {
//
document.removeEventListener('click', el.__vueClickOutside__)
delete el.__vueClickOutside__
@ -43,7 +45,7 @@ const clickoutside = {
export default {
name: "MsApiJsonpathSuggest",
components: {MsInstructionsIcon, MsDrawer},
directives: { clickoutside },
directives: {clickoutside},
data() {
return {
visible: false,
@ -59,116 +61,115 @@ export default {
}
},
},
methods: {
close() {
this.visible = false;
},
open(objStr) {
this.data = {};
try {
//
// let stringedJSON = objStr.replace(/:\s*([-+Ee0-9.]+)/g, ': "$1"');
//
let param = JSON.parse(objStr);
if (param instanceof Array) {
this.$warning('不支持解析JSON数组');
return;
}
this.data = param;
} catch (e) {
this.$warning(this.$t('api_test.request.assertions.json_path_err'));
methods: {
close() {
this.visible = false;
},
open(objStr) {
this.data = {};
try {
//
let JSONBig = require('json-bigint')({"storeAsString": true});
let param = JSON.parse(JSON.stringify(JSONBig.parse(objStr)));
if (param instanceof Array) {
this.$warning('不支持解析JSON数组');
return;
}
this.visible = true;
},
pathChangeHandler(data) {
let paramNames = [];
let result = {};
try {
paramNames = this.parseSpecialChar(data);
result = this.getParamValue(this.data, 0, paramNames);
} catch (e) {
result = {};
result.key = 'var';
}
result.path = '$.' + data;
this.$emit('addSuggest', result);
},
// .
parseSpecialChar(data) {
let paramNames = [];
let reg = /\['.*'\]/;
let searchStr = reg.exec(data);
if (searchStr) {
searchStr.forEach(item => {
if (data.startsWith("['")) {
data = data.replace(item, item.replace('.', dotReplace));
} else {
data = data.replace(item, '.' + item.replace('.', dotReplace));
}
});
paramNames = data.split('.');
} else {
paramNames = data.split('.');
}
for (let i in paramNames) {
if (paramNames[i].search(reg) > -1) {
paramNames[i] = paramNames[i].substring(2, paramNames[i].length - 2);
}
paramNames[i] = paramNames[i].replace(dotReplace, '.');
}
return paramNames;
},
getParamValue(obj, index, params) {
if (params.length < 1) {
return {};
}
let param = params[index];
let childObj;
let reg = /\[\d\]$/;
let regIndex = param.search(reg);
if (regIndex > -1) {
let paramName = param.substring(0, regIndex);
let paramIndex = param.substring(regIndex + 1, param.length - 1);
param = paramIndex;
childObj = obj[paramName][paramIndex];
} else {
childObj = obj[params[index]];
}
if (index === params.length - 1) {
if (childObj instanceof Object) {
childObj = JSON.stringify(childObj);
} else {
childObj = childObj + "";
}
return {
key: param,
value: childObj
};
}
index++;
return this.getParamValue(childObj, index, params);
this.data = param;
} catch (e) {
this.$warning(this.$t('api_test.request.assertions.json_path_err'));
return;
}
this.visible = true;
},
pathChangeHandler(data) {
let paramNames = [];
let result = {};
try {
paramNames = this.parseSpecialChar(data);
result = this.getParamValue(this.data, 0, paramNames);
} catch (e) {
result = {};
result.key = 'var';
}
result.path = '$.' + data;
this.$emit('addSuggest', result);
},
// .
parseSpecialChar(data) {
let paramNames = [];
let reg = /\['.*'\]/;
let searchStr = reg.exec(data);
if (searchStr) {
searchStr.forEach(item => {
if (data.startsWith("['")) {
data = data.replace(item, item.replace('.', dotReplace));
} else {
data = data.replace(item, '.' + item.replace('.', dotReplace));
}
});
paramNames = data.split('.');
} else {
paramNames = data.split('.');
}
for (let i in paramNames) {
if (paramNames[i].search(reg) > -1) {
paramNames[i] = paramNames[i].substring(2, paramNames[i].length - 2);
}
paramNames[i] = paramNames[i].replace(dotReplace, '.');
}
return paramNames;
},
getParamValue(obj, index, params) {
if (params.length < 1) {
return {};
}
let param = params[index];
let childObj;
let reg = /\[\d\]$/;
let regIndex = param.search(reg);
if (regIndex > -1) {
let paramName = param.substring(0, regIndex);
let paramIndex = param.substring(regIndex + 1, param.length - 1);
param = paramIndex;
childObj = obj[paramName][paramIndex];
} else {
childObj = obj[params[index]];
}
if (index === params.length - 1) {
if (childObj instanceof Object) {
childObj = JSON.stringify(childObj);
} else {
childObj = childObj + "";
}
return {
key: param,
value: childObj
};
}
index++;
return this.getParamValue(childObj, index, params);
}
}
}
</script>
<style scoped>
.json-path-picker {
padding: 10px 13px;
}
.json-path-picker {
padding: 10px 13px;
}
.json-path-picker >>> .json-tree {
margin-top: 0px;
margin-left: 6px;
}
.json-path-picker >>> .json-tree {
margin-top: 0px;
margin-left: 6px;
}
/deep/ .el-icon-close:hover {
font-size: 30px;
font-weight: bold;
}
/deep/ .el-icon-close:hover {
font-size: 30px;
font-weight: bold;
}
</style>