|
|
|
@ -1,6 +1,5 @@
|
|
|
|
|
import {defineStore} from 'pinia';
|
|
|
|
|
import request from '@/utils/request';
|
|
|
|
|
import { ElMessage } from 'element-plus';
|
|
|
|
|
|
|
|
|
|
export const usePostListStore = defineStore('postList', {
|
|
|
|
|
state: () => ({
|
|
|
|
@ -37,13 +36,12 @@ export const usePostListStore = defineStore('postList', {
|
|
|
|
|
async getList() {
|
|
|
|
|
if (this.loading || this.finished) return;
|
|
|
|
|
this.loading = true;
|
|
|
|
|
const requestData = {
|
|
|
|
|
lastVal:this.lastVal,
|
|
|
|
|
offset: this.offset,
|
|
|
|
|
size: this.pageSize,
|
|
|
|
|
};
|
|
|
|
|
try {
|
|
|
|
|
const res = await request.post('/post/list', requestData);
|
|
|
|
|
// 保证 lastVal 不为 null
|
|
|
|
|
const lastVal = (typeof this.lastVal === 'number' && this.lastVal > 0) ? this.lastVal : Date.now();
|
|
|
|
|
// 拼接参数到URL
|
|
|
|
|
const url = `/post/list?lastVal=${lastVal}&offset=${this.offset}&size=${this.pageSize}`;
|
|
|
|
|
const res = await request.get(url);
|
|
|
|
|
if (res.code === 200) {
|
|
|
|
|
const { records, lastVal: newLastVal, offset: newOffset, size: newSize } = res.data;
|
|
|
|
|
if (records && records.length > 0) {
|
|
|
|
@ -62,7 +60,8 @@ export const usePostListStore = defineStore('postList', {
|
|
|
|
|
userName: post.userName,
|
|
|
|
|
}));
|
|
|
|
|
this.posts = [...this.posts, ...mappedRecords];
|
|
|
|
|
this.lastVal = newLastVal;
|
|
|
|
|
// 只在 newLastVal 有效时才赋值
|
|
|
|
|
this.lastVal = (typeof newLastVal === 'number' && newLastVal > 0) ? newLastVal : lastVal;
|
|
|
|
|
this.offset = newOffset;
|
|
|
|
|
this.pageSize = newSize;
|
|
|
|
|
}
|
|
|
|
@ -70,20 +69,10 @@ export const usePostListStore = defineStore('postList', {
|
|
|
|
|
this.finished = true; // 没有更多数据
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
console.error('获取帖子列表返回错误:', res.msg);
|
|
|
|
|
ElMessage({
|
|
|
|
|
message: res.msg || '获取帖子列表失败,请稍后重试',
|
|
|
|
|
type: 'error',
|
|
|
|
|
duration: 1500
|
|
|
|
|
});
|
|
|
|
|
// 处理错误情况
|
|
|
|
|
}
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error("获取帖子列表失败:", error);
|
|
|
|
|
ElMessage({
|
|
|
|
|
message: error.response?.data?.msg || '获取帖子列表失败,请稍后重试',
|
|
|
|
|
type: 'error',
|
|
|
|
|
duration: 1500
|
|
|
|
|
});
|
|
|
|
|
// 处理异常情况
|
|
|
|
|
} finally {
|
|
|
|
|
this.loading = false;
|
|
|
|
|
}
|
|
|
|
|