mm.config.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. const api = require(`./api/index.js`)
  2. const { wrapApiData } = require(`./util.js`)
  3. /**
  4. * 配置说明请参考文档:
  5. * https://hongqiye.com/doc/mockm/config/option.html
  6. * @type {import('mockm/@types/config').Config}
  7. */
  8. module.exports = async (util) => {
  9. const joi = await util.tool.generate.initPackge(`joi`)
  10. return {
  11. plugin: [util.plugin.validate, util.plugin.apiDoc],
  12. guard: false,
  13. port: 8100,
  14. testPort: 8105,
  15. replayPort: 8101,
  16. watch: [`./api/`],
  17. proxy: {
  18. '/': `http://www.httpbin.org/`, // 后端接口主域
  19. '/anything/intercept': [`origin`, `127.0.0.1`], // 修改接口返回的数据
  20. },
  21. api: {
  22. // 在其他文件里的 api
  23. ...api(util).api,
  24. 'get /api/currentWeather': util.side({
  25. tags: [`公用`],
  26. summary: `当前天气`,
  27. async action(req, res) {
  28. res.json(
  29. wrapApiData(
  30. util.libObj.mockjs.mock({
  31. data: {
  32. 温度: `@integer(0, 40)`,
  33. 时间: `@now()`,
  34. 天气: `@pick(多云, 少云, 晴, 雨, 雪, 雾, 暴雨)`,
  35. },
  36. })
  37. )
  38. )
  39. },
  40. }),
  41. 'get /api/realTimeStatistics': util.side({
  42. tags: [`综合总览`],
  43. summary: `实时统计`,
  44. async action(req, res) {
  45. res.json(
  46. wrapApiData(
  47. util.libObj.mockjs.mock({
  48. data: {
  49. 目标完成率: `@integer(0, 100)`,
  50. 年移栽数量: `@integer(9, 9e4)`,
  51. 年移栽面积: `@integer(9, 9e4)`,
  52. 移载区域数: `@integer(9, 9e4)`,
  53. 烟农数量: `@integer(9, 9e4)`,
  54. },
  55. })
  56. )
  57. )
  58. },
  59. }),
  60. 'get /api/analysisOfTransplantYieldInVariousRegions': util.side({
  61. tags: [`综合总览`],
  62. summary: `各区域移栽产量分析`,
  63. async action(req, res) {
  64. res.json(
  65. wrapApiData(
  66. util.libObj.mockjs.mock({
  67. 'data|10': [
  68. {
  69. 名称: `@county`,
  70. 值: `@integer(0, 1000)`,
  71. },
  72. ],
  73. })
  74. )
  75. )
  76. },
  77. }),
  78. 'get /api/weatherForecast': util.side({
  79. tags: [`综合总览`],
  80. summary: `天气预报`,
  81. async action(req, res) {
  82. res.json(
  83. wrapApiData(
  84. util.libObj.mockjs.mock({
  85. 'data|24': [
  86. {
  87. 'index|+1': 0,
  88. 时间() {
  89. return util.libObj.mockjs.Random.now(
  90. `yyyy-MM-mm ${String(this.index).padStart(
  91. 2,
  92. `0`
  93. )}:00:00`
  94. )
  95. },
  96. 温度: `@integer(0, 40)`,
  97. 天气: `@pick(多云, 少云, 晴, 雨, 雪, 雾, 暴雨)`,
  98. },
  99. ],
  100. })
  101. )
  102. )
  103. },
  104. }),
  105. 'get /api/transplantAreaInEachRegion': util.side({
  106. tags: [`综合总览`],
  107. summary: `各区域移栽面积`,
  108. async action(req, res) {
  109. res.json(
  110. wrapApiData(
  111. util.libObj.mockjs.mock({
  112. 'data|10': [
  113. {
  114. 名称: `@county`,
  115. 今年: `@integer(0, 80)`,
  116. 去年: `@integer(0, 80)`,
  117. },
  118. ],
  119. })
  120. )
  121. )
  122. },
  123. }),
  124. 'get /api/analysisOfTransplantingSituation': util.side({
  125. tags: [`综合总览`],
  126. summary: `移栽情况分析`,
  127. async action(req, res) {
  128. res.json(
  129. wrapApiData(
  130. util.libObj.mockjs.mock({
  131. 'data|10': [
  132. {
  133. 名称: `@county`,
  134. 已移载: `@integer(9, 9e4)`,
  135. 未移栽: `@integer(9, 9e4)`,
  136. 比率() {
  137. return (this.已移载 / (this.已移载 + this.未移栽)) * 100
  138. },
  139. },
  140. ],
  141. })
  142. )
  143. )
  144. },
  145. }),
  146. 'get /api/transplantTrendAnalysis': util.side({
  147. tags: [`综合总览`],
  148. summary: `移栽趋势分析`,
  149. async action(req, res) {
  150. res.json(
  151. wrapApiData(
  152. util.libObj.mockjs.mock({
  153. 'data|31': [
  154. {
  155. '时间|+1': 1,
  156. 今年: `@integer(0, 2000)`,
  157. 去年: `@integer(0, 2000)`,
  158. },
  159. ],
  160. })
  161. )
  162. )
  163. },
  164. }),
  165. 'get /base': util.side({
  166. tags: [`种植分析`],
  167. summary: `区域排行`,
  168. schema: {
  169. query: joi
  170. .object({
  171. _sort: joi.string().default(`比率`).description(`排序字段`),
  172. _order: joi
  173. .string()
  174. .default(`desc`)
  175. .description(`排序方式, asc | desc`),
  176. q: joi.string().description(`模糊搜索`),
  177. })
  178. .description(`搜索项`),
  179. },
  180. async action(req, res, next) {
  181. next()
  182. },
  183. }),
  184. 'get /transplantAreaInEachRegion2': util.side({
  185. tags: [`种植分析`],
  186. summary: `各区域移栽面积`,
  187. async action(req, res, next) {
  188. res.json(
  189. wrapApiData(
  190. util.libObj.mockjs.mock({
  191. 'data|10': [
  192. {
  193. 名称: `@county`,
  194. 已移载: `@integer(9, 9e4)`,
  195. 未移栽: `@integer(9, 9e4)`,
  196. 比率() {
  197. return (this.已移载 / (this.已移载 + this.未移栽)) * 100
  198. },
  199. },
  200. ],
  201. })
  202. )
  203. )
  204. },
  205. }),
  206. 'get /trendOfTransplantQuantityThisMonth': util.side({
  207. tags: [`种植分析`],
  208. summary: `本月移栽数量趋势`,
  209. async action(req, res, next) {
  210. res.json(
  211. wrapApiData(
  212. util.libObj.mockjs.mock({
  213. 'data|31': [
  214. {
  215. 'index|+1': 1,
  216. 时间() {
  217. return util.libObj.mockjs.Random.now(
  218. `yyyy-MM-${String(this.index).padStart(2, `0`)}`
  219. )
  220. },
  221. 今年: `@integer(0, 2000)`,
  222. 去年: `@integer(0, 2000)`,
  223. 较上日: `@integer(0, 100)`,
  224. 较去年: `@integer(0, 100)`,
  225. },
  226. ],
  227. })
  228. )
  229. )
  230. },
  231. }),
  232. 'get /thisYearsTransplantingDataTrend': util.side({
  233. tags: [`种植分析`],
  234. summary: `本年移栽数据趋势`,
  235. async action(req, res, next) {
  236. res.json(
  237. wrapApiData(
  238. util.libObj.mockjs.mock({
  239. 'data|12': [
  240. {
  241. 'index|+1': 1,
  242. 时间() {
  243. return util.libObj.mockjs.Random.now(
  244. `yyyy-${String(this.index).padStart(2, `0`)}`
  245. )
  246. },
  247. 今年: `@integer(0, 2000)`,
  248. 去年: `@integer(0, 2000)`,
  249. 较上日: `@integer(0, 100)`,
  250. 较去年: `@integer(0, 100)`,
  251. },
  252. ],
  253. })
  254. )
  255. )
  256. },
  257. }),
  258. // 当为基本数据类型时, 直接返回数据, 这个接口返回 {"msg":"ok"}
  259. '/api/1': { msg: `ok` },
  260. // 也可以像 express 一样返回数据
  261. '/api/2'(req, res) {
  262. res.send({ msg: `ok` })
  263. },
  264. // 一个只能使用 post 方法访问的接口
  265. 'post /api/3': { msg: `ok` },
  266. // // 一个 websocket 接口, 会发送收到的消息
  267. // 'ws /api/4' (ws, req) {
  268. // ws.on('message', (msg) => ws.send(msg))
  269. // },
  270. // 一个下载文件的接口
  271. '/file'(req, res) {
  272. res.download(__filename, `mm.config.js`)
  273. },
  274. // 获取动态的接口路径的参数 code
  275. '/status/:code'(req, res) {
  276. res.json({ statusCode: req.params.code })
  277. },
  278. // 使用 mockjs 生成数据
  279. '/user'(req, res) {
  280. const json = util.libObj.mockjs.mock({
  281. 'data|3-7': [
  282. {
  283. userId: `@id`,
  284. userName: `@cname`,
  285. },
  286. ],
  287. })
  288. res.json(json)
  289. },
  290. },
  291. static: [],
  292. dbCover: true,
  293. db: util.libObj.mockjs.mock({
  294. 'base|10': [
  295. {
  296. 'id|+1': 1,
  297. 名称: `@county()@ctitle()基地`,
  298. 温度: `@integer(0, 40)`,
  299. 管理人员: `@cname`,
  300. 联系电话: `@phone`,
  301. 简介: `<p>@cparagraph()</p><p>@cparagraph()</p><p>@cparagraph()</p><p>@cparagraph()</p><p>@cparagraph()</p><p>@cparagraph()</p>`,
  302. 面积: `@integer(9e3, 9e4)`,
  303. 烟农数量: `@integer(10, 999)`,
  304. 天气: `@pick(多云, 少云, 晴, 雨, 雪, 雾, 暴雨)`,
  305. 已种植: `@integer(9, 9e4)`,
  306. 未种植: `@integer(9, 9e4)`,
  307. 地址: `@county()@ctitle()`,
  308. '图片|1-5': [`@image`],
  309. 完成率() {
  310. return (this.已种植 / (this.已种植 + this.未种植)) * 100
  311. },
  312. },
  313. ],
  314. 'holePunchingMachine|10': [
  315. {
  316. 'id|+1': 1,
  317. 名称: `@county()@ctitle()基地打孔机`,
  318. 管理人员: `@cname`,
  319. ip: `@ip`,
  320. 投入时间: `@date`,
  321. 联系电话: `@phone`,
  322. '图片|1-5': [`@image`],
  323. 状态: `@pick(运行中, 待机, 故障)`,
  324. 完成率() {
  325. return (this.已种植 / (this.已种植 + this.未种植)) * 100
  326. },
  327. },
  328. ],
  329. }),
  330. resHandleReplay: ({ req, res }) => wrapApiData({ code: 200, data: {} }),
  331. resHandleJsonApi: ({ req, res: { statusCode: code }, data }) =>
  332. wrapApiData({ code, data }),
  333. }
  334. }