Skip to content

Commit

Permalink
Open API 新增播放器播放/暂停、切歌、收藏当前播放歌曲调用,详情看开放API文档 (原始 PR #2077)
Browse files Browse the repository at this point in the history
  • Loading branch information
lyswhut committed Oct 12, 2024
1 parent 86af25b commit 489bbb6
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 12 deletions.
1 change: 1 addition & 0 deletions publish/changeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

- 新增托盘图标颜色 跟随系统亮暗模式 设置,可以在 设置-其他 启用 (#2016
- 支持本地同名 `krc` 格式歌词文件的读取(#2053
- Open API 新增播放器播放/暂停、切歌、收藏当前播放歌曲调用,详情看开放API文档 (原始 PR #2077)

### 优化

Expand Down
47 changes: 35 additions & 12 deletions src/main/modules/openApi/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,19 @@ import http from 'node:http'
import querystring from 'node:querystring'
import type { Socket } from 'node:net'
import { getAddress } from '@common/utils/nodejs'
import { sendTaskbarButtonClick } from '@main/modules/winMain'

const sendResponse = (res: http.ServerResponse, code = 200, msg: string | Record<any, unknown> = 'OK', contentType = 'text/plain; charset=utf-8') => {
res.writeHead(code, {
'Content-Type': contentType,
'Access-Control-Allow-Origin': '*',
})
if (typeof msg === 'object') {
res.end(JSON.stringify(msg))
} else {
res.end(msg)
}
}

let status: LX.OpenAPI.Status = {
status: false,
Expand Down Expand Up @@ -37,10 +50,7 @@ const handleSendStatus = (res: http.ServerResponse<http.IncomingMessage>, query?
const keys = parseFilter(querystring.parse(query ?? '').filter)
const resp: Partial<Record<SubscribeKeys, any>> = {}
for (const k of keys) resp[k] = global.lx.player_status[k]
res.setHeader('Content-Type', 'application/json; charset=utf-8')
res.setHeader('Access-Control-Allow-Origin', '*')
res.writeHead(200)
res.end(JSON.stringify(resp))
sendResponse(res, 200, resp, 'application/json; charset=utf-8')
}
const handleSubscribePlayerStatus = (req: http.IncomingMessage, res: http.ServerResponse<http.IncomingMessage>, query?: string) => {
res.writeHead(200, {
Expand All @@ -67,8 +77,8 @@ const handleStartServer = async(port: number, ip: string) => new Promise<void>((
playerStatusKeys = Object.keys(global.lx.player_status) as SubscribeKeys[]
httpServer = http.createServer((req, res): void => {
const [endUrl, query] = `/${req.url?.split('/').at(-1) ?? ''}`.split('?')
let code
let msg
let code = 200
let msg = 'OK'
switch (endUrl) {
case '/status':
handleSendStatus(res, query)
Expand Down Expand Up @@ -118,11 +128,26 @@ const handleStartServer = async(port: number, ip: string) => new Promise<void>((
// </html>`
// break
case '/lyric':
code = 200
res.setHeader('Content-Type', 'text/plain; charset=utf-8')
res.setHeader('Access-Control-Allow-Origin', '*')
msg = global.lx.player_status.lyric
break
case '/play':
sendTaskbarButtonClick('play')
break
case '/pause':
sendTaskbarButtonClick('pause')
break
case '/skip-next':
sendTaskbarButtonClick('next')
break
case '/skip-prev':
sendTaskbarButtonClick('prev')
break
case '/collect':
sendTaskbarButtonClick('collect')
break
case '/uncollect':
sendTaskbarButtonClick('unCollect')
break
case '/subscribe-player-status':
try {
handleSubscribePlayerStatus(req, res, query)
Expand All @@ -138,9 +163,7 @@ const handleStartServer = async(port: number, ip: string) => new Promise<void>((
msg = 'Forbidden'
break
}
if (!code) return
res.writeHead(code)
res.end(msg)
sendResponse(res, code, msg)
})
httpServer.on('error', error => {
console.log(error)
Expand Down

0 comments on commit 489bbb6

Please sign in to comment.