feat: align with upstream 1.5.9.9
This commit is contained in:
@@ -0,0 +1,162 @@
|
||||
import { formatPlayTime } from '../../../utils/native_common'
|
||||
|
||||
/**
|
||||
* 咪咕网页 v5 搜索接口
|
||||
*/
|
||||
async function searchPC(str: string, page: number, limit: number) {
|
||||
const response: any = await $fetch(
|
||||
`https://app.u.nf.migu.cn/pc/resource/song/item/search/v1.0?text=${encodeURIComponent(str)}` +
|
||||
`&pageSize=${limit}&pageNo=${page}`,
|
||||
{
|
||||
headers: {
|
||||
Origin: 'https://music.migu.cn',
|
||||
Referer: 'https://music.migu.cn/v5',
|
||||
birth: 'h5page',
|
||||
channel: '014X031'
|
||||
},
|
||||
timeout: 10000
|
||||
}
|
||||
)
|
||||
|
||||
// v5 接口直接返回歌曲扁平数组,无 total 字段
|
||||
if (!Array.isArray(response)) throw createError({ statusCode: 502, message: 'Migu PC API Error' })
|
||||
|
||||
const list = await Promise.all(
|
||||
response
|
||||
.filter((item: any) => item?.contentId)
|
||||
.map(async (item: any) => {
|
||||
// 歌手列表处理
|
||||
const singerList = item.singerList || []
|
||||
const singer = singerList.map((s: any) => s.name).join('/') || '未知艺术家'
|
||||
|
||||
// 封面图(相对路径补全域名)
|
||||
const img = item.img2 || item.img3 || item.img1 || ''
|
||||
const cover = img && !/^https?:/.test(img) ? `https://d.musicapp.migu.cn${img}` : img
|
||||
const contentId = item.contentId
|
||||
|
||||
// 音质格式列表
|
||||
const formats = item.audioFormats?.map((f: any) => f.formatType) || ['PQ']
|
||||
|
||||
return {
|
||||
singer,
|
||||
name: item.songName || item.name || '',
|
||||
albumName: item.album || '',
|
||||
albumId: item.albumId || '',
|
||||
source: 'mg',
|
||||
interval: formatPlayTime(item.duration || 0),
|
||||
duration: item.duration || 0,
|
||||
songmid: contentId,
|
||||
copyrightId: item.copyrightId || '',
|
||||
img: cover,
|
||||
lrc: item.lrcUrl || null,
|
||||
mrcUrl: item.mrcurl || null,
|
||||
types: formats,
|
||||
_types: {},
|
||||
typeUrl: {}
|
||||
}
|
||||
})
|
||||
)
|
||||
|
||||
return {
|
||||
list,
|
||||
// v5 接口无 total 字段,按当前页数量推算(满页则视为还有下一页)
|
||||
total: list.length < limit ? (page - 1) * limit + list.length : page * limit + 1
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 咪咕移动端搜索接口(app.c.nf.migu.cn,海外服务器可访问)
|
||||
*/
|
||||
async function searchMobile(str: string, page: number, limit: number) {
|
||||
const response: any = await $fetch('https://app.c.nf.migu.cn/bmw/search/song/v1.0', {
|
||||
params: {
|
||||
pageNo: page,
|
||||
text: str
|
||||
},
|
||||
timeout: 10000
|
||||
})
|
||||
|
||||
if (!response?.data) {
|
||||
throw createError({ statusCode: 502, message: 'Migu Mobile API Error' })
|
||||
}
|
||||
|
||||
const items = response.data.items || []
|
||||
|
||||
const list = await Promise.all(
|
||||
items
|
||||
// contentId 位于 item.song 内,而非 item 顶层
|
||||
.filter((item: any) => item?.song?.contentId)
|
||||
.map(async (item: any) => {
|
||||
const song = item.song
|
||||
|
||||
// 歌手列表处理
|
||||
const singerList = song.singerList || []
|
||||
const singer = singerList.map((s: any) => s.name).join('/') || '未知艺术家'
|
||||
|
||||
// 封面图(相对路径补全域名)
|
||||
const img = song.img2 ? `https://d.musicapp.migu.cn${song.img2}` : ''
|
||||
const contentId = song.contentId
|
||||
|
||||
// 音质格式列表
|
||||
const formats = song.audioFormats?.map((f: any) => f.formatType) || ['PQ']
|
||||
|
||||
return {
|
||||
singer,
|
||||
name: song.songName || '',
|
||||
albumName: song.album || '',
|
||||
albumId: song.albumId || '',
|
||||
source: 'mg',
|
||||
interval: formatPlayTime(song.duration || 0),
|
||||
duration: song.duration || 0,
|
||||
songmid: contentId,
|
||||
copyrightId: '',
|
||||
img: img,
|
||||
lrc: song.lrcUrl || null,
|
||||
mrcUrl: null,
|
||||
types: formats,
|
||||
_types: {},
|
||||
typeUrl: {}
|
||||
}
|
||||
})
|
||||
)
|
||||
|
||||
return {
|
||||
list,
|
||||
// 移动端接口无 total 字段,按当前页数量推算(满页则视为还有下一页)
|
||||
total: list.length < limit ? (page - 1) * limit + list.length : page * limit + 1
|
||||
}
|
||||
}
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
const query = getQuery(event)
|
||||
const str = query.str as string
|
||||
const page = parseInt((query.page as string) || '1')
|
||||
const limit = parseInt((query.limit as string) || '30')
|
||||
|
||||
if (!str) {
|
||||
throw createError({ statusCode: 400, message: 'Missing search query' })
|
||||
}
|
||||
|
||||
try {
|
||||
let result
|
||||
try {
|
||||
result = await searchPC(str, page, limit)
|
||||
} catch (err) {
|
||||
console.warn('[mg.get] 咪咕网页 v5 接口请求失败,回退到移动端接口:', err)
|
||||
result = await searchMobile(str, page, limit)
|
||||
}
|
||||
|
||||
return {
|
||||
...result,
|
||||
page,
|
||||
limit,
|
||||
source: 'mg'
|
||||
}
|
||||
} catch (err: any) {
|
||||
console.error('[mg.get] 咪咕搜索失败:', err)
|
||||
throw createError({
|
||||
statusCode: err.statusCode || 500,
|
||||
message: err.message || 'Internal Server Error'
|
||||
})
|
||||
}
|
||||
})
|
||||
@@ -0,0 +1,181 @@
|
||||
import { createTxSearchBody, txRequest, txSignedRequest } from '../../../utils/native_tx'
|
||||
import { decodeName, formatPlayTime, sizeFormate } from '../../../utils/native_common'
|
||||
import { searchQqMusic } from '~~/server/utils/qq_music_sdk'
|
||||
|
||||
const stripHtml = (value: unknown) => String(value ?? '').replace(/[<>]/g, '')
|
||||
|
||||
const formatSdkSearchList = (items: any[]) => {
|
||||
return items.map((item: any) => {
|
||||
const songmid = item.songmid || item.mid
|
||||
const strMediaMid = item.strMediaMid || item.media_mid || item.file?.media_mid || songmid
|
||||
const albumMid = item.albummid || item.albumMid || item.album?.mid || ''
|
||||
const singers = Array.isArray(item.singer)
|
||||
? item.singer.map((s: any) => s.name).filter(Boolean).join('、')
|
||||
: item.singer || ''
|
||||
const interval = Number(item.interval || item.duration || 0)
|
||||
const types = []
|
||||
const _types: any = {}
|
||||
|
||||
if (Number(item.size128 || item.file?.size_128mp3 || 0) !== 0) {
|
||||
const size = sizeFormate(Number(item.size128 || item.file?.size_128mp3))
|
||||
types.push({ type: '128k', size })
|
||||
_types['128k'] = { size }
|
||||
}
|
||||
if (Number(item.size320 || item.file?.size_320mp3 || 0) !== 0) {
|
||||
const size = sizeFormate(Number(item.size320 || item.file?.size_320mp3))
|
||||
types.push({ type: '320k', size })
|
||||
_types['320k'] = { size }
|
||||
}
|
||||
if (Number(item.sizeflac || item.file?.size_flac || 0) !== 0) {
|
||||
const size = sizeFormate(Number(item.sizeflac || item.file?.size_flac))
|
||||
types.push({ type: 'flac', size })
|
||||
_types.flac = { size }
|
||||
}
|
||||
|
||||
return {
|
||||
singer: decodeName(stripHtml(singers)),
|
||||
name: decodeName(stripHtml(item.songname || item.name || item.title)),
|
||||
albumName: decodeName(stripHtml(item.albumname || item.albumName || item.album?.name || '')),
|
||||
albumId: albumMid,
|
||||
source: 'tx',
|
||||
interval: formatPlayTime(interval),
|
||||
duration: interval,
|
||||
songId: item.songid || item.songId || item.id,
|
||||
albumMid,
|
||||
strMediaMid,
|
||||
songmid,
|
||||
img:
|
||||
albumMid === '' || albumMid === '空'
|
||||
? Array.isArray(item.singer) && item.singer.length > 0 && item.singer[0]?.mid
|
||||
? `https://y.gtimg.cn/music/photo_new/T001R500x500M000${item.singer[0].mid}.jpg`
|
||||
: ''
|
||||
: `https://y.gtimg.cn/music/photo_new/T002R500x500M000${albumMid}.jpg`,
|
||||
types,
|
||||
_types,
|
||||
typeUrl: {}
|
||||
}
|
||||
}).filter((item) => item.songmid)
|
||||
}
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
const query = getQuery(event)
|
||||
const str = query.str as string
|
||||
const page = parseInt((query.page as string) || '1')
|
||||
const limit = parseInt((query.limit as string) || '50') // 腾讯接口默认每页 50 条
|
||||
const cookie = String(query.cookie || '').trim()
|
||||
|
||||
if (!str) {
|
||||
throw createError({ statusCode: 400, message: 'Missing search query' })
|
||||
}
|
||||
|
||||
const body = createTxSearchBody(str, page, limit)
|
||||
|
||||
let result: any
|
||||
try {
|
||||
try {
|
||||
const sdkResult: any = await searchQqMusic({ key: str, page, limit, cookie })
|
||||
const sdkList = sdkResult?.song?.list || sdkResult?.data?.song?.list || []
|
||||
const list = formatSdkSearchList(sdkList)
|
||||
|
||||
if (list.length > 0) {
|
||||
return {
|
||||
list,
|
||||
total: sdkResult?.song?.totalnum || sdkResult?.data?.song?.totalnum || list.length,
|
||||
page,
|
||||
limit,
|
||||
source: 'qq-music-api'
|
||||
}
|
||||
}
|
||||
} catch (sdkErr) {
|
||||
console.warn('[tx.get] qq-music-api 搜索失败,回退到原生搜索:', sdkErr)
|
||||
}
|
||||
|
||||
try {
|
||||
result = await txSignedRequest(body)
|
||||
} catch (signedErr) {
|
||||
console.warn('[tx.get] 签名请求失败,回退到普通请求:', signedErr)
|
||||
result = await txRequest('https://u.y.qq.com/cgi-bin/musicu.fcg', body)
|
||||
}
|
||||
|
||||
const service = result?.['music.search.SearchCgiService']
|
||||
|
||||
if (result.code !== 0 || service?.code !== 0) {
|
||||
throw createError({ statusCode: 502, message: 'Tencent API Error' })
|
||||
}
|
||||
|
||||
const rawList = service?.data?.body?.song?.list || []
|
||||
|
||||
const list = []
|
||||
for (const item of rawList) {
|
||||
if (!item.file?.media_mid) continue
|
||||
|
||||
const types = []
|
||||
const _types: any = {}
|
||||
const file = item.file
|
||||
|
||||
if (file.size_128mp3 != 0) {
|
||||
const size = sizeFormate(file.size_128mp3)
|
||||
types.push({ type: '128k', size })
|
||||
_types['128k'] = { size }
|
||||
}
|
||||
if (file.size_320mp3 !== 0) {
|
||||
const size = sizeFormate(file.size_320mp3)
|
||||
types.push({ type: '320k', size })
|
||||
_types['320k'] = { size }
|
||||
}
|
||||
if (file.size_flac !== 0) {
|
||||
const size = sizeFormate(file.size_flac)
|
||||
types.push({ type: 'flac', size })
|
||||
_types.flac = { size }
|
||||
}
|
||||
if (file.size_hires !== 0) {
|
||||
const size = sizeFormate(file.size_hires)
|
||||
types.push({ type: 'flac24bit', size })
|
||||
_types.flac24bit = { size }
|
||||
}
|
||||
|
||||
let albumId = ''
|
||||
let albumName = ''
|
||||
if (item.album) {
|
||||
albumName = item.album.name
|
||||
albumId = item.album.mid
|
||||
}
|
||||
|
||||
const singerName = item.singer ? item.singer.map((s: any) => s.name).join('、') : ''
|
||||
|
||||
list.push({
|
||||
singer: decodeName(singerName),
|
||||
name: decodeName(item.name + (item.title_extra ?? '')),
|
||||
albumName: decodeName(albumName),
|
||||
albumId,
|
||||
source: 'tx',
|
||||
interval: formatPlayTime(item.interval),
|
||||
duration: item.interval,
|
||||
songId: item.id,
|
||||
albumMid: item.album?.mid ?? '',
|
||||
strMediaMid: item.file.media_mid,
|
||||
songmid: item.mid,
|
||||
img:
|
||||
albumId === '' || albumId === '空'
|
||||
? Array.isArray(item.singer) && item.singer.length > 0 && item.singer[0]?.mid
|
||||
? `https://y.gtimg.cn/music/photo_new/T001R500x500M000${item.singer[0].mid}.jpg`
|
||||
: ''
|
||||
: `https://y.gtimg.cn/music/photo_new/T002R500x500M000${albumId}.jpg`,
|
||||
types,
|
||||
_types,
|
||||
typeUrl: {}
|
||||
})
|
||||
}
|
||||
|
||||
return {
|
||||
list,
|
||||
total: service?.data?.meta?.sum ?? list.length,
|
||||
page,
|
||||
limit,
|
||||
source: 'tx'
|
||||
}
|
||||
} catch (err) {
|
||||
console.error(err)
|
||||
throw createError({ statusCode: 500, message: 'Internal Server Error' })
|
||||
}
|
||||
})
|
||||
@@ -0,0 +1,92 @@
|
||||
import { wyEapiRequest } from '../../../utils/native_wy'
|
||||
import { formatPlayTime, sizeFormate } from '../../../utils/native_common'
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
const query = getQuery(event)
|
||||
const str = query.str as string
|
||||
const page = parseInt((query.page as string) || '1')
|
||||
const limit = parseInt((query.limit as string) || '30')
|
||||
|
||||
if (!str) {
|
||||
throw createError({ statusCode: 400, message: 'Missing search query' })
|
||||
}
|
||||
|
||||
const offset = limit * (page - 1)
|
||||
|
||||
const data = {
|
||||
s: str,
|
||||
type: 1, // 1: 单曲
|
||||
limit,
|
||||
total: page === 1,
|
||||
offset
|
||||
}
|
||||
|
||||
try {
|
||||
const result: any = await wyEapiRequest('/api/cloudsearch/pc', data)
|
||||
|
||||
if (!result || result.code !== 200) {
|
||||
throw createError({ statusCode: 502, message: 'Netease API Error' })
|
||||
}
|
||||
|
||||
const songs = result.result?.songs || []
|
||||
|
||||
// 处理歌曲列表,参考 LX Music 的 handleResult 逻辑
|
||||
const list = songs.map((item: any) => {
|
||||
const types: any[] = []
|
||||
const _types: any = {}
|
||||
let size
|
||||
|
||||
if (item.privilege.maxBrLevel == 'hires') {
|
||||
size = item.hr ? sizeFormate(item.hr.size) : null
|
||||
types.push({ type: 'flac24bit', size })
|
||||
_types.flac24bit = { size }
|
||||
}
|
||||
|
||||
switch (item.privilege.maxbr) {
|
||||
case 999000:
|
||||
size = item.sq ? sizeFormate(item.sq.size) : null
|
||||
types.push({ type: 'flac', size })
|
||||
_types.flac = { size }
|
||||
// LX 代码中有意为之的 switch 贯穿 (fallthrough)
|
||||
case 320000:
|
||||
size = item.h ? sizeFormate(item.h.size) : null
|
||||
types.push({ type: '320k', size })
|
||||
_types['320k'] = { size }
|
||||
// 贯穿
|
||||
case 192000:
|
||||
case 128000:
|
||||
size = item.l ? sizeFormate(item.l.size) : null
|
||||
types.push({ type: '128k', size })
|
||||
_types['128k'] = { size }
|
||||
}
|
||||
types.reverse()
|
||||
|
||||
return {
|
||||
singer: item.ar.map((s: any) => s.name).join('、'),
|
||||
name: item.name,
|
||||
albumName: item.al.name,
|
||||
albumId: item.al.id,
|
||||
source: 'wy',
|
||||
interval: formatPlayTime(item.dt / 1000),
|
||||
duration: item.dt / 1000,
|
||||
songmid: item.id,
|
||||
img: item.al.picUrl,
|
||||
lrc: null,
|
||||
types,
|
||||
_types,
|
||||
typeUrl: {}
|
||||
}
|
||||
})
|
||||
|
||||
return {
|
||||
list,
|
||||
total: result.result?.songCount || 0,
|
||||
page,
|
||||
limit,
|
||||
source: 'wy'
|
||||
}
|
||||
} catch (err) {
|
||||
console.error(err)
|
||||
throw createError({ statusCode: 500, message: 'Internal Server Error' })
|
||||
}
|
||||
})
|
||||
Reference in New Issue
Block a user