fix: 修复jsondiff 乱码问题

This commit is contained in:
gaoxiaolin.gao 2018-07-10 20:38:37 +08:00
parent d77e51e07c
commit 3c64435cf1
4 changed files with 91 additions and 94 deletions

View File

@ -9,11 +9,14 @@ import { Link } from 'react-router-dom';
import { fetchNewsData, fetchMoreNews } from '../../reducer/modules/news.js';
import { fetchInterfaceList } from '../../reducer/modules/interface.js';
import ErrMsg from '../ErrMsg/ErrMsg.js';
const jsondiffpatch = require('jsondiffpatch/public/build/jsondiffpatch-full.js');
const formattersHtml = require('jsondiffpatch/public/build/jsondiffpatch-formatters.js').html;
import 'jsondiffpatch/public/formatters-styles/annotated.css';
import 'jsondiffpatch/public/formatters-styles/html.css';
// const jsondiffpatch = require('jsondiffpatch/public/build/jsondiffpatch-full.js');
// const formattersHtml = require('jsondiffpatch/public/build/jsondiffpatch-formatters.js').html;
// import 'jsondiffpatch/public/formatters-styles/annotated.css';
// import 'jsondiffpatch/public/formatters-styles/html.css';
const jsondiffpatch = require('jsondiffpatch/dist/jsondiffpatch.umd.js');
const formattersHtml = jsondiffpatch.formatters.html;
import 'jsondiffpatch/dist/formatters-styles/annotated.css';
import 'jsondiffpatch/dist/formatters-styles/html.css';
import './TimeLine.scss';
// const Option = AutoComplete.Option;

View File

@ -2,16 +2,15 @@
const json5 = require('json5');
module.exports = function (jsondiffpatch, formattersHtml, curDiffData) {
const json5_parse = (json) => {
module.exports = function(jsondiffpatch, formattersHtml, curDiffData) {
const json5_parse = json => {
if (typeof json === 'object' && json) return json;
try {
return json5.parse(json);
} catch (err) {
return json;
}
}
};
const diffText = (left, right) => {
left = left || '';
@ -19,39 +18,47 @@ module.exports = function (jsondiffpatch, formattersHtml, curDiffData) {
if (left == right) {
return null;
}
var delta = jsondiffpatch.diff(left, right);
return formattersHtml.format(delta, left)
}
let result = formattersHtml.format(delta, left);
return result;
};
const diffJson = (left, right) => {
left = json5_parse(left);
right = json5_parse(right);
let delta = jsondiffpatch.diff(left, right);
return formattersHtml.format(delta, left)
return formattersHtml.format(delta, left);
// return '';
}
};
const valueMaps = {
'1': '必需',
'0': '非必需',
'text': '文本',
'file': '文件',
'undone': '未完成',
'done': '已完成'
}
text: '文本',
file: '文件',
undone: '未完成',
done: '已完成'
};
const handleParams = (item) => {
const handleParams = item => {
let newItem = Object.assign({}, item);
newItem._id = undefined;
Object.keys(newItem).forEach(key => {
switch (key) {
case 'required': newItem[key] = valueMaps[newItem[key]]; break;
case 'type': newItem[key] = valueMaps[newItem[key]]; break;
case 'required':
newItem[key] = valueMaps[newItem[key]];
break;
case 'type':
newItem[key] = valueMaps[newItem[key]];
break;
}
})
});
return newItem;
}
};
const diffArray = (arr1, arr2) => {
arr1 = arr1 || [];
@ -59,98 +66,94 @@ module.exports = function (jsondiffpatch, formattersHtml, curDiffData) {
arr1 = arr1.map(handleParams);
arr2 = arr2.map(handleParams);
return diffJson(arr1, arr2);
}
};
let diffView = [];
if (curDiffData && typeof curDiffData === 'object' && curDiffData.current) {
const { current, old, type } = curDiffData;
// wiki 信息的diff 输出
if(type === 'wiki') {
if (type === 'wiki') {
if (current != old) {
diffView.push({
title: 'wiki更新',
content: diffText(old, current)
})
});
}
return diffView = diffView.filter(item => item.content)
return (diffView = diffView.filter(item => item.content));
}
if (current.path != old.path) {
diffView.push({
title: 'Api 路径',
content: diffText(old.path, current.path)
})
});
}
if (current.title != old.title) {
diffView.push({
title: 'Api 名称',
content: diffText(old.title, current.title)
})
});
}
if (current.method != old.method) {
diffView.push({
title: 'Method',
content: diffText(old.method, current.method)
})
});
}
if (current.catid != old.catid) {
diffView.push({
title: '分类 id',
content: diffText(old.catid, current.catid)
})
});
}
if (current.status != old.status) {
diffView.push({
title: '接口状态',
content: diffText(valueMaps[old.status], valueMaps[current.status])
})
});
}
diffView.push({
title: 'Request Path Params',
content: diffArray(old.req_params, current.req_params)
})
});
diffView.push({
title: 'Request Query',
content: diffArray(old.req_query, current.req_query)
})
});
diffView.push({
title: 'Request Header',
content: diffArray(old.req_headers, current.req_headers)
})
});
let oldValue = current.req_body_type === 'form' ? old.req_body_form : old.req_body_other;
if (current.req_body_type !== old.req_body_type) {
diffView.push({
title: 'Request Type',
content: diffText(old.req_body_type, current.req_body_type)
})
});
oldValue = null;
}
if (current.req_body_type === 'json') {
diffView.push({
title: 'Request Body',
content: diffJson(oldValue, current.req_body_other)
})
});
} else if (current.req_body_type === 'form') {
diffView.push({
title: 'Request Form Body',
content: diffArray(oldValue, current.req_body_form)
})
});
} else {
diffView.push({
title: 'Request Raw Body',
content: diffText(oldValue, current.req_body_other)
})
});
}
let oldResValue = old.res_body;
@ -158,7 +161,7 @@ module.exports = function (jsondiffpatch, formattersHtml, curDiffData) {
diffView.push({
title: 'Response Type',
content: diffText(old.res_body_type, current.res_body_type)
})
});
oldResValue = '';
}
@ -166,17 +169,14 @@ module.exports = function (jsondiffpatch, formattersHtml, curDiffData) {
diffView.push({
title: 'Response Body',
content: diffJson(oldResValue, current.res_body)
})
});
} else {
diffView.push({
title: 'Response Body',
content: diffText(oldResValue, current.res_body)
})
});
}
}
return diffView = diffView.filter(item => item.content)
}
return (diffView = diffView.filter(item => item.content));
};

