ArticleController.java 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. package com.caimei.www.controller.unlimited;
  2. import com.caimei.www.controller.BaseController;
  3. import com.caimei.www.pojo.JsonModel;
  4. import com.caimei.www.pojo.page.Article;
  5. import com.caimei.www.pojo.page.BaseLink;
  6. import com.caimei.www.pojo.page.ImageLink;
  7. import com.caimei.www.service.page.ArticleService;
  8. import com.github.pagehelper.PageInfo;
  9. import lombok.extern.slf4j.Slf4j;
  10. import org.springframework.beans.factory.annotation.Autowired;
  11. import org.springframework.http.HttpHeaders;
  12. import org.springframework.http.server.reactive.ServerHttpRequest;
  13. import org.springframework.http.server.reactive.ServerHttpResponse;
  14. import org.springframework.stereotype.Controller;
  15. import org.springframework.ui.Model;
  16. import org.springframework.web.bind.annotation.*;
  17. import org.springframework.web.server.ServerWebExchange;
  18. import org.thymeleaf.util.StringUtils;
  19. import java.text.SimpleDateFormat;
  20. import java.util.Date;
  21. import java.util.List;
  22. /**
  23. * 文章页鉴于seo,保留旧链接
  24. *
  25. * @author : Charles
  26. * @date : 2020/7/31
  27. */
  28. @Slf4j
  29. @Controller
  30. public class ArticleController extends BaseController {
  31. /** 错误页面 */
  32. private static final String ERROR_PATH = "article/error";
  33. private static final String ARTICLE_LIST_PATH = "article/list";
  34. private static final String ARTICLE_DETAIL_PATH = "article/detail";
  35. private ArticleService articleService;
  36. @Autowired
  37. public void setArticleService(ArticleService articleService) {
  38. this.articleService = articleService;
  39. }
  40. /**
  41. * 文章列表【旧center】
  42. */
  43. @GetMapping("/info/center-{id}-{pageNum}.html")
  44. public String toArticleList(@PathVariable("id") Integer id, @PathVariable("pageNum") Integer pageNum, final Model model) {
  45. List<BaseLink> typeList = articleService.getArticleTypes();
  46. List<BaseLink> data = articleService.getArticleLabels().getData();
  47. List<ImageLink> Ads = articleService.getLastestInfoAds().getData();
  48. model.addAttribute("ads", Ads);
  49. model.addAttribute("labels",data);
  50. model.addAttribute("articleType", typeList);
  51. model.addAttribute("typeId", id);
  52. model.addAttribute("labelId", 0);
  53. model.addAttribute("pageNum", pageNum);
  54. //model.addAttribute("pagePath", String.format("/info/center-%s-1.html", id));
  55. return ARTICLE_LIST_PATH;
  56. }
  57. /**
  58. * 文章列表【旧label】
  59. */
  60. @GetMapping("/info/label-{id}-{pageNum}.html")
  61. public String toArticleLabel(@PathVariable("id") Integer id, @PathVariable("pageNum") Integer pageNum, final Model model) {
  62. List<BaseLink> typeList = articleService.getArticleTypes();
  63. List<BaseLink> data = articleService.getArticleLabels().getData();
  64. List<ImageLink> Ads = articleService.getLastestInfoAds().getData();
  65. model.addAttribute("ads", Ads);
  66. model.addAttribute("labels",data);
  67. model.addAttribute("articleType", typeList);
  68. model.addAttribute("typeId", 0);
  69. model.addAttribute("labelId", id);
  70. model.addAttribute("pageNum", pageNum);
  71. //model.addAttribute("pagePath", String.format("/info/label-%s-1.html", id));
  72. return ARTICLE_LIST_PATH;
  73. }
  74. /**
  75. * 文章搜索结果
  76. */
  77. @GetMapping("/info/search-{pageNum}.html")
  78. public String toArticleSearch(@PathVariable("pageNum") Integer pageNum, final Model model) {
  79. List<BaseLink> typeList = articleService.getArticleTypes();
  80. List<BaseLink> data = articleService.getArticleLabels().getData();
  81. List<ImageLink> Ads = articleService.getLastestInfoAds().getData();
  82. model.addAttribute("ads", Ads);
  83. model.addAttribute("labels",data);
  84. model.addAttribute("articleType", typeList);
  85. model.addAttribute("typeId", 0);
  86. model.addAttribute("labelId", 0);
  87. model.addAttribute("pageNum", pageNum);
  88. //model.addAttribute("pagePath", "/info/search.html");
  89. return ARTICLE_LIST_PATH;
  90. }
  91. /**
  92. * 文章详情【旧】
  93. */
  94. @GetMapping("/info/detail-{id}-1.html")
  95. public String toArticleDetail(@PathVariable("id") Integer id, final Model model, ServerWebExchange serverWebExchange, ServerHttpResponse response) {
  96. List<BaseLink> typeList = articleService.getArticleTypes();
  97. Article article = articleService.getArticleInfo(id);
  98. if(article == null){
  99. // return super.errorPath();
  100. return "redirect:/info/404.html";
  101. }
  102. // 获取referer 判断访问来源
  103. ServerHttpRequest request = serverWebExchange.getRequest();
  104. String ip = "";
  105. String referer = "";
  106. String source = "";
  107. HttpHeaders headers = request.getHeaders();
  108. ip = String.valueOf(headers.get("x-forwarded-for"));
  109. if (StringUtils.isEmpty(ip)) {
  110. ip = String.valueOf(headers.get("X-Real-IP"));
  111. }
  112. if (null != ip && "" != ip) {
  113. ip = ip.replaceAll("[^\\d.]", "");
  114. }
  115. List<String> referer1 = headers.get("Referer");
  116. if (referer1 != null) {
  117. referer = referer1.get(0);
  118. }
  119. String subReferer = referer.substring(0, 190);
  120. source = source(referer);
  121. SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  122. String format = dateFormat.format(new Date());
  123. articleService.insertRecord(ip,subReferer,source,format);
  124. log.info("文章详情访问来源记录完成========》"+source);
  125. List<BaseLink> data = articleService.getArticleLabels().getData();
  126. List<ImageLink> Ads = articleService.getLastestInfoAds().getData();
  127. List<Article> articles=articleService.getInfoById(id);
  128. model.addAttribute("articles",articles);
  129. model.addAttribute("ads", Ads);
  130. model.addAttribute("labels",data);
  131. model.addAttribute("article", article);
  132. model.addAttribute("articleType", typeList);
  133. model.addAttribute("articleId", id);
  134. model.addAttribute("typeId", (article!=null ? article.getTypeId() : 0));
  135. model.addAttribute("labelId", 0);
  136. return ARTICLE_DETAIL_PATH;
  137. }
  138. /**
  139. * 404
  140. */
  141. @GetMapping("/info/404.html")
  142. public String errorPage(final Model model) {
  143. model.addAttribute("msg", "404页面");
  144. model.addAttribute("type", "article");
  145. return ERROR_PATH;
  146. }
  147. /**
  148. * 获取文章热门标签
  149. */
  150. @GetMapping("/article/labels")
  151. @ResponseBody
  152. public JsonModel<List<BaseLink>> getArticleLabels() {
  153. return articleService.getArticleLabels();
  154. }
  155. /**
  156. * 获取文章推荐
  157. */
  158. @GetMapping("/article/recommend")
  159. @ResponseBody
  160. public JsonModel<PageInfo<ImageLink>> getArticleRecommended(Integer typeId,
  161. @RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
  162. @RequestParam(value = "pageSize", defaultValue = "3") int pageSize) {
  163. return articleService.getArticleRecommended(typeId, pageNum, pageSize);
  164. }
  165. /**
  166. * 获取相关文章
  167. */
  168. @GetMapping("/article/related")
  169. @ResponseBody
  170. public JsonModel<PageInfo<Article>> getArticleRelated(Integer id, String labels,
  171. @RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
  172. @RequestParam(value = "pageSize", defaultValue = "3") int pageSize) {
  173. return articleService.getArticleRelated(id, labels, pageNum, pageSize);
  174. }
  175. /**
  176. * 获取文章广告
  177. */
  178. @GetMapping("/article/ads")
  179. @ResponseBody
  180. public JsonModel<List<ImageLink>> getLastestInfoAds() {
  181. return articleService.getLastestInfoAds();
  182. }
  183. /**
  184. * 点击标签
  185. */
  186. @GetMapping("/article/label/click")
  187. @ResponseBody
  188. public JsonModel clickArticleLabel(Integer id) {
  189. return articleService.clickArticleLabel(id);
  190. }
  191. /**
  192. * 点击广告
  193. */
  194. @GetMapping("/article/ad/click")
  195. @ResponseBody
  196. public JsonModel clickArticleAd(Integer id) {
  197. return articleService.clickArticleAd(id);
  198. }
  199. /**
  200. * 文章点赞
  201. */
  202. @GetMapping("/article/like")
  203. @ResponseBody
  204. public JsonModel articleLike(Integer id, ServerWebExchange serverWebExchange) {
  205. return articleService.articleLike(id, serverWebExchange);
  206. }
  207. /**
  208. * 文章浏览量增加
  209. */
  210. @GetMapping("/article/pv")
  211. @ResponseBody
  212. public JsonModel articlePv(Integer id) {
  213. return articleService.articlePv(id);
  214. }
  215. private String source(String link) {
  216. if (link.contains("www.baidu.com")) {
  217. return "1";
  218. }
  219. if (link.contains("www.so.com")) {
  220. return "2";
  221. }
  222. if (link.contains("www.google.cn")) {
  223. return "3";
  224. }
  225. if (link.contains("m.sm.cn")) {
  226. return "4";
  227. }
  228. if (link.contains("toutiao.com")) {
  229. return "5";
  230. }
  231. if (link.contains("www.sogou.com")) {
  232. return "6";
  233. }
  234. if (link.contains("servicewechat.com")) {
  235. return "7";
  236. }
  237. if (link.contains("www.caimei365.com")) {
  238. return "0";
  239. }
  240. if (link.contains("zzjtest.gz.aeert.com")) {
  241. return "0";
  242. }
  243. return null;
  244. }
  245. }