-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #89 from MarleneJiang/issue-80-前端接口设计
feat: add /api/articles/list api
- Loading branch information
Showing
10 changed files
with
118 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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}` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<IArticleItem[]> => { | ||
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) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22d2401
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
byte-dream-jue-jin – ./
byte-dream-jue-jin-byte-dream.vercel.app
byte-dream-jue-jin-git-main-byte-dream.vercel.app
vercel.bytedream.top
byte-dream-jue-jin.vercel.app