12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- const pagesService = require('../service/pages.service');
- const projectsService = require('../service/projects.service');
- const templates = require('../service/templates.service');
- const util = require('../utils/util');
- module.exports = {
- async list(ctx) {
- const { pageNum = 1, pageSize = 10, type = 1, keyword = '' } = ctx.request.query;
- const { total } = await templates.listCount(type, keyword);
- if (total == 0) {
- return util.success(ctx, {
- list: [],
- total: 0,
- pageSize: +pageSize || 10,
- pageNum: +pageNum || 1,
- });
- }
- const list = await templates.list(pageNum, pageSize, type, keyword);
- util.success(ctx, {
- list,
- total,
- pageSize: +pageSize,
- pageNum: +pageNum,
- });
- },
- async detail(ctx) {
- const { id } = ctx.request.params;
- if (!util.isNotEmpty(id)) {
- return ctx.throw(400, '模板id不能为空');
- }
- const { userId } = util.decodeToken(ctx);
- const [result = {}] = await templates.getDetailById(+id, userId);
- util.success(ctx, result);
- },
- async create(ctx) {
- const { unionId, type, name, description, imageUrl, tags } = ctx.request.body;
- const { userId, userName } = util.decodeToken(ctx);
- if (!util.isNumber(unionId)) {
- return ctx.throw(400, 'unionId参数异常');
- }
- if (!util.isNumber(type)) {
- return ctx.throw(400, 'type参数异常');
- }
- if (!name || !description) {
- return ctx.throw(400, '模板描述不能为空');
- }
- if (!imageUrl) {
- return ctx.throw(400, '请上传模板封面');
- }
- if (!Array.isArray(tags)) {
- return ctx.throw(400, '模板标签格式异常');
- }
- if (tags.length === 0) {
- return ctx.throw(400, '模板标签不能为空');
- }
- const tags_arr = tags.join(',');
- await templates.create({ ...ctx.request.body, unionId, tags: tags_arr, userId, userName });
- if (type == 1) {
- projectsService.updateProjectTemplate(unionId, 1);
- } else if (type == 2) {
- pagesService.updatePageTemplate(unionId, 1);
- }
- util.success(ctx);
- },
- async delete(ctx) {
- const { unionId, type } = ctx.request.body;
- if (!util.isNumber(unionId)) {
- return ctx.throw(400, '[unionId]参数错误');
- }
- if (!util.isNumber(type)) {
- return ctx.throw(400, '[type]参数错误');
- }
- const { userId } = util.decodeToken(ctx);
- await templates.deleteById(unionId, userId);
- if (type == 1) {
- projectsService.updateProjectTemplate(unionId, 0);
- } else if (type == 2) {
- pagesService.updatePageTemplate(unionId, 0);
- }
- util.success(ctx);
- },
- async update(ctx) {
- const { id, name, description, isRecommend, imageUrl, installCount } = ctx.request.body;
- if (!util.isNumber(id)) {
- return ctx.throw(400, '组件id不正确');
- }
- const { userId } = util.decodeToken(ctx);
- await templates.update(id, name, description, isRecommend, imageUrl, installCount, userId);
- util.success(ctx);
- },
- };
|