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 6e29f06940
commit fe3caea2d9
2 changed files with 108 additions and 106 deletions

View File

@ -130,6 +130,7 @@ export default {
this.fullTreeNodes = []; this.fullTreeNodes = [];
this.failsTreeNodes = []; this.failsTreeNodes = [];
this.isRequestResult = false; this.isRequestResult = false;
this.activeName = "total";
}, },
handleClick(tab, event) { handleClick(tab, event) {
this.isRequestResult = false; 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"> <ms-drawer class="json-path-picker" :visible="visible" :size="30" @close="close" direction="right" v-clickoutside="close">
<template v-slot:header> <template v-slot:header>
<ms-instructions-icon :content="tip"/> <ms-instructions-icon :content="tip"/>
{{tip}} {{ tip }}
</template> </template>
<jsonpath-picker :code="data" v-on:path="pathChangeHandler" ref="jsonpathPicker"/> <jsonpath-picker :code="data" v-on:path="pathChangeHandler" ref="jsonpathPicker"/>
</ms-drawer> </ms-drawer>
@ -16,8 +16,8 @@ let dotReplace = "#DOT_MASK#";
const clickoutside = { const clickoutside = {
// //
bind (el, binding, vnode) { bind(el, binding, vnode) {
function documentHandler (e) { function documentHandler(e) {
// //
if (el.contains(e.target)) { if (el.contains(e.target)) {
return false return false
@ -28,12 +28,14 @@ const clickoutside = {
binding.value(e) binding.value(e)
} }
} }
// 便unbind // 便unbind
el.__vueClickOutside__ = documentHandler el.__vueClickOutside__ = documentHandler
document.addEventListener('click', documentHandler) document.addEventListener('click', documentHandler)
}, },
update () { }, update() {
unbind (el, binding) { },
unbind(el, binding) {
// //
document.removeEventListener('click', el.__vueClickOutside__) document.removeEventListener('click', el.__vueClickOutside__)
delete el.__vueClickOutside__ delete el.__vueClickOutside__
@ -43,7 +45,7 @@ const clickoutside = {
export default { export default {
name: "MsApiJsonpathSuggest", name: "MsApiJsonpathSuggest",
components: {MsInstructionsIcon, MsDrawer}, components: {MsInstructionsIcon, MsDrawer},
directives: { clickoutside }, directives: {clickoutside},
data() { data() {
return { return {
visible: false, visible: false,
@ -59,116 +61,115 @@ export default {
} }
}, },
}, },
methods: { methods: {
close() { close() {
this.visible = false; this.visible = false;
}, },
open(objStr) { open(objStr) {
this.data = {}; this.data = {};
try { try {
// //
// let stringedJSON = objStr.replace(/:\s*([-+Ee0-9.]+)/g, ': "$1"'); let JSONBig = require('json-bigint')({"storeAsString": true});
// let param = JSON.parse(JSON.stringify(JSONBig.parse(objStr)));
let param = JSON.parse(objStr); if (param instanceof Array) {
if (param instanceof Array) { this.$warning('不支持解析JSON数组');
this.$warning('不支持解析JSON数组');
return;
}
this.data = param;
} catch (e) {
this.$warning(this.$t('api_test.request.assertions.json_path_err'));
return; return;
} }
this.visible = true; this.data = param;
}, } catch (e) {
pathChangeHandler(data) { this.$warning(this.$t('api_test.request.assertions.json_path_err'));
let paramNames = []; return;
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.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> </script>
<style scoped> <style scoped>
.json-path-picker { .json-path-picker {
padding: 10px 13px; padding: 10px 13px;
} }
.json-path-picker >>> .json-tree { .json-path-picker >>> .json-tree {
margin-top: 0px; margin-top: 0px;
margin-left: 6px; margin-left: 6px;
} }
/deep/ .el-icon-close:hover { /deep/ .el-icon-close:hover {
font-size: 30px; font-size: 30px;
font-weight: bold; font-weight: bold;
} }
</style> </style>