templates.controller.js 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. const pagesService = require('../service/pages.service');
  2. const projectsService = require('../service/projects.service');
  3. const templates = require('../service/templates.service');
  4. const util = require('../utils/util');
  5. module.exports = {
  6. async list(ctx) {
  7. const { pageNum = 1, pageSize = 10, type = 1, keyword = '' } = ctx.request.query;
  8. const { total } = await templates.listCount(type, keyword);
  9. if (total == 0) {
  10. return util.success(ctx, {
  11. list: [],
  12. total: 0,
  13. pageSize: +pageSize || 10,
  14. pageNum: +pageNum || 1,
  15. });
  16. }
  17. const list = await templates.list(pageNum, pageSize, type, keyword);
  18. util.success(ctx, {
  19. list,
  20. total,
  21. pageSize: +pageSize,
  22. pageNum: +pageNum,
  23. });
  24. },
  25. async detail(ctx) {
  26. const { id } = ctx.request.params;
  27. if (!util.isNotEmpty(id)) {
  28. return ctx.throw(400, '模板id不能为空');
  29. }
  30. const { userId } = util.decodeToken(ctx);
  31. const [result = {}] = await templates.getDetailById(+id, userId);
  32. util.success(ctx, result);
  33. },
  34. async create(ctx) {
  35. const { unionId, type, name, description, imageUrl, tags } = ctx.request.body;
  36. const { userId, userName } = util.decodeToken(ctx);
  37. if (!util.isNumber(unionId)) {
  38. return ctx.throw(400, 'unionId参数异常');
  39. }
  40. if (!util.isNumber(type)) {
  41. return ctx.throw(400, 'type参数异常');
  42. }
  43. if (!name || !description) {
  44. return ctx.throw(400, '模板描述不能为空');
  45. }
  46. if (!imageUrl) {
  47. return ctx.throw(400, '请上传模板封面');
  48. }
  49. if (!Array.isArray(tags)) {
  50. return ctx.throw(400, '模板标签格式异常');
  51. }
  52. if (tags.length === 0) {
  53. return ctx.throw(400, '模板标签不能为空');
  54. }
  55. const tags_arr = tags.join(',');
  56. await templates.create({ ...ctx.request.body, unionId, tags: tags_arr, userId, userName });
  57. if (type == 1) {
  58. projectsService.updateProjectTemplate(unionId, 1);
  59. } else if (type == 2) {
  60. pagesService.updatePageTemplate(unionId, 1);
  61. }
  62. util.success(ctx);
  63. },
  64. async delete(ctx) {
  65. const { unionId, type } = ctx.request.body;
  66. if (!util.isNumber(unionId)) {
  67. return ctx.throw(400, '[unionId]参数错误');
  68. }
  69. if (!util.isNumber(type)) {
  70. return ctx.throw(400, '[type]参数错误');
  71. }
  72. const { userId } = util.decodeToken(ctx);
  73. await templates.deleteById(unionId, userId);
  74. if (type == 1) {
  75. projectsService.updateProjectTemplate(unionId, 0);
  76. } else if (type == 2) {
  77. pagesService.updatePageTemplate(unionId, 0);
  78. }
  79. util.success(ctx);
  80. },
  81. async update(ctx) {
  82. const { id, name, description, isRecommend, imageUrl, installCount } = ctx.request.body;
  83. if (!util.isNumber(id)) {
  84. return ctx.throw(400, '组件id不正确');
  85. }
  86. const { userId } = util.decodeToken(ctx);
  87. await templates.update(id, name, description, isRecommend, imageUrl, installCount, userId);
  88. util.success(ctx);
  89. },
  90. };