ArticleController.java 15 KB

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