Files
toutoukan_front/miniprogram/pages/home/home.ts
2025-09-27 23:14:50 +08:00

174 lines
5.5 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import envConfig from "../../env";
Component({
data: {
processedCardsData: [] as any[],
refresherTriggered: false,
url:""
},
lifetimes: {
attached() {
// 从全局获取处理后的卡片数据
const app = getApp();
app.fetchRawCardData().then(cards => {
this.setData({ processedCardsData: cards });
}).catch(err => {
console.log("错误fetchRaw",err)
wx.showToast({ title: '加载失败1', icon: 'none' });
});
const apiBaseUrl = `${envConfig.apiBaseUrl}`;
this.setData({
url:apiBaseUrl
})
},
},
methods: {
processVoteData(cards: any[]): any[] {
return cards.map(card => {
const totalVoters = card.total_voters;
const optionsWithPercentage = card.options.map((option: any) => {
let percentage: number;
if (totalVoters === 0) {
percentage = 0;
} else {
percentage = Number((option.votes / totalVoters * 100).toFixed(0));
}
return {
...option,
percentage: percentage,
isSelected: false
};
});
return {
...card,
options: optionsWithPercentage,
// 在每个卡片对象内部维护自己的选中状态数组
selectedOptions: [] as number[]
};
});
},
selectOption(e: WechatMiniprogram.TouchEvent) {
const { optionId, cardId } = e.currentTarget.dataset as { optionId: number; cardId: number };
const cardIndex = this.data.processedCardsData.findIndex(card => card.article_id === cardId);
if (cardIndex === -1) {
return;
}
const card = this.data.processedCardsData[cardIndex];
if (card.is_ended) {
return;
}
const newProcessedCardsData = [...this.data.processedCardsData];
const currentCard = newProcessedCardsData[cardIndex];
if (currentCard.vote_type === 'multiple') {
// 多选逻辑
const option = currentCard.options.find((o: any) => o.id === optionId);
if (option) {
option.isSelected = !option.isSelected;
}
currentCard.selectedOptions = currentCard.options
.filter((o: any) => o.isSelected)
.map((o: any) => o.id);
} else if (currentCard.vote_type === 'single') {
// 单选逻辑
currentCard.options.forEach((option: any) => {
option.isSelected = (option.id === optionId);
});
currentCard.selectedOptions = currentCard.options
.filter((o: any) => o.isSelected)
.map((o: any) => o.id);
}
this.setData({
processedCardsData: newProcessedCardsData
}, () => {
console.log(`卡片 ${cardId} 当前选中项:`, currentCard.selectedOptions);
});
},
submitVote(e: WechatMiniprogram.TouchEvent) {
const { cardId } = e.currentTarget.dataset as { cardId: number };
const card = this.data.processedCardsData.find(c => c.article_id === cardId);
if (!card) {
console.error('未找到对应的卡片数据');
return;
}
const selected = card.selectedOptions;
if (!selected || selected.length === 0) {
wx.showToast({
title: '请先选择至少一个选项',
icon: 'none',
duration: 2000
});
return;
}
// 在这里输出你想要的信息
console.log(`正在提交卡片ID: ${card.article_id}`);
console.log('所选选项ID:', selected);
// 触发父组件的事件并传递卡片ID和选中的选项
this.triggerEvent('voteSuccess', {
cardId: card.article_id,
selectedOptions: selected,
timestamp: Date.now()
});
wx.showToast({
title: '投票成功',
icon: 'success',
duration: 2000
});
},
goToDetail(e: { currentTarget: { dataset: { id: any; }; }; }) {
const id = e.currentTarget.dataset.id;
console.log(id)
wx.navigateTo({
url: `/pages/articledetail/articledetail?id=${id}`,
});
},
onRefresherPulling() {
console.log("正在下拉...");
},
onRefresherRestore() {
console.log("刷新完成,已恢复原位");
},
onRefresherRefresh() {
console.log("用户文评刷新")
this.setData({
refresherTriggered: true
});
const app = getApp();
setTimeout(()=>{
app.fetchRawCardData().then(cards => {
this.setData({ processedCardsData: cards });
this.setData({
refresherTriggered: false
});
}).catch(err => {
wx.showToast({ title: '加载失败2', icon: 'none' });
this.setData({
refresherTriggered: false
});
});
},2000)
}
}
});