package com.caimei.www.controller.unlimited; import com.caimei.www.controller.BaseController; import com.caimei.www.pojo.JsonModel; import com.caimei.www.pojo.link.CmBrandLandingVO; import com.caimei.www.pojo.page.*; import com.caimei.www.service.page.ArticleService; import com.caimei.www.service.page.CmBrandLandingService; import com.caimei.www.service.page.ProductService; import com.github.pagehelper.PageInfo; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpHeaders; import org.springframework.http.server.reactive.ServerHttpRequest; import org.springframework.http.server.reactive.ServerHttpResponse; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.*; import org.springframework.web.server.ServerWebExchange; import org.thymeleaf.util.StringUtils; import java.lang.reflect.Array; import java.text.SimpleDateFormat; import java.util.Arrays; import java.util.Date; import java.util.List; /** * 文章页鉴于seo,保留旧链接 * * @author : Charles * @date : 2020/7/31 */ @Slf4j @Controller public class ArticleController extends BaseController { /** * 错误页面 */ private static final String ERROR_PATH = "article/error"; private static final String ARTICLE_LIST_PATH = "article/list"; private static final String ARTICLE_DETAIL_PATH = "article/detail"; private static final String ARTICLE_RECOMMENDATION = "article/recommendation"; private ArticleService articleService; @Autowired private CmBrandLandingService cmBrandLandingService; @Autowired private ProductService productService; @Autowired public void setArticleService(ArticleService articleService) { this.articleService = articleService; } /** * 文章列表【旧center】 */ @GetMapping("/info/center-{id}-{pageNum}.html") public String toArticleList(@PathVariable("id") Integer id, @PathVariable("pageNum") Integer pageNum, final Model model) { List typeList = articleService.getArticleTypes(); List cmBrandLandingList = cmBrandLandingService.getCmBrandLandingList(new CmBrandLanding() .setType("4").setAuthorId(id.toString())); List productSellNumbers = productService.getProductSellNumbers(); List data = articleService.getArticleLabels().getData(); List Ads = articleService.getLastestInfoAds().getData(); model.addAttribute("ads", Ads); model.addAttribute("labels", data); model.addAttribute("articleType", typeList); model.addAttribute("articleImages", cmBrandLandingList); model.addAttribute("productSellNumbers", productSellNumbers); model.addAttribute("typeId", id); model.addAttribute("labelId", 0); model.addAttribute("pageNum", pageNum); model.addAttribute("isSearch", true); System.out.println("articleImages" + cmBrandLandingList); System.out.println("productSellNumbers" + productSellNumbers); //model.addAttribute("pagePath", String.format("/info/center-%s-1.html", id)); return ARTICLE_LIST_PATH; } /** * 文章列表【旧label】 */ @GetMapping("/info/label-{id}-{pageNum}.html") public String toArticleLabel(@PathVariable("id") Integer id, @PathVariable("pageNum") Integer pageNum, final Model model) { List typeList = articleService.getArticleTypes(); List data = articleService.getArticleLabels().getData(); List Ads = articleService.getLastestInfoAds().getData(); model.addAttribute("ads", Ads); model.addAttribute("labels", data); model.addAttribute("articleType", typeList); model.addAttribute("typeId", 0); model.addAttribute("labelId", id); model.addAttribute("pageNum", pageNum); model.addAttribute("isSearch", false); //model.addAttribute("pagePath", String.format("/info/label-%s-1.html", id)); return ARTICLE_LIST_PATH; } /** * 文章搜索结果 */ @GetMapping("/info/search-{pageNum}.html") public String toArticleSearch(@PathVariable("pageNum") Integer pageNum, final Model model) { List typeList = articleService.getArticleTypes(); List data = articleService.getArticleLabels().getData(); List Ads = articleService.getLastestInfoAds().getData(); model.addAttribute("ads", Ads); model.addAttribute("labels", data); model.addAttribute("articleType", typeList); model.addAttribute("typeId", 0); model.addAttribute("labelId", 0); model.addAttribute("pageNum", pageNum); model.addAttribute("isSearch", false); //model.addAttribute("pagePath", "/info/search.html"); return ARTICLE_LIST_PATH; } /** * 文章详情【旧】 */ @GetMapping("/info/detail-{id}-1.html") public String toArticleDetail(@PathVariable("id") Integer id, final Model model, ServerWebExchange serverWebExchange, ServerHttpResponse response) { List typeList = articleService.getArticleTypes(); Article article = articleService.getArticleInfo(id); if (article == null) { // return super.errorPath(); return "redirect:/info/404.html"; } // 获取referer 判断访问来源 ServerHttpRequest request = serverWebExchange.getRequest(); String ip = ""; String referer = ""; String source = ""; HttpHeaders headers = request.getHeaders(); ip = String.valueOf(headers.get("x-forwarded-for")); if (StringUtils.isEmpty(ip)) { ip = String.valueOf(headers.get("X-Real-IP")); } if (null != ip && "" != ip) { ip = ip.replaceAll("[^\\d.]", ""); } List referer1 = headers.get("Referer"); if (referer1 != null) { referer = referer1.get(0); } String subReferer = ""; if (referer.length() > 200) { subReferer = referer.substring(0, 190); } source = source(referer); SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String format = dateFormat.format(new Date()); articleService.insertRecord(ip, subReferer, source, format); log.info("文章详情访问来源记录完成========》" + source); List data = articleService.getArticleLabels().getData(); List Ads = articleService.getLastestInfoAds().getData(); List
articles = articleService.getInfoById(id); String relatedLabels = articleService.relatedLabel(id); model.addAttribute("articles", articles); model.addAttribute("ads", Ads); model.addAttribute("labels", data); model.addAttribute("article", article); model.addAttribute("articleType", typeList); model.addAttribute("articleId", id); model.addAttribute("relatedLabels", relatedLabels); model.addAttribute("typeId", (article != null ? article.getTypeId() : 0)); model.addAttribute("labelId", 0); return ARTICLE_DETAIL_PATH; } /** * 404 */ @GetMapping("/info/404.html") public String errorPage(final Model model) { model.addAttribute("msg", "404页面"); model.addAttribute("type", "article"); List typeList = articleService.getArticleTypes(); List data = articleService.getArticleLabels().getData(); List Ads = articleService.getLastestInfoAds().getData(); model.addAttribute("ads", Ads); model.addAttribute("labels", data); model.addAttribute("articleType", typeList); model.addAttribute("typeId", 0); model.addAttribute("labelId", 0); return ERROR_PATH; } /** * 文章推荐和文章 */ @GetMapping("/info/articlerecommendation-{id}-{pageNum}.html") public String toArticleRecommendation(@PathVariable("id") Integer id, @PathVariable("pageNum") Integer pageNum, final Model model) { List typeList = articleService.getArticleTypes(); List data = articleService.getArticleLabels().getData(); List Ads = articleService.getLastestInfoAds().getData(); model.addAttribute("ads", Ads); model.addAttribute("labels", data); model.addAttribute("articleType", typeList); model.addAttribute("pageNum", pageNum); model.addAttribute("typeId", id); // model.addAttribute("typeId", 0); model.addAttribute("labelId", 0); return ARTICLE_RECOMMENDATION; } /** * 获取文章热门标签 */ @GetMapping("/article/labels") @ResponseBody public JsonModel> getArticleLabels() { return articleService.getArticleLabels(); } /** * 获取文章推荐 */ @GetMapping("/article/recommend") @ResponseBody public JsonModel> getArticleRecommended(Integer typeId, @RequestParam(value = "pageNum", defaultValue = "1") int pageNum, @RequestParam(value = "pageSize", defaultValue = "3") int pageSize) { return articleService.getArticleRecommended(typeId, pageNum, pageSize); } /** * 获取相关文章 */ @GetMapping("/article/related") @ResponseBody public JsonModel> getArticleRelated(Integer id, String labels, @RequestParam(value = "pageNum", defaultValue = "1") int pageNum, @RequestParam(value = "pageSize", defaultValue = "3") int pageSize) { return articleService.getArticleRelated(id, labels, pageNum, pageSize); } /** * 获取文章广告 */ @GetMapping("/article/ads") @ResponseBody public JsonModel> getLastestInfoAds() { return articleService.getLastestInfoAds(); } /** * 点击标签 */ @GetMapping("/article/label/click") @ResponseBody public JsonModel clickArticleLabel(Integer id) { return articleService.clickArticleLabel(id); } /** * 点击广告 */ @GetMapping("/article/ad/click") @ResponseBody public JsonModel clickArticleAd(Integer id) { return articleService.clickArticleAd(id); } /** * 文章点赞 */ @GetMapping("/article/like") @ResponseBody public JsonModel articleLike(Integer id, ServerWebExchange serverWebExchange) { return articleService.articleLike(id, serverWebExchange); } /** * 文章浏览量增加 */ @GetMapping("/article/pv") @ResponseBody public JsonModel articlePv(Integer id) { return articleService.articlePv(id); } private String source(String link) { if (link.contains("baidu.com")) { return "1"; } if (link.contains("www.so.com")) { return "2"; } if (link.contains("www.google.cn")) { return "3"; } if (link.contains("m.sm.cn")) { return "4"; } if (link.contains("toutiao.com")) { return "5"; } if (link.contains("www.sogou.com")) { return "6"; } if (link.contains("servicewechat.com")) { return "7"; } if (link.contains("www.caimei365.com")) { return "0"; } if (link.contains("zzjtest.gz.aeert.com")) { return "0"; } return null; } }