javascript 邮差测试
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javascript 邮差测试相关的知识,希望对你有一定的参考价值。
var modelPrefix = "Test";
var json = pm.response.json();
if (json.status != 1) {
console.log(json.info);
return;
}
var jsonData = json.data;
var objectTree = buildObjectTree('Model', jsonData, null);
var classes = [];
walkObjectTree(objectTree, function (object) {
if (object.type === 'object' || object.type === 'array') {
var clazz = {};
clazz.name = modelPrefix + capitalizeFirstLetter(object.name);
clazz.properties = [];
for (var i in object.nodes) {
var node = object.nodes[i];
switch (node.type) {
case 'string':
clazz.properties.push({ name: node.name, type: 'NSString' });
break;
case 'integer':
clazz.properties.push({ name: node.name, type: 'NSInteger' });
break;
case 'float':
clazz.properties.push({ name: node.name, type: 'CGFloat' });
break;
case 'boolean':
clazz.properties.push({ name: node.name, type: 'BOOL' });
break;
case 'object':
clazz.properties.push({ name: node.name, type: modelPrefix + capitalizeFirstLetter(node.name) });
break;
case 'array':
clazz.properties.push({ name: node.name, type: '@' + modelPrefix + capitalizeFirstLetter(node.name) });
break;
case 'null':
clazz.properties.push({ name: node.name, type: 'NSString' });
break;
}
}
classes.push(clazz);
}
});
var hStr = '', mStr = '';
hStr += "#import <Foundation/Foundation.h>\n\n";
if (classes.length > 1) {
var classStr = classes.slice(1).map(function (currentValue, index, arr) {
return currentValue.name;
}).join(', ');
hStr += "@class " + classStr + ";\n\n";
}
for (var i in classes) {
var clazz = classes[i];
hStr += "@interface " + clazz.name + " : NSObject\n\n";
for (var j in clazz.properties) {
var property = clazz.properties[j];
var type = property.type;
var name = property.name;
if (name === 'description') {
name = 'desc';
}
if (type === 'NSString') {
hStr += "@property (strong, nonatomic) NSString *" + name + ";\n";
} else if (type === 'NSInteger') {
hStr += "@property (assign, nonatomic) NSInteger " + name + ";\n";
} else if (type === 'CGFloat') {
hStr += "@property (assign, nonatomic) CGFloat " + name + ";\n";
} else if (type === 'BOOL') {
hStr += "@property (assign, nonatomic) BOOL " + name + ";\n";
} else if (type[0] === '@') {
hStr += "@property (strong, nonatomic) NSArray<" + type.slice(1) + " *> *" + name + ";\n";
} else {
hStr += "@property (strong, nonatomic) " + type + " *" + name + ";\n";
}
}
hStr += "\n@end\n\n";
}
createFile(classes[0].name + '.h', hStr);
mStr += "#import \"" + classes[0].name + ".h\"\n\n";
for (var i in classes) {
var clazz = classes[i];
mStr += "@implementation " + clazz.name + "\n\n";
for (var j in clazz.properties) {
var property = clazz.properties[j];
var type = property.type;
var name = property.name;
if (name === 'description') {
name = 'desc';
mStr += '+ (NSDictionary *)mj_replacedKeyFromPropertyName {\n';
mStr += ' return @{@"desc" : @"description"};\n';
mStr += '}\n\n';
}
if (type[0] === '@') {
mStr += "+ (NSDictionary *)mj_objectClassInArray {\n";
mStr += " return @{ @\"" + name + "\" : [" + type.slice(1) + " class] };\n";
mStr += "}\n\n";
}
}
mStr += "@end\n\n";
}
createFile(classes[0].name + '.m', mStr);
function isInt(n) {
return n % 1 === 0;
}
function _typeof(obj) {
var type = typeof obj;
if (type === 'object') {
if (obj === null) {
return 'null';
} else if (Array.isArray(obj)) {
return 'array';
} else {
return type;
}
}
if (type === 'number') {
return isInt(obj) ? 'integer' : 'float';
}
return type;
}
function capitalizeFirstLetter(string) {
return string.charAt(0).toUpperCase() + string.slice(1);
}
function buildObjectTree(name, data, node) {
var type = _typeof(data);
var object = {
name: name,
type: type,
nodes: null,
};
switch (type) {
case 'object':
for (var key in data) {
object.nodes = object.nodes || [];
object.nodes.push(buildObjectTree(key, data[key], object));
}
break;
case 'array':
if (data.length) {
for (key in data[0]) {
object.nodes = object.nodes || [];
object.nodes.push(buildObjectTree(key, data[0][key], object));
}
}
break;
default:
break;
}
return object;
}
function walkObjectTree(tree, callback) {
if (!tree) {
return;
}
callback(tree);
for (var i in tree.nodes) {
walkObjectTree(tree.nodes[i], callback);
}
}
function createFile(filename, contents) {
const echoPostRequest = {
url: 'http://doc.test/Api/Test/createFile',
method: 'POST',
header: '',
body: {
mode: 'formdata',
formdata: [
{
disabled: false,
description: {
content: '',
type: 'text/plain'
},
key: 'filename',
value: filename,
type: 'text'
},
{
disabled: false,
description: {
content: '',
type: 'text/plain'
},
key: 'contents',
value: contents,
type: 'text'
}
]
}
};
pm.sendRequest(echoPostRequest, function (err, res) {
if (err) {
console.log(res);
}
});
}
以上是关于javascript 邮差测试的主要内容,如果未能解决你的问题,请参考以下文章