From 1702598b11ffe744f8aba35a9e7f220ec19ad1c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=91=E8=BD=BB=E7=8B=82?= <1677568218@qq.com> Date: Fri, 27 Jan 2023 00:57:07 +0800 Subject: [PATCH] feat: add /api/articles/list api --- .../1.0.0/full_documentation.json | 4 + .../composables/{graphql.ts => useGraphql.ts} | 0 frontend/composables/useTime.ts | 16 ++++ frontend/server/api/articles/[id].ts | 2 +- frontend/server/api/articles/list.ts | 92 +++++++++++++++++++ frontend/server/api/articles/tags.ts | 2 +- frontend/server/api/authors/list.ts | 2 +- frontend/server/api/global/index.ts | 2 +- frontend/server/api/global/navs.ts | 2 +- frontend/server/api/global/types.ts | 2 +- 10 files changed, 118 insertions(+), 6 deletions(-) rename frontend/composables/{graphql.ts => useGraphql.ts} (100%) create mode 100644 frontend/composables/useTime.ts create mode 100644 frontend/server/api/articles/list.ts diff --git a/backend/src/extensions/documentation/documentation/1.0.0/full_documentation.json b/backend/src/extensions/documentation/documentation/1.0.0/full_documentation.json index 87b09a4..3c8a9e9 100644 --- a/backend/src/extensions/documentation/documentation/1.0.0/full_documentation.json +++ b/backend/src/extensions/documentation/documentation/1.0.0/full_documentation.json @@ -14,7 +14,11 @@ "name": "Apache 2.0", "url": "https://www.apache.org/licenses/LICENSE-2.0.html" }, +<<<<<<< Updated upstream "x-generation-date": "2023-01-26T05:03:19.417Z" +======= + "x-generation-date": "2023-01-26T13:01:50.925Z" +>>>>>>> Stashed changes }, "x-strapi-config": { "path": "/documentation", diff --git a/frontend/composables/graphql.ts b/frontend/composables/useGraphql.ts similarity index 100% rename from frontend/composables/graphql.ts rename to frontend/composables/useGraphql.ts diff --git a/frontend/composables/useTime.ts b/frontend/composables/useTime.ts new file mode 100644 index 0000000..fa67db1 --- /dev/null +++ b/frontend/composables/useTime.ts @@ -0,0 +1,16 @@ +function getChineseDate() { + return new Date( + new Date().getTime() + + new Date().getTimezoneOffset() * 60 * 1000 + + 8 * 60 * 60 * 1000, + ) +} + +// 返回num天前的日期 +export function useTime(num: number) { + const date = new Date(getChineseDate().getTime() - num * 24 * 60 * 60 * 1000) + const year = date.getFullYear() + const month = date.getMonth() + 1 + const day = date.getDate() + return `${year}-${month}-${day}` +} diff --git a/frontend/server/api/articles/[id].ts b/frontend/server/api/articles/[id].ts index 88a703c..a0f372e 100644 --- a/frontend/server/api/articles/[id].ts +++ b/frontend/server/api/articles/[id].ts @@ -1,4 +1,4 @@ -import { useGraphql } from '~~/composables/graphql' +import { useGraphql } from '~~/composables/useGraphql' interface IAuthor { name: string motto: string diff --git a/frontend/server/api/articles/list.ts b/frontend/server/api/articles/list.ts new file mode 100644 index 0000000..d72c981 --- /dev/null +++ b/frontend/server/api/articles/list.ts @@ -0,0 +1,92 @@ +import { useGraphql } from '~~/composables/useGraphql' +import { useTime } from '~~/composables/useTime' +interface IAuthor { + name: string +} + +interface ITagItem { + tag: string +} + +interface IArticleItem { + id: string + title: string + viewed: number + liked: number + commented: number + summary: string + cover: string + createdAt: string + authorId: IAuthor + tagIds: { data: ITagItem[] } +} +export default defineEventHandler(async (event): Promise => { + const query = getQuery(event) + const sort = query.sort || ''// recommended、newest、hottest(全部)、three_days_hottest、weekly_hottest、monthly_hottest + const strategy = ['', ''] + switch (sort) { + case 'recommended': + strategy[1] = 'sort: "shared:desc"' + break + case 'newest': + strategy[1] = 'sort: "createdAt:desc"' + break + case 'hottest': + strategy[1] = 'sort: "viewed:desc"' + break + case 'three_days_hottest': + strategy[0] = `{createdAt: { gte: "${useTime(3)}T00:00:00.000Z" }}` + strategy[1] = 'sort: "viewed:desc"' + break + case 'weekly_hottest': + strategy[0] = `{createdAt: { gte: "${useTime(7)}T00:00:00.000Z" }}` + strategy[1] = 'sort: "viewed:desc"' + break + case 'monthly_hottest': + strategy[0] = `{createdAt: { gte: "${useTime(30)}T00:00:00.000Z" }}` + strategy[1] = 'sort: "viewed:desc"' + break + } + const type = query.type || ''// all、others + const pageNum = query.pageNum || '1'// 1 to ∞ + const reqQuery = `query{ + articles( + filters: { + and:[ + ${strategy[0]}, + {typeId: { type: { ${type ? `eq: "${type}"` : ''} } }} + ] + } + ${strategy[1]} + pagination: { page: ${pageNum || '1'}, pageSize: 20 } + ){ + data{ + id + attributes{ + title + viewed + liked + commented + summary + cover + createdAt + authorId{ + data{ + attributes{ + name + } + } + } + tagIds{ + data{ + attributes{ + tag + } + } + } + } + } + } +}` + return await useGraphql(reqQuery) +}) diff --git a/frontend/server/api/articles/tags.ts b/frontend/server/api/articles/tags.ts index c5a6b6f..11d62e4 100644 --- a/frontend/server/api/articles/tags.ts +++ b/frontend/server/api/articles/tags.ts @@ -1,4 +1,4 @@ -import { useGraphql } from '~~/composables/graphql' +import { useGraphql } from '~~/composables/useGraphql' interface ITagItem { tag: string } diff --git a/frontend/server/api/authors/list.ts b/frontend/server/api/authors/list.ts index 5a62de1..7e885ef 100644 --- a/frontend/server/api/authors/list.ts +++ b/frontend/server/api/authors/list.ts @@ -1,4 +1,4 @@ -import { useGraphql } from '~~/composables/graphql' +import { useGraphql } from '~~/composables/useGraphql' interface IAuthorListItem { name: string motto: string diff --git a/frontend/server/api/global/index.ts b/frontend/server/api/global/index.ts index dabfbda..bb44629 100644 --- a/frontend/server/api/global/index.ts +++ b/frontend/server/api/global/index.ts @@ -1,4 +1,4 @@ -import { useGraphql } from '~~/composables/graphql' +import { useGraphql } from '~~/composables/useGraphql' interface IGadget { title: string subscribe: string diff --git a/frontend/server/api/global/navs.ts b/frontend/server/api/global/navs.ts index 175a2c0..1693618 100644 --- a/frontend/server/api/global/navs.ts +++ b/frontend/server/api/global/navs.ts @@ -1,4 +1,4 @@ -import { useGraphql } from '~~/composables/graphql' +import { useGraphql } from '~~/composables/useGraphql' interface INavItem { name: string url: string diff --git a/frontend/server/api/global/types.ts b/frontend/server/api/global/types.ts index 2c3ac6c..40a3885 100644 --- a/frontend/server/api/global/types.ts +++ b/frontend/server/api/global/types.ts @@ -1,4 +1,4 @@ -import { useGraphql } from '~~/composables/graphql' +import { useGraphql } from '~~/composables/useGraphql' interface ITypeItem { type: string }