ArticleController.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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.caimei.www.utils.PaginationVo;
  10. import com.github.pagehelper.PageHelper;
  11. import com.github.pagehelper.PageInfo;
  12. import lombok.extern.slf4j.Slf4j;
  13. import org.springframework.beans.factory.annotation.Autowired;
  14. import org.springframework.http.HttpHeaders;
  15. import org.springframework.http.server.reactive.ServerHttpRequest;
  16. import org.springframework.http.server.reactive.ServerHttpResponse;
  17. import org.springframework.stereotype.Controller;
  18. import org.springframework.ui.Model;
  19. import org.springframework.web.bind.annotation.*;
  20. import org.springframework.web.server.ServerWebExchange;
  21. import org.thymeleaf.util.StringUtils;
  22. import java.text.SimpleDateFormat;
  23. import java.util.ArrayList;
  24. import java.util.Date;
  25. import java.util.List;
  26. /**
  27. * 文章页鉴于seo,保留旧链接
  28. *
  29. * @author : Charles
  30. * @date : 2020/7/31
  31. */
  32. @Slf4j
  33. @Controller
  34. public class ArticleController extends BaseController {
  35. /**
  36. * 错误页面
  37. */
  38. private static final String ERROR_PATH = "article/error";
  39. private static final String ARTICLE_LIST_PATH = "article/list";
  40. private static final String ARTICLE_DETAIL_PATH = "article/detail";
  41. private static final String ARTICLE_RECOMMENDATION = "article/recommendation";
  42. private ArticleService articleService;
  43. @Autowired
  44. private CmBrandLandingService cmBrandLandingService;
  45. @Autowired
  46. private ProductService productService;
  47. @Autowired
  48. public void setArticleService(ArticleService articleService) {
  49. this.articleService = articleService;
  50. }
  51. /**
  52. * 文章列表【旧center】
  53. */
  54. @GetMapping("/info/center-{id}-{pageNum}.html")
  55. public String toArticleList(@PathVariable("id") Integer id, @PathVariable("pageNum") Integer pageNum, final Model model) {
  56. List<BaseLink> typeList = articleService.getArticleTypes();
  57. List<CmBrandLandingVO> cmBrandLandingList = cmBrandLandingService.getCmBrandLandingList(new CmBrandLanding()
  58. .setType("4").setAuthorId(id.toString()));
  59. List<Article> infoSelected = articleService.getArticleSelected(0, 5);
  60. List<CmProductArchiveFile> archiveFiles = articleService.findArchiveFileAddTime(0, 3);
  61. List<ProductList> productSellNumbers = productService.getProductSellNumbers();
  62. List<BaseLink> data = articleService.getArticleLabels().getData();
  63. List<ImageLink> Ads = articleService.getLastestInfoAds().getData();
  64. model.addAttribute("ads", Ads);
  65. model.addAttribute("labels", data);
  66. model.addAttribute("articleType", typeList);//导航栏
  67. model.addAttribute("articleImages", cmBrandLandingList);//导航栏图片
  68. model.addAttribute("infoSelected", infoSelected);//精选文章
  69. model.addAttribute("productSellNumbers", productSellNumbers);//销量前三商品
  70. model.addAttribute("archiveFiles", archiveFiles);//最新美业资料top3
  71. model.addAttribute("typeId", id);
  72. model.addAttribute("labelId", 0);
  73. model.addAttribute("pageNum", pageNum);
  74. model.addAttribute("isSearch", true);
  75. //model.addAttribute("pagePath", String.format("/info/center-%s-1.html", id));
  76. return ARTICLE_LIST_PATH;
  77. }
  78. /**
  79. * 文章列表【旧label】
  80. */
  81. @GetMapping("/info/label-{id}-{pageNum}.html")
  82. public String toArticleLabel(@PathVariable("id") Integer id, @PathVariable("pageNum") Integer pageNum, final Model model) {
  83. List<BaseLink> typeList = articleService.getArticleTypes();
  84. List<BaseLink> data = articleService.getArticleLabels().getData();
  85. List<ImageLink> Ads = articleService.getLastestInfoAds().getData();
  86. model.addAttribute("ads", Ads);
  87. model.addAttribute("labels", data);
  88. model.addAttribute("articleType", typeList);
  89. model.addAttribute("typeId", 0);
  90. model.addAttribute("labelId", id);
  91. model.addAttribute("pageNum", pageNum);
  92. model.addAttribute("isSearch", false);
  93. //model.addAttribute("pagePath", String.format("/info/label-%s-1.html", id));
  94. return ARTICLE_LIST_PATH;
  95. }
  96. /**
  97. * 精选文章和美业资料列表
  98. */
  99. @GetMapping("/info/articlerecommendation-{id}-{pageNum}.html")
  100. public String selectedList(@PathVariable("id") Integer id, @PathVariable("pageNum") Integer pageNum, final Model model) {
  101. List<BaseLink> typeList = articleService.getArticleTypes();
  102. if (id == 1) {//精选文章
  103. List<CmProductArchiveFile> archiveFiles = articleService.findArchiveFileAddTime(0, 3);
  104. List<ProductList> productSellNumbers = productService.getProductSellNumbers();
  105. model.addAttribute("productSellNumbers", productSellNumbers);//销量前三商品
  106. model.addAttribute("archiveFiles", archiveFiles);//最新美业资料top3
  107. }
  108. List<BaseLink> data = articleService.getArticleLabels().getData();
  109. List<ImageLink> Ads = articleService.getLastestInfoAds().getData();
  110. model.addAttribute("ads", Ads);
  111. model.addAttribute("labels", data);
  112. model.addAttribute("articleType", typeList);//导航栏
  113. model.addAttribute("labelId", 0);
  114. model.addAttribute("pageNum", pageNum);
  115. model.addAttribute("isSearch", true);
  116. return ARTICLE_LIST_PATH;
  117. }
  118. /**
  119. * 精选文章和美业资料列表数据
  120. */
  121. @ResponseBody
  122. @GetMapping("/info/articlerecommendation/{id}/{pageNum}")
  123. public JsonModel<PaginationVo> toPagination(@PathVariable("id") Integer id,@PathVariable("pageNum") Integer pageNum) {
  124. PageHelper.startPage(pageNum, 10);
  125. PaginationVo pageData = null;
  126. if (id == 1) {//精选文章
  127. List<Article> infoSelected = articleService.getArticleSelected(null, null);
  128. pageData = new PaginationVo<Article>(infoSelected);
  129. } else {//美业资料
  130. List<CmProductArchiveFile> archiveFiles = articleService.findArchiveFileAddTime(null, null);
  131. pageData = new PaginationVo<CmProductArchiveFile>(archiveFiles);
  132. }
  133. return JsonModel.success(pageData);
  134. }
  135. @GetMapping("/info/search-{pageNum}.html")
  136. public String toArticleSearch(@PathVariable("pageNum") Integer pageNum, final Model model) {
  137. List<BaseLink> typeList = articleService.getArticleTypes();
  138. List<BaseLink> data = articleService.getArticleLabels().getData();
  139. List<ImageLink> Ads = articleService.getLastestInfoAds().getData();
  140. model.addAttribute("ads", Ads);
  141. model.addAttribute("labels", data);
  142. model.addAttribute("articleType", typeList);
  143. model.addAttribute("typeId", 0);
  144. model.addAttribute("labelId", 0);
  145. model.addAttribute("pageNum", pageNum);
  146. model.addAttribute("isSearch", false);
  147. //model.addAttribute("pagePath", "/info/search.html");
  148. return ARTICLE_LIST_PATH;
  149. }
  150. /**
  151. * 文章详情【旧】
  152. */
  153. @GetMapping("/info/detail-{id}-1.html")
  154. public String toArticleDetail(@PathVariable("id") Integer id, final Model model, ServerWebExchange serverWebExchange, ServerHttpResponse response) {
  155. List<BaseLink> typeList = articleService.getArticleTypes();
  156. Article article = articleService.getArticleInfo(id);
  157. if (article == null) {
  158. // return super.errorPath();
  159. return "redirect:/info/404.html";
  160. }
  161. // 获取referer 判断访问来源
  162. ServerHttpRequest request = serverWebExchange.getRequest();
  163. String ip = "";
  164. String referer = "";
  165. String source = "";
  166. HttpHeaders headers = request.getHeaders();
  167. ip = String.valueOf(headers.get("x-forwarded-for"));
  168. if (StringUtils.isEmpty(ip)) {
  169. ip = String.valueOf(headers.get("X-Real-IP"));
  170. }
  171. if (null != ip && "" != ip) {
  172. ip = ip.replaceAll("[^\\d.]", "");
  173. }
  174. List<String> referer1 = headers.get("Referer");
  175. if (referer1 != null) {
  176. referer = referer1.get(0);
  177. }
  178. String subReferer = "";
  179. if (referer.length() > 200) {
  180. subReferer = referer.substring(0, 190);
  181. }
  182. source = source(referer);
  183. SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  184. String format = dateFormat.format(new Date());
  185. articleService.insertRecord(ip, subReferer, source, format);
  186. log.info("文章详情访问来源记录完成========》" + source);
  187. List<BaseLink> data = articleService.getArticleLabels().getData();
  188. List<ImageLink> Ads = articleService.getLastestInfoAds().getData();
  189. List<Article> articles = new ArrayList<>();
  190. if (null != article && article.getAutoStatus() == 0) {
  191. articles = articleService.getInfoById(id);
  192. } else {
  193. articles = articleService.getArticleRelatedId(id.toString());
  194. }
  195. String relatedLabels = articleService.relatedLabel(id);
  196. List<Article> infoSelected = articleService.getArticleSelected(0, 5);
  197. List<CmProductArchiveFile> archiveFiles = articleService.findArchiveFileAddTime(0, 3);
  198. List<ProductList> productSellNumbers = productService.getProductSellNumbers();
  199. model.addAttribute("infoSelected", infoSelected);//精选文章
  200. model.addAttribute("productSellNumbers", productSellNumbers);//销量前三商品
  201. model.addAttribute("archiveFiles", archiveFiles);//最新美业资料top3
  202. model.addAttribute("articles", articles);//相关文章
  203. model.addAttribute("ads", Ads);
  204. model.addAttribute("labels", data);
  205. model.addAttribute("article", article);
  206. model.addAttribute("articleType", typeList);
  207. model.addAttribute("articleId", id);
  208. model.addAttribute("relatedLabels", relatedLabels);
  209. model.addAttribute("typeId", (article != null ? article.getTypeId() : 0));
  210. model.addAttribute("labelId", 0);
  211. return ARTICLE_DETAIL_PATH;
  212. }
  213. /**
  214. * 404
  215. */
  216. @GetMapping("/info/404.html")
  217. public String errorPage(final Model model) {
  218. model.addAttribute("msg", "404页面");
  219. model.addAttribute("type", "article");
  220. List<BaseLink> typeList = articleService.getArticleTypes();
  221. List<BaseLink> data = articleService.getArticleLabels().getData();
  222. List<ImageLink> Ads = articleService.getLastestInfoAds().getData();
  223. model.addAttribute("ads", Ads);
  224. model.addAttribute("labels", data);
  225. model.addAttribute("articleType", typeList);
  226. model.addAttribute("typeId", 0);
  227. model.addAttribute("labelId", 0);
  228. return ERROR_PATH;
  229. }
  230. /**
  231. * 文章推荐和文章
  232. */
  233. @GetMapping("/info/articlerecommendation.html")
  234. public String toArticleRecommendation(final Model model) {
  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 ARTICLE_RECOMMENDATION;
  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. }