fix(接口定义): 修复前端精度丢失缺陷
This commit is contained in:
parent
069b420f29
commit
fc64d0b608
|
@ -53,6 +53,7 @@
|
|||
"pinia-plugin-persistedstate": "^1.6.3",
|
||||
"print-js": "^1.6.0",
|
||||
"sha.js": "^2.4.11",
|
||||
"shepherd.js": "^10.0.0",
|
||||
"vue": "^2.7.3",
|
||||
"vue-calendar-heatmap": "^0.8.4",
|
||||
"vue-clipboard2": "^0.3.1",
|
||||
|
@ -64,13 +65,12 @@
|
|||
"vue-papa-parse": "^2.0.0",
|
||||
"vue-pdf": "^4.2.0",
|
||||
"vue-router": "^3.1.3",
|
||||
"vue-shepherd": "^0.3.0",
|
||||
"vue-virtual-scroll-list": "^2.3.3",
|
||||
"vue2-ace-editor": "0.0.15",
|
||||
"vuedraggable": "^2.24.3",
|
||||
"xml-js": "^1.6.11",
|
||||
"yan-progress": "^1.0.3",
|
||||
"vue-shepherd": "^0.3.0",
|
||||
"shepherd.js": "^10.0.0"
|
||||
"yan-progress": "^1.0.3"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@babel/core": "^7.12.16",
|
||||
|
|
|
@ -7,6 +7,7 @@ const isObject = require('lodash.isobject');
|
|||
const isString = require('lodash.isstring');
|
||||
const { post } = require('@/api/base-network');
|
||||
const { schemaToJson, apiPreview } = require('@/api/definition');
|
||||
const { default: CustomNum } = require('./customNum');
|
||||
const isArray = Array.isArray;
|
||||
|
||||
class Convert {
|
||||
|
@ -113,7 +114,7 @@ class Convert {
|
|||
if (!result['properties']) {
|
||||
continue;
|
||||
}
|
||||
if (isObject(element)) {
|
||||
if (isObject(element) && !(element instanceof CustomNum)) {
|
||||
// 创建当前属性的基本信息
|
||||
result['properties'][key] = this._value2object(element, $id, key);
|
||||
if (isArray(element)) {
|
||||
|
@ -235,11 +236,15 @@ class Convert {
|
|||
} else if (isArray(value)) {
|
||||
objectTemplate.type = 'array';
|
||||
objectTemplate['mock'] = undefined;
|
||||
} else if (isObject(value)) {
|
||||
} else if (value instanceof CustomNum) {
|
||||
// 解决丢失精度问题
|
||||
objectTemplate.type = 'number';
|
||||
objectTemplate['mock'].mock = value.get();
|
||||
}
|
||||
else if (isObject(value)) {
|
||||
objectTemplate.type = 'object';
|
||||
objectTemplate['mock'] = undefined;
|
||||
}
|
||||
|
||||
return objectTemplate;
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,9 @@
|
|||
class CustomNum{
|
||||
constructor(value) {
|
||||
this.value = value
|
||||
}
|
||||
get() {
|
||||
return this.value
|
||||
}
|
||||
}
|
||||
export default CustomNum;
|
|
@ -0,0 +1,91 @@
|
|||
import CustomNum from './customNum';
|
||||
|
||||
export const jsonParse = (jsonStr) => {
|
||||
let index = 0;
|
||||
function parseValue() {
|
||||
let char = jsonStr[index];
|
||||
if (char === '{') {
|
||||
return parseObject();
|
||||
} else if (char === '[') {
|
||||
return parseArray();
|
||||
} else if (char === '"') {
|
||||
return parseString();
|
||||
} else if (char === 't' || char === 'f') {
|
||||
return parseBoolean();
|
||||
} else if (char === 'n') {
|
||||
return parseNull();
|
||||
} else {
|
||||
return parseNumber();
|
||||
}
|
||||
}
|
||||
function parseObject() {
|
||||
let obj = {};
|
||||
index++;
|
||||
while (jsonStr[index] !== '}') {
|
||||
let key = parseString();
|
||||
index++;
|
||||
let value = parseValue();
|
||||
obj[key] = value;
|
||||
if (jsonStr[index] === ',') {
|
||||
index++;
|
||||
}
|
||||
}
|
||||
index++;
|
||||
return obj;
|
||||
}
|
||||
function parseArray() {
|
||||
let arr = [];
|
||||
index++;
|
||||
while (jsonStr[index] !== ']') {
|
||||
arr.push(parseValue());
|
||||
if (jsonStr[index] === ',') {
|
||||
index++;
|
||||
}
|
||||
}
|
||||
index++;
|
||||
return arr;
|
||||
}
|
||||
function parseString() {
|
||||
let str = '';
|
||||
index++;
|
||||
while (jsonStr[index] !== '"') {
|
||||
str += jsonStr[index];
|
||||
index++;
|
||||
}
|
||||
index++;
|
||||
return str;
|
||||
}
|
||||
function parseNumber() {
|
||||
let numStr = '';
|
||||
while (/[0-9.+-]/.test(jsonStr[index])) {
|
||||
numStr += jsonStr[index];
|
||||
index++;
|
||||
}
|
||||
if (!isInteger(numStr) || numStr.length > 15) {
|
||||
return new CustomNum(numStr);
|
||||
}
|
||||
return parseFloat(numStr);
|
||||
}
|
||||
function parseBoolean() {
|
||||
if (jsonStr[index] === 't') {
|
||||
index += 4;
|
||||
return true;
|
||||
} else {
|
||||
index += 5;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
function parseNull() {
|
||||
index += 4;
|
||||
return null;
|
||||
}
|
||||
return parseValue();
|
||||
};
|
||||
export const isInteger = (num) => {
|
||||
return /^\d+$/.test(num);
|
||||
};
|
||||
export const trimAll = (ele) => {
|
||||
if (typeof ele === 'string') {
|
||||
return ele.split(/[\t\r\f\n\s]*/g).join('');
|
||||
}
|
||||
};
|
|
@ -104,6 +104,7 @@ import BatchAddParameter from '../basis/BatchAddParameter';
|
|||
import Convert from '@/business/commons/json-schema/convert/convert';
|
||||
import { getApiParamsConfigFields } from 'metersphere-frontend/src/utils/custom_field';
|
||||
import ApiParamsConfig from '@/business/definition/components/request/components/ApiParamsConfig';
|
||||
import {jsonParse, trimAll} from '@/business/commons/json-schema/convert/jsonParse'
|
||||
|
||||
export default {
|
||||
name: 'MsApiBody',
|
||||
|
@ -256,15 +257,9 @@ export default {
|
|||
if (this.body.format === 'JSON-SCHEMA') {
|
||||
if (this.body.raw) {
|
||||
try {
|
||||
if (!this.body.jsonSchema) {
|
||||
this.body.jsonSchema = MsConvert.format(JSON.parse(this.body.raw));
|
||||
} else {
|
||||
let data = MsConvert.format(JSON.parse(this.body.raw));
|
||||
if (!this.body.jsonSchema.type) {
|
||||
this.body.jsonSchema.type = data.type;
|
||||
}
|
||||
this.body.jsonSchema = JSON.parse(JSON.stringify(this.deepAssign(this.body.jsonSchema, data)));
|
||||
}
|
||||
const tmpStr = trimAll(this.body.raw)
|
||||
const tmpObj = jsonParse(tmpStr)
|
||||
this.body.jsonSchema = MsConvert.format(tmpObj);
|
||||
} catch (e) {
|
||||
this.body.format = 'JSON';
|
||||
this.$message.error(this.$t('api_definition.body.json_format_error'));
|
||||
|
|
Loading…
Reference in New Issue