banner.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. const express = require('express');
  2. const _ = require('lodash');
  3. const { query, body } = require('express-validator');
  4. const { onSuccess, fill } = require('../../../utils');
  5. const { limit } = require('../../config');
  6. const check = require('../../../middleware/check');
  7. const Banner = require('../../../db/models/banner');
  8. const router = express.Router();
  9. async function fillSingle(data) {
  10. return data;
  11. }
  12. /**
  13. * showdoc
  14. * @catalog v1/焦点图
  15. * @title 焦点图列表
  16. * @description 焦点图列表接口
  17. * @method GET
  18. * @url /master/banner/
  19. * @param page 0 Int 页码: 不传默认取第1页数据,每页20条数据
  20. * @return {"code":0, "count":1, "data": []}
  21. * @return_param id integer id
  22. * @return_param name string 名
  23. * @return_param img string 图片
  24. * @return_param href string 地址
  25. * @return_param status string 状态
  26. * @return_param createdAt date -
  27. * @return_param updatedAt date -
  28. * @remark
  29. * @number
  30. */
  31. router.get('/', async (req, res) => {
  32. const { page = 0 } = req.query;
  33. const where = _.omit(req.query, ['page']);
  34. const info = onSuccess();
  35. info.data = await Banner.findAll({ where, limit, offset: page * limit });
  36. info.data = await fill(info.data, fillSingle);
  37. if (!page) {
  38. info.count = await Banner.count({ where });
  39. }
  40. res.send(info);
  41. });
  42. /**
  43. * showdoc
  44. * @catalog v1/焦点图
  45. * @title 焦点图添加
  46. * @description
  47. * @method POST
  48. * @url /master/banner/
  49. * @param name string 名
  50. * @param img string 图片
  51. * @param href string 地址
  52. * @param status string 状态
  53. * @return {"code":0, "data": {}}
  54. * @remark
  55. * @number
  56. */
  57. router.post('/', check([body([]).notEmpty()]), async (req, res) => {
  58. const data = await Banner.create(req.body);
  59. res.send(onSuccess(data));
  60. });
  61. /**
  62. * showdoc
  63. * @catalog v1/焦点图
  64. * @title 焦点图排序
  65. * @description
  66. * @method PUT
  67. * @url /master/banner/sort
  68. * @param data 1 array idlist
  69. * @return {"code":0, "msg": "修改成功"}
  70. * @remark
  71. * @number
  72. */
  73. router.put('/sort', async (req, res) => {
  74. for (const i in req.body.data) {
  75. await Banner.update({ weight: i }, { where: { id: req.body.data[i] }, limit: 1 });
  76. }
  77. res.send(onSuccess(null, '修改成功'));
  78. });
  79. /**
  80. * showdoc
  81. * @catalog v1/焦点图
  82. * @title 焦点图修改
  83. * @description
  84. * @method PUT
  85. * @url /master/banner/:id
  86. * @param :id 1 STRING 详情ID
  87. * @param name string 名
  88. * @param img string 图片
  89. * @param href string 地址
  90. * @param status string 状态
  91. * @return {"code":0, "msg": "修改成功"}
  92. * @remark
  93. * @number
  94. */
  95. router.put('/:id', async (req, res) => {
  96. const data = await Banner.update(req.body, { where: req.params, limit: 1 });
  97. res.send(onSuccess(null, '修改成功'));
  98. });
  99. /**
  100. * showdoc
  101. * @catalog v1/焦点图
  102. * @title 焦点图删除
  103. * @description
  104. * @method DELETE
  105. * @url /master/banner/:id
  106. * @param :id 1 STRING 详情ID
  107. * @return {"status":0, "msg": "删除成功"}
  108. * @remark
  109. * @number
  110. */
  111. router.delete('/:id', async (req, res) => {
  112. const data = await Banner.destroy({ where: req.params, limit: 1 });
  113. res.send(onSuccess(null, '删除成功'));
  114. });
  115. module.exports = router;