mf转lx需要修改的地方和lx模板

回复
rxxx
网站管理员
帖子: 50
注册时间: 2025年 11月 4日 11:57 星期二

mf转lx需要修改的地方和lx模板

帖子 rxxx »

需要修改:
1. axios_1.default.get 改成 httpFetch
2. axios_1.default 改成 httpFetch
3.如果是通过歌名搜索的要将${title}改成${encodeURIComponent(title)}

下载电脑版测试api时,需要调试时:(设置为true,就可以看到运行日志)
//const DEV_ENABLE = true

代码: 全选

/*!
 * @name rx 2026.06.29
 */
const DEV_ENABLE = true
//const DEV_ENABLE = false
const MUSIC_QUALITY = {
  kw: ['128k', '320k'],
  kg: ['128k', '320k'],
  tx: ['128k', '320k'],
  wy: ['128k', '320k'],
  mg: ['128k', '320k'],
}
const MUSIC_SOURCE = Object.keys(MUSIC_QUALITY)
const { EVENT_NAMES, request, on, send, utils, env, version } = globalThis.lx
//自用修改的httpFetch
/*
const httpFetch = (config = {}) =>
  new Promise((resolve, reject) => {
    if (typeof config === 'string') config = { url: config }
	
    const { url, method = 'GET', headers = {}, data, timeout = 10000, maxRedirects } = config
	
    request( url, {method, headers, timeout, maxRedirects, body: method === 'POST' && typeof data === 'object' ? JSON.stringify(data) : data,},
      (err, resp, body) => {
        if (err) return reject(err)

        let d = body
        try { if (typeof body === 'string') d = JSON.parse(body) } catch { d = body }
        resolve({status: resp.statusCode, headers: resp.headers, data: d, body: d, request: resp.request,})//body: d兼容LX,data:d方便不用修改代码
      }
    )
  })
  */

//====
//====
//====
//====
//====
const kg_apis = [];
const qq_apis = [];
const kw_apis = [];
const wy_apis = [];
//-------------------------------------------------------------------------------------------------------------------------------------------------------

//-------------------------------------------------------------------------------------------------------------------------------------------------------

//-------------------------------------------------------------------------------------------------------------------------------------------------------

//-------------------------------------------------------------------------------------------------------------------------------------------------------

//-------------------------------------------------------------------------------------------------------------------------------------------------------
//====
//====
//====
//====
//====

/**
 * 统一日志
 */
function logApi(apiName, msg, err = null) {
    if (err) {
        console.error(`[${apiName}] ${msg}`, err.message || err);
    } else {
        console.log(`[${apiName}] ${msg}`);
    }
}

/**
 * 通用执行器
 * @param {string} source tx | kw | kg | wy
 * @param {object} musicItem
 * @param {string} quality
 * @returns {string} musicUrl
 */
async function getMediaSource(source, musicItem, quality) {
    console.log("========== 开始获取音源 ==========");

    // 根据 source 选择 API 列表
    const apiMap = {
        tx: qq_apis,
        kw: kw_apis,
        kg: kg_apis,
        wy: wy_apis,
    };

    const apis = apiMap[source];

    // source 不合法
    if (!apis) {console.log(`未知音源类型: ${source}`); return "";}

    for (const api of apis) {
        const apiName = api.name;

        try {
            logApi(apiName, "开始:");
            const musicUrl = await api(musicItem, quality);
            if (musicUrl && musicUrl !== "None") {
                logApi(apiName, `成功获取: ${musicUrl}`);
                // 获取成功立即返回
                return musicUrl;
            } else {
                logApi(apiName, "获取失败!");
            }
        } catch (err) {
            logApi(apiName, "执行异常", err);
        }
    }
}

/**
 * 洛雪调用接口 - handleGetMusicUrl
 * 自己在里面改变了调用,自己使用
 */
const handleGetMusicUrl = async (source, musicInfo, quality) => {
  console.log('musicInfo:',musicInfo);//显示传入值,方便查看信息
  const songId = musicInfo.hash ?? musicInfo.songmid;//kg使用的hash,其他都是songmid
  const musicItem = {[source === 'tx' ? 'songmid' : 'id']: songId,'title':musicInfo.name,'artist':musicInfo.singer ?? musicInfo.artist};//因musicfree代码格式,改变传入值,方便代码拿来直接使用,不用改原代码
  const musicUrl = await getMediaSource(source, musicItem, quality)

  if (!musicUrl) {
    throw new Error('rx get musicUrl failed')
  }

  return musicUrl
}

/**
 * httpFetch - 自用修改的
 */
async function httpFetch(config = {}) {
  if (typeof config === 'string') config = { url: config }

  const {url, method = 'GET', headers = {}, data, timeout = 10000, maxRedirects} = config

  return new Promise((resolve, reject) => {
    request( url, {method, headers, timeout, maxRedirects, body: method === 'POST' && typeof data === 'object' ? JSON.stringify(data) : data,},
      (err, resp, body) => {
        if (err) return reject(err)

        let d = body
        try { if (typeof body === 'string') d = JSON.parse(body) } catch { d = body }
        resolve({status: resp.statusCode, headers: resp.headers, data: d, body: d, request: resp.request,})//body: d兼容LX,data:d方便不用修改代码
      }
    )
  })
}
//上面是自己编写代码
//下面是原代码
const musicSources = {}
MUSIC_SOURCE.forEach(item => {
  musicSources[item] = {
    name: item,
    type: 'music',
    actions: ['musicUrl'],
    qualitys: MUSIC_QUALITY[item],
  }
})
on(EVENT_NAMES.request, ({ action, source, info }) => {
  switch (action) {
    case 'musicUrl':
      if (env != 'mobile') {
        console.group(`Handle Action(musicUrl)`)
        console.log('source', source)
        console.log('quality', info.type)
        console.log('musicInfo', info.musicInfo)
        console.groupEnd()
      } else {
        console.log(`Handle Action(musicUrl)`)
        console.log('source', source)
        console.log('quality', info.type)
        console.log('musicInfo', info.musicInfo)
      }
      return handleGetMusicUrl(source, info.musicInfo, info.type)
        .then(data => Promise.resolve(data))
        .catch(err => Promise.reject(err))
    default:
      console.error(`action(${action}) not support`)
      return Promise.reject('action not support')
  }
})
send(EVENT_NAMES.inited, { status: true, openDevTools: DEV_ENABLE, sources: musicSources })
回复