把rap2接口json生成接口方式代码service层
直接上代码
数据部分:
let data = { "data": { "id": 269363, "name": "系统名称", "description": "", "logo": null, "token": "mcCjhNjBmH-jLMblaXFuruwL749OPtMd", "visibility": true, "ownerId": 176237, "organizationId": 1236258, "creatorId": 176237, "lockerId": null, "createdAt": "2020-10-28T03:38:28.000Z", "updatedAt": "2020-10-28T03:38:45.000Z", "deletedAt": null, "creator": { "id": 176237, "fullname": "作者", "email": "495982911@qq.com" }, "owner": { "id": 176237, "fullname": "作者", "email": "xxxxx@qq.com" }, "locker": null, "members": [], "organization": { "id": 1236258, "name": "xxxxx", "description": "", "logo": null, "visibility": false, "creatorId": 129547, "ownerId": 129547, "createdAt": "2020-10-19T01:28:27.000Z", "updatedAt": "2020-10-26T03:12:33.000Z", "deletedAt": null }, "collaborators": [], "modules": [ { "id": 420042, "name": "示例模块", "description": "示例模块", "priority": 1, "creatorId": 176237, "repositoryId": 269363, "createdAt": "2020-10-28T03:38:28.000Z", "updatedAt": "2020-10-28T03:38:28.000Z", "deletedAt": null, "interfaces": [ { "id": 1775373, "name": "示例接口", "url": "/example/1603856308811", "method": "GET", "bodyOption": null, "description": "示例接口描述", "priority": 1, "status": 200, "creatorId": 176237, "lockerId": null, "moduleId": 420042, "repositoryId": 269363, "createdAt": "2020-10-28T03:38:28.000Z", "updatedAt": "2020-10-28T03:38:28.000Z", "deletedAt": null, "locker": null, "properties": [ { "id": 20748472, "scope": "request", "type": "String", "pos": 2, "name": "foo", "rule": "", "value": "@ctitle", "description": "请求属性示例", "parentId": -1, "priority": 1, "interfaceId": 1775373, "creatorId": 176237, "moduleId": 420042, "repositoryId": 269363, "required": false, "createdAt": "2020-10-28T03:38:28.000Z", "updatedAt": "2020-10-28T03:38:28.000Z", "deletedAt": null }, { "id": 20748473, "scope": "response", "type": "String", "pos": 2, "name": "string", "rule": "1-10", "value": "★", "description": "字符串属性示例", "parentId": -1, "priority": 1, "interfaceId": 1775373, "creatorId": 176237, "moduleId": 420042, "repositoryId": 269363, "required": false, "createdAt": "2020-10-28T03:38:28.000Z", "updatedAt": "2020-10-28T03:38:28.000Z", "deletedAt": null }, { "id": 20748474, "scope": "response", "type": "Number", "pos": 2, "name": "number", "rule": "1-100", "value": "1", "description": "数字属性示例", "parentId": -1, "priority": 1, "interfaceId": 1775373, "creatorId": 176237, "moduleId": 420042, "repositoryId": 269363, "required": false, "createdAt": "2020-10-28T03:38:28.000Z", "updatedAt": "2020-10-28T03:38:28.000Z", "deletedAt": null }, { "id": 20748475, "scope": "response", "type": "Boolean", "pos": 2, "name": "boolean", "rule": "1-2", "value": "true", "description": "布尔属性示例", "parentId": -1, "priority": 1, "interfaceId": 1775373, "creatorId": 176237, "moduleId": 420042, "repositoryId": 269363, "required": false, "createdAt": "2020-10-28T03:38:28.000Z", "updatedAt": "2020-10-28T03:38:28.000Z", "deletedAt": null }, { "id": 20748476, "scope": "response", "type": "RegExp", "pos": 2, "name": "regexp", "rule": "", "value": "/[a-z][A-Z][0-9]/", "description": "正则属性示例", "parentId": -1, "priority": 1, "interfaceId": 1775373, "creatorId": 176237, "moduleId": 420042, "repositoryId": 269363, "required": false, "createdAt": "2020-10-28T03:38:28.000Z", "updatedAt": "2020-10-28T03:38:28.000Z", "deletedAt": null }, { "id": 20748477, "scope": "response", "type": "Function", "pos": 2, "name": "function", "rule": "", "value": "() => Math.random()", "description": "函数属性示例", "parentId": -1, "priority": 1, "interfaceId": 1775373, "creatorId": 176237, "moduleId": 420042, "repositoryId": 269363, "required": false, "createdAt": "2020-10-28T03:38:28.000Z", "updatedAt": "2020-10-28T03:38:28.000Z", "deletedAt": null }, { "id": 20748478, "scope": "response", "type": "Array", "pos": 2, "name": "array", "rule": "1-10", "value": "", "description": "数组属性示例", "parentId": -1, "priority": 1, "interfaceId": 1775373, "creatorId": 176237, "moduleId": 420042, "repositoryId": 269363, "required": false, "createdAt": "2020-10-28T03:38:28.000Z", "updatedAt": "2020-10-28T03:38:28.000Z", "deletedAt": null }, { "id": 20748479, "scope": "response", "type": "Number", "pos": 2, "name": "foo", "rule": "+1", "value": "1", "description": "数组元素示例", "parentId": 20748478, "priority": 1, "interfaceId": 1775373, "creatorId": 176237, "moduleId": 420042, "repositoryId": 269363, "required": false, "createdAt": "2020-10-28T03:38:28.000Z", "updatedAt": "2020-10-28T03:38:28.000Z", "deletedAt": null }, { "id": 20748480, "scope": "response", "type": "String", "pos": 2, "name": "bar", "rule": "1-10", "value": "★", "description": "数组元素示例", "parentId": 20748478, "priority": 1, "interfaceId": 1775373, "creatorId": 176237, "moduleId": 420042, "repositoryId": 269363, "required": false, "createdAt": "2020-10-28T03:38:28.000Z", "updatedAt": "2020-10-28T03:38:28.000Z", "deletedAt": null }, { "id": 20748481, "scope": "response", "type": "Array", "pos": 2, "name": "items", "rule": "", "value": "[1, true, 'hello', /\\w{10}/]", "description": "自定义数组元素示例", "parentId": -1, "priority": 1, "interfaceId": 1775373, "creatorId": 176237, "moduleId": 420042, "repositoryId": 269363, "required": false, "createdAt": "2020-10-28T03:38:28.000Z", "updatedAt": "2020-10-28T03:38:28.000Z", "deletedAt": null }, { "id": 20748482, "scope": "response", "type": "Object", "pos": 2, "name": "object", "rule": "", "value": "", "description": "对象属性示例", "parentId": -1, "priority": 1, "interfaceId": 1775373, "creatorId": 176237, "moduleId": 420042, "repositoryId": 269363, "required": false, "createdAt": "2020-10-28T03:38:28.000Z", "updatedAt": "2020-10-28T03:38:28.000Z", "deletedAt": null }, { "id": 20748483, "scope": "response", "type": "Number", "pos": 2, "name": "foo", "rule": "+1", "value": "1", "description": "对象属性示例", "parentId": 20748482, "priority": 1, "interfaceId": 1775373, "creatorId": 176237, "moduleId": 420042, "repositoryId": 269363, "required": false, "createdAt": "2020-10-28T03:38:28.000Z", "updatedAt": "2020-10-28T03:38:28.000Z", "deletedAt": null }, { "id": 20748484, "scope": "response", "type": "String", "pos": 2, "name": "bar", "rule": "1-10", "value": "★", "description": "对象属性示例", "parentId": 20748482, "priority": 1, "interfaceId": 1775373, "creatorId": 176237, "moduleId": 420042, "repositoryId": 269363, "required": false, "createdAt": "2020-10-28T03:38:28.000Z", "updatedAt": "2020-10-28T03:38:28.000Z", "deletedAt": null }, { "id": 20748485, "scope": "response", "type": "String", "pos": 2, "name": "placeholder", "rule": "", "value": "@title", "description": "占位符示例", "parentId": -1, "priority": 1, "interfaceId": 1775373, "creatorId": 176237, "moduleId": 420042, "repositoryId": 269363, "required": false, "createdAt": "2020-10-28T03:38:28.000Z", "updatedAt": "2020-10-28T03:38:28.000Z", "deletedAt": null } ] } ] } ], "canUserEdit": true } }
实现代码部分:
let interfacesList = [] let modules = data.data.modules for (let i = 0; i < modules.length; i++) { interfacesList = interfacesList.concat(modules[i].interfaces) } // console.log(interfacesList) let codeList = [] for (let i = 0; i < interfacesList.length; i++) { const item = interfacesList[i] if (item.name == '示例接口') { continue } const description = item.name const method = item.method == 'GET' ? 'get' : 'post' const url = item.url const methodName = url.substr(url.lastIndexOf('/')+1) const properties = item.properties const propertiesArray = [] properties.forEach(prop => { propertiesArray.push(`*@${prop.name} {${prop.type}} ${prop.value}`) }) let code=`/**\r\n` code+=`*${description}\r\n` code+=`${propertiesArray.join('\r\n')}\r\n` code+=`*/\r\n` code+=`static ${methodName}(params) {\r\n\t` code+=`return axiosMethods.${method}('${url}', params)\r\n` code+=`}` // const code =` // /**\r\n // *${description}\r\n // ${propertiesArray.join('\r\n')}\r\n // */\r\n // static ${methodName}(params) {\r\n // return axiosMethods.${method}('${url}', params)\r\n // }\r\n` codeList.push(code) } console.log(codeList.join('\r\n\r\n'))