159 lines
3.1 KiB
JavaScript
159 lines
3.1 KiB
JavaScript
// pages/articleList/index.js
|
|
import request from '~/api/request';
|
|
Page({
|
|
|
|
/**
|
|
* 页面的初始数据
|
|
*/
|
|
data: {
|
|
title: '',
|
|
articles: [],
|
|
page: 1,
|
|
page_size: 10,
|
|
hasMore: true,
|
|
loading: false,
|
|
searchKeyword: ''
|
|
},
|
|
|
|
// Helper function to strip HTML tags and convert to plain text
|
|
stripHtml(html) {
|
|
if (!html) return '';
|
|
return html.replace(/<[^>]*>/g, '');
|
|
},
|
|
|
|
// 搜索输入处理
|
|
onSearchInput(e) {
|
|
this.setData({
|
|
searchKeyword: e.detail.value
|
|
});
|
|
},
|
|
|
|
// 执行搜索
|
|
async onSearch() {
|
|
this.setData({
|
|
page: 1,
|
|
articles: [],
|
|
hasMore: true
|
|
});
|
|
await this.getList();
|
|
},
|
|
|
|
// 获取文章列表
|
|
async getList(){
|
|
if (this.data.loading || !this.data.hasMore) return;
|
|
|
|
this.setData({ loading: true });
|
|
|
|
try {
|
|
const res = await request('patient/articles', 'get', {
|
|
title: this.data.searchKeyword,
|
|
page: this.data.page,
|
|
page_size: this.data.page_size
|
|
});
|
|
|
|
// Convert rich text content to plain text for each article
|
|
const articlesWithPlainText = res.list.map(article => ({
|
|
...article,
|
|
contentText: this.stripHtml(article.content)
|
|
}));
|
|
|
|
// 如果是第一页,直接替换;否则追加
|
|
const newArticles = this.data.page === 1 ? articlesWithPlainText : [...this.data.articles, ...articlesWithPlainText];
|
|
|
|
this.setData({
|
|
articles: newArticles,
|
|
hasMore: res.list.length === this.data.page_size,
|
|
loading: false
|
|
});
|
|
} catch (error) {
|
|
console.error('获取文章列表失败:', error);
|
|
this.setData({ loading: false });
|
|
wx.showToast({
|
|
title: '获取数据失败',
|
|
icon: 'none'
|
|
});
|
|
}
|
|
},
|
|
|
|
// 加载更多数据
|
|
async loadMore() {
|
|
if (this.data.hasMore && !this.data.loading) {
|
|
this.setData({
|
|
page: this.data.page + 1
|
|
});
|
|
await this.getList();
|
|
}
|
|
},
|
|
|
|
async preview(e){
|
|
console.log(e.currentTarget.dataset.index)
|
|
const index = e.currentTarget.dataset.index
|
|
await wx.setStorageSync('article',JSON.stringify(this.data.articles[index]));
|
|
wx.navigateTo({
|
|
url: '/pages/article/index',
|
|
})
|
|
},
|
|
|
|
/**
|
|
* 生命周期函数--监听页面加载
|
|
*/
|
|
onLoad(options) {
|
|
this.getList()
|
|
},
|
|
|
|
/**
|
|
* 生命周期函数--监听页面初次渲染完成
|
|
*/
|
|
onReady() {
|
|
|
|
},
|
|
|
|
/**
|
|
* 生命周期函数--监听页面显示
|
|
*/
|
|
onShow() {
|
|
|
|
},
|
|
|
|
/**
|
|
* 生命周期函数--监听页面隐藏
|
|
*/
|
|
onHide() {
|
|
|
|
},
|
|
|
|
/**
|
|
* 生命周期函数--监听页面卸载
|
|
*/
|
|
onUnload() {
|
|
|
|
},
|
|
|
|
/**
|
|
* 页面相关事件处理函数--监听用户下拉动作
|
|
*/
|
|
onPullDownRefresh() {
|
|
this.setData({
|
|
page: 1,
|
|
articles: [],
|
|
hasMore: true
|
|
});
|
|
this.getList().then(() => {
|
|
wx.stopPullDownRefresh();
|
|
});
|
|
},
|
|
|
|
/**
|
|
* 页面上拉触底事件的处理函数
|
|
*/
|
|
onReachBottom() {
|
|
this.loadMore();
|
|
},
|
|
|
|
/**
|
|
* 用户点击右上角分享
|
|
*/
|
|
onShareAppMessage() {
|
|
|
|
}
|
|
}) |