index.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. const { Random } = require('mockjs')
  2. const { getDB, saveDB } = require('../utils')
  3. const db = getDB('schedule')
  4. function save () {
  5. saveDB('schedule', db)
  6. }
  7. module.exports = [
  8. {
  9. url: '/scheduling/listByPage',
  10. type: 'post',
  11. response: config => {
  12. const { name = '', currentPage = 1, pageCount = 10 } = config.body
  13. if (currentPage < 1) {
  14. return {
  15. success: false,
  16. errMessage: '参数错误'
  17. }
  18. }
  19. const list = name ? db.filter(item => ~item.name.indexOf(name)) : db
  20. const start = (currentPage - 1) * pageCount
  21. return {
  22. success: true,
  23. data: list.slice(start, start + pageCount),
  24. totalCount: list.length
  25. }
  26. }
  27. },
  28. {
  29. url: '/scheduling/detail',
  30. type: 'get',
  31. response: config => {
  32. const { id } = config.query
  33. const schedule = db.find(item => item.id === id)
  34. if (!schedule) {
  35. return {
  36. success: false,
  37. errMessage: '排期不存在'
  38. }
  39. }
  40. return {
  41. success: true,
  42. data: schedule
  43. }
  44. }
  45. },
  46. {
  47. url: '/scheduling/add',
  48. type: 'post',
  49. response: config => {
  50. const { name, resolutionRatio } = config.body
  51. const layout = {
  52. id: Random.id(),
  53. name,
  54. resolutionRatio,
  55. status: 0,
  56. schedulingStatus: 0,
  57. createTime: Random.now('yyyy-MM-dd HH:mm:ss'),
  58. programs: []
  59. }
  60. db.unshift(layout)
  61. save()
  62. return {
  63. success: true,
  64. data: layout.id
  65. }
  66. }
  67. },
  68. {
  69. url: '/scheduling/delete',
  70. type: 'get',
  71. response: config => {
  72. const { id } = config.query
  73. const index = db.findIndex(item => item.id === id)
  74. if (~index) {
  75. db.splice(index, 1)
  76. save()
  77. }
  78. return {
  79. success: true
  80. }
  81. }
  82. },
  83. {
  84. url: '/scheduling/update',
  85. type: 'post',
  86. response: config => {
  87. const { id, programs } = config.body
  88. const index = db.findIndex(item => item.id === id)
  89. if (~index) {
  90. db[index].timestamp = Random.now('yyyy-MM-dd HH:mm:ss')
  91. db[index].programs = programs
  92. db.unshift(db.splice(index, 1)[0])
  93. save()
  94. }
  95. return {
  96. success: true,
  97. }
  98. }
  99. }
  100. ]