mm.config.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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. 较上日: `@integer(0, 100)`,
  159. 较去年: `@integer(0, 100)`,
  160. },
  161. ],
  162. })
  163. )
  164. )
  165. },
  166. }),
  167. 'get /base': util.side({
  168. tags: [`增删改查`],
  169. summary: `基地 crud`,
  170. schema: {
  171. query: joi
  172. .object({
  173. _sort: joi.string().default(`比率`).description(`排序字段`),
  174. _order: joi
  175. .string()
  176. .default(`desc`)
  177. .description(`排序方式, asc | desc`),
  178. q: joi.string().description(`模糊搜索`),
  179. })
  180. .description(`搜索项`),
  181. },
  182. async action(req, res, next) {
  183. next()
  184. },
  185. }),
  186. 'get /holePunchingMachine': util.side({
  187. tags: [`增删改查`],
  188. summary: `打孔机 crud`,
  189. schema: {
  190. query: joi
  191. .object({
  192. q: joi.string().description(`模糊搜索`),
  193. })
  194. .description(`搜索项`),
  195. },
  196. async action(req, res, next) {
  197. next()
  198. },
  199. }),
  200. 'get /transplantAreaInEachRegion2': util.side({
  201. tags: [`种植分析`],
  202. summary: `各区域移栽面积`,
  203. async action(req, res, next) {
  204. res.json(
  205. wrapApiData(
  206. util.libObj.mockjs.mock({
  207. 'data|10': [
  208. {
  209. 名称: `@county`,
  210. 已移载: `@integer(9, 9e4)`,
  211. 未移栽: `@integer(9, 9e4)`,
  212. 比率() {
  213. return (this.已移载 / (this.已移载 + this.未移栽)) * 100
  214. },
  215. },
  216. ],
  217. })
  218. )
  219. )
  220. },
  221. }),
  222. 'get /trendOfTransplantQuantityThisMonth': util.side({
  223. tags: [`种植分析`, `种值分析详情`],
  224. summary: `本月移栽数量趋势, 月度移栽面积分析`,
  225. async action(req, res, next) {
  226. res.json(
  227. wrapApiData(
  228. util.libObj.mockjs.mock({
  229. 'data|31': [
  230. {
  231. 'index|+1': 1,
  232. 时间() {
  233. return util.libObj.mockjs.Random.now(
  234. `yyyy-MM-${String(this.index).padStart(2, `0`)}`
  235. )
  236. },
  237. 今年: `@integer(0, 2000)`,
  238. 去年: `@integer(0, 2000)`,
  239. 较上日: `@integer(0, 100)`,
  240. 较去年: `@integer(0, 100)`,
  241. },
  242. ],
  243. })
  244. )
  245. )
  246. },
  247. }),
  248. 'get /thisYearsTransplantingDataTrend': util.side({
  249. tags: [`种植分析`, `种值分析详情`],
  250. summary: `本年移栽数据趋势, 年度移栽趋势, 年度移栽面积分析`,
  251. async action(req, res, next) {
  252. res.json(
  253. wrapApiData(
  254. util.libObj.mockjs.mock({
  255. 'data|12': [
  256. {
  257. 'index|+1': 1,
  258. 时间() {
  259. return util.libObj.mockjs.Random.now(
  260. `yyyy-${String(this.index).padStart(2, `0`)}`
  261. )
  262. },
  263. 今年: `@integer(0, 2000)`,
  264. 去年: `@integer(0, 2000)`,
  265. 较上日: `@integer(0, 100)`,
  266. 较去年: `@integer(0, 100)`,
  267. },
  268. ],
  269. })
  270. )
  271. )
  272. },
  273. }),
  274. 'get /api/transplantData': util.side({
  275. tags: [`种值分析详情`],
  276. summary: `移栽数据`,
  277. async action(req, res) {
  278. res.json(
  279. wrapApiData(
  280. util.libObj.mockjs.mock({
  281. data: {
  282. 处理率: `@integer(0, 100)`,
  283. 今日种植数: `@integer(9, 9e4)`,
  284. 目标移栽数: `@integer(9, 9e4)`,
  285. 累计移栽: `@integer(9, 9e4)`,
  286. },
  287. })
  288. )
  289. )
  290. },
  291. }),
  292. // 当为基本数据类型时, 直接返回数据, 这个接口返回 {"msg":"ok"}
  293. '/api/1': { msg: `ok` },
  294. // 也可以像 express 一样返回数据
  295. '/api/2'(req, res) {
  296. res.send({ msg: `ok` })
  297. },
  298. // 一个只能使用 post 方法访问的接口
  299. 'post /api/3': { msg: `ok` },
  300. // // 一个 websocket 接口, 会发送收到的消息
  301. // 'ws /api/4' (ws, req) {
  302. // ws.on('message', (msg) => ws.send(msg))
  303. // },
  304. // 一个下载文件的接口
  305. '/file'(req, res) {
  306. res.download(__filename, `mm.config.js`)
  307. },
  308. // 获取动态的接口路径的参数 code
  309. '/status/:code'(req, res) {
  310. res.json({ statusCode: req.params.code })
  311. },
  312. // 使用 mockjs 生成数据
  313. '/user'(req, res) {
  314. const json = util.libObj.mockjs.mock({
  315. 'data|3-7': [
  316. {
  317. userId: `@id`,
  318. userName: `@cname`,
  319. },
  320. ],
  321. })
  322. res.json(json)
  323. },
  324. },
  325. static: [],
  326. dbCover: true,
  327. db: util.libObj.mockjs.mock({
  328. 'base|10': [
  329. {
  330. 'id|+1': 1,
  331. 名称: `@county()@ctitle()基地`,
  332. 温度: `@integer(0, 40)`,
  333. 管理人员: `@cname`,
  334. 联系电话: `@phone`,
  335. 简介: `<p>@cparagraph()</p><p>@cparagraph()</p><p>@cparagraph()</p><p>@cparagraph()</p><p>@cparagraph()</p><p>@cparagraph()</p>`,
  336. 面积: `@integer(9e3, 9e4)`,
  337. 烟农数量: `@integer(10, 999)`,
  338. 天气: `@pick(多云, 少云, 晴, 雨, 雪, 雾, 暴雨)`,
  339. 已种植: `@integer(9, 9e4)`,
  340. 未种植: `@integer(9, 9e4)`,
  341. 地址: `@county()@ctitle()`,
  342. '图片|1-5': [`@image`],
  343. 完成率() {
  344. return (this.已种植 / (this.已种植 + this.未种植)) * 100
  345. },
  346. },
  347. ],
  348. 'holePunchingMachine|10': [
  349. {
  350. 'id|+1': 1,
  351. 名称: `@county()@ctitle()基地打孔机`,
  352. 管理人员: `@cname`,
  353. ip: `@ip`,
  354. 投入时间: `@date`,
  355. 联系电话: `@phone`,
  356. '图片|1-5': [`@image`],
  357. 状态: `@pick(运行中, 待机, 故障)`,
  358. 完成率() {
  359. return (this.已种植 / (this.已种植 + this.未种植)) * 100
  360. },
  361. },
  362. ],
  363. }),
  364. resHandleReplay: ({ req, res }) => wrapApiData({ code: 200, data: {} }),
  365. resHandleJsonApi: ({ req, res: { statusCode: code }, data }) =>
  366. wrapApiData({ code, data }),
  367. }
  368. }