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`], // 修改接口返回的数据 '/datav/': `https://geo.datav.aliyun.com/`, // 修改接口返回的数据 }, 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(`模糊搜索`), }) .unknown(true) .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`, 简介: `

@cparagraph()

@cparagraph()

@cparagraph()

@cparagraph()

@cparagraph()

@cparagraph()

`, 面积: `@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 }), } }