mm.config.js 12 KB

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