ykt-wxapp/pages/message/article-list.vue

584 lines
13 KiB
Vue
Raw Permalink Normal View History

2026-01-26 18:08:01 +08:00
<template>
<view class="article-page">
<view class="header">
<view class="search-bar">
<uni-icons type="search" size="18" color="#999" />
<input
class="search-input"
v-model="searchTitle"
placeholder="输入内容名称搜索"
@input="handleSearch"
/>
</view>
</view>
<view class="content">
<view class="category-sidebar">
<scroll-view scroll-y class="category-scroll">
<view
v-for="cate in categoryList"
:key="cate._id || 'all'"
class="category-item"
2026-01-27 13:42:59 +08:00
:class="{ active: currentCateId === cate._id }"
2026-01-26 18:08:01 +08:00
@click="selectCategory(cate)"
>
2026-01-27 13:42:59 +08:00
{{ cate.label }}
2026-01-26 18:08:01 +08:00
</view>
</scroll-view>
</view>
<view class="article-list">
<scroll-view
scroll-y
class="article-scroll"
@scrolltolower="loadMore"
lower-threshold="50"
>
<view
v-if="loading && articleList.length === 0"
class="loading-container"
>
<uni-icons type="spinner-cycle" size="30" color="#999" />
<text class="loading-text">加载中...</text>
</view>
<view v-else-if="articleList.length === 0" class="empty-container">
<empty-data title="暂无文章" />
</view>
<view v-else>
<view
v-for="article in articleList"
:key="article._id"
class="article-item"
>
<view class="article-content" @click="previewArticle(article)">
<text class="article-title">{{ article.title }}</text>
2026-01-27 13:42:59 +08:00
<view class="article-footer">
<text class="article-date">{{ article.date }}</text>
<button
class="send-btn"
size="mini"
type="primary"
@click.stop="sendArticle(article)"
>
发送
</button>
</view>
2026-01-26 18:08:01 +08:00
</view>
</view>
<view v-if="loading && articleList.length > 0" class="loading-more">
<uni-icons type="spinner-cycle" size="20" color="#999" />
<text>加载中...</text>
</view>
<view
v-if="!loading && articleList.length >= total"
2026-01-27 15:07:39 +08:00
class="no-more"
>
2026-01-26 18:08:01 +08:00
没有更多了
</view>
</view>
</scroll-view>
</view>
</view>
<!-- 文章预览弹窗 -->
<uni-popup ref="previewPopup" type="bottom" :safe-area="false">
<view class="preview-container">
<view class="preview-header">
<text class="preview-title">{{ previewArticleData.title }}</text>
<view class="preview-close" @click="closePreview">
<uni-icons type="closeempty" size="24" color="#333" />
</view>
</view>
<scroll-view scroll-y class="preview-content">
2026-01-27 13:42:59 +08:00
<view class="rich-text-wrapper">
<rich-text :nodes="previewArticleData.content"></rich-text>
</view>
2026-01-26 18:08:01 +08:00
</scroll-view>
<view class="preview-footer">
<button class="preview-close-btn" @click="closePreview">关闭</button>
</view>
</view>
</uni-popup>
</view>
</template>
<script setup>
import { ref, onMounted } from "vue";
2026-01-27 15:07:39 +08:00
import { onLoad } from "@dcloudio/uni-app";
2026-01-28 16:35:14 +08:00
import api from "@/utils/api.js";
2026-01-26 18:08:01 +08:00
import useAccountStore from "@/store/account.js";
import EmptyData from "@/components/empty-data.vue";
const accountStore = useAccountStore();
2026-01-27 15:07:39 +08:00
const env = __VITE_ENV__;
2026-01-26 18:08:01 +08:00
const corpId = env.MP_CORP_ID;
2026-01-27 15:07:39 +08:00
// 从页面参数获取群组信息
const pageParams = ref({
groupId: "",
userId: "",
corpId: "",
});
2026-01-26 18:08:01 +08:00
// 搜索关键词
const searchTitle = ref("");
let searchTimer = null;
// 分类列表
2026-01-27 13:42:59 +08:00
const categoryList = ref([{ _id: "", label: "全部" }]);
const currentCateId = ref(""); // 默认选中"全部"_id 为空字符串)
2026-01-26 18:08:01 +08:00
// 文章列表
const articleList = ref([]);
const loading = ref(false);
const page = ref(1);
const pageSize = 30;
const total = ref(0);
// 预览文章数据
const previewArticleData = ref({
title: "",
content: "",
});
const previewPopup = ref(null);
// 获取分类列表
const getCategoryList = async () => {
try {
2026-01-28 16:35:14 +08:00
const res = await api("getArticleCateList", { corpId: corpId });
2026-01-26 18:08:01 +08:00
if (res.success && res.list) {
const cates = res.list || [];
2026-01-27 13:42:59 +08:00
categoryList.value = [{ _id: "", label: "全部" }, ...cates];
2026-01-26 18:08:01 +08:00
}
} catch (error) {
console.error("获取分类列表失败:", error);
}
};
// 选择分类
const selectCategory = (cate) => {
currentCateId.value = cate._id || "";
page.value = 1;
articleList.value = [];
loadArticleList();
};
// 搜索处理
const handleSearch = () => {
if (searchTimer) {
clearTimeout(searchTimer);
}
searchTimer = setTimeout(() => {
page.value = 1;
articleList.value = [];
loadArticleList();
}, 500);
};
// 加载文章列表
const loadArticleList = async () => {
if (loading.value) return;
loading.value = true;
try {
const params = {
corpId: corpId,
page: page.value,
pageSize: pageSize,
enable: true,
title: searchTitle.value,
};
// 如果选择了分类添加分类ID
if (currentCateId.value) {
params.cateIds = [currentCateId.value];
}
2026-01-28 16:35:14 +08:00
const res = await api("getArticleList", params);
2026-01-26 18:08:01 +08:00
if (res.success && res.list) {
const { list = [], total: count = 0 } = res;
const formattedList = list.map((item) => {
// 格式化日期
let date = "";
if (item.createTime) {
const d = new Date(item.createTime);
const year = d.getFullYear();
const month = String(d.getMonth() + 1).padStart(2, "0");
const day = String(d.getDate()).padStart(2, "0");
date = `${year}-${month}-${day}`;
}
return {
...item,
date,
};
});
if (page.value === 1) {
articleList.value = formattedList;
} else {
articleList.value = [...articleList.value, ...formattedList];
}
total.value = count;
} else {
uni.showToast({
title: res.message || "获取文章列表失败",
icon: "none",
});
}
} catch (error) {
console.error("加载文章列表失败:", error);
uni.showToast({
title: "加载失败,请重试",
icon: "none",
});
} finally {
loading.value = false;
}
};
// 加载更多
const loadMore = () => {
if (loading.value || articleList.value.length >= total.value) return;
page.value += 1;
loadArticleList();
};
2026-01-27 13:42:59 +08:00
// 处理富文本内容,使图片自适应
const processRichTextContent = (html) => {
2026-01-27 15:07:39 +08:00
if (!html) return "";
2026-01-27 13:42:59 +08:00
// 给所有 img 标签添加样式
let processedHtml = html.replace(
/<img/gi,
'<img style="max-width:100%;height:auto;display:block;margin:10px 0;"'
);
2026-01-27 15:07:39 +08:00
2026-01-27 13:42:59 +08:00
// 移除可能存在的固定宽度样式
processedHtml = processedHtml.replace(
/style="[^"]*width:\s*\d+px[^"]*"/gi,
(match) => {
2026-01-27 15:07:39 +08:00
return match.replace(/width:\s*\d+px;?/gi, "max-width:100%;");
2026-01-27 13:42:59 +08:00
}
);
2026-01-27 15:07:39 +08:00
2026-01-27 13:42:59 +08:00
// 处理表格,添加自适应样式
processedHtml = processedHtml.replace(
/<table/gi,
'<table style="max-width:100%;overflow-x:auto;display:block;"'
);
2026-01-27 15:07:39 +08:00
2026-01-27 13:42:59 +08:00
// 给整体内容添加容器样式
processedHtml = `<div style="width:100%;overflow-x:hidden;word-wrap:break-word;word-break:break-all;">${processedHtml}</div>`;
2026-01-27 15:07:39 +08:00
2026-01-27 13:42:59 +08:00
return processedHtml;
};
2026-01-26 18:08:01 +08:00
// 预览文章
const previewArticle = async (article) => {
try {
uni.showLoading({ title: "加载中..." });
2026-01-28 16:35:14 +08:00
const res = await api("getArticle", { id: article._id, corpId: corpId });
2026-01-26 18:08:01 +08:00
uni.hideLoading();
2026-01-27 13:42:59 +08:00
if (res.success && res.data) {
2026-01-26 18:08:01 +08:00
previewArticleData.value = {
title: res.data.title || article.title,
2026-01-27 13:42:59 +08:00
content: processRichTextContent(res.data.content || ""),
2026-01-26 18:08:01 +08:00
};
previewPopup.value?.open();
} else {
uni.showToast({
title: res.message || "预览文章失败",
icon: "none",
});
}
} catch (error) {
uni.hideLoading();
console.error("预览文章失败:", error);
uni.showToast({
title: "预览失败,请重试",
icon: "none",
});
}
};
// 关闭预览
const closePreview = () => {
previewPopup.value?.close();
};
// 发送文章
2026-01-27 15:07:39 +08:00
const sendArticle = async (article) => {
try {
const { doctorInfo } = useAccountStore();
2026-01-28 16:35:14 +08:00
const result = await api("sendArticleMessage", {
2026-01-27 15:07:39 +08:00
groupId: pageParams.value.groupId,
2026-01-28 16:35:14 +08:00
fromAccount: doctorInfo.userid,
2026-01-27 15:07:39 +08:00
articleId: article._id,
title: article.title || "宣教文章",
imgUrl: article.cover || "",
desc: "点击查看详情",
2026-01-26 18:08:01 +08:00
});
2026-01-27 15:07:39 +08:00
if (result.success) {
uni.navigateBack();
} else {
throw new Error(result.message || "发送失败");
}
} catch (error) {
console.error("发送文章失败:", error);
2026-01-26 18:08:01 +08:00
uni.showToast({
2026-01-27 15:07:39 +08:00
title: error.message || "发送失败",
icon: "none",
2026-01-26 18:08:01 +08:00
});
2026-01-27 15:07:39 +08:00
} finally {
loading.value = false;
2026-01-26 18:08:01 +08:00
}
};
// 返回
const goBack = () => {
uni.navigateBack();
};
2026-01-27 15:07:39 +08:00
// 页面加载时接收参数
onLoad((options) => {
if (options.groupId) {
pageParams.value.groupId = options.groupId;
}
if (options.userId) {
pageParams.value.userId = options.userId;
}
if (options.corpId) {
pageParams.value.corpId = options.corpId;
}
});
2026-01-26 18:08:01 +08:00
onMounted(() => {
getCategoryList();
loadArticleList();
});
</script>
<style scoped lang="scss">
.article-page {
display: flex;
flex-direction: column;
height: 100vh;
background-color: #f5f5f5;
}
.header {
background-color: #fff;
padding: 20rpx;
border-bottom: 1px solid #eee;
}
.search-bar {
display: flex;
align-items: center;
background-color: #f5f5f5;
border-radius: 8rpx;
padding: 16rpx 24rpx;
}
.search-input {
flex: 1;
margin-left: 16rpx;
font-size: 28rpx;
}
.content {
flex: 1;
display: flex;
overflow: hidden;
}
.category-sidebar {
width: 200rpx;
background-color: #f8f8f8;
border-right: 1px solid #eee;
}
.category-scroll {
height: 100%;
}
.category-item {
2026-01-27 13:42:59 +08:00
padding: 20rpx 24rpx;
2026-01-26 18:08:01 +08:00
font-size: 28rpx;
color: #333;
text-align: center;
border-bottom: 1px solid #eee;
2026-01-27 13:42:59 +08:00
transition: all 0.3s ease;
position: relative;
2026-01-26 18:08:01 +08:00
}
.category-item.active {
background-color: #fff;
2026-01-27 15:07:39 +08:00
color: #0877f1;
2026-01-26 18:08:01 +08:00
font-weight: bold;
2026-01-27 15:07:39 +08:00
border-left: 4rpx solid #0877f1;
2026-01-26 18:08:01 +08:00
}
.article-list {
flex: 1;
background-color: #fff;
}
.article-scroll {
height: 100%;
}
.loading-container,
.empty-container {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
padding: 100rpx 0;
}
.loading-text {
margin-top: 20rpx;
font-size: 28rpx;
color: #999;
}
.article-item {
padding: 24rpx 30rpx;
border-bottom: 1px solid #eee;
2026-01-27 13:42:59 +08:00
transition: background-color 0.2s ease;
}
.article-item:active {
background-color: #f5f5f5;
2026-01-26 18:08:01 +08:00
}
.article-content {
display: flex;
flex-direction: column;
2026-01-27 13:42:59 +08:00
gap: 16rpx;
2026-01-26 18:08:01 +08:00
}
.article-title {
font-size: 28rpx;
color: #333;
2026-01-27 13:42:59 +08:00
line-height: 1.6;
2026-01-26 18:08:01 +08:00
word-break: break-all;
2026-01-27 13:42:59 +08:00
font-weight: 500;
}
.article-footer {
display: flex;
align-items: center;
justify-content: space-between;
gap: 20rpx;
2026-01-26 18:08:01 +08:00
}
.article-date {
2026-01-27 13:42:59 +08:00
flex: 1;
2026-01-26 18:08:01 +08:00
font-size: 24rpx;
color: #999;
}
.send-btn {
2026-01-27 13:42:59 +08:00
flex-shrink: 0;
2026-01-26 18:08:01 +08:00
font-size: 26rpx;
2026-01-27 13:42:59 +08:00
padding: 8rpx 32rpx;
height: auto;
line-height: 1.4;
2026-01-26 18:08:01 +08:00
}
.loading-more,
.no-more {
display: flex;
align-items: center;
justify-content: center;
padding: 30rpx 0;
font-size: 24rpx;
color: #999;
gap: 10rpx;
}
.footer {
background-color: #fff;
padding: 20rpx;
border-top: 1px solid #eee;
}
.cancel-btn {
width: 100%;
background-color: #fff;
color: #333;
border: 1px solid #ddd;
}
/* 预览弹窗样式 */
.preview-container {
background-color: #fff;
border-radius: 20rpx 20rpx 0 0;
height: 80vh;
display: flex;
flex-direction: column;
}
.preview-header {
display: flex;
align-items: center;
justify-content: space-between;
padding: 30rpx;
border-bottom: 1px solid #eee;
}
.preview-title {
flex: 1;
font-size: 32rpx;
font-weight: bold;
color: #333;
}
.preview-close {
padding: 10rpx;
}
.preview-content {
flex: 1;
2026-01-27 13:42:59 +08:00
padding: 0;
2026-01-26 18:08:01 +08:00
overflow-y: auto;
}
2026-01-27 13:42:59 +08:00
.rich-text-wrapper {
padding: 30rpx;
width: 100%;
box-sizing: border-box;
overflow: hidden;
}
/* rich-text 内部样式 */
.rich-text-wrapper ::v-deep rich-text {
width: 100%;
}
.rich-text-wrapper ::v-deep rich-text img {
max-width: 100% !important;
height: auto !important;
display: block;
}
2026-01-26 18:08:01 +08:00
.preview-footer {
padding: 20rpx;
border-top: 1px solid #eee;
}
.preview-close-btn {
width: 100%;
background-color: #1890ff;
color: #fff;
}
</style>