mm.config.js 12 KB

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