index.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. const { Random } = require("mockjs");
  2. const { join } = require("path");
  3. const fs = require("fs");
  4. /**
  5. * @copyright chuzhixin 1204505056@qq.com
  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. * @copyright chuzhixin 1204505056@qq.com
  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. };