buddhism/pages/institutionalStructure/mixins/donation-mixin.js

238 lines
6.1 KiB
JavaScript
Raw Normal View History

2025-08-01 16:01:58 +08:00
/**
* 捐款记录相关 Mixin
* 提供捐款记录的数据获取搜索分页等功能
* 基于重构后的 data-manager.js
2025-08-01 16:01:58 +08:00
*/
2025-09-16 10:00:58 +08:00
import { dataManagerMixin } from "./data-manager.js";
import { getDonorList } from "@/api/donor/donor.js";
import { getInstitutionalDetail } from "@/api/institutionalStructure/institutionalStructureDetail.js";
2025-08-01 16:01:58 +08:00
export const donationMixin = {
mixins: [dataManagerMixin],
2025-08-14 11:22:53 +08:00
2025-08-01 16:01:58 +08:00
data() {
return {
// 项目信息
2025-08-01 16:01:58 +08:00
projectInfo: {},
// 搜索关键词
2025-09-16 10:00:58 +08:00
searchKeyword: "",
// 项目ID
2025-09-16 10:00:58 +08:00
formedId: "",
getBaseParams: {
minAmount: 0,
maxAmount: 10000,
sortAmount: "amount",
orderAmount: "asc",
sortTime: "time",
orderTime: "desc",
},
2025-09-16 10:00:58 +08:00
};
2025-08-01 16:01:58 +08:00
},
computed: {
// 计算总造价(从项目详情获取,如果没有则计算捐款总和)
totalAmount() {
2025-08-14 11:22:53 +08:00
return (
2025-09-16 10:00:58 +08:00
this.projectInfo.totalAmount ||
this.dataList.reduce((sum, item) => sum + item.amount, 0)
);
2025-08-01 16:01:58 +08:00
},
// 计算参与捐款人次(从项目详情获取,如果没有则计算捐款记录数量)
participantCount() {
2025-09-16 10:00:58 +08:00
return this.projectInfo.donorCount || this.dataList.length;
2025-08-14 11:22:53 +08:00
},
2025-08-01 16:01:58 +08:00
},
methods: {
/**
* 获取项目详情
*/
async loadProjectInfo() {
if (!this.formedId) {
2025-09-16 10:00:58 +08:00
console.error("缺少项目ID");
return;
2025-08-01 16:01:58 +08:00
}
2025-08-14 11:22:53 +08:00
2025-08-01 16:01:58 +08:00
try {
2025-09-16 10:00:58 +08:00
const response = await getInstitutionalDetail(this.formedId);
console.log("项目详情API响应:", response);
2025-08-01 16:01:58 +08:00
if (response.code === 200) {
if (response.data) {
2025-09-16 10:00:58 +08:00
this.projectInfo = response.data;
console.log("使用 response.data 作为项目信息");
}
2025-08-01 16:01:58 +08:00
}
} catch (error) {
2025-09-16 10:00:58 +08:00
console.error("获取项目信息失败:", error);
2025-08-01 16:01:58 +08:00
uni.showToast({
2025-09-16 10:00:58 +08:00
title: "网络错误",
icon: "none",
});
2025-08-01 16:01:58 +08:00
}
},
/**
* 捐款记录数据转换器
*/
transformDonationData(dataArray) {
2025-09-16 10:00:58 +08:00
return dataArray.map((item) => ({
id: item.id,
name: item.realName,
amount: item.amount,
2025-09-16 10:00:58 +08:00
// time: this.formatDate(item.donationDate),
time: this.formatDate(item.createTime),
}));
},
/**
* 格式化日期
* @param {string} dateStr 日期字符串
* @returns {string} 格式化后的日期
*/
formatDate(dateStr) {
2025-09-16 10:00:58 +08:00
if (!dateStr) return "";
const date = new Date(dateStr);
const year = date.getFullYear();
const month = String(date.getMonth() + 1).padStart(2, "0");
const day = String(date.getDate()).padStart(2, "0");
return `${year}/${month}/${day}`;
},
2025-08-01 16:01:58 +08:00
/**
* 获取捐款记录
* @param {string} keyword 搜索关键词
*/
2025-09-16 10:00:58 +08:00
async loadDonationRecords(keyword = "") {
const baseParams = { ...this.getBaseParams, formedId: this.formedId };
2025-08-14 11:22:53 +08:00
// 如果有搜索关键词,添加姓名搜索
if (keyword) {
2025-09-16 10:00:58 +08:00
baseParams.realName = keyword;
2025-08-01 16:01:58 +08:00
}
await this.refreshData({
apiCall: getDonorList,
dataTransformer: this.transformDonationData,
params: baseParams,
2025-09-16 10:00:58 +08:00
dataPath: "data.list.rows",
2025-09-17 16:00:22 +08:00
totalPath: "data.list.total",
onSuccess: (data, response) => {
2025-09-16 10:00:58 +08:00
console.log("捐款记录加载成功:", data.length, "条");
},
2025-09-16 10:00:58 +08:00
onError: (errorMsg) => {
console.error("捐款记录加载失败:", errorMsg);
2025-08-14 11:22:53 +08:00
},
2025-09-16 10:00:58 +08:00
});
2025-08-01 16:01:58 +08:00
},
/**
* 搜索捐款记录
* @param {string} val 搜索关键词
*/
async onSearch(val) {
console.log("val", val);
2025-09-16 10:00:58 +08:00
this.searchKeyword = val;
2025-09-17 16:00:22 +08:00
console.log("this.searchKeyword", this.searchKeyword);
2025-09-17 16:00:22 +08:00
await this.searchData(
{
...this.getBaseParams,
formedId: this.formedId,
realName: val.trim(),
},
{
apiCall: getDonorList,
dataTransformer: this.transformDonationData,
2025-09-16 10:00:58 +08:00
dataPath: "data.list.rows",
2025-09-17 16:00:22 +08:00
totalPath: "data.list.total",
onSuccess: (data, response) => {
2025-09-16 10:00:58 +08:00
console.log("搜索完成,找到:", data.length, "条记录");
2025-08-14 11:22:53 +08:00
},
2025-09-16 10:00:58 +08:00
},
);
2025-08-01 16:01:58 +08:00
},
/**
* 筛选功能
*/
async filterSearch(params) {
this.getBaseParams = params;
console.log("this.searchKeyword", this.searchKeyword);
await this.searchData(
{
...this.getBaseParams,
formedId: this.formedId,
realName: this.searchKeyword.trim(),
},
{
apiCall: getDonorList,
dataTransformer: this.transformDonationData,
dataPath: "data.list.rows",
totalPath: "data.list.total",
onSuccess: (data, response) => {
console.log("搜索完成,找到:", data.length, "条记录");
},
},
);
},
2025-08-01 16:01:58 +08:00
/**
* 加载更多捐款记录
2025-08-01 16:01:58 +08:00
*/
async loadMoreDonationRecords() {
2025-09-16 10:00:58 +08:00
const currentParams = this.getDataState().currentParams;
2025-08-14 11:22:53 +08:00
await this.loadMoreData({
apiCall: getDonorList,
dataTransformer: this.transformDonationData,
params: currentParams,
2025-09-16 10:00:58 +08:00
dataPath: "data.list.rows",
2025-09-17 16:00:22 +08:00
totalPath: "data.list.total",
onSuccess: (data, response) => {
2025-09-16 10:00:58 +08:00
console.log("加载更多完成,新增:", data.length, "条记录");
2025-08-14 11:22:53 +08:00
},
2025-09-16 10:00:58 +08:00
});
2025-08-01 16:01:58 +08:00
},
/**
* 初始化数据
* @param {string} formedId 建制ID
*/
async initData(formedId) {
2025-09-16 10:00:58 +08:00
console.log("初始化捐款记录数据, formedId:", formedId);
2025-08-14 11:22:53 +08:00
try {
2025-09-16 10:00:58 +08:00
this.formedId = formedId;
await this.loadProjectInfo();
await this.loadDonationRecords();
console.log("捐款记录数据初始化完成");
} catch (error) {
2025-09-16 10:00:58 +08:00
console.error("初始化捐款记录数据失败:", error);
uni.showToast({
2025-09-16 10:00:58 +08:00
title: "初始化数据失败",
icon: "none",
});
}
},
/**
* 重置搜索
*/
resetSearch() {
2025-09-16 10:00:58 +08:00
this.searchKeyword = "";
this.loadDonationRecords();
},
/**
* 刷新数据
*/
async refreshDonationData() {
2025-09-16 10:00:58 +08:00
await this.loadDonationRecords(this.searchKeyword);
2025-08-14 11:22:53 +08:00
},
},
2025-09-16 10:00:58 +08:00
};