-
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.
- Loading branch information
1 parent
869b173
commit 1702598
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