$(document).ready(function () {
var baseUrl = 'http://39.104.206.29/prod-api';
var pageNum = 1;
// ---------- 获取分类列表(设计成果分类)----------
function getTitle() {
$.ajax({
type: 'GET',
url: baseUrl + '/business/mi/getDictTypeList',
dataType: "json",
success: function (data) {
// 筛选 remark 为 "设计成果分类" 的项
var typeList = data.data.filter(function (it) {
return it.remark == "设计成果分类";
});
// 清空分类容器
$(".dictlist").empty();
// 生成分类按钮,第一个加上 current_menu_item 类
typeList.forEach(function (item, idx) {
var cls = (idx === 0) ? "menuitem current_menu_item" : "menuitem";
var liHtml = '';
$(".dictlist").append(liHtml);
});
// 绑定分类点击事件
$(".menuitem").click(function () {
$(".menuitem").removeClass("current_menu_item");
$(this).addClass("current_menu_item");
pageNum = 1;
getArtList5();
});
// 立即加载第一个分类的文章
getArtList5();
},
error: function (jqXHR, textStatus) {
console.log("分类请求失败", textStatus);
}
});
}
// ---------- 获取文章列表(设计成果)----------
function getArtList5() {
// 文章列表容器(根据你的 HTML 使用 .row.yxal)
var $listContainer = $(".row.yxal");
// 分页容器(根据你的 HTML 使用 .pageList)
var $pageContainer = $(".pageList");
$listContainer.empty(); // 清空列表
var textv = $(".current_menu_item").attr("data-id");
console.log("当前选中分类ID:", textv);
var obj = {
pageNum: pageNum,
pageSize: 8,
type: 5
};
if (textv && textv != 'undefined') {
obj.acType = textv; // 原代码中使用 acType,保持与接口一致
}
$.ajax({
type: 'GET',
url: baseUrl + '/business/mi/getArticleList',
data: obj,
dataType: "json",
success: function (data) {
// 生成文章卡片(使用你原先的 HTML 结构)
data.data.list.forEach(function (item, idx) {
// 处理图片(取第一张)
var imgUrl = '';
if (item.imgs) {
imgUrl = baseUrl + item.imgs.split(',')[0];
}
// 处理详情链接
var link = 'single-blog-concat.html?id=' + item.id;
if (item.delFlag == 1 && item.remark) {
link = item.remark;
}
// 卡片 HTML(完全沿用你原先的样式和类名)
var cardHtml =
'' +
'
' +
'
' +
'
' +
'
' +
'
' +
'' +
'
' +
'
' +
'
' +
'
' +
'
' + item.content + '
' +
'
' +
'
' +
'
';
$listContainer.append(cardHtml);
});
// ---------- 分页处理(使用你原先的逻辑,但优化事件绑定)----------
var pageCount = data.data.pages;
var currentPage = data.data.pageNum;
var idxcount = 0;
// 计算起始页码(与原代码一致)
var startPage = currentPage == 1 ? 1 : (currentPage >= 5 ? currentPage - 1 : 1);
$pageContainer.empty(); // 清空分页容器
// 生成页码
for (var i = startPage; i <= pageCount; i++) {
idxcount++;
if (idxcount <= 5) {
var cls = (i == currentPage) ? 'pagebtn current' : 'pagebtn';
$pageContainer.append('' + i + '');
} else {
break;
}
}
// 下一页按钮
if (!data.data.isLastPage) {
$pageContainer.append('');
}
// 绑定页码点击事件(使用事件委托,避免动态元素问题)
$pageContainer.off('click', '.pagebtn').on('click', '.pagebtn', function () {
pageNum = parseInt($(this).text());
getArtList5();
});
// 绑定下一页点击事件
$pageContainer.off('click', '.pagenextbtn').on('click', '.pagenextbtn', function () {
pageNum++;
getArtList5();
});
},
error: function (jqXHR, textStatus) {
console.log("文章列表请求失败", textStatus);
}
});
}
// 启动
getTitle();
});