ArticleController.java 9.5 KB

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