-
Notifications
You must be signed in to change notification settings - Fork 7.7k
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
Showing
5 changed files
with
200 additions
and
10 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
import { Route } from '@/types'; | ||
import cache from '@/utils/cache'; | ||
import ofetch from '@/utils/ofetch'; | ||
import { phoneBaseUrl, webBaseUrl, generateNonce, sign, getPost } from './utils'; | ||
import { config } from '@/config'; | ||
import { BoardInfo, PostListData } from './types'; | ||
|
||
export const route: Route = { | ||
path: '/bbs/board/:boardId', | ||
categories: ['bbs'], | ||
example: '/dxy/bbs/board/46', | ||
parameters: { specialId: '板块 ID,可在对应板块页 URL 中找到' }, | ||
name: '板块', | ||
maintainers: ['TonyRL'], | ||
radar: [ | ||
{ | ||
source: ['www.dxy.cn/bbs/newweb/pc/category/:boardIdId'], | ||
target: '/bbs/board/:boardIdId', | ||
}, | ||
{ | ||
source: ['3g.dxy.cn/bbs/board/:boardIdId'], | ||
target: '/bbs/board/:boardIdId', | ||
}, | ||
], | ||
handler, | ||
}; | ||
|
||
async function handler(ctx) { | ||
const { boardId } = ctx.req.param(); | ||
const { limit = '20' } = ctx.req.query(); | ||
|
||
const boardDetail = (await cache.tryGet(`dxy:board:detail:${boardId}`, async () => { | ||
const detailParams = { | ||
boardId, | ||
timestamp: Date.now(), | ||
noncestr: generateNonce(8, 'number'), | ||
}; | ||
|
||
const detail = await ofetch(`${phoneBaseUrl}/bbsapi/bbs/board/detail`, { | ||
query: { | ||
...detailParams, | ||
sign: sign(detailParams), | ||
}, | ||
}); | ||
if (detail.code !== 'success') { | ||
throw new Error(detail.message); | ||
} | ||
return detail.data; | ||
})) as BoardInfo; | ||
|
||
const boardList = (await cache.tryGet( | ||
`dxy:board:list:${boardId}`, | ||
async () => { | ||
const listParams = { | ||
boardId, | ||
postType: '0', | ||
orderType: '1', | ||
pageNum: '1', | ||
pageSize: limit, | ||
timestamp: Date.now(), | ||
noncestr: generateNonce(8, 'number'), | ||
}; | ||
|
||
const recommendList = await ofetch(`${phoneBaseUrl}/bbsapi/bbs/board/post/list`, { | ||
query: { | ||
...listParams, | ||
sign: sign(listParams), | ||
}, | ||
}); | ||
if (recommendList.code !== 'success') { | ||
throw new Error(recommendList.message); | ||
} | ||
return recommendList.data; | ||
}, | ||
config.cache.routeExpire, | ||
false | ||
)) as PostListData; | ||
|
||
const list = boardList.result.map((item) => ({ | ||
title: item.subject, | ||
author: item.postUser.nickname, | ||
category: [boardDetail.title], | ||
link: `${webBaseUrl}/bbs/newweb/pc/post/${item.postId}`, | ||
postId: item.postId, | ||
})); | ||
|
||
const items = await Promise.all(list.map((item) => getPost(item, cache.tryGet))); | ||
|
||
return { | ||
title: boardDetail.title, | ||
description: `${boardDetail.postCount} 內容 ${boardDetail.followCount} 关注`, | ||
link: `${webBaseUrl}/bbs/newweb/pc/category/${boardId}`, | ||
image: boardDetail.boardAvatar, | ||
item: items, | ||
}; | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
interface BoardPerm { | ||
errCode: number; | ||
} | ||
interface PostPerm { | ||
errCode: number; | ||
} | ||
|
||
export interface BoardInfo { | ||
id: number; | ||
boardId: number; | ||
title: string; | ||
shortTitle: string; | ||
postCount: number; | ||
followCount: number; | ||
boardAvatar: string; | ||
boardPerm: BoardPerm; | ||
followStatus: boolean; | ||
} | ||
|
||
interface PostUser { | ||
userId: number; | ||
avatar: string; | ||
username: string; | ||
nickname: string; | ||
bbsStatus: number; | ||
userTitle: UserTitle; | ||
} | ||
|
||
interface PostDetail { | ||
postId: number; | ||
boardId: number; | ||
reads: number; | ||
pv: number; | ||
subject: string; | ||
postUser: PostUser; | ||
screenUrlList: string[]; | ||
videoScreen: boolean; | ||
isEditorRecommend: boolean; | ||
} | ||
|
||
export interface PostListData { | ||
pageNum: number; | ||
pageSize: number; | ||
total: number; | ||
result: PostDetail[]; | ||
empty: boolean; | ||
} | ||
|
||
interface UserTitle { | ||
type: number; | ||
titles: string[]; | ||
} | ||
|
||
interface TagInfo { | ||
tagName: string; | ||
} | ||
|
||
interface Post { | ||
id: number; | ||
createTime: number; | ||
subject: string; | ||
body: string; | ||
reads: number; | ||
pv: number; | ||
ipLocation: string; | ||
isCurrentUser: boolean; | ||
anonymous: boolean; | ||
boardInfo: BoardInfo; | ||
postPerm: PostPerm; | ||
showStatus: boolean; | ||
postUser: PostUser; | ||
hintInfos: false[]; | ||
isDisableRepost: boolean; | ||
tagInfos: TagInfo[]; | ||
isVoteActivityPost: boolean; | ||
isShowCaseTag: boolean; | ||
isPaidContent: boolean; | ||
isNeedPay: boolean; | ||
} | ||
|
||
export interface PostData { | ||
code: string; | ||
message: string; | ||
data: Post; | ||
} |
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