- 移除 bd/mg 两平台及磁盘残留产物;清理 api-test 死脚本、musicSdk/message+options 死模块、infSign vendor - musicSdk/ 改名 sdk/;src/common、src/auth、src/apis 整体移入 sdk/,依赖方向定为 src → sdk 严格单向 - src/ 手写 JS 全量转 .ts,删除 JS 类型垫片 modules.d.ts;源码 import 统一 .ts 后缀 - Node 原生类型剥离直接运行 .ts(engines >=22.18),取消 tsc 发射产物,tsc 仅类型检查(pnpm typecheck) - 删除 kwdes 备用实现与 src/index.ts 转发垫片;测试移至 test/smoke-test.ts - common/request.ts 补显式导出类型;修复 set-cookie 单值形态、ApiError 类字段等隐患 验证:tsc 零错误,smoke-test 41/41
170 lines
4.7 KiB
TypeScript
170 lines
4.7 KiB
TypeScript
import { formatPlayTime, sizeFormate } from '../index.ts'
|
|
import { formatSingerName } from '../utils.ts'
|
|
import { signRequest } from './utils/index.ts'
|
|
import type { SongInfo, SongType, Quality, QualityInfo } from '../types.ts'
|
|
|
|
export default {
|
|
limit: 50,
|
|
total: 0,
|
|
page: 0,
|
|
allPage: 1,
|
|
successCode: 0,
|
|
musicSearch(str, page, limit, retryNum = 0) {
|
|
if (retryNum > 5) return Promise.reject(new Error('搜索失败'))
|
|
const searchRequest = signRequest({
|
|
comm: {
|
|
ct: '11',
|
|
cv: '14090508',
|
|
v: '14090508',
|
|
tmeAppID: 'qqmusic',
|
|
phonetype: 'EBG-AN10',
|
|
deviceScore: '553.47',
|
|
devicelevel: '50',
|
|
newdevicelevel: '20',
|
|
rom: 'HuaWei/EMOTION/EmotionUI_14.2.0',
|
|
os_ver: '12',
|
|
OpenUDID: '0',
|
|
OpenUDID2: '0',
|
|
QIMEI36: '0',
|
|
udid: '0',
|
|
chid: '0',
|
|
aid: '0',
|
|
oaid: '0',
|
|
taid: '0',
|
|
tid: '0',
|
|
wid: '0',
|
|
uid: '0',
|
|
sid: '0',
|
|
modeSwitch: '6',
|
|
teenMode: '0',
|
|
ui_mode: '2',
|
|
nettype: '1020',
|
|
v4ip: '',
|
|
},
|
|
req: {
|
|
module: 'music.search.SearchCgiService',
|
|
method: 'DoSearchForQQMusicMobile',
|
|
param: {
|
|
search_type: 0,
|
|
searchid: Math.random().toString().slice(2),
|
|
query: str,
|
|
page_num: page,
|
|
num_per_page: limit,
|
|
highlight: 0,
|
|
nqc_flag: 0,
|
|
multi_zhida: 0,
|
|
cat: 2,
|
|
grp: 1,
|
|
sin: 0,
|
|
sem: 0,
|
|
},
|
|
},
|
|
})
|
|
return searchRequest.then(({ body }) => {
|
|
// console.log(body)
|
|
if (!body || !body.req || body.code != this.successCode || body.req.code != this.successCode) {
|
|
return this.musicSearch(str, page, limit, ++retryNum)
|
|
}
|
|
return body.req.data
|
|
})
|
|
},
|
|
// randomInt(min, max) {
|
|
// return Math.floor(Math.random() * (max - min + 1)) + min
|
|
// },
|
|
// getSearchId() {
|
|
// const e = BigInt(this.randomInt(1, 20))
|
|
// const t = e * 18014398509481984n
|
|
// const n = BigInt(this.randomInt(0, 4194304)) * 4294967296n
|
|
// const a = BigInt(Date.now())
|
|
// const r = (a * 1000n) % (24n * 60n * 60n * 1000n)
|
|
// return String(t + n + r)
|
|
// },
|
|
handleResult(rawList) {
|
|
// console.log(rawList)
|
|
if (!rawList || !Array.isArray(rawList)) return []
|
|
const list: SongInfo[] = []
|
|
rawList.forEach(item => {
|
|
if (!item.file?.media_mid) return
|
|
|
|
let types: SongType[] = []
|
|
let _types: Partial<Record<Quality, QualityInfo>> = {}
|
|
const file = item.file
|
|
if (file.size_128mp3 != 0) {
|
|
let size = sizeFormate(file.size_128mp3)
|
|
types.push({ type: '128k', size })
|
|
_types['128k'] = {
|
|
size,
|
|
}
|
|
}
|
|
if (file.size_320mp3 !== 0) {
|
|
let size = sizeFormate(file.size_320mp3)
|
|
types.push({ type: '320k', size })
|
|
_types['320k'] = {
|
|
size,
|
|
}
|
|
}
|
|
if (file.size_flac !== 0) {
|
|
let size = sizeFormate(file.size_flac)
|
|
types.push({ type: 'flac', size })
|
|
_types.flac = {
|
|
size,
|
|
}
|
|
}
|
|
if (file.size_hires !== 0) {
|
|
let size = sizeFormate(file.size_hires)
|
|
types.push({ type: 'flac24bit', size })
|
|
_types.flac24bit = {
|
|
size,
|
|
}
|
|
}
|
|
// types.reverse()
|
|
let albumId = ''
|
|
let albumName = ''
|
|
if (item.album) {
|
|
albumName = item.album.name
|
|
albumId = item.album.mid
|
|
}
|
|
list.push({
|
|
singer: formatSingerName(item.singer, 'name'),
|
|
// name: item.name + (item.title_extra ?? ''),
|
|
name: item.title,
|
|
albumName,
|
|
albumId,
|
|
source: 'tx',
|
|
interval: formatPlayTime(item.interval),
|
|
songId: item.id,
|
|
albumMid: item.album?.mid ?? '',
|
|
strMediaMid: item.file.media_mid,
|
|
songmid: item.mid,
|
|
img: (albumId === '' || albumId === '空')
|
|
? item.singer?.length ? `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: {},
|
|
})
|
|
})
|
|
// console.log(list)
|
|
return list
|
|
},
|
|
search(str, page = 1, limit) {
|
|
if (limit == null) limit = this.limit
|
|
// http://newlyric.kuwo.cn/newlyric.lrc?62355680
|
|
return this.musicSearch(str, page, limit).then(({ body, meta }) => {
|
|
let list = this.handleResult(body.item_song)
|
|
|
|
this.total = meta.estimate_sum
|
|
this.page = page
|
|
this.allPage = Math.ceil(this.total / limit)
|
|
|
|
return Promise.resolve({
|
|
list,
|
|
allPage: this.allPage,
|
|
limit,
|
|
total: this.total,
|
|
source: 'tx',
|
|
})
|
|
})
|
|
},
|
|
}
|