xiaoyong931011
2022-07-08 fe87cd1b8168b9ef6f89e80bc5c1d6a5757af3c8
src/main/java/com/xcong/farmer/cms/modules/system/service/Impl/CmsTemplateServiceImpl.java
@@ -1,6 +1,7 @@
package com.xcong.farmer.cms.modules.system.service.Impl;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.file.FileWriter;
import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.ZipUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
@@ -8,9 +9,10 @@
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xcong.farmer.cms.common.exception.GlobalException;
import com.xcong.farmer.cms.common.response.Result;
import com.xcong.farmer.cms.common.utils.FileUtils;
import com.xcong.farmer.cms.configurations.GlobalExceptionHandler;
import com.xcong.farmer.cms.core.template.TemplateConfiguration;
import com.xcong.farmer.cms.configurations.properties.CmsProperties;
import com.xcong.farmer.cms.modules.system.dto.AdminSaveTemplateInfoDto;
import com.xcong.farmer.cms.modules.system.dto.TemplateListDto;
import com.xcong.farmer.cms.modules.system.entity.CmsTemplateEntity;
import com.xcong.farmer.cms.modules.system.mapper.CmsTemplateMapper;
@@ -20,11 +22,14 @@
import org.jsoup.nodes.Document;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.List;
@@ -36,14 +41,15 @@
public class CmsTemplateServiceImpl extends ServiceImpl<CmsTemplateMapper, CmsTemplateEntity> implements ICmsTemplateService {
    @Autowired
    private TemplateConfiguration cfg;
    private CmsProperties properties;
    private List<String> fileSuffix = Arrays.asList(".zip", ".html");
    @Override
    public void updateTemplate(MultipartFile upload) {
        String templatePath = cfg.templatePath;
        String staticPath = cfg.staticPath;
        String templatePath = properties.getTemplatePath();
        String staticPath = properties.getStaticPath();
        Long companyId = LoginUserUtil.getCompanyId();
        String filename = upload.getOriginalFilename();
        String suffix = filename.substring(filename.lastIndexOf("."));
@@ -69,6 +75,7 @@
                for (File templateFile : files) {
                    if (!templateFile.isFile()) {
                        FileUtil.move(templateFile, new File(staticPath), true);
                        continue;
                    }
@@ -77,14 +84,14 @@
                    }
                    String name = templateFile.getName();
                    if (!name.endsWith(".list.html") && !name.endsWith(".article.html") && !name.endsWith(".index.html")) {
                    if (!name.endsWith(".list.html") && !name.endsWith(".article.html") && !name.endsWith("index.html")) {
                        continue;
                    }
                    Document parse = Jsoup.parse(templateFile, null);
                    String attr = parse.head().attr("name");
                    CmsTemplateEntity cmsTemplate = new CmsTemplateEntity();
                    cmsTemplate.setCompanyId(10L);
                    cmsTemplate.setCompanyId(companyId);
                    if (name.endsWith(".list.html")) {
                        cmsTemplate.setType(2);
                    } else if (name.endsWith(".article.html")) {
@@ -106,7 +113,7 @@
                Document parse = Jsoup.parse(file, null);
                String attr = parse.head().attr("name");
                CmsTemplateEntity cmsTemplate = new CmsTemplateEntity();
                cmsTemplate.setCompanyId(LoginUserUtil.getCompanyId());
                cmsTemplate.setCompanyId(companyId);
                if (file.getName().endsWith(".list.html")) {
                    cmsTemplate.setType(2);
                } else if (file.getName().endsWith(".article.html")) {
@@ -139,4 +146,67 @@
        Long companyId = LoginUserUtil.getCompanyId();
        this.baseMapper.delete(id, companyId);
    }
    @Override
    public Result dropdownList() {
        Long companyId = LoginUserUtil.getCompanyId();
        QueryWrapper<CmsTemplateEntity> objectQueryWrapper = new QueryWrapper<>();
        objectQueryWrapper.eq("company_id",companyId);
        List<CmsTemplateEntity> cmsTemplateEntities = this.baseMapper.selectList(objectQueryWrapper);
        return Result.ok(cmsTemplateEntities);
    }
    @Autowired
    private CmsProperties cmsProperties;
    @Override
    public Result viewTemplateInfo(Long id) {
        Result result = new Result();
        CmsTemplateEntity cmsTemplateEntity = this.baseMapper.selectById(id);
        String templatePath = cmsProperties.getTemplatePath();
        String pathName = FileUtils.path(templatePath, cmsTemplateEntity.getPath());
        byte[] bytes = new byte[0];
        try {
            bytes = Files.readAllBytes(Paths.get(pathName));
        } catch (IOException e) {
            e.printStackTrace();
            return result.fail("未找到模板");
        }
        String content = new String(bytes, StandardCharsets.UTF_8);
        result.setData(content);
        return result;
    }
    @Override
    @Transactional
    public Result saveTemplateInfo(AdminSaveTemplateInfoDto adminSaveTemplateInfoDto) {
        Long companyId = LoginUserUtil.getCompanyId();
        Long id = adminSaveTemplateInfoDto.getId();
        CmsTemplateEntity cmsTemplateEntity = this.baseMapper.selectByIdAndCompanyId(id,companyId);
        String name = cmsTemplateEntity.getName();
        Integer type = cmsTemplateEntity.getType();
        String templatePath = cmsProperties.getTemplatePath();
        String path = cmsTemplateEntity.getPath();
        this.baseMapper.delete(id,companyId);
        String pathNew = FileUtils.path(templatePath, path);
        File file = new File(pathNew);
        FileUtil.touch(file);
        //文件写入,直接覆盖
        FileWriter writer = new FileWriter(file);
        writer.write(adminSaveTemplateInfoDto.getTemplateInfo(), false);
        CmsTemplateEntity cmsTemplate = new CmsTemplateEntity();
        cmsTemplate.setCompanyId(companyId);
        cmsTemplate.setType(type);
        cmsTemplate.setName(name);
        cmsTemplate.setPath(path);
        this.baseMapper.insert(cmsTemplate);
        return Result.ok("保存成功");
    }
}