123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434 |
- /**
- * 配置说明请参考文档:
- * 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: [`./api/`],
- proxy: {
- '/': `http://192.168.1.196:8088/`, // 后端接口主域
- '/anything/intercept': [`origin`, `127.0.0.1`], // 修改接口返回的数据
- },
- apiX: {
- 'get /api/currentWeather': util.side({
- tags: [`公用`],
- summary: `当前天气`,
- async action(req, res) {
- res.json(
- wrapApiData(
- util.libObj.mockjs.mock({
- data: {
- 温度: `@integer(0, 40)`,
- 时间: `@now()`,
- 天气: `@pick(多云, 少云, 晴, 雨, 雪, 雾, 暴雨)`,
- },
- })
- )
- )
- },
- }),
- 'post /api/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 /api/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 /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 /api/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(`/api`, ``))
- },
- }),
- 'get /api/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(`/api`, ``))
- },
- }),
- 'get /api/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 /api/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 /api/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/analysisOfAnnualTransplantArea': util.side({
- tags: [`种植分析`, `种值分析详情`],
- summary: `年度移栽面积分析`,
- async action(req, res, next) {
- res.redirect(`/api/thisYearsTransplantingDataTrend`)
- },
- }),
- 'get /api/monthlyTransplantationAreaAnalysis': util.side({
- tags: [`种植分析`, `种值分析详情`],
- summary: `月度移栽面积分析`,
- async action(req, res, next) {
- res.redirect(`/api/trendOfTransplantQuantityThisMonth`)
- },
- }),
- '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)`,
- },
- })
- )
- )
- },
- }),
- },
- static: [],
- dbCover: true,
- db: util.libObj.mockjs.mock({
- 'base|10': [
- {
- 'id|+1': 1,
- 名称: `@county()@ctitle()基地`,
- 温度: [`@integer(0, 20)`, `@integer(20, 40)`],
- 天气: `@pick(多云, 少云, 晴, 雨, 雪, 雾, 暴雨)`,
- 管理人员: `@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)`,
- 已种植: `@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,
- }
- }
|