fix(open-api): 修复开放API两处缺陷并支持小程序取 token
- songs.get.ts: 引用未定义变量 songsWithDetails 导致 /api/open/songs 缓存未命中时必然 500,改为 formattedSongs;并让缓存分支兼容 requester 为字符串的情况,避免返回 { name: undefined }
- schedules.get.ts: 数据库分支补齐 sequence / played / song.voteCount,与此前只有缓存分支才返回这些字段的行为对齐(缺少 sequence 会让前端无法显示歌曲序号)
- login.post.ts: 响应体返回 token,便于小程序/App 通过 Authorization: Bearer 访问(不影响原有 httpOnly cookie 行为)
This commit is contained in:
@@ -73,12 +73,16 @@ export default defineEventHandler(async (event) => {
|
||||
played: song.played || false,
|
||||
playedAt: song.playedAt,
|
||||
createdAt: song.createdAt,
|
||||
// 缓存中 requester 可能是姓名字符串(formattedSongs 即如此),也可能已是对象,
|
||||
// 两种都要兼容,否则缓存命中时会返回 { name: undefined, ... }
|
||||
requester: song.requester
|
||||
? {
|
||||
name: song.requester.name,
|
||||
grade: song.requester.grade,
|
||||
class: song.requester.class
|
||||
}
|
||||
? typeof song.requester === 'string'
|
||||
? { name: song.requester, grade: null, class: null }
|
||||
: {
|
||||
name: song.requester.name,
|
||||
grade: song.requester.grade,
|
||||
class: song.requester.class
|
||||
}
|
||||
: null
|
||||
})),
|
||||
pagination: {
|
||||
@@ -337,7 +341,7 @@ export default defineEventHandler(async (event) => {
|
||||
const normalApiResult = {
|
||||
success: true,
|
||||
data: {
|
||||
songs: songsWithDetails,
|
||||
songs: formattedSongs,
|
||||
total,
|
||||
pagination: {
|
||||
page,
|
||||
@@ -351,7 +355,7 @@ export default defineEventHandler(async (event) => {
|
||||
|
||||
await cache.set(normalApiCacheKey, normalApiResult, 180) // 3分钟缓存
|
||||
console.log(
|
||||
`[OpenAPI Cache] 歌曲数据已缓存到普通API缓存: ${normalApiCacheKey},数量: ${songsWithDetails.length}`
|
||||
`[OpenAPI Cache] 歌曲数据已缓存到普通API缓存: ${normalApiCacheKey},数量: ${formattedSongs.length}`
|
||||
)
|
||||
|
||||
// 可选:也缓存到开放API专用缓存(保留原有逻辑,但优先级较低)
|
||||
|
||||
Reference in New Issue
Block a user