fix subscribe

This commit is contained in:
jxxghp
2023-07-02 00:17:50 +08:00
parent b2b819ae3f
commit af40056809
4 changed files with 77 additions and 4 deletions

View File

@@ -1 +1 @@
VITE_API_BASE_URL=http://10.10.10.10:3001/api/v1/ VITE_API_BASE_URL=http://localhost:3001/api/v1/

View File

@@ -90,6 +90,8 @@ export interface MediaInfo {
title?: string title?: string
// 年份 // 年份
year?: string year?: string
// 季号
season?: number;
// TMDB ID // TMDB ID
tmdb_id?: number tmdb_id?: number
// IMDB ID // IMDB ID

View File

@@ -1,5 +1,6 @@
<script lang="ts" setup> <script lang="ts" setup>
import { MediaInfo } from "@/api/types"; import api from "@/api";
import { MediaInfo, Subscribe } from "@/api/types";
// 输入参数 // 输入参数
const props = defineProps({ const props = defineProps({
@@ -11,6 +12,10 @@ const props = defineProps({
// 图片加载状态 // 图片加载状态
const isImageLoaded = ref(false); const isImageLoaded = ref(false);
// 订阅状态
const isSubscribed = ref(false);
// 角标颜色
const getChipColor = (type: string) => { const getChipColor = (type: string) => {
if (type === "电影") { if (type === "电影") {
return "border-blue-500 bg-blue-600"; return "border-blue-500 bg-blue-600";
@@ -20,6 +25,69 @@ const getChipColor = (type: string) => {
return "border-purple-600 bg-purple-600"; return "border-purple-600 bg-purple-600";
} }
}; };
// 添加订阅
const addSubscribe = async () => {
try {
const result: { [key: string]: any } = await api.post("subscribe", {
name: props.media?.title,
type: props.media?.type,
year: props.media?.year,
tmdbid: props.media?.tmdb_id,
doubanid: props.media?.douban_id,
season: props.media?.season,
});
isSubscribed.value = result.success || false;
} catch (error) {
console.error(error);
}
};
// 取消订阅
const removeSubscribe = async () => {
try {
let mediaid = props.media?.tmdb_id
? `tmdb:${props.media?.tmdb_id}`
: `douban:${props.media?.douban_id}`;
const result: { [key: string]: any } = await api.delete(`subscribe/${mediaid}`, {
params: {
season: props.media?.season,
},
});
isSubscribed.value = !(result.success || false);
} catch (error) {
console.error(error);
}
};
// 查询是否已订阅
const checkSubscribe = async () => {
try {
let mediaid = props.media?.tmdb_id
? `tmdb:${props.media?.tmdb_id}`
: `douban:${props.media?.douban_id}`;
const result: Subscribe = await api.get(`subscribe/${mediaid}`, {
params: {
season: props.media?.season,
},
});
isSubscribed.value = !!result.id;
} catch (error) {
console.error(error);
}
};
// 订阅按钮响应
const handleSubscribe = () => {
if (isSubscribed.value) {
removeSubscribe();
} else {
addSubscribe();
}
};
// 装载时检查是否已订阅
onMounted(checkSubscribe);
</script> </script>
<template> <template>
@@ -83,7 +151,11 @@ const getChipColor = (type: string) => {
</p> </p>
<div class="flex align-center justify-between"> <div class="flex align-center justify-between">
<IconBtn icon="mdi-magnify" color="white" /> <IconBtn icon="mdi-magnify" color="white" />
<IconBtn icon="mdi-heart" color="white" /> <IconBtn
icon="mdi-heart"
:color="isSubscribed ? 'error' : 'white'"
@click="handleSubscribe"
/>
</div> </div>
</VCardText> </VCardText>
</VImg> </VImg>

View File

@@ -42,7 +42,6 @@ const getParams = () => {
// 获取订阅列表数据 // 获取订阅列表数据
const fetchData = async ({ done }) => { const fetchData = async ({ done }) => {
console.log("entry", props.apipath);
try { try {
if (!props.apipath) { if (!props.apipath) {
return; return;