/** * 配置说明请参考文档: * 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`) util.libObj.mockjs.Random.extend({ image2: function () { return this.pick( `https://dummyimage.com/${this.integer(100, 1000)}x${this.integer( 100, 1000 )}/${this.hex()}/${this.hex()}.jpg`.replace(/#/g, ``) ) }, }) return { plugin: [util.plugin.validate, util.plugin.apiDoc], guard: false, port: 8100, testPort: 8105, replayPort: 8101, watch: [`./apix/`], proxy: { '/': `http://192.168.1.70:8088/`, // 后端接口主域 '/anything/intercept': [`origin`, `127.0.0.1`], // 修改接口返回的数据 }, api: { 'get /apix/currentWeather': util.side({ tags: [`公用`], summary: `当前天气`, async action(req, res) { res.json( wrapApiData( util.libObj.mockjs.mock({ data: { 温度: `@integer(0, 40)`, 时间: `@now()`, 天气: `@pick(多云, 少云, 晴, 雨, 雪, 雾, 暴雨)`, }, }) ) ) }, }), 'post /apix/login': util.side({ tags: [`公用`], summary: `登录`, schema: { body: joi .object({ userName: joi .string() .default(`admin`) .required() .description(`用户名`), userPwd: joi .string() .default(`123456`) .required() .description(`密码`), }) .description(`用户信息`), }, async action(req, res) { const { userName, userPwd } = req.body if (userName == `admin` && userPwd == `123456`) { res.json( wrapApiData( util.libObj.mockjs.mock({ data: { token: `@uuid`, }, }) ) ) } else { res.status(401).json( wrapApiData( util.libObj.mockjs.mock({ code: 401, data: { message: `用户名或密码错误`, }, }) ) ) } }, }), 'get /apix/mapOfTongren': util.side({ tags: [`公用`], summary: `铜仁地图数据`, async action(req, res) { const data = await util.libObj.axios .get( `https://geo.datav.aliyun.com/areas_v3/bound/geojson?code=520600_full` ) .then((res) => res.data) res.json( wrapApiData( util.libObj.mockjs.mock({ data, }) ) ) }, }), 'get /apix/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 /apix/analysisOfTransplantYieldInVariousRegions': util.side({ tags: [`综合总览`], summary: `各区域移栽产量分析`, async action(req, res) { res.json( wrapApiData( util.libObj.mockjs.mock({ 'data|10': [ { 名称: `@county`, 值: `@integer(0, 1000)`, }, ], }) ) ) }, }), 'get /apix/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 /apix/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 /apix/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 /apix/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 /apix/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) { res.redirect(req.originalUrl.replace(`/apix`, ``)) }, }), 'get /apix/base/:id': util.side({ tags: [`增删改查`, `种植分析`, `种值分析详情`], summary: `基地详情`, schema: { path: joi.object({ id: joi.string(), }), }, async action(req, res, next) { res.redirect(req.originalUrl.replace(`/apix`, ``)) }, }), 'get /apix/holePunchingMachine': util.side({ tags: [`增删改查`, `种值分析详情`], summary: `打孔机 crud`, schema: { query: joi .object({ q: joi.string().description(`模糊搜索`), }) .unknown(true) .description(`搜索项`), }, async action(req, res, next) { res.redirect(req.originalUrl.replace(`/apix`, ``)) }, }), 'get /apix/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 /apix/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 /apix/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 /apix/analysisOfAnnualTransplantArea': util.side({ tags: [`种植分析`, `种值分析详情`], summary: `年度移栽面积分析`, async action(req, res, next) { res.redirect(`/apix/thisYearsTransplantingDataTrend`) }, }), 'get /apix/monthlyTransplantationAreaAnalysis': util.side({ tags: [`种植分析`, `种值分析详情`], summary: `月度移栽面积分析`, async action(req, res, next) { res.redirect(`/apix/trendOfTransplantQuantityThisMonth`) }, }), 'get /apix/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)`, }, }) ) ) }, }), }, static: [], dbCover: true, db: util.libObj.mockjs.mock({ 'base|10': [ { 'id|+1': 1, 区域编码() { return getAdcode(this.id) }, 名称: `@county()@ctitle()基地`, 温度: [`@integer(0, 20)`, `@integer(20, 40)`], 天气: `@pick(多云, 少云, 晴, 雨, 雪, 雾, 暴雨)`, 管理人员: `@cname`, 联系电话: `@phone`, 简介: `

@cparagraph()

@cparagraph()

@cparagraph()

@cparagraph()

@cparagraph()

@cparagraph()

`, 面积: `@integer(9e3, 9e4)`, 烟农数量: `@integer(10, 999)`, 已种植: `@integer(9, 9e4)`, 未种植: `@integer(9, 9e4)`, 地址: `@county()@ctitle()`, '图片|3-5': [`@image2()`], 完成率() { return (this.已种植 / (this.已种植 + this.未种植)) * 100 }, }, ], 'holePunchingMachine|10': [ { 'id|+1': 1, 名称: `@county()@ctitle()基地打孔机`, 管理人员: `@cname`, ip: `@ip`, 投入时间: `@date`, 联系电话: `@phone`, '图片|1-5': [`@image2()`], 状态: `@pick(运行中, 关机, 故障)`, }, ], }), resHandleReplay: ({ req, res }) => wrapApiData({ code: 200, data: {} }), resHandleJsonApi: ({ req, res: { statusCode: code }, data }) => wrapApiData({ code, data }), } } /** * 包裹 api 的返回值 * @param {*} param0 * @param {object} param0.data - 原始数据 * @param {number|string} [param0.code=200] - http状态码 * @returns */ function wrapApiData({ data, code = 200 }) { code = String(code) return { code, success: Boolean(code.match(/^[2]/)), // 如果状态码以2开头则为 true data, } } function getAdcode(index) { const list = [ { name: `碧江区`, adcode: 520602, }, { name: `万山区`, adcode: 520603, }, { name: `江口县`, adcode: 520621, }, { name: `玉屏侗族自治县`, adcode: 520622, }, { name: `石阡县`, adcode: 520623, }, { name: `思南县`, adcode: 520624, }, { name: `印江土家族苗族自治县`, adcode: 520625, }, { name: `德江县`, adcode: 520626, }, { name: `沿河土家族自治县`, adcode: 520627, }, { name: `松桃苗族自治县`, adcode: 520628, }, ] return list[index % list.length].adcode }