ArticleController.java 15 KB

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