mm.config.js 12 KB

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