76
package-lock.json generated
View File

@ -287,15 +287,13 @@
"integrity": "sha1-gTWEAhliqenm/QOflA0S9WynhZ4=",
"dev": true
},
"ansi-regex": {
"version": "0.2.1",
"resolved": "http://registry.npm.taobao.org/ansi-regex/download/ansi-regex-0.2.1.tgz",
"integrity": "sha1-DY6UaWej2BQ/k+JOKYUl/BsiNfk="
},
"ansi-styles": {
"version": "1.1.0",
"resolved": "http://registry.npm.taobao.org/ansi-styles/download/ansi-styles-1.1.0.tgz",
"integrity": "sha1-6uy/Zs1waIJ2Cy9GkVgrj1XXp94="
"version": "3.2.1",
"resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-3.2.1.tgz",
"integrity": "sha512-VT0ZI6kZRdTh8YyJw3SMbYm/u+NqfsAxEpWO0Pf9sq8/e94WxxOpPKx9FR1FlyCtOVDNOQ+8ntlqFxiRc+r5qA==",
"requires": {
"color-convert": "1.9.1"
}
},
"antd": {
"version": "3.2.2",
@ -2977,15 +2975,13 @@
"dev": true
},
"chalk": {
"version": "0.5.1",
"resolved": "http://registry.npm.taobao.org/chalk/download/chalk-0.5.1.tgz",
"integrity": "sha1-Zjs6ZItotV0EaQ1JFnqoN4WPIXQ=",
"version": "2.4.1",
"resolved": "https://registry.npmjs.org/chalk/-/chalk-2.4.1.tgz",
"integrity": "sha512-ObN6h1v2fTJSmUXoS3nMQ92LbDK9be4TV+6G+omQlGJFdcUX5heKi1LZ1YnRMIgwTLEj3E24bT6tYni50rlCfQ==",
"requires": {
"ansi-styles": "1.1.0",
"ansi-styles": "3.2.1",
"escape-string-regexp": "1.0.5",
"has-ansi": "0.1.0",
"strip-ansi": "0.3.0",
"supports-color": "0.2.0"
"supports-color": "5.4.0"
}
},
"chardet": {
@ -3447,7 +3443,6 @@
"version": "1.9.1",
"resolved": "http://registry.npm.taobao.org/color-convert/download/color-convert-1.9.1.tgz",
"integrity": "sha1-wSYRB66y8pTr/+ye2eytUppgl+0=",
"dev": true,
"requires": {
"color-name": "1.1.3"
}
@ -3455,8 +3450,7 @@
"color-name": {
"version": "1.1.3",
"resolved": "http://registry.npm.taobao.org/color-name/download/color-name-1.1.3.tgz",
"integrity": "sha1-p9BVi9icQveV3UIyj3QIMcpTvCU=",
"dev": true
"integrity": "sha1-p9BVi9icQveV3UIyj3QIMcpTvCU="
},
"color-string": {
"version": "0.3.0",
@ -4647,6 +4641,11 @@
"integrity": "sha1-sdhVB9rzlkgo3lSzfQ1zumfdpWw=",
"dev": true
},
"diff-match-patch": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/diff-match-patch/-/diff-match-patch-1.0.1.tgz",
"integrity": "sha512-A0QEhr4PxGUMEtKxd6X+JLnOTFd3BfIPSDpsc4dMvj+CbSaErDwTpoTo/nFJDMSrjxLW4BiNq+FbNisAAHhWeQ=="
},
"diffie-hellman": {
"version": "5.0.2",
"resolved": "http://registry.npm.taobao.org/diffie-hellman/download/diffie-hellman-5.0.2.tgz",
@ -8017,14 +8016,6 @@
"function-bind": "1.1.1"
}
},
"has-ansi": {
"version": "0.1.0",
"resolved": "http://registry.npm.taobao.org/has-ansi/download/has-ansi-0.1.0.tgz",
"integrity": "sha1-hPJlqujA5qiKEtcCKJS3VoiUxi4=",
"requires": {
"ansi-regex": "0.2.1"
}
},
"has-color": {
"version": "0.1.7",
"resolved": "http://registry.npm.taobao.org/has-color/download/has-color-0.1.7.tgz",
@ -10444,11 +10435,12 @@
"integrity": "sha1-Hq3nrMASA0rYTiOWdn6tn6VJWCE="
},
"jsondiffpatch": {
"version": "0.2.5",
"resolved": "http://registry.npm.taobao.org/jsondiffpatch/download/jsondiffpatch-0.2.5.tgz",
"integrity": "sha1-UDYdmVz4yGE36NVYnyD6UiDbNRE=",
"version": "0.3.11",
"resolved": "https://registry.npmjs.org/jsondiffpatch/-/jsondiffpatch-0.3.11.tgz",
"integrity": "sha512-Xi3Iygdt/BGhml6bdUFhgDki1TgOsp3hG3iiH3KtzP+CahtGcdPfKRLlnZbSw+3b1umZkhmKrqXUgUcKenyhtA==",
"requires": {
"chalk": "0.5.1"
"chalk": "2.4.1",
"diff-match-patch": "1.0.1"
}
},
"jsonfile": {
@ -20121,14 +20113,6 @@
"resolved": "http://registry.npm.taobao.org/stringstream/download/stringstream-0.0.5.tgz",
"integrity": "sha1-TkhM1N5aC7vuGORjB3EKioFiGHg="
},
"strip-ansi": {
"version": "0.3.0",
"resolved": "http://registry.npm.taobao.org/strip-ansi/download/strip-ansi-0.3.0.tgz",
"integrity": "sha1-JfSOoiynkYfzF0pNuHWTR7sSYiA=",
"requires": {
"ansi-regex": "0.2.1"
}
},
"strip-bom": {
"version": "3.0.0",
"resolved": "http://registry.npm.taobao.org/strip-bom/download/strip-bom-3.0.0.tgz",
@ -20195,9 +20179,19 @@
"dev": true
},
"supports-color": {
"version": "0.2.0",
"resolved": "http://registry.npm.taobao.org/supports-color/download/supports-color-0.2.0.tgz",
"integrity": "sha1-2S3iaU6z9nMjlz1649i1W0wiGQo="
"version": "5.4.0",
"resolved": "https://registry.npmjs.org/supports-color/-/supports-color-5.4.0.tgz",
"integrity": "sha512-zjaXglF5nnWpsq470jSv6P9DwPvgLkuapYmfDm3JWOm0vkNTVF2tI4UrN2r6jH1qM/uc/WtxYY1hYoA2dOKj5w==",
"requires": {
"has-flag": "3.0.0"
},
"dependencies": {
"has-flag": {
"version": "3.0.0",
"resolved": "https://registry.npmjs.org/has-flag/-/has-flag-3.0.0.tgz",
"integrity": "sha1-tdRU3CGZriJWmfNGfloH87lVuv0="
}
}
},
"svgo": {
"version": "0.7.2",

View File

@ -43,7 +43,7 @@
"json-schema-faker": "^0.5.0-rc13",
"json-schema-ref-parser": "4.0.0",
"json5": "0.5.1",
"jsondiffpatch": "0.2.5",
"jsondiffpatch": "^0.3.11",
"jsonwebtoken": "7.4.1",
"koa": "2.0.0",
"koa-body": "^2.5.0",