index.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. const { Random } = require('mockjs')
  2. const { join } = require('path')
  3. const fs = require('fs')
  4. /**
  5. * @description 随机生成图片url。
  6. * @param width
  7. * @param height
  8. * @returns {string}
  9. */
  10. function handleRandomImage(width = 50, height = 50) {
  11. return `https://picsum.photos/${width}/${height}?random=${Random.guid()}`
  12. }
  13. /**
  14. * @description 处理所有 controller 模块,npm run serve时在node环境中自动输出controller文件夹下Mock接口,请勿修改。
  15. * @returns {[]}
  16. */
  17. function handleMockArray() {
  18. const mockArray = []
  19. const getFiles = (jsonPath) => {
  20. const jsonFiles = []
  21. const findJsonFile = (path) => {
  22. const files = fs.readdirSync(path)
  23. files.forEach((item) => {
  24. const fPath = join(path, item)
  25. const stat = fs.statSync(fPath)
  26. if (stat.isDirectory() === true) findJsonFile(item)
  27. if (stat.isFile() === true) jsonFiles.push(item)
  28. })
  29. }
  30. findJsonFile(jsonPath)
  31. jsonFiles.forEach((item) => mockArray.push(`./controller/${item}`))
  32. }
  33. getFiles('mock/controller')
  34. return mockArray
  35. }
  36. module.exports = {
  37. handleRandomImage,
  38. handleMockArray,
  39. }