123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375 |
- const api = require(`./api/index.js`)
- const { wrapApiData } = require(`./util.js`)
- /**
- * 配置说明请参考文档:
- * https://hongqiye.com/doc/mockm/config/option.html
- * @type {import('mockm/@types/config').Config}
- */
- module.exports = async (util) => {
- const joi = await util.tool.generate.initPackge(`joi`)
- return {
- plugin: [util.plugin.validate, util.plugin.apiDoc],
- guard: false,
- port: 8100,
- testPort: 8105,
- replayPort: 8101,
- watch: [`./api/`],
- proxy: {
- '/': `http://www.httpbin.org/`, // 后端接口主域
- '/anything/intercept': [`origin`, `127.0.0.1`], // 修改接口返回的数据
- },
- api: {
- // 在其他文件里的 api
- ...api(util).api,
- 'get /api/currentWeather': util.side({
- tags: [`公用`],
- summary: `当前天气`,
- async action(req, res) {
- res.json(
- wrapApiData(
- util.libObj.mockjs.mock({
- data: {
- 温度: `@integer(0, 40)`,
- 时间: `@now()`,
- 天气: `@pick(多云, 少云, 晴, 雨, 雪, 雾, 暴雨)`,
- },
- })
- )
- )
- },
- }),
- 'get /api/realTimeStatistics': util.side({
- tags: [`综合总览`],
- summary: `实时统计`,
- async action(req, res) {
- res.json(
- wrapApiData(
- util.libObj.mockjs.mock({
- data: {
- 目标完成率: `@integer(0, 100)`,
- 年移栽数量: `@integer(9, 9e4)`,
- 年移栽面积: `@integer(9, 9e4)`,
- 移载区域数: `@integer(9, 9e4)`,
- 烟农数量: `@integer(9, 9e4)`,
- },
- })
- )
- )
- },
- }),
- 'get /api/analysisOfTransplantYieldInVariousRegions': util.side({
- tags: [`综合总览`],
- summary: `各区域移栽产量分析`,
- async action(req, res) {
- res.json(
- wrapApiData(
- util.libObj.mockjs.mock({
- 'data|10': [
- {
- 名称: `@county`,
- 值: `@integer(0, 1000)`,
- },
- ],
- })
- )
- )
- },
- }),
- 'get /api/weatherForecast': util.side({
- tags: [`综合总览`, `种值分析详情`],
- summary: `天气预报, 实时天气`,
- async action(req, res) {
- res.json(
- wrapApiData(
- util.libObj.mockjs.mock({
- 'data|24': [
- {
- 'index|+1': 0,
- 时间() {
- return util.libObj.mockjs.Random.now(
- `yyyy-MM-mm ${String(this.index).padStart(
- 2,
- `0`
- )}:00:00`
- )
- },
- 温度: `@integer(0, 40)`,
- 天气: `@pick(多云, 少云, 晴, 雨, 雪, 雾, 暴雨)`,
- },
- ],
- })
- )
- )
- },
- }),
- 'get /api/transplantAreaInEachRegion': util.side({
- tags: [`综合总览`],
- summary: `各区域移栽面积`,
- async action(req, res) {
- res.json(
- wrapApiData(
- util.libObj.mockjs.mock({
- 'data|10': [
- {
- 名称: `@county`,
- 今年: `@integer(0, 80)`,
- 去年: `@integer(0, 80)`,
- },
- ],
- })
- )
- )
- },
- }),
- 'get /api/analysisOfTransplantingSituation': util.side({
- tags: [`综合总览`],
- summary: `移栽情况分析`,
- async action(req, res) {
- res.json(
- wrapApiData(
- util.libObj.mockjs.mock({
- 'data|10': [
- {
- 名称: `@county`,
- 已移载: `@integer(9, 9e4)`,
- 未移栽: `@integer(9, 9e4)`,
- 比率() {
- return (this.已移载 / (this.已移载 + this.未移栽)) * 100
- },
- },
- ],
- })
- )
- )
- },
- }),
- 'get /api/transplantTrendAnalysis': util.side({
- tags: [`综合总览`, `种值分析详情`],
- summary: `移栽趋势分析, 月度移栽趋势`,
- async action(req, res) {
- res.json(
- wrapApiData(
- util.libObj.mockjs.mock({
- 'data|31': [
- {
- '时间|+1': 1,
- 今年: `@integer(0, 2000)`,
- 去年: `@integer(0, 2000)`,
- 较上日: `@integer(0, 100)`,
- 较去年: `@integer(0, 100)`,
- },
- ],
- })
- )
- )
- },
- }),
- 'get /base': util.side({
- tags: [`增删改查`],
- summary: `基地 crud`,
- schema: {
- query: joi
- .object({
- _sort: joi.string().default(`比率`).description(`排序字段`),
- _order: joi
- .string()
- .default(`desc`)
- .description(`排序方式, asc | desc`),
- q: joi.string().description(`模糊搜索`),
- })
- .description(`搜索项`),
- },
- async action(req, res, next) {
- next()
- },
- }),
- 'get /holePunchingMachine': util.side({
- tags: [`增删改查`],
- summary: `打孔机 crud`,
- schema: {
- query: joi
- .object({
- q: joi.string().description(`模糊搜索`),
- })
- .description(`搜索项`),
- },
- async action(req, res, next) {
- next()
- },
- }),
- 'get /transplantAreaInEachRegion2': util.side({
- tags: [`种植分析`],
- summary: `各区域移栽面积`,
- async action(req, res, next) {
- res.json(
- wrapApiData(
- util.libObj.mockjs.mock({
- 'data|10': [
- {
- 名称: `@county`,
- 已移载: `@integer(9, 9e4)`,
- 未移栽: `@integer(9, 9e4)`,
- 比率() {
- return (this.已移载 / (this.已移载 + this.未移栽)) * 100
- },
- },
- ],
- })
- )
- )
- },
- }),
- 'get /trendOfTransplantQuantityThisMonth': util.side({
- tags: [`种植分析`, `种值分析详情`],
- summary: `本月移栽数量趋势, 月度移栽面积分析`,
- async action(req, res, next) {
- res.json(
- wrapApiData(
- util.libObj.mockjs.mock({
- 'data|31': [
- {
- 'index|+1': 1,
- 时间() {
- return util.libObj.mockjs.Random.now(
- `yyyy-MM-${String(this.index).padStart(2, `0`)}`
- )
- },
- 今年: `@integer(0, 2000)`,
- 去年: `@integer(0, 2000)`,
- 较上日: `@integer(0, 100)`,
- 较去年: `@integer(0, 100)`,
- },
- ],
- })
- )
- )
- },
- }),
- 'get /thisYearsTransplantingDataTrend': util.side({
- tags: [`种植分析`, `种值分析详情`],
- summary: `本年移栽数据趋势, 年度移栽趋势, 年度移栽面积分析`,
- async action(req, res, next) {
- res.json(
- wrapApiData(
- util.libObj.mockjs.mock({
- 'data|12': [
- {
- 'index|+1': 1,
- 时间() {
- return util.libObj.mockjs.Random.now(
- `yyyy-${String(this.index).padStart(2, `0`)}`
- )
- },
- 今年: `@integer(0, 2000)`,
- 去年: `@integer(0, 2000)`,
- 较上日: `@integer(0, 100)`,
- 较去年: `@integer(0, 100)`,
- },
- ],
- })
- )
- )
- },
- }),
- 'get /api/transplantData': util.side({
- tags: [`种值分析详情`],
- summary: `移栽数据`,
- async action(req, res) {
- res.json(
- wrapApiData(
- util.libObj.mockjs.mock({
- data: {
- 处理率: `@integer(0, 100)`,
- 今日种植数: `@integer(9, 9e4)`,
- 目标移栽数: `@integer(9, 9e4)`,
- 累计移栽: `@integer(9, 9e4)`,
- },
- })
- )
- )
- },
- }),
- // 当为基本数据类型时, 直接返回数据, 这个接口返回 {"msg":"ok"}
- '/api/1': { msg: `ok` },
- // 也可以像 express 一样返回数据
- '/api/2'(req, res) {
- res.send({ msg: `ok` })
- },
- // 一个只能使用 post 方法访问的接口
- 'post /api/3': { msg: `ok` },
- // // 一个 websocket 接口, 会发送收到的消息
- // 'ws /api/4' (ws, req) {
- // ws.on('message', (msg) => ws.send(msg))
- // },
- // 一个下载文件的接口
- '/file'(req, res) {
- res.download(__filename, `mm.config.js`)
- },
- // 获取动态的接口路径的参数 code
- '/status/:code'(req, res) {
- res.json({ statusCode: req.params.code })
- },
- // 使用 mockjs 生成数据
- '/user'(req, res) {
- const json = util.libObj.mockjs.mock({
- 'data|3-7': [
- {
- userId: `@id`,
- userName: `@cname`,
- },
- ],
- })
- res.json(json)
- },
- },
- static: [],
- dbCover: true,
- db: util.libObj.mockjs.mock({
- 'base|10': [
- {
- 'id|+1': 1,
- 名称: `@county()@ctitle()基地`,
- 温度: `@integer(0, 40)`,
- 管理人员: `@cname`,
- 联系电话: `@phone`,
- 简介: `<p>@cparagraph()</p><p>@cparagraph()</p><p>@cparagraph()</p><p>@cparagraph()</p><p>@cparagraph()</p><p>@cparagraph()</p>`,
- 面积: `@integer(9e3, 9e4)`,
- 烟农数量: `@integer(10, 999)`,
- 天气: `@pick(多云, 少云, 晴, 雨, 雪, 雾, 暴雨)`,
- 已种植: `@integer(9, 9e4)`,
- 未种植: `@integer(9, 9e4)`,
- 地址: `@county()@ctitle()`,
- '图片|1-5': [`@image`],
- 完成率() {
- return (this.已种植 / (this.已种植 + this.未种植)) * 100
- },
- },
- ],
- 'holePunchingMachine|10': [
- {
- 'id|+1': 1,
- 名称: `@county()@ctitle()基地打孔机`,
- 管理人员: `@cname`,
- ip: `@ip`,
- 投入时间: `@date`,
- 联系电话: `@phone`,
- '图片|1-5': [`@image`],
- 状态: `@pick(运行中, 待机, 故障)`,
- 完成率() {
- return (this.已种植 / (this.已种植 + this.未种植)) * 100
- },
- },
- ],
- }),
- resHandleReplay: ({ req, res }) => wrapApiData({ code: 200, data: {} }),
- resHandleJsonApi: ({ req, res: { statusCode: code }, data }) =>
- wrapApiData({ code, data }),
- }
- }
|