This commit is contained in:
yanghao05
2025-04-29 10:15:36 +08:00
parent 75e39a0283
commit da8ad94eda
9 changed files with 232 additions and 35 deletions

View File

@@ -66,6 +66,7 @@ func (ctl *posts) List(ctx fiber.Ctx, pagination *requests.Pagination, query *Li
}) })
return PostItem{ return PostItem{
ID: item.ID,
Title: item.Title, Title: item.Title,
Description: item.Description, Description: item.Description,
Price: item.Price, Price: item.Price,
@@ -84,6 +85,7 @@ func (ctl *posts) List(ctx fiber.Ctx, pagination *requests.Pagination, query *Li
} }
type PostItem struct { type PostItem struct {
ID int64 `json:"id"`
Bought bool `json:"bought"` Bought bool `json:"bought"`
Title string `json:"title"` Title string `json:"title"`
Description string `json:"description"` Description string `json:"description"`
@@ -127,6 +129,7 @@ func (ctl *posts) Show(ctx fiber.Ctx, id int64, user *model.Users) (*PostItem, e
}) })
return &PostItem{ return &PostItem{
ID: post.ID,
Title: post.Title, Title: post.Title,
Description: post.Description, Description: post.Description,
Content: post.Content, Content: post.Content,

View File

@@ -19,6 +19,13 @@ func (f *Middlewares) Auth(ctx fiber.Ctx) error {
return ctx.Next() return ctx.Next()
} }
user, err := models.Users.GetByID(ctx.Context(), 1)
if err != nil {
return ctx.Send([]byte("User not found"))
}
ctx.Locals("user", user)
return ctx.Next()
fullUrl := string(ctx.Request().URI().FullURI()) fullUrl := string(ctx.Request().URI().FullURI())
u, err := url.Parse(fullUrl) u, err := url.Parse(fullUrl)
if err != nil { if err != nil {
@@ -45,7 +52,7 @@ func (f *Middlewares) Auth(ctx fiber.Ctx) error {
return ctx.Redirect().To(fullUrl) return ctx.Redirect().To(fullUrl)
} }
user, err := models.Users.GetByID(ctx.Context(), jwt.UserID) user, err = models.Users.GetByID(ctx.Context(), jwt.UserID)
if err != nil { if err != nil {
// remove cookie // remove cookie
ctx.ClearCookie("token") ctx.ClearCookie("token")

View File

@@ -1,3 +1,4 @@
import router from '@/router';
import { useAuthStore } from '@/stores/auth'; import { useAuthStore } from '@/stores/auth';
import axios from 'axios'; import axios from 'axios';
@@ -32,7 +33,12 @@ httpClient.interceptors.response.use(
error => { error => {
// Handle HTTP errors here // Handle HTTP errors here
if (error.response) { if (error.response) {
// Server responded with error status // Handle 401 Unauthorized
if (error.response.status === 401) {
const authStore = useAuthStore();
authStore.logout();
router.push('/login');
}
console.error('API Error:', error.response.status, error.response.data); console.error('API Error:', error.response.status, error.response.data);
} else if (error.request) { } else if (error.request) {
// Request made but no response received // Request made but no response received

View File

@@ -0,0 +1,177 @@
{
"page": 2,
"limit": 10,
"total": 103,
"items": [
{
"id": 93,
"bought": false,
"title": "test-title-92",
"description": "test-description-92",
"content": "",
"price": 1356,
"discount": 77,
"views": 7396,
"likes": 3507,
"tags": [
"tag1",
"tag2",
"tag3"
],
"head_images": []
},
{
"id": 92,
"bought": false,
"title": "test-title-91",
"description": "test-description-91",
"content": "",
"price": 394,
"discount": 31,
"views": 6578,
"likes": 5173,
"tags": [
"tag1",
"tag2",
"tag3"
],
"head_images": []
},
{
"id": 91,
"bought": false,
"title": "test-title-90",
"description": "test-description-90",
"content": "",
"price": 8669,
"discount": 47,
"views": 5172,
"likes": 2039,
"tags": [
"tag1",
"tag2",
"tag3"
],
"head_images": []
},
{
"id": 90,
"bought": false,
"title": "test-title-89",
"description": "test-description-89",
"content": "",
"price": 2402,
"discount": 32,
"views": 5388,
"likes": 7944,
"tags": [
"tag1",
"tag2",
"tag3"
],
"head_images": []
},
{
"id": 89,
"bought": false,
"title": "test-title-88",
"description": "test-description-88",
"content": "",
"price": 5696,
"discount": 79,
"views": 782,
"likes": 7308,
"tags": [
"tag1",
"tag2",
"tag3"
],
"head_images": []
},
{
"id": 88,
"bought": false,
"title": "test-title-87",
"description": "test-description-87",
"content": "",
"price": 8014,
"discount": 0,
"views": 395,
"likes": 4078,
"tags": [
"tag1",
"tag2",
"tag3"
],
"head_images": []
},
{
"id": 87,
"bought": false,
"title": "test-title-86",
"description": "test-description-86",
"content": "",
"price": 3076,
"discount": 39,
"views": 2375,
"likes": 7745,
"tags": [
"tag1",
"tag2",
"tag3"
],
"head_images": []
},
{
"id": 86,
"bought": false,
"title": "test-title-85",
"description": "test-description-85",
"content": "",
"price": 1653,
"discount": 60,
"views": 700,
"likes": 2822,
"tags": [
"tag1",
"tag2",
"tag3"
],
"head_images": []
},
{
"id": 85,
"bought": false,
"title": "test-title-84",
"description": "test-description-84",
"content": "",
"price": 6214,
"discount": 16,
"views": 7468,
"likes": 9144,
"tags": [
"tag1",
"tag2",
"tag3"
],
"head_images": []
},
{
"id": 84,
"bought": false,
"title": "test-title-83",
"description": "test-description-83",
"content": "",
"price": 9710,
"discount": 43,
"views": 9576,
"likes": 8256,
"tags": [
"tag1",
"tag2",
"tag3"
],
"head_images": []
}
]
}

View File

@@ -17,23 +17,28 @@ const discountPrice = computed(() => {
return (props.article.price * props.article.discount / 100).toFixed(2) return (props.article.price * props.article.discount / 100).toFixed(2)
}) })
const mediaTypes = computed(() => { const showArticle = (article) => {
return [...new Set(props.article.assets.map(asset => asset.type))] // Since there's no id in the data structure, we might need to use title or other identifier
}) // For now, just console log the article
console.log('Selected article:', article)
router.push(`/posts/${article.id}`)
}
</script> </script>
<template> <template>
<div class="p-4 bg-white rounded-lg shadow-sm hover:shadow transition-all cursor-pointer"> <div class="bg-white rounded-lg shadow overflow-hidden hover:shadow transition-all cursor-pointer"
<div class="flex-1 flex flex-col min-w-0"> @click="showArticle(article)">
<h3 class="text-lg font-medium mb-2 truncate text-gray-800">{{ article.title }}</h3> <div v-if="article.head_images && article.head_images.length > 0" class="w-full h-48 bg-gray-200">
<div class="py-2"> <img :src="article.head_images[0]" class="w-full h-full object-cover" alt="文章图片">
<span v-for="type in mediaTypes" :key="type" </div>
class="inline-block px-1.5 py-0.5 text-xs bg-blue-50 text-blue-600 rounded mr-1.5"> <div class="p-4">
{{ type }} <h3 class="text-lg break-all font-medium text-gray-800 line-clamp-1">
</span> {{ article.title }}
</div> </h3>
<p class="text-gray-500 text-sm line-clamp-2 mb-2">{{ article.description }}</p> <p v-if="article.description" class="text-gray-600 text-sm mb-2 line-clamp-2">{{ article.description }}
</p>
<div class="py-2 flex items-center gap-2"> <div class="py-2 flex items-center gap-2">
<span v-for="tag in article.tags" :key="tag" <span v-for="tag in article.tags" :key="tag"
@@ -42,7 +47,7 @@ const mediaTypes = computed(() => {
</span> </span>
</div> </div>
<div class="flex items-center justify-between mt-auto text-sm py-2"> <div class="flex items-center justify-between mt-auto text-sm">
<div class="flex flex-wrap items-center gap-3"> <div class="flex flex-wrap items-center gap-3">
<span class="flex items-center gap-1 text-gray-700"> <span class="flex items-center gap-1 text-gray-700">
<AiOutlineEye class="w-3.5 h-3.5" /> <AiOutlineEye class="w-3.5 h-3.5" />
@@ -53,9 +58,16 @@ const mediaTypes = computed(() => {
{{ article.likes }} {{ article.likes }}
</span> </span>
</div> </div>
<div class="flex items-center gap-2 ml-4 flex-shrink-0"> <div class="flex items-center gap-2 ml-4 flex-shrink-0 font-mono">
<span class="text-sm font-bold text-red-600">¥{{ discountPrice }}</span> <span v-if="article.bought" class="px-2 py-0.5 text-xs bg-green-50 text-green-600 rounded">
<span class="text-sm line-through text-gray-500">¥{{ article.price }}</span> 已购买
</span>
<template v-else>
<span v-if="article.discount < 100" class="text-xs line-through text-gray-500">
¥{{ article.price }}
</span>
<span class="text-xl text-orange-600">&yen;{{ discountPrice }}</span>
</template>
</div> </div>
</div> </div>
</div> </div>

View File

@@ -2,6 +2,7 @@
import Plyr from 'plyr' import Plyr from 'plyr'
import 'plyr/dist/plyr.css' import 'plyr/dist/plyr.css'
import { onMounted, onUnmounted, ref } from 'vue' import { onMounted, onUnmounted, ref } from 'vue'
import { BsChevronLeft } from 'vue-icons-plus/bs'
import { useRoute, useRouter } from 'vue-router' import { useRoute, useRouter } from 'vue-router'
import { postApi } from '../api/postApi' import { postApi } from '../api/postApi'
@@ -111,10 +112,7 @@ onUnmounted(() => {
<div class="min-h-screen bg-gray-50 flex flex-col"> <div class="min-h-screen bg-gray-50 flex flex-col">
<div class="bg-white h-12 flex items-center px-4 border-b border-gray-100"> <div class="bg-white h-12 flex items-center px-4 border-b border-gray-100">
<button @click="handleBack" class="text-gray-700"> <button @click="handleBack" class="text-gray-700">
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" <BsChevronLeft class="w-6 h-6" />
stroke="currentColor" class="w-6 h-6">
<path stroke-linecap="round" stroke-linejoin="round" d="M15.75 19.5L8.25 12l7.5-7.5" />
</svg>
</button> </button>
</div> </div>

View File

@@ -49,9 +49,6 @@ useIntersectionObserver(
} }
) )
const showArticle = (id) => {
router.push(`/posts/${id}`)
}
const handleSearch = () => { const handleSearch = () => {
articles.value = [] articles.value = []
@@ -86,8 +83,8 @@ onMounted(() => {
暂无文章 暂无文章
</div> </div>
<ArticleListItem v-for="article in articles" :key="article.id" :article="article" <ArticleListItem v-for="article in articles" :key="article.id" :article="{ ...article, bought: true }"
@click="showArticle(article.id)" class="mb-4" /> class="mb-4" />
<div ref="loadingTrigger" class="py-4 text-center" v-show="hasMore || loading"> <div ref="loadingTrigger" class="py-4 text-center" v-show="hasMore || loading">
<div v-if="loading" <div v-if="loading"

View File

@@ -47,10 +47,6 @@ useIntersectionObserver(
} }
) )
const showArticle = (id) => {
router.push(`/posts/${id}`)
}
onMounted(() => { onMounted(() => {
fetchArticles() fetchArticles()
}) })
@@ -70,8 +66,9 @@ onMounted(() => {
暂无已购买 暂无已购买
</div> </div>
<ArticleListItem v-for="article in purchasedArticles" :key="article.id" :article="article" <div v-for="article in purchasedArticles" :key="article.id" class="mb-4">
@click="showArticle(article.id)" class="mb-4" /> <ArticleListItem :article="{ ...article, bought: true }" />
</div>
<div ref="loadingTrigger" class="py-4 text-center" v-show="hasMore || loading"> <div ref="loadingTrigger" class="py-4 text-center" v-show="hasMore || loading">
<div v-if="loading" <div v-if="loading"

View File

@@ -19,7 +19,7 @@ export default defineConfig({
port: 3000, port: 3000,
open: true, open: true,
proxy: { proxy: {
'/api': 'http://localhost:8088', '/v1': 'http://localhost:8088',
} }
} }
}); });