mm.config.js 13 KB

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