2025/9/26/1:02
This commit is contained in:
@@ -1,9 +1,15 @@
|
||||
// app.ts
|
||||
|
||||
import { CardData, Comment, IAppOption } from "../typings";
|
||||
import envConfig from "./env";
|
||||
|
||||
App<IAppOption>({
|
||||
globalData: {
|
||||
token: "",
|
||||
userInfo: null
|
||||
userInfo: null,
|
||||
rawCardData: [
|
||||
] ,
|
||||
processedCardsData: []
|
||||
},
|
||||
onLaunch() {
|
||||
const token = wx.getStorageSync("token");
|
||||
@@ -11,6 +17,82 @@ App<IAppOption>({
|
||||
this.globalData.token = token;
|
||||
}
|
||||
},
|
||||
fetchRawCardData(): Promise<CardData[]> {
|
||||
return new Promise((resolve, reject) => {
|
||||
wx.request({
|
||||
url: `${envConfig.apiBaseUrl}/article/get`,
|
||||
method: "POST",
|
||||
data: {
|
||||
uid: "1c3e5a7d-9b1f-4c3a-8e5f-7d9b1c3e5a7d"
|
||||
},
|
||||
success: (res) => {
|
||||
if (res.data && res.data.success && Array.isArray(res.data.data)) {
|
||||
const rawData = res.data.data as CardData[];
|
||||
this.globalData.rawCardData = rawData;
|
||||
|
||||
const processedData = this.processVoteData(rawData);
|
||||
this.globalData.processedCardsData = processedData;
|
||||
|
||||
console.log("成功获取文评列表:", processedData);
|
||||
resolve(processedData);
|
||||
} else {
|
||||
const error = new Error("接口返回格式不正确");
|
||||
console.error(error, res);
|
||||
reject(error);
|
||||
}
|
||||
},
|
||||
fail: (err) => {
|
||||
console.error("拉取投票数据失败", err);
|
||||
reject(err);
|
||||
}
|
||||
});
|
||||
});
|
||||
},
|
||||
getComments(articleId: number): Promise<Comment[]> {
|
||||
return new Promise((resolve, reject) => {
|
||||
wx.request({
|
||||
url: `${envConfig.apiBaseUrl}/comment/get`,
|
||||
method: "POST",
|
||||
data: {
|
||||
articleId: articleId // int 类型
|
||||
},
|
||||
success: (res) => {
|
||||
const response = res.data as CommentResponse;
|
||||
|
||||
if (response.success && Array.isArray(response.data)) {
|
||||
resolve(response.data);
|
||||
} else {
|
||||
reject(new Error(response.message || "获取评论失败"));
|
||||
}
|
||||
},
|
||||
fail: (err) => {
|
||||
reject(new Error("网络请求失败"));
|
||||
}
|
||||
});
|
||||
});
|
||||
},
|
||||
// 处理投票数据的方法
|
||||
processVoteData(rawData: CardData[]): CardData[] {
|
||||
return rawData.map(card => {
|
||||
// 计算每个选项的百分比
|
||||
const optionsWithPercentage = card.options.map(option => {
|
||||
const percentage = card.total_voters > 0
|
||||
? Math.round((option.votes / card.total_voters) * 100)
|
||||
: 0;
|
||||
|
||||
return {
|
||||
...option,
|
||||
percentage,
|
||||
isSelected: false // 初始化选中状态
|
||||
};
|
||||
});
|
||||
|
||||
return {
|
||||
...card,
|
||||
options: optionsWithPercentage
|
||||
};
|
||||
});
|
||||
},
|
||||
setToken(token: string) {
|
||||
this.globalData.token = token;
|
||||
wx.setStorageSync("token", token); // 同步到缓存,持久化存储
|
||||
|
||||
Reference in New Issue
Block a user