|
@@ -0,0 +1,227 @@
|
|
|
+package com.caimei.www.service.generate.impl;
|
|
|
+
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.caimei.www.pojo.page.BaseLink;
|
|
|
+import com.caimei.www.pojo.page.ImageLink;
|
|
|
+import com.caimei.www.pojo.page.TopMenu;
|
|
|
+import com.caimei.www.service.generate.GenerateHtml;
|
|
|
+import com.caimei.www.service.page.BaseService;
|
|
|
+import com.caimei.www.utils.RequestUtil;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.commons.io.FileUtils;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
+import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
|
|
|
+import org.springframework.core.io.support.ResourcePatternResolver;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.web.server.ServerWebExchange;
|
|
|
+import org.thymeleaf.TemplateEngine;
|
|
|
+import org.thymeleaf.spring5.context.webflux.SpringWebFluxContext;
|
|
|
+import org.thymeleaf.util.StringUtils;
|
|
|
+
|
|
|
+import javax.annotation.Resource;
|
|
|
+import java.io.File;
|
|
|
+import java.io.IOException;
|
|
|
+import java.io.InputStream;
|
|
|
+import java.io.PrintWriter;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+/**
|
|
|
+ * Description
|
|
|
+ *
|
|
|
+ * @author : Charles
|
|
|
+ * @date : 2021/5/7
|
|
|
+ */
|
|
|
+@Slf4j
|
|
|
+@Service
|
|
|
+public class GenerateHtmlImpl implements GenerateHtml {
|
|
|
+ @Value("${caimei.destPath}")
|
|
|
+ private String destPath;
|
|
|
+ @Value("${caimei.coreServer}")
|
|
|
+ private String coreServer;
|
|
|
+ @Value("${caimei.spiServer}")
|
|
|
+ private String spiServer;
|
|
|
+ /** 打包时间 */
|
|
|
+ @Value("${caimei.siteEnv}")
|
|
|
+ private String siteEnv;
|
|
|
+ /** 打包时间 */
|
|
|
+ @Value("${spring.application.build-time}")
|
|
|
+ private String buildTime;
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private TemplateEngine templateEngine;
|
|
|
+ @Resource
|
|
|
+ private BaseService baseService;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 生成静态首页
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public String generateStaticHome(ServerWebExchange exchange) {
|
|
|
+ // 上下文
|
|
|
+ SpringWebFluxContext context = new SpringWebFluxContext(exchange);
|
|
|
+ // 设置页面数据
|
|
|
+ context.setVariables(setStaticHomeData());
|
|
|
+ // 输出流
|
|
|
+ File dest = new File(destPath, "index.html");
|
|
|
+ if (dest.exists()) {
|
|
|
+ boolean delete = dest.delete();
|
|
|
+ }
|
|
|
+ try (PrintWriter writer = new PrintWriter(dest, "UTF-8")) {
|
|
|
+ // 生成html
|
|
|
+ templateEngine.process("index", context, writer);
|
|
|
+ log.info("[静态页服务]:生成静态首页成功! ^_^");
|
|
|
+ return "[静态页服务]:生成静态首页成功! ^_^";
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("[静态页服务]:生成静态首页异常!", e);
|
|
|
+ return "[静态页服务]:生成静态首页异常!"+e.toString();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 删除静态页
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public String deleteStaticHome(String pageName) {
|
|
|
+ // 输出流
|
|
|
+ File dest = new File(destPath, pageName + ".html");
|
|
|
+ if (dest.exists()) {
|
|
|
+ boolean delete = dest.delete();
|
|
|
+ if (delete) {
|
|
|
+ return "[静态页服务]:删除静态页成功! ^_^";
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return "[静态页服务]:删除静态页失败!";
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 拷贝静态资源文件
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public String generateStaticFiles() {
|
|
|
+ // 拷贝静态资源文件
|
|
|
+ try {
|
|
|
+ copyResourceToFile();
|
|
|
+ log.info("[静态页服务]:拷贝静态资源文件成功! ^_^");
|
|
|
+ return "[静态页服务]:拷贝静态资源文件成功! ^_^";
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("[静态页服务]:拷贝静态资源文件异常!", e);
|
|
|
+ return "[静态页服务]:拷贝静态资源文件异常!"+e.toString();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 设置静态首页数据
|
|
|
+ */
|
|
|
+ private Map<String, Object> setStaticHomeData() {
|
|
|
+ // 这里加载的数据是从数据库查询出来的,demo就写固定了
|
|
|
+ Map<String, Object> map = new HashMap<>();
|
|
|
+
|
|
|
+ // 环境变量,(2:正式环境,1:测试环境,0:开发环境)
|
|
|
+ map.put("siteEnv", siteEnv);
|
|
|
+ map.put("agent", "");
|
|
|
+ // 静态文件版本号
|
|
|
+ map.put("version", buildTime);
|
|
|
+ // spi服务器地址
|
|
|
+ map.put("coreServer", coreServer);
|
|
|
+ map.put("spiServer", spiServer);
|
|
|
+ // 搜索热门关键字
|
|
|
+ List<String> searchHotWord = baseService.getSearchHotWord();
|
|
|
+ map.put("searchHotWord", searchHotWord);
|
|
|
+ // 头部菜单
|
|
|
+ List<TopMenu> menuList = baseService.getNavMenu();
|
|
|
+ map.put("topMenuList", menuList);
|
|
|
+ // 获取banner图
|
|
|
+ List<ImageLink> bannerList = baseService.getHomeBanners();
|
|
|
+ map.put("bannerList", bannerList);
|
|
|
+ // 获取首页楼层
|
|
|
+ String floor = null;
|
|
|
+ try {
|
|
|
+ String floorResult = RequestUtil.sendGet(coreServer+"/commodity/home/floor?userId=0&source=1");
|
|
|
+ log.debug(floorResult);
|
|
|
+ Map<String, Object> floorMap = JSONObject.parseObject(floorResult, Map.class);
|
|
|
+ floor = floorMap.get("data").toString();
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("try-catch:",e);
|
|
|
+ }
|
|
|
+ map.put("floorJson", floor);
|
|
|
+ // 底部帮助页
|
|
|
+ List<BaseLink> helpPages = baseService.getHelpPages();
|
|
|
+ map.put("helpPages", helpPages);
|
|
|
+ // 友情链接
|
|
|
+ List<BaseLink> friendLinks = baseService.getFriendLinks();
|
|
|
+ map.put("friendLinks", friendLinks);
|
|
|
+
|
|
|
+ log.debug(map.toString());
|
|
|
+
|
|
|
+ return map;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 资源清单获取并拷贝
|
|
|
+ */
|
|
|
+ private void copyResourceToFile() throws IOException {
|
|
|
+ // 资源清单获取
|
|
|
+ ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
|
|
|
+ org.springframework.core.io.Resource[] resources = resolver.getResources("static/**");
|
|
|
+ for (org.springframework.core.io.Resource resource : resources) {
|
|
|
+ String fileName = resource.getFilename();
|
|
|
+ assert fileName != null;
|
|
|
+ if (fileName.indexOf(".") > 0) {
|
|
|
+ InputStream inputStream = null;
|
|
|
+ try {
|
|
|
+ inputStream = resource.getInputStream();
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.warn(String.format("[%s]获取输入流发生异常!", resource.getURL()));
|
|
|
+ throw new RuntimeException(String.format("[%s]获取输入流发生异常!", resource.getURL()));
|
|
|
+ }
|
|
|
+ //分析相对目录
|
|
|
+ String tempPath = "";
|
|
|
+ String[] urls = resource.getURL().toString().split("/static/");
|
|
|
+ if (urls.length >= 2 ){
|
|
|
+ tempPath = urls[urls.length-1];
|
|
|
+ } else {
|
|
|
+ throw new RuntimeException("relativeRootPath有误:无法分析相对目录");
|
|
|
+ }
|
|
|
+ tempPath = tempPath.substring(0, tempPath.length() - fileName.length());
|
|
|
+ if (StringUtils.isEmpty(tempPath)) {
|
|
|
+ tempPath = File.separator;
|
|
|
+ }
|
|
|
+ String filePath = destPath + File.separator + tempPath;
|
|
|
+ if (createDir(filePath)) {
|
|
|
+ String destName = filePath + fileName;
|
|
|
+ // 输出流
|
|
|
+ File dest = new File(destName);
|
|
|
+ if (dest.exists()) {
|
|
|
+ boolean delete = dest.delete();
|
|
|
+ }
|
|
|
+ FileUtils.copyInputStreamToFile(inputStream, dest);
|
|
|
+ } else {
|
|
|
+ throw new RuntimeException(String.format("创建本地目录[%s]失败!", resource.getURL()));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 创建目录
|
|
|
+ */
|
|
|
+ private static boolean createDir(String dirName) {
|
|
|
+ File dir = new File(dirName);
|
|
|
+ if (dir.exists()) {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ if (!dirName.endsWith(File.separator)) {
|
|
|
+ dirName = dirName + File.separator;
|
|
|
+ }
|
|
|
+ if (dir.mkdirs()) {
|
|
|
+ log.warn("创建目录" + dirName + "成功!");
|
|
|
+ return true;
|
|
|
+ } else {
|
|
|
+ log.warn("创建目录" + dirName + "失败!");
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+}
|