index.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. import { Random } from "mockjs";
  2. import { join } from "path";
  3. import fs from "fs";
  4. /**
  5. * @copyright chuzhixin 1204505056@qq.com
  6. * @description 随机生成图片url。
  7. * @param width
  8. * @param height
  9. * @returns {string}
  10. */
  11. export function handleRandomImage(width = 50, height = 50) {
  12. return `https://picsum.photos/${width}/${height}?random=${Random.guid()}`;
  13. }
  14. /**
  15. * @copyright chuzhixin 1204505056@qq.com
  16. * @description 处理所有 controller 模块,npm run serve时在node环境中自动输出controller文件夹下Mock接口,请勿修改。
  17. * @returns {[]}
  18. */
  19. export 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. }