mock平台

MockDoc.js 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. import './MockDoc.scss';
  2. import React, { PureComponent as Component } from 'react';
  3. import PropTypes from 'prop-types';
  4. // 组件用法 <MockDoc mock= mockData doc= docData />
  5. // mockData: mock数据 格式为json
  6. // docData:docData数据 格式为array
  7. class MockDoc extends Component {
  8. constructor(props) {
  9. super(props);
  10. this.state = {
  11. release: []
  12. };
  13. }
  14. static propTypes = {
  15. mock: PropTypes.object,
  16. doc: PropTypes.array
  17. };
  18. // btnCol(start,col){
  19. // return function(){
  20. // console.log(start,col);
  21. // }
  22. // }
  23. render() {
  24. let htmlData = mockToArr(this.props.mock);
  25. htmlData = arrToHtml(htmlData, this.props.doc);
  26. return (
  27. <div className="MockDoc">
  28. {htmlData.map(function(item, i) {
  29. {
  30. /*//类型:Object 必有字段 备注:qwqwqw*/
  31. }
  32. if (item.mes) {
  33. var mes = [];
  34. item.mes.type
  35. ? mes.push(
  36. <span key={i} className="keymes">
  37. {' '}
  38. / /类型:{item.mes.type}
  39. </span>
  40. )
  41. : '';
  42. item.mes.required
  43. ? mes.push(
  44. <span key={i + 1} className="keymes">
  45. 必有字段
  46. </span>
  47. )
  48. : '';
  49. item.mes.desc
  50. ? mes.push(
  51. <span key={i + 2} className="keymes">
  52. 备注:{item.mes.desc}
  53. </span>
  54. )
  55. : '';
  56. }
  57. return (
  58. <div className="jsonItem" key={i}>
  59. {<span className="jsonitemNum">{i + 1}.</span>}
  60. {produceSpace(item.space)}
  61. {setStrToHtml(item.str)}
  62. {mes}
  63. </div>
  64. );
  65. })}
  66. </div>
  67. );
  68. }
  69. }
  70. MockDoc.defaultProps = {
  71. mock: {
  72. ersrcode: '@integer',
  73. 'data|9-19': [
  74. '123',
  75. {
  76. name: '@name',
  77. name1: [
  78. {
  79. name3: '1'
  80. }
  81. ]
  82. }
  83. ],
  84. data1: '123',
  85. data3: {
  86. err: 'errCode',
  87. arr: [1, 2]
  88. }
  89. },
  90. doc: [
  91. { type: 'strisng', key: 'ersrcode', required: true, desc: '错误编码' },
  92. { type: 'number', key: 'data[]', required: true, desc: '返回数据' },
  93. { type: 'object', key: 'data[].name', required: true, desc: '数据名' },
  94. { type: 'object', key: 'data[].name1[].name3', required: true, desc: '数据名1' },
  95. { type: 'object', key: 'data1', required: true, desc: '数据名1' },
  96. { type: 'object', key: 'data3.err', required: true, desc: '数据名1' },
  97. { type: 'object', key: 'data3', required: true, desc: '数据名1' },
  98. { type: 'object', key: 'data3.arr[]', required: true, desc: '数据名1' }
  99. ]
  100. };
  101. function produceSpace(count) {
  102. var space = [];
  103. for (var i = 0; i < count; i++) {
  104. space.push(<span key={i} className="spaces" />);
  105. }
  106. return space;
  107. }
  108. function setStrToHtml(str) {
  109. return <span dangerouslySetInnerHTML={{ __html: `${str}` }} />;
  110. }
  111. function arrToHtml(mockArr, mock) {
  112. for (var i in mockArr) {
  113. for (var item in mock) {
  114. // if(mockArr[i].key){
  115. // console.log(mockArr[i].key,mock[item].key)
  116. // }
  117. if (mockArr[i].key && mockArr[i].key === mock[item].key) {
  118. mockArr[i].mes = mock[item];
  119. }
  120. }
  121. }
  122. return mockArr;
  123. }
  124. function mockToArr(mock, html, space, key) {
  125. html = html || [];
  126. space = space || 0;
  127. key = key || [];
  128. if (typeof mock === 'object' && space === 0) {
  129. if (mock.constructor === Array) {
  130. html.push({
  131. space: space,
  132. str: '['
  133. });
  134. space++;
  135. } else {
  136. html.push({
  137. space: space,
  138. str: '{'
  139. });
  140. space++;
  141. }
  142. }
  143. for (var i in mock) {
  144. if (!mock.hasOwnProperty(i)) {
  145. continue;
  146. }
  147. var index = i;
  148. if (/^\w+(\|\w+)?/.test(i)) {
  149. index = i.split('|')[0];
  150. }
  151. if (typeof mock[i] === 'object') {
  152. if (mock[i].constructor === Array) {
  153. // shuzu
  154. if (mock.constructor != Array) {
  155. if (key.length) {
  156. key.push('.' + index + '[]');
  157. } else {
  158. key.push(index + '[]');
  159. }
  160. } else {
  161. key.push('[]');
  162. }
  163. html.push({
  164. space: space,
  165. str: index + ' : [',
  166. key: key.join('')
  167. });
  168. } else {
  169. // object
  170. if (mock.constructor != Array) {
  171. if (key.length) {
  172. key.push('.' + index);
  173. } else {
  174. key.push(index);
  175. }
  176. html.push({
  177. space: space,
  178. str: index + ' : {'
  179. });
  180. } else {
  181. html.push({
  182. space: space,
  183. str: '{'
  184. });
  185. }
  186. }
  187. space++;
  188. mockToArr(mock[i], html, space, key);
  189. key.pop();
  190. space--;
  191. } else {
  192. if (mock.constructor === Array) {
  193. // html.push(produceSpace(space) + mock[i]+ ",");
  194. html.push({
  195. space: space,
  196. str: `<span class = "valueLight">${mock[i]}</span>` + ','
  197. });
  198. } else {
  199. // html.push(produceSpace(space) + index + ":" + mock[i] + ",");
  200. if (mock.constructor != Array) {
  201. if (key.length) {
  202. // doc.push(key+"."+index);
  203. html.push({
  204. space: space,
  205. str: index + ' : ' + `<span class = "valueLight">${mock[i]}</span>` + ',',
  206. key: key.join('') + '.' + index
  207. });
  208. } else {
  209. // doc.push(key + index);
  210. html.push({
  211. space: space,
  212. str: index + ' : ' + `<span class = "valueLight">${mock[i]}</span>` + ',',
  213. key: key.join('') + index
  214. });
  215. }
  216. } else {
  217. html.push({
  218. space: space,
  219. str: index + ' : ' + `<span class = "valueLight">${mock[i]}</span>` + ',',
  220. key: key.join('')
  221. });
  222. }
  223. }
  224. }
  225. }
  226. if (typeof mock === 'object') {
  227. html[html.length - 1].str = html[html.length - 1].str.substr(
  228. 0,
  229. html[html.length - 1].str.length - 1
  230. );
  231. if (mock.constructor === Array) {
  232. space--;
  233. // html.push(produceSpace(space)+"]");
  234. html.push({
  235. space: space,
  236. str: ']'
  237. });
  238. } else {
  239. space--;
  240. // html.push(produceSpace(space)+"}");
  241. html.push({
  242. space: space,
  243. str: '}'
  244. });
  245. }
  246. }
  247. if (space != 0) {
  248. html[html.length - 1].str = html[html.length - 1].str + ',';
  249. }
  250. return html;
  251. }
  252. export default MockDoc;