85 lines
2.1 KiB
TypeScript
85 lines
2.1 KiB
TypeScript
export default function useGetAiReviewResult({
|
|
cardInfo,
|
|
updateAiReview,
|
|
startAiReviewFn,
|
|
getAiReviewResultFn,
|
|
}: {
|
|
cardInfo: any;
|
|
updateAiReview: (ai_review: any) => void;
|
|
startAiReviewFn: (params: {
|
|
id: string;
|
|
platform: string;
|
|
content: string;
|
|
writerCode: string | undefined;
|
|
}) => Promise<any>;
|
|
getAiReviewResultFn: (id: string, ticket: string, writerCode: string | undefined) => Promise<any>;
|
|
}) {
|
|
const route = useRoute();
|
|
const statusPollingTimer = ref<number | null>(null);
|
|
const ticket = ref('');
|
|
const checkLoading = ref(false);
|
|
const checkResult = ref<any>({});
|
|
|
|
const writerCode = computed(() => route.params.writerCode);
|
|
|
|
const handleStartCheck = async () => {
|
|
checkLoading.value = true;
|
|
const { id, platform, content } = cardInfo.value;
|
|
const { code, data } = await startAiReviewFn({
|
|
id,
|
|
platform,
|
|
content,
|
|
writerCode: writerCode.value as string | undefined,
|
|
});
|
|
if (code === 200) {
|
|
ticket.value = data.ticket;
|
|
startStatusPolling();
|
|
}
|
|
};
|
|
const resetAiReviewInfo = () => {
|
|
checkResult.value = {};
|
|
ticket.value = '';
|
|
clearStatusPollingTimer();
|
|
};
|
|
const handleAgainCheck = async () => {
|
|
resetAiReviewInfo();
|
|
handleStartCheck();
|
|
};
|
|
const startStatusPolling = () => {
|
|
clearStatusPollingTimer();
|
|
statusPollingTimer.value = setInterval(async () => {
|
|
const { code, data } = await getAiReviewResultFn(
|
|
cardInfo.value.id,
|
|
ticket.value,
|
|
writerCode.value as string | undefined,
|
|
);
|
|
if (code === 200 && data.status === 1) {
|
|
checkResult.value = data.ai_review;
|
|
updateAiReview?.(data.ai_review);
|
|
checkLoading.value = false;
|
|
clearStatusPollingTimer();
|
|
}
|
|
}, 3000);
|
|
};
|
|
|
|
const clearStatusPollingTimer = () => {
|
|
if (statusPollingTimer.value) {
|
|
clearInterval(statusPollingTimer.value);
|
|
statusPollingTimer.value = null;
|
|
}
|
|
};
|
|
|
|
onUnmounted(() => {
|
|
clearStatusPollingTimer();
|
|
});
|
|
|
|
return {
|
|
handleStartCheck,
|
|
handleAgainCheck,
|
|
checkResult,
|
|
checkLoading,
|
|
ticket,
|
|
resetAiReviewInfo,
|
|
};
|
|
}
|