index.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. const { Random } = require('mockjs')
  2. const { join } = require('path')
  3. const fs = require('fs')
  4. /**
  5. * 随机生成图片url
  6. * @param {*} width
  7. * @param {*} height
  8. * @returns
  9. */
  10. function handleRandomImage(width = 50, height = 50) {
  11. return `https://picsum.photos/${width}/${height}?random=${Random.guid()}`
  12. }
  13. /**
  14. * 处理所有 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. // 图标数组
  37. const icons = [
  38. 'ad',
  39. 'address-book',
  40. 'arrow',
  41. 'ban',
  42. 'balance',
  43. 'bell',
  44. 'book',
  45. 'bookmark',
  46. 'bug',
  47. 'bullhorn',
  48. 'calc',
  49. 'circle',
  50. 'cloud-sun',
  51. 'close',
  52. 'checkbox',
  53. 'coffee',
  54. 'copyright',
  55. 'component',
  56. 'compress',
  57. 'expand',
  58. 'eye',
  59. 'eye-slash',
  60. 'form',
  61. 'ganged',
  62. 'gift',
  63. 'home',
  64. 'hotent',
  65. 'info',
  66. 'laptop',
  67. 'lock',
  68. 'move',
  69. 'more',
  70. 'search',
  71. 'setting',
  72. 'simple-collapse',
  73. 'simple-expand',
  74. 'table',
  75. 'user',
  76. 'users',
  77. 'upload',
  78. 'video',
  79. 'palette',
  80. 'plan',
  81. 'refresh',
  82. 'time',
  83. 'typography',
  84. 'dialog',
  85. 'duration',
  86. 'attachment',
  87. 'radio',
  88. 'select',
  89. 'tree',
  90. ]
  91. module.exports = {
  92. handleRandomImage,
  93. handleMockArray,
  94. icons,
  95. }