| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261 |
- import './MockDoc.scss';
- import React, { PureComponent as Component } from 'react';
- import PropTypes from 'prop-types';
- // 组件用法 <MockDoc mock= mockData doc= docData />
- // mockData: mock数据 格式为json
- // docData:docData数据 格式为array
- class MockDoc extends Component {
- constructor(props) {
- super(props);
- this.state = {
- release: []
- };
- }
- static propTypes = {
- mock: PropTypes.object,
- doc: PropTypes.array
- };
- // btnCol(start,col){
- // return function(){
- // console.log(start,col);
- // }
- // }
- render() {
- let htmlData = mockToArr(this.props.mock);
- htmlData = arrToHtml(htmlData, this.props.doc);
- return (
- <div className="MockDoc">
- {htmlData.map(function(item, i) {
- {
- /*//类型:Object 必有字段 备注:qwqwqw*/
- }
- if (item.mes) {
- var mes = [];
- item.mes.type
- ? mes.push(
- <span key={i} className="keymes">
- {' '}
- / /类型:{item.mes.type}
- </span>
- )
- : '';
- item.mes.required
- ? mes.push(
- <span key={i + 1} className="keymes">
- 必有字段
- </span>
- )
- : '';
- item.mes.desc
- ? mes.push(
- <span key={i + 2} className="keymes">
- 备注:{item.mes.desc}
- </span>
- )
- : '';
- }
- return (
- <div className="jsonItem" key={i}>
- {<span className="jsonitemNum">{i + 1}.</span>}
- {produceSpace(item.space)}
- {setStrToHtml(item.str)}
- {mes}
- </div>
- );
- })}
- </div>
- );
- }
- }
- MockDoc.defaultProps = {
- mock: {
- ersrcode: '@integer',
- 'data|9-19': [
- '123',
- {
- name: '@name',
- name1: [
- {
- name3: '1'
- }
- ]
- }
- ],
- data1: '123',
- data3: {
- err: 'errCode',
- arr: [1, 2]
- }
- },
- doc: [
- { type: 'strisng', key: 'ersrcode', required: true, desc: '错误编码' },
- { type: 'number', key: 'data[]', required: true, desc: '返回数据' },
- { type: 'object', key: 'data[].name', required: true, desc: '数据名' },
- { type: 'object', key: 'data[].name1[].name3', required: true, desc: '数据名1' },
- { type: 'object', key: 'data1', required: true, desc: '数据名1' },
- { type: 'object', key: 'data3.err', required: true, desc: '数据名1' },
- { type: 'object', key: 'data3', required: true, desc: '数据名1' },
- { type: 'object', key: 'data3.arr[]', required: true, desc: '数据名1' }
- ]
- };
- function produceSpace(count) {
- var space = [];
- for (var i = 0; i < count; i++) {
- space.push(<span key={i} className="spaces" />);
- }
- return space;
- }
- function setStrToHtml(str) {
- return <span dangerouslySetInnerHTML={{ __html: `${str}` }} />;
- }
- function arrToHtml(mockArr, mock) {
- for (var i in mockArr) {
- for (var item in mock) {
- // if(mockArr[i].key){
- // console.log(mockArr[i].key,mock[item].key)
- // }
- if (mockArr[i].key && mockArr[i].key === mock[item].key) {
- mockArr[i].mes = mock[item];
- }
- }
- }
- return mockArr;
- }
- function mockToArr(mock, html, space, key) {
- html = html || [];
- space = space || 0;
- key = key || [];
- if (typeof mock === 'object' && space === 0) {
- if (mock.constructor === Array) {
- html.push({
- space: space,
- str: '['
- });
- space++;
- } else {
- html.push({
- space: space,
- str: '{'
- });
- space++;
- }
- }
- for (var i in mock) {
- if (!mock.hasOwnProperty(i)) {
- continue;
- }
- var index = i;
- if (/^\w+(\|\w+)?/.test(i)) {
- index = i.split('|')[0];
- }
- if (typeof mock[i] === 'object') {
- if (mock[i].constructor === Array) {
- // shuzu
- if (mock.constructor != Array) {
- if (key.length) {
- key.push('.' + index + '[]');
- } else {
- key.push(index + '[]');
- }
- } else {
- key.push('[]');
- }
- html.push({
- space: space,
- str: index + ' : [',
- key: key.join('')
- });
- } else {
- // object
- if (mock.constructor != Array) {
- if (key.length) {
- key.push('.' + index);
- } else {
- key.push(index);
- }
- html.push({
- space: space,
- str: index + ' : {'
- });
- } else {
- html.push({
- space: space,
- str: '{'
- });
- }
- }
- space++;
- mockToArr(mock[i], html, space, key);
- key.pop();
- space--;
- } else {
- if (mock.constructor === Array) {
- // html.push(produceSpace(space) + mock[i]+ ",");
- html.push({
- space: space,
- str: `<span class = "valueLight">${mock[i]}</span>` + ','
- });
- } else {
- // html.push(produceSpace(space) + index + ":" + mock[i] + ",");
- if (mock.constructor != Array) {
- if (key.length) {
- // doc.push(key+"."+index);
- html.push({
- space: space,
- str: index + ' : ' + `<span class = "valueLight">${mock[i]}</span>` + ',',
- key: key.join('') + '.' + index
- });
- } else {
- // doc.push(key + index);
- html.push({
- space: space,
- str: index + ' : ' + `<span class = "valueLight">${mock[i]}</span>` + ',',
- key: key.join('') + index
- });
- }
- } else {
- html.push({
- space: space,
- str: index + ' : ' + `<span class = "valueLight">${mock[i]}</span>` + ',',
- key: key.join('')
- });
- }
- }
- }
- }
- if (typeof mock === 'object') {
- html[html.length - 1].str = html[html.length - 1].str.substr(
- 0,
- html[html.length - 1].str.length - 1
- );
- if (mock.constructor === Array) {
- space--;
- // html.push(produceSpace(space)+"]");
- html.push({
- space: space,
- str: ']'
- });
- } else {
- space--;
- // html.push(produceSpace(space)+"}");
- html.push({
- space: space,
- str: '}'
- });
- }
- }
- if (space != 0) {
- html[html.length - 1].str = html[html.length - 1].str + ',';
- }
- return html;
- }
- export default MockDoc;
|