zoukankan      html  css  js  c++  java
  • 把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
        }
      }
    View Code

    实现代码部分:

    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=`/**
    `
      code+=`*${description}
    `
      code+=`${propertiesArray.join('
    ')}
    `
      code+=`*/
    `
      code+=`static ${methodName}(params) {
    	`
      code+=`return axiosMethods.${method}('${url}', params)
    `
      code+=`}`
      // const code =`
      //   /**
    
      //   *${description}
    
      //   ${propertiesArray.join('
    ')}
    
      //   */
    
      //   static ${methodName}(params) {
    
      //     return axiosMethods.${method}('${url}', params)
    
      //   }
    `
      codeList.push(code)
    }
    console.log(codeList.join('
    
    '))
  • 相关阅读:
    iOS截取http/https流量
    Jenkins拾遗--第五篇-git插件填坑
    Jenkins拾遗--第三篇(用户权限管理)
    Jenkins拾遗--第四篇-适当的让构建失败
    Jenkins拾遗--第二篇(初步配置Jenkins)
    Jenkins拾遗--第一篇(安装Jenkins)
    一个测试人员的2015的回顾和2016年展望
    xcode升级导致git无法使用
    在intellj idea下用sbt的坑
    本人已转战知乎,此处不玩了。
  • 原文地址:https://www.cnblogs.com/yeminglong/p/13896419.html
Copyright © 2011-2022 走看看