index.js 1.3 KB

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