| | |
| | | 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.entity.CompanyEntity; |
| | | import com.xcong.farmer.cms.modules.system.mapper.CmsTemplateMapper; |
| | | import com.xcong.farmer.cms.modules.system.mapper.CompanyMapper; |
| | | import com.xcong.farmer.cms.modules.system.service.ICmsTemplateService; |
| | | import com.xcong.farmer.cms.modules.system.util.LoginUserUtil; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.jsoup.Jsoup; |
| | | import org.jsoup.nodes.Document; |
| | | import org.jsoup.nodes.Element; |
| | |
| | | import org.springframework.web.multipart.MultipartFile; |
| | | |
| | | import java.io.File; |
| | | import java.io.FileOutputStream; |
| | | import java.io.IOException; |
| | | import java.nio.charset.StandardCharsets; |
| | | import java.nio.file.Files; |
| | |
| | | * @author wzy |
| | | * @date 2022-07-04 |
| | | **/ |
| | | @Slf4j |
| | | @Service |
| | | public class CmsTemplateServiceImpl extends ServiceImpl<CmsTemplateMapper, CmsTemplateEntity> implements ICmsTemplateService { |
| | | |
| | | @Autowired |
| | | private CmsProperties properties; |
| | | |
| | | @Autowired |
| | | private CompanyMapper companyMapper; |
| | | |
| | | |
| | | private List<String> fileSuffix = Arrays.asList(".zip", ".html"); |
| | | |
| | |
| | | String templatePath = properties.getTemplatePath(); |
| | | String staticPath = properties.getStaticPath(); |
| | | Long companyId = LoginUserUtil.getCompanyId(); |
| | | CompanyEntity company = companyMapper.selectById(companyId); |
| | | |
| | | String companyCode = company.getCode(); |
| | | templatePath = FileUtils.path(templatePath, companyCode); |
| | | staticPath = FileUtils.path(staticPath, companyCode); |
| | | |
| | | String filename = upload.getOriginalFilename(); |
| | | String suffix = filename.substring(filename.lastIndexOf(".")); |
| | |
| | | continue; |
| | | } |
| | | |
| | | insertTemplate(templateFile, companyId); |
| | | insertTemplate(templateFile, companyId, companyCode); |
| | | } |
| | | } |
| | | |
| | | if (".html".equals(suffix)) { |
| | | FileUtil.touch(file); |
| | | |
| | | insertTemplate(file, companyId); |
| | | insertTemplate(file, companyId, companyCode); |
| | | } |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | |
| | | } |
| | | } |
| | | |
| | | private void insertTemplate(File file, Long companyId) throws IOException { |
| | | private void insertTemplate(File file, Long companyId, String companyCode) throws IOException { |
| | | Document parse = Jsoup.parse(file, null); |
| | | String attr = parse.head().attr("name"); |
| | | |
| | | staticPathParser(parse, "img", "src"); |
| | | staticPathParser(parse, "href", "link"); |
| | | staticPathParser(parse, "script", "src"); |
| | | staticPathParser(parse, "img", "src", companyCode); |
| | | staticPathParser(parse, "link", "href", companyCode); |
| | | staticPathParser(parse, "script", "src", companyCode); |
| | | |
| | | FileOutputStream outputStream = new FileOutputStream(file); |
| | | outputStream.write(parse.html().getBytes()); |
| | | outputStream.close(); |
| | | |
| | | CmsTemplateEntity cmsTemplate = new CmsTemplateEntity(); |
| | | cmsTemplate.setCompanyId(companyId); |
| | |
| | | this.baseMapper.insert(cmsTemplate); |
| | | } |
| | | |
| | | private void staticPathParser(Document document, String tagName, String attrKey) { |
| | | private void staticPathParser(Document document, String tagName, String attrKey, String companyCode) { |
| | | Elements elements = document.getElementsByTag(tagName); |
| | | if (elements.isEmpty()) { |
| | | return; |
| | |
| | | for (Element element : elements) { |
| | | String attr = element.attr(attrKey); |
| | | if (StrUtil.isNotBlank(attr) && !attr.contains("http://") && !attr.contains("https://")) { |
| | | element.attr(attrKey, cmsProperties.getBaseUrl() + attr); |
| | | element.attr(attrKey, cmsProperties.getStaticUrl() + companyCode + "/" + attr); |
| | | } |
| | | } |
| | | } |
| | |
| | | public Result dropdownList() { |
| | | Long companyId = LoginUserUtil.getCompanyId(); |
| | | QueryWrapper<CmsTemplateEntity> objectQueryWrapper = new QueryWrapper<>(); |
| | | objectQueryWrapper.eq("company_id",companyId); |
| | | objectQueryWrapper.eq("company_id", companyId); |
| | | List<CmsTemplateEntity> cmsTemplateEntities = this.baseMapper.selectList(objectQueryWrapper); |
| | | return Result.ok(cmsTemplateEntities); |
| | | } |
| | |
| | | @Override |
| | | public Result viewTemplateInfo(Long id) { |
| | | Result result = new Result(); |
| | | |
| | | CompanyEntity companyEntity = companyMapper.selectById(LoginUserUtil.getCompanyId()); |
| | | CmsTemplateEntity cmsTemplateEntity = this.baseMapper.selectById(id); |
| | | String templatePath = cmsProperties.getTemplatePath(); |
| | | String templatePath = FileUtils.path(cmsProperties.getTemplatePath(), companyEntity.getCode()); |
| | | String pathName = FileUtils.path(templatePath, cmsTemplateEntity.getPath()); |
| | | |
| | | byte[] bytes = new byte[0]; |
| | |
| | | public Result saveTemplateInfo(AdminSaveTemplateInfoDto adminSaveTemplateInfoDto) { |
| | | Long companyId = LoginUserUtil.getCompanyId(); |
| | | Long id = adminSaveTemplateInfoDto.getId(); |
| | | CmsTemplateEntity cmsTemplateEntity = this.baseMapper.selectByIdAndCompanyId(id,companyId); |
| | | |
| | | CompanyEntity company = this.companyMapper.selectById(companyId); |
| | | CmsTemplateEntity cmsTemplateEntity = this.baseMapper.selectByIdAndCompanyId(id, companyId); |
| | | String name = cmsTemplateEntity.getName(); |
| | | Integer type = cmsTemplateEntity.getType(); |
| | | String templatePath = cmsProperties.getTemplatePath(); |
| | | String templatePath = FileUtils.path(cmsProperties.getTemplatePath(), company.getCode()); |
| | | String path = cmsTemplateEntity.getPath(); |
| | | |
| | | this.baseMapper.delete(id,companyId); |
| | | this.baseMapper.delete(id, companyId); |
| | | String pathNew = FileUtils.path(templatePath, path); |
| | | log.info("模板写入地址:{}", pathNew); |
| | | File file = new File(pathNew); |
| | | FileUtil.touch(file); |
| | | |