index.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. const { Random } = require('mockjs')
  2. const { getDB, saveDB } = require('../utils')
  3. const db = getDB('program')
  4. function save () {
  5. saveDB('program', db)
  6. }
  7. module.exports = [
  8. {
  9. url: '/item/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: '/item/getById/:id',
  30. type: 'get',
  31. response: config => {
  32. const { id } = config.params
  33. const program = db.find(item => item.id === id)
  34. if (!program) {
  35. return {
  36. success: false,
  37. errMessage: '节目不存在'
  38. }
  39. }
  40. return {
  41. success: true,
  42. data: program
  43. }
  44. }
  45. },
  46. {
  47. url: '/item/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. createTime: Random.now('yyyy-MM-dd HH:mm:ss')
  56. }
  57. db.unshift(layout)
  58. save()
  59. return {
  60. success: true,
  61. data: layout.id
  62. }
  63. }
  64. },
  65. {
  66. url: '/item/delete',
  67. type: 'get',
  68. response: config => {
  69. const { id } = config.query
  70. const index = db.findIndex(item => item.id === id)
  71. if (~index) {
  72. db.splice(index, 1)
  73. save()
  74. }
  75. return {
  76. success: true
  77. }
  78. }
  79. },
  80. {
  81. url: '/item/update',
  82. type: 'post',
  83. response: config => {
  84. const { id, itemJsonStr } = config.body
  85. const index = db.findIndex(item => item.id === id)
  86. if (~index) {
  87. db[index].itemJsonStr = itemJsonStr
  88. db.unshift(db.splice(index, 1)[0])
  89. save()
  90. }
  91. return {
  92. success: true
  93. }
  94. }
  95. }
  96. ]