Open jiefancis opened 2 years ago
{ "fields": [ { "id": 1659404089847, "name": "data", "pageType": "Array", "range": "N/A", "example": "", "desc": "", "required": true, "children": [ { "id": 1659409035588, "name": "items", "pageType": "Object", "range": "N/A", "example": "", "desc": "", "required": true, "children": [ { "id": 1659409055766, "name": "fields_2", "pageType": "String", "range": "N/A", "example": "", "desc": "", "required": true } ], "disabled": true } ] }, { "name": "message", "pageType": "String" } ], "pageType": "Object" }
function adapterJson(response) { if (response?.fields && response.fields.length > 0) { response.fields.forEach(convertData); } return response; } function convertData(data) { let res = data; switch (data.pageType) { case 'Array': res = convertArray(data); break; case 'Object': res = convertObject(data); break; } return res; } function convertArray(data) { if (data.children?.length) { const child = data.children[0]; data.item = convertData(child); delKey(data, 'children'); } else if (data.item) { data.children = [convertData(data.item)]; delKey(data, 'item'); } return data; } function convertObject(data) { if (data.children) { const children = data.children; delKey(data, 'children'); data.fields = children.map(convertData); } else if (data.fields) { data.children = data.fields.map(convertData); delKey(data, 'fields'); } return data; } function delKey(o, key) { delete o[key]; }
function deepClone(json) { json = JSON.parse(JSON.stringify(json)); json.children = json.fields; delKey(json, 'fields'); return json; } function transformToJson(data) { return transformData(deepClone(data)); } function transformData(data) { let res = null; switch (data.pageType) { case 'Object': res = transformObject(data); break; case 'Array': res = transformArray(data); break; case 'Map': res = transformMap(data); break; default: res = transformBasic(data); break; } return res; } function transformMap(data){ // console.log('data',data) return { key: 'value' }; } function transformArray(data) { return data.children.map(item => transformData(item)); } function transformObject(data) { const res = {}; data.children.forEach(data => { res[data.name || 'fields'] = transformData(data); }); return res; } function transformBasic(data) { const map = { 'Text': () => 'Text', 'String': () => 'String', 'Number': () => 1, 'Float': () => 1.2, 'Integer': () => 1, 'Double': () => 1.02, 'DateTime': () => Date.now(), 'TimeStamp': () => Date.now(), 'Boolean': () => true, }; return map[data.pageType](); }
将下面的数据结构中 fields转换成Children,
接口请求(响应)参数数据预览的转换(将上面fields格式转换为对应的参数:值的形式