create-apis.js 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. const fs = require('fs')
  2. const os = require('os')
  3. const path = require('path')
  4. const axios = require('axios')
  5. const host = 'http://120.25.146.52:3090/api/open/plugin/export-full?type=json&pid=18&status=all&token=9a5d2297461e28edfb8324006c52e8561bcef2df4481df0240348a66f6b97e6e'
  6. const code = []
  7. axios.get(host).then(response => {
  8. // console.log(response)
  9. response.data.forEach(item => {
  10. if (item.desc == 'scene-edit' || item.desc == 'scene-view') {
  11. code.push(`import { http } from '@sdk/utils/request'`)
  12. item.list.forEach(item => {
  13. if (item.path.indexOf('/service/scene/') != -1) {
  14. if (item.path.indexOf('/service/scene/edit/') != -1) {
  15. generate(item, 'edit')
  16. } else {
  17. generate(item, 'scene')
  18. }
  19. }
  20. })
  21. if (code.length) {
  22. fs.writeFileSync(path.resolve(__dirname, `../src/apis/${item.desc}.js`), code.join(os.EOL))
  23. }
  24. }
  25. code.length = 0
  26. })
  27. })
  28. function generate(scheme, split) {
  29. scheme.method = scheme.method.toLowerCase()
  30. if (scheme.method == 'get' || scheme.method == 'post') {
  31. code.push(`/**`)
  32. code.push(`* ${scheme.title}`)
  33. code.push(`* @param {object} data 传入的对象参数`)
  34. if (scheme.req_body_other) {
  35. let req_body = JSON.parse(scheme.req_body_other)
  36. if (req_body.properties) {
  37. for (let key in req_body.properties) {
  38. code.push(`* @param {${req_body.properties[key].type}} data.${key} ${req_body.properties[key].description || ''}`)
  39. if (req_body.properties[key].properties) {
  40. let properties = req_body.properties[key].properties
  41. for (let subKey in properties) {
  42. code.push(`* @param {${properties[subKey].type}} data.${key}.${subKey} ${properties[subKey].description}`)
  43. }
  44. }
  45. }
  46. }
  47. } else if (scheme.req_body_form) {
  48. scheme.req_body_form.forEach(item => {
  49. code.push(`* @param {${item.type}} data.${item.name} ${item.desc || ''}`)
  50. })
  51. }
  52. code.push(`* @returns {Promise}`)
  53. code.push(`**/`)
  54. code.push(`export const ${scheme.path.split(split)[1].substring(1).replace(/\//g, '_')} = data => {`)
  55. //&& scheme.req_body_form && scheme.req_body_form.length && scheme.req_body_form.find(c => c.type == 'file')
  56. if (scheme.req_body_type == 'form') {
  57. code.push(` return http.postFile('${scheme.path}', data)`)
  58. } else {
  59. code.push(` return http.${scheme.method}('${scheme.path}', data)`)
  60. }
  61. code.push(`}`)
  62. }
  63. }