2026-03-09 21:29:03 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 投票服务
|
|
|
|
|
|
* 处理投票相关的API调用
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
2026-04-13 01:30:37 +08:00
|
|
|
|
import { api } from '../core/api';
|
|
|
|
|
|
import { Post, VoteResultDTO, CreateVotePostRequest } from '@/types';
|
2026-03-09 21:29:03 +08:00
|
|
|
|
|
|
|
|
|
|
// 投票响应
|
|
|
|
|
|
interface VoteResponse {
|
|
|
|
|
|
success: boolean;
|
|
|
|
|
|
message?: string;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 更新投票选项响应
|
|
|
|
|
|
interface UpdateVoteOptionResponse {
|
|
|
|
|
|
success: boolean;
|
|
|
|
|
|
message?: string;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 投票服务类
|
|
|
|
|
|
*/
|
|
|
|
|
|
class VoteService {
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 创建投票帖子
|
|
|
|
|
|
* @param data 创建投票帖子请求数据
|
|
|
|
|
|
* @returns 创建的帖子或null
|
|
|
|
|
|
*/
|
|
|
|
|
|
async createVotePost(data: CreateVotePostRequest): Promise<Post | null> {
|
|
|
|
|
|
// 验证投票选项数量
|
|
|
|
|
|
if (!data.vote_options || data.vote_options.length < 2) {
|
|
|
|
|
|
console.error('[VoteService] 投票选项至少需要2个');
|
|
|
|
|
|
return null;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (data.vote_options.length > 10) {
|
|
|
|
|
|
console.error('[VoteService] 投票选项最多10个');
|
|
|
|
|
|
return null;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const response = await api.post<Post>('/posts/vote', data);
|
|
|
|
|
|
return response.data;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 获取投票结果
|
|
|
|
|
|
* @param postId 帖子ID
|
|
|
|
|
|
* @returns 投票结果或null
|
|
|
|
|
|
*/
|
|
|
|
|
|
async getVoteResult(postId: string): Promise<VoteResultDTO | null> {
|
|
|
|
|
|
try {
|
|
|
|
|
|
const response = await api.get<VoteResultDTO>(`/posts/${postId}/vote`);
|
|
|
|
|
|
return response.data;
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
|
console.error('[VoteService] 获取投票结果失败:', error);
|
|
|
|
|
|
return null;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 投票
|
|
|
|
|
|
* @param postId 帖子ID
|
|
|
|
|
|
* @param optionId 选项ID
|
|
|
|
|
|
* @returns 是否成功
|
|
|
|
|
|
*/
|
|
|
|
|
|
async vote(postId: string, optionId: string): Promise<boolean> {
|
|
|
|
|
|
try {
|
|
|
|
|
|
const response = await api.post<VoteResponse>(`/posts/${postId}/vote`, {
|
|
|
|
|
|
option_id: optionId,
|
|
|
|
|
|
});
|
|
|
|
|
|
return response.data.success;
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
|
console.error('[VoteService] 投票失败:', error);
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 取消投票
|
|
|
|
|
|
* @param postId 帖子ID
|
|
|
|
|
|
* @returns 是否成功
|
|
|
|
|
|
*/
|
|
|
|
|
|
async unvote(postId: string): Promise<boolean> {
|
|
|
|
|
|
try {
|
|
|
|
|
|
const response = await api.delete<VoteResponse>(`/posts/${postId}/vote`);
|
|
|
|
|
|
return response.data.success;
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
|
console.error('[VoteService] 取消投票失败:', error);
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 更新投票选项(作者权限)
|
|
|
|
|
|
* @param optionId 选项ID
|
|
|
|
|
|
* @param content 新内容
|
|
|
|
|
|
* @param postId 帖子ID
|
|
|
|
|
|
* @returns 是否成功
|
|
|
|
|
|
*/
|
|
|
|
|
|
async updateVoteOption(optionId: string, content: string, postId: string): Promise<boolean> {
|
|
|
|
|
|
try {
|
|
|
|
|
|
const response = await api.put<UpdateVoteOptionResponse>(
|
|
|
|
|
|
`/posts/${postId}/vote/options/${optionId}`,
|
|
|
|
|
|
{ content }
|
|
|
|
|
|
);
|
|
|
|
|
|
return response.data.success;
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
|
console.error('[VoteService] 更新投票选项失败:', error);
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export const voteService = new VoteService();
|