This commit is contained in:
2026-05-29 16:18:11 +08:00
parent e2bcdc31bd
commit a12ef31f26
3 changed files with 167 additions and 86 deletions
@@ -263,35 +263,65 @@ public class SysHomeController {
// @CacheDefaults(cacheName = RedisConstant.PLATFORM_REDIS_WKCACHE_PREFIX + "web_news", isHash = true) // @CacheDefaults(cacheName = RedisConstant.PLATFORM_REDIS_WKCACHE_PREFIX + "web_news", isHash = true)
// @CacheResult(cacheKey = "news", ignoreNull = true, cacheLiveTime = 60 * 60 * 24) // @CacheResult(cacheKey = "news", ignoreNull = true, cacheLiveTime = 60 * 60 * 24)
public Result getNews() { public Result getNews() {
// 定义要爬取的URL
String domain = "https://gh.hgu.edu.cn"; String domain = "https://gh.hgu.edu.cn";
try {
// 使用Jsoup连接到URL并获取页面内容
Document doc = Jsoup.connect(domain).get();
JSONObject root = new JSONObject(); JSONObject root = new JSONObject();
root.set("grassroots", new ArrayList<>());
root.set("notices", new ArrayList<>());
try {
// 工会官网首页栏目结构由外部站点维护,抓取时设置超时,避免首页接口长时间阻塞。
Document doc = Jsoup.connect(domain).timeout(5000).get();
root.set("grassroots", parseWebsiteNews(doc.select(".sec1 .s1-right li a"), domain, false));
root.set("notices", parseWebsiteNews(doc.select(".sec2 .dt-list2 li a"), domain, true));
return Result.success(root);
} catch (Exception e) {
log.error(e);
return Result.success(root);
}
}
/**
* 解析工会官网首页新闻栏目。
*
* @param elements 官网栏目链接节点,工会动态为.sec1 .s1-right li a,通知公告为.sec2 .dt-list2 li a
* @param domain 官网域名,用于把info/xxx.htm、/__local/xxx等相对地址补全为可直接打开的绝对地址
* @param notice 是否为通知公告;通知公告日期由“年月”和“日”拆分展示,需要单独拼接
* @return List<JSONObject>,字段包括title标题、date发布日期、url详情地址,前端直接按数组轮播展示
*/
private List<JSONObject> parseWebsiteNews(Elements elements, String domain, boolean notice) {
List<JSONObject> newsArray = new ArrayList<>(); List<JSONObject> newsArray = new ArrayList<>();
Elements newsItems = doc.select("table[cellspacing=3] tr"); for (Element item : elements) {
for (Element item : newsItems) { String title = StrUtil.blankToDefault(item.attr("title").trim(), item.select("p,h3").first() == null ? "" : item.select("p,h3").first().text().trim());
Element aElement = item.select("a").first(); String url = item.attr("href").trim();
if(aElement == null) { if (StrUtil.isBlank(title) || StrUtil.isBlank(url)) {
continue; continue;
} }
String date = item.select(".timestyle125262").text().trim();
String title = aElement.attr("title").trim(); String date;
String url = aElement.attr("href").trim(); if (notice) {
String day = item.select(".date p").text().trim();
String yearMonth = item.select(".date span").text().trim();
date = StrUtil.isBlank(yearMonth) ? day : yearMonth + "-" + day;
} else {
date = item.select("span").text().trim();
}
JSONObject node = new JSONObject(); JSONObject node = new JSONObject();
node.set("title", title); node.set("title", title);
node.set("date", date); node.set("date", date);
node.set("url", "https://gh.hgu.edu.cn/" + url); node.set("url", buildWebsiteUrl(domain, url));
newsArray.add(node); newsArray.add(node);
} }
root.set("grassroots", newsArray); return newsArray;
return Result.success(root);
} catch (Exception e) {
log.error(e);
return Result.success();
} }
private String buildWebsiteUrl(String domain, String url) {
if (StrUtil.startWithIgnoreCase(url, "http")) {
return url;
}
if (url.startsWith("/")) {
return domain + url;
}
return domain + "/" + url;
} }
} }
@@ -86,7 +86,7 @@ layout("/layouts/v4/baseLayout.html"){
</div> </div>
<jcdt :list="websiteNews.grassroots"></jcdt> <jcdt :grassroots="websiteNews.grassroots" :notices="websiteNews.notices"></jcdt>
</div> </div>
</div> </div>
@@ -105,6 +105,7 @@ layout("/layouts/v4/baseLayout.html"){
return{ return{
websiteNews:{ websiteNews:{
grassroots:[], grassroots:[],
notices:[],
} }
} }
}, },
@@ -122,7 +123,11 @@ layout("/layouts/v4/baseLayout.html"){
getWebSiteNews(){ getWebSiteNews(){
this.$axios.post('/platform/home/getNews').then(res => { this.$axios.post('/platform/home/getNews').then(res => {
if (res.code === 0) { if (res.code === 0) {
this.websiteNews = res.data const data = res.data || {}
this.websiteNews = {
grassroots: data.grassroots || [],
notices: data.notices || [],
}
} }
}) })
} }
@@ -2,28 +2,27 @@ const jcdt = {
template: /*language=HTML*/ ` template: /*language=HTML*/ `
<div class="jcdt-wrapper"> <div class="jcdt-wrapper">
<div class="jcdt-container"> <div class="jcdt-container">
<el-carousel class="jcdt-carousel" height="340px" :interval="10000" indicator-position="none" arrow="always">
<el-carousel-item v-for="section in sections" :key="section.key">
<div class="jcdt-title"> <div class="jcdt-title">
<span>工会动态</span> <span>{{section.title}}</span>
</div> </div>
<div class="jcdt-content"> <div class="jcdt-content">
<div class="jcdt-list"> <div v-if="section.list.length > 0" class="jcdt-list">
<div class="jcdt-item" v-for="item in list" :key="item.title" @click="onLink(item)"> <div class="jcdt-item" v-for="item in section.list" :key="item.url || item.title" @click="onLink(item)">
<div class="date" v-if="item.date">{{item.date}}</div> <div class="date" v-if="item.date">
<div style="display: flex; align-items: center"> <i class="el-icon-alarm-clock"></i>
<div > <span>{{item.date}}</span>
<el-image style="width: 30px" src="/assets/platform/img/v4/xw.png" alt="流程中心"></el-image>
</div> </div>
<div class="jcdt-item-main">
<div class="title">{{item.title}}</div> <div class="title">{{item.title}}</div>
</div> </div>
<!--<div class="image">
<img v-if="item.image" :src="item.image" alt="">
<img v-else src="/assets/platform/img/v4/not-image.png" alt="">
</div>-->
<!-- <div class="summary">{{item.summary}}</div>-->
</div> </div>
</div> </div>
<div v-else class="jcdt-empty">暂无{{section.title}}</div>
</div> </div>
</el-carousel-item>
</el-carousel>
</div> </div>
</div> </div>
`, `,
@@ -31,9 +30,29 @@ const jcdt = {
return {} return {}
}, },
props: { props: {
list: { grassroots: {
type: Array, type: Array,
default: [] default: () => []
},
notices: {
type: Array,
default: () => []
}
},
computed: {
sections() {
return [
{
key: "grassroots",
title: "工会要闻",
list: (this.grassroots || []).slice(0, 8)
},
{
key: "notices",
title: "通知公告",
list: (this.notices || []).slice(0, 8)
}
]
} }
}, },
methods: { methods: {
@@ -54,94 +73,112 @@ const jcdt = {
width: 80%; width: 80%;
max-width: 80%; max-width: 80%;
margin: 0 auto; margin: 0 auto;
background-color: #FFFFFF; background-color: #FFFFFF;
border-radius: 12px; border-radius: 12px;
padding: 20px; padding: 42px 20px 40px;
margin-bottom: 10px; margin-bottom: 10px;
box-shadow: 0 6px 20px rgba(0, 0, 0, 0.06); box-shadow: 0 10px 28px rgba(0, 0, 0, 0.08);
} }
.jcdt-title{ .jcdt-carousel {
width: 100%;
}
/deep/ .jcdt-carousel .el-carousel__container {
height: 340px;
}
/deep/ .jcdt-carousel .el-carousel__item {
overflow: visible;
}
/deep/ .jcdt-title{
font-size: 30px; font-size: 30px;
color: #000000; color: #000000;
display: block; display: block;
font-weight: bold; font-weight: bold;
position: relative; position: relative;
text-align: center; text-align: center;
padding: 20px 0; padding: 0 0 24px;
} }
/deep/ .jcdt-container .jcdt-title span::before { /deep/ .jcdt-container .jcdt-title span::before {
content: ''; content: '';
width: 24px;
height: 11px;
background: url(https://www.ncu.edu.cn/images/titl.svg) no-repeat center;
background-size: 24px 11px;
display: inline-block; display: inline-block;
margin-right: 0; margin-right: 8px;
vertical-align: middle; vertical-align: middle;
color: #e60012;
font-size: 16px;
font-weight: 400;
} }
/deep/ .jcdt-container .jcdt-title span::after { /deep/ .jcdt-container .jcdt-title span::after {
content: ''; content: '';
width: 24px;
height: 11px;
background: url(https://www.ncu.edu.cn/images/titr.svg) no-repeat center;
background-size: 24px 11px;
display: inline-block; display: inline-block;
margin-left: 0; margin-left: 8px;
vertical-align: middle; vertical-align: middle;
color: #e60012;
font-size: 16px;
font-weight: 400;
} }
.jcdt-list { /deep/ .jcdt-list {
display: grid; display: grid;
grid-template-columns: repeat(4, 1fr); grid-template-columns: repeat(4, minmax(0, 1fr));
gap: 20px; gap: 20px;
} }
.jcdt-item { /deep/ .jcdt-item {
display: flex; display: flex;
flex-direction: column; flex-direction: column;
padding: 16px; padding: 16px;
border: none; border: none;
border-bottom: 3px solid transparent; border-bottom: 3px solid #c11623;
transition: all 0.3s ease; transition: all 0.3s ease;
cursor: pointer; cursor: pointer;
height: auto;
border-radius: 12px; border-radius: 12px;
} min-height: 84px;
.jcdt-item:hover{
background: #ffffff; background: #ffffff;
border-bottom-color: var(--color-primary); box-shadow: 0 8px 18px rgba(0, 0, 0, 0.08);
transform: translateY(-4px);
box-shadow: 0 8px 20px rgba(0, 0, 0, 0.15);
} }
.jcdt-item .date { /deep/ .jcdt-item-main {
display: flex;
align-items: center;
}
/deep/ .jcdt-item:hover{
background: #ffffff;
transform: translateY(-4px);
box-shadow: 0 10px 24px rgba(0, 0, 0, 0.14);
}
/deep/ .jcdt-item .date {
font-size: 20px; font-size: 20px;
/*font-weight: bold;*/ font-weight: bold;
white-space: nowrap; white-space: nowrap;
text-align: left; text-align: left;
margin-bottom: 8px; margin-bottom: 8px;
transition: opacity 0.3s ease; transition: opacity 0.3s ease;
} }
.jcdt-item .title { /deep/ .jcdt-item .date i {
color: #c11623;
font-size: 18px;
margin-right: 4px;
}
/deep/ .jcdt-item .title {
font-size: 16px; font-size: 16px;
font-weight: 600; font-weight: 600;
/*margin-bottom: 12px;*/
line-height: 1.4; line-height: 1.4;
display: -webkit-box; display: -webkit-box;
-webkit-line-clamp: 2; -webkit-line-clamp: 2;
-webkit-box-orient: vertical; -webkit-box-orient: vertical;
overflow: hidden; overflow: hidden;
/*height: 45px;*/
} }
.jcdt-item .image { /deep/ .jcdt-item .image {
width: 100%; width: 100%;
height: 180px; height: 180px;
margin: 0 0 16px 0; margin: 0 0 16px 0;
@@ -155,14 +192,14 @@ const jcdt = {
transition: transform 0.3s ease; transition: transform 0.3s ease;
} }
.jcdt-item .image img { /deep/ .jcdt-item .image img {
width: 100%; width: 100%;
height: 100%; height: 100%;
object-fit: cover; object-fit: cover;
transition: transform 0.3s ease; transition: transform 0.3s ease;
} }
.jcdt-item .summary { /deep/ .jcdt-item .summary {
font-size: 14px; font-size: 14px;
color: #666; color: #666;
line-height: 1.6; line-height: 1.6;
@@ -174,5 +211,14 @@ const jcdt = {
margin-bottom: 12px; margin-bottom: 12px;
} }
/deep/ .jcdt-empty {
height: 160px;
display: flex;
align-items: center;
justify-content: center;
color: #909399;
font-size: 16px;
}
` `
}; };