ArticleController.java 12 KB

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