create-apis.js 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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=0badd469d66bec93d28d3bb9ac053ddd9eacb3ebe53a446576fb0fa0ce2ca63c'
  6. const code = []
  7. axios.get(host).then(response => {
  8. response.data.forEach(item => {
  9. if (item.desc == 'scene-edit') {
  10. code.push(`import { http } from '@/utils/request'`)
  11. item.list.forEach(item => {
  12. if (item.path.indexOf('/api/scene/') != -1) {
  13. generate(item, 'edit')
  14. }
  15. })
  16. if (code.length) {
  17. fs.writeFileSync(path.resolve(__dirname, `../src/apis/${item.desc}.js`), code.join(os.EOL))
  18. }
  19. }
  20. code.length = 0
  21. })
  22. })
  23. function generate(scheme, split) {
  24. scheme.method = scheme.method.toLowerCase()
  25. if (scheme.method == 'get' || scheme.method == 'post') {
  26. code.push(`/**`)
  27. code.push(`* ${scheme.title}`)
  28. code.push(`* @param {object} data 传入的对象参数`)
  29. if (scheme.req_body_other) {
  30. let req_body = JSON.parse(scheme.req_body_other)
  31. if (req_body.properties) {
  32. for (let key in req_body.properties) {
  33. code.push(`* @param {${req_body.properties[key].type}} data.${key} ${req_body.properties[key].description || ''}`)
  34. if (req_body.properties[key].properties) {
  35. let properties = req_body.properties[key].properties
  36. for (let subKey in properties) {
  37. code.push(`* @param {${properties[subKey].type}} data.${key}.${subKey} ${properties[subKey].description}`)
  38. }
  39. }
  40. }
  41. }
  42. } else if (scheme.req_body_form) {
  43. scheme.req_body_form.forEach(item => {
  44. code.push(`* @param {${item.type}} data.${item.name} ${item.desc || ''}`)
  45. })
  46. }
  47. code.push(`* @returns {Promise}`)
  48. code.push(`**/`)
  49. code.push(`export const ${scheme.path.split(split)[1].substring(1).replace(/\//g, '_')} = data => {`)
  50. if (scheme.req_body_type == 'form') {
  51. code.push(` return http.postFile('${scheme.path}', data)`)
  52. } else {
  53. code.push(` return http.${scheme.method}('${scheme.path}', data)`)
  54. }
  55. code.push(`}`)
  56. }
  57. }