mm.config.js 12 KB

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