mm.config.js 12 KB

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