hi-ucs/front/mock/utils/index.js

44 lines
1.2 KiB
Vue
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

const { Random } = require('mockjs')
const { join } = require('path')
const fs = require('fs')
/**
* @author chuzhixin 1204505056@qq.com
* @description 随机生成图片url。
* @param width
* @param height
* @returns {string}
*/
function handleRandomImage(width = 50, height = 50) {
return `https://picsum.photos/${width}/${height}?random=${Random.guid()}`
}
/**
* @author chuzhixin 1204505056@qq.com
* @description 处理所有 controller 模块npm run serve时在node环境中自动输出controller文件夹下Mock接口请勿修改。
* @returns {[]}
*/
function handleMockArray() {
const mockArray = []
const getFiles = (jsonPath) => {
const jsonFiles = []
const findJsonFile = (path) => {
const files = fs.readdirSync(path)
files.forEach((item) => {
const fPath = join(path, item)
const stat = fs.statSync(fPath)
if (stat.isDirectory() === true) findJsonFile(item)
if (stat.isFile() === true) jsonFiles.push(item)
})
}
findJsonFile(jsonPath)
jsonFiles.forEach((item) => mockArray.push(`./controller/${item}`))
}
getFiles('mock/controller')
return mockArray
}
module.exports = {
handleRandomImage,
handleMockArray,
}