Files
workspace/code/app/app-rn/src/helpers/feed.ts
T
2026-06-25 16:47:54 +08:00

100 lines
2.4 KiB
TypeScript

import { loadDataApi, loadDataPageApi, saveDataApi, getUniqueIdApi } from "@/request/api";
export type FeedType = "family" | "finance" | "favorite";
export interface FeedRecord {
id: number;
family_id: number;
user_id: number;
type: FeedType;
content: string;
meta: string | null;
create_time: string;
}
/** 写入一条动态 */
export async function recordFeed(
familyId: number,
userId: number,
type: FeedType,
content: string,
) {
const idRes = await getUniqueIdApi(1);
const feedId = idRes.data?.[0];
if (!feedId) return;
await saveDataApi([
{
table_name: "b_family_feed",
key_field: "id",
inserts: [
{
id: feedId,
family_id: familyId,
user_id: userId,
type,
content,
},
],
updates: [],
deletes: [],
},
]);
}
/** 加载家庭动态列表 */
export async function loadFeeds(familyId: number): Promise<FeedRecord[]> {
const res = await loadDataApi({
view_name: "b_family_feed",
search_condition: "family_id = ?",
order_by: "create_time DESC",
search_columns: ["id", "family_id", "user_id", "type", "content", "meta", "create_time"],
args: [familyId],
});
return (res.data || []) as FeedRecord[];
}
/** 分页加载家庭动态列表 */
export async function loadFeedsPage(
familyId: number,
page: number,
pageSize: number,
type?: string,
dateFrom?: string,
dateTo?: string,
): Promise<{ rows: FeedRecord[]; total: number; page: number; page_size: number }> {
const conditions: string[] = ["family_id = ?"];
const args: any[] = [familyId];
if (type) {
conditions.push("type = ?");
args.push(type);
}
if (dateFrom) {
conditions.push("create_time >= ?");
args.push(dateFrom);
}
if (dateTo) {
conditions.push("create_time <= ?");
args.push(dateTo + " 23:59:59");
}
const res = await loadDataPageApi({
view_name: "b_family_feed",
search_condition: conditions.join(" AND "),
order_by: "create_time DESC",
search_columns: ["id", "family_id", "user_id", "type", "content", "meta", "create_time"],
args,
page,
page_size: pageSize,
});
if (!res.isSuccess || !res.data) {
return { rows: [], total: 0, page, page_size: pageSize };
}
return { rows: res.data.rows as FeedRecord[], total: res.data.total, page: res.data.page, page_size: res.data.page_size };
}