ArticleController.java 14 KB

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