|
| 1 | +package com.yqz.openblog.seo.controller; |
| 2 | + |
| 3 | +import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| 4 | +import com.baomidou.mybatisplus.core.toolkit.Wrappers; |
| 5 | +import com.yqz.openblog.article.entity.Article; |
| 6 | +import com.yqz.openblog.article.entity.ArticleStatus; |
| 7 | +import com.yqz.openblog.article.repo.ArticleMapper; |
| 8 | +import com.yqz.openblog.seo.SeoProperties; |
| 9 | +import org.springframework.http.MediaType; |
| 10 | +import org.springframework.web.bind.annotation.GetMapping; |
| 11 | +import org.springframework.web.bind.annotation.RestController; |
| 12 | + |
| 13 | +import java.time.ZoneOffset; |
| 14 | +import java.time.format.DateTimeFormatter; |
| 15 | +import java.util.List; |
| 16 | + |
| 17 | +@RestController |
| 18 | +public class SitemapController { |
| 19 | + |
| 20 | + private final ArticleMapper articleMapper; |
| 21 | + private final SeoProperties seoProperties; |
| 22 | + |
| 23 | + public SitemapController(ArticleMapper articleMapper, SeoProperties seoProperties) { |
| 24 | + this.articleMapper = articleMapper; |
| 25 | + this.seoProperties = seoProperties; |
| 26 | + } |
| 27 | + |
| 28 | + @GetMapping(value = "/sitemap.xml", produces = MediaType.APPLICATION_XML_VALUE) |
| 29 | + public String sitemap() { |
| 30 | + LambdaQueryWrapper<Article> w = Wrappers.lambdaQuery(); |
| 31 | + w.eq(Article::getStatus, ArticleStatus.PUBLISHED) |
| 32 | + .select(Article::getId, Article::getUpdatedAt, Article::getPublishedAt) |
| 33 | + .orderByDesc(Article::getPublishedAt); |
| 34 | + |
| 35 | + List<Article> articles = articleMapper.selectList(w); |
| 36 | + String siteUrl = seoProperties.getSiteUrl(); |
| 37 | + DateTimeFormatter fmt = DateTimeFormatter.ISO_DATE.withZone(ZoneOffset.UTC); |
| 38 | + |
| 39 | + StringBuilder sb = new StringBuilder(); |
| 40 | + sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"); |
| 41 | + sb.append("<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">\n"); |
| 42 | + |
| 43 | + sb.append(" <url>\n"); |
| 44 | + sb.append(" <loc>").append(siteUrl).append("/</loc>\n"); |
| 45 | + sb.append(" <changefreq>daily</changefreq>\n"); |
| 46 | + sb.append(" <priority>1.0</priority>\n"); |
| 47 | + sb.append(" </url>\n"); |
| 48 | + |
| 49 | + for (Article a : articles) { |
| 50 | + sb.append(" <url>\n"); |
| 51 | + sb.append(" <loc>").append(siteUrl).append("/article/").append(a.getId()).append("</loc>\n"); |
| 52 | + if (a.getUpdatedAt() != null) { |
| 53 | + sb.append(" <lastmod>").append(fmt.format(a.getUpdatedAt())).append("</lastmod>\n"); |
| 54 | + } else if (a.getPublishedAt() != null) { |
| 55 | + sb.append(" <lastmod>").append(fmt.format(a.getPublishedAt())).append("</lastmod>\n"); |
| 56 | + } |
| 57 | + sb.append(" <changefreq>weekly</changefreq>\n"); |
| 58 | + sb.append(" <priority>0.8</priority>\n"); |
| 59 | + sb.append(" </url>\n"); |
| 60 | + } |
| 61 | + |
| 62 | + sb.append("</urlset>"); |
| 63 | + return sb.toString(); |
| 64 | + } |
| 65 | +} |
0 commit comments