const PrimitiveType = ['boolean', 'string', 'integer', 'number'];
// key是 name/title/description/desc/url/city/ img/avatar/image 特殊处理一下
const keywordMap = {
name: '@cname',
title: '@ctitle',
description: '@cparagraph',
desc: '@cparagraph',
url: '@url',
time: '@datetime',
start_time: '@datetime',
created_time: '@datetime',
end_time: '@datetime',
updated_time: '@datetime',
img: '@image(200*200)',
avatar: '@image(200*200)',
image: '@image(200*200)',
email: '@email',
province: '@province',
country: '@county',
region: '@region',
city: '@city',
};
function test(response) {
const mockObj = {
state: 1,
data: {},
};
const obj = processItem({}, response);
mockObj.data = obj;
if (mockObj.data.data) {
mockObj.data = mockObj.data.data;
}
return mockObj
}
function processItem(obj = {}, r) {
const { type, key } = r;
// 原始类型
if (PrimitiveType.indexOf(type) > -1) {
if (key === 'id') {
obj.id = '@guid';
return;
}
if (keywordMap[key]) {
obj.key = keywordMap[key];
return;
}
if (type === 'boolean') {
obj[`${key}|1`] = true;
} else if (type === 'string') {
obj[key] = '@string';
} else {
obj[key] = '@integer(1, 300)';
}
}
// 对象
if (type === 'object') {
const properties = (r).properties;
obj[key] = {};
// tslint:disable-next-line: forin
for (const i in properties) {
processItem(obj[key], properties[i]);
}
}
// 数组
if (type === 'array') {
const { items } = r;
if (PrimitiveType.indexOf(items.type) > -1) {
if (items.type === 'boolean') {
obj[`${key}|1-15`] = [true, false];
} else if (items.type === 'string') {
obj[`${key}|1-15`] = ['@string'];
} else {
obj[`${key}|1-15`] = ['@integer(1, 30)'];
}
} else {
if (items.type === 'object') {
// console.log('-------------------')
obj[`${key}|1-15`] = [{}]
// tslint:disable-next-line: forin
for (let i in items.properties) {
processItem(obj[`${key}|1-15`][0], items.properties[i]);
}
}
if (items.type === 'array') {
// 暂不考虑
}
}
}
return obj;
}
// boolean
let a = {"key":"data","type":"boolean","description":"正确返回的数据"}
console.log(1, JSON.stringify(test(a)))
// string
let a2 = {"key":"data","type":"string","description":"正确返回的数据"}
console.log(2, JSON.stringify(test(a2)))
// string
let a3 = {"key":"data","type":"number","description":"正确返回的数据"}
console.log(3, JSON.stringify(test(a3)))
// array
let a4 = {"key":"data","type":"object","description":"正确返回的数据","items":{"type":"object","properties":[{"key":"id","type":"integer","description":"","expanded":true},{"key":"name","type":"string","description":"","expanded":true},{"key":"type","type":"integer","description":"","expanded":true},{"key":"subtype","type":"integer","description":"","expanded":true},{"key":"width","type":"integer","description":"","expanded":true},{"key":"height","type":"integer","description":"","expanded":true},{"key":"format_id","type":"integer","description":"","expanded":true},{"key":"quality","type":"integer","description":"","expanded":true},{"key":"compression","type":"integer","description":"","expanded":true},{"key":"is_default","type":"integer","description":"","expanded":true}],"description":"","expanded":true},"properties":{"items":{"key":"items","type":"array","description":"","items":{"type":"object","properties":{"id":{"key":"id","type":"integer","description":""},"name":{"key":"name","type":"string","description":""},"type":{"key":"type","type":"integer","description":""},"subtype":{"key":"subtype","type":"integer","description":""},"width":{"key":"width","type":"integer","description":""},"height":{"key":"height","type":"number","description":""},"format_id":{"key":"format_id","type":"integer","description":""},"quality":{"key":"quality","type":"integer","description":""},"compression":{"key":"compression","type":"integer","description":""},"is_default":{"key":"is_default","type":"integer","description":""}},"description":""}},"total":{"key":"total","type":"integer","description":""}}}
let a44={key: 'name', type: 'array', items: {type: 'string'}}
console.log(4, JSON.stringify(test(a4)))
console.log(44, JSON.stringify(test(a44)))
// object
let a5={"key":"data","type":"object","description":"正确返回的数据","properties":{"id":{"key":"id","type":"integer","description":""},"name":{"key":"name","type":"string","description":""},"type":{"key":"type","type":"integer","description":""},"subtype":{"key":"subtype","type":"integer","description":""},"width":{"key":"width","type":"integer","description":""},"height":{"key":"height","type":"integer","description":""},"format_id":{"key":"format_id","type":"integer","description":""},"quality":{"key":"quality","type":"integer","description":""},"compression":{"key":"compression","type":"integer","description":""},"is_default":{"key":"is_default","type":"integer","description":""}}}
console.log(5, JSON.stringify(test(a5)))
console