From e29ca2e79ce1cf1269960fba1c657e436d4c9e58 Mon Sep 17 00:00:00 2001 From: lyswhut Date: Sat, 16 May 2020 20:19:58 +0800 Subject: [PATCH] =?UTF-8?q?=E6=95=B4=E7=90=86=E9=85=8D=E7=BD=AE=E9=80=BB?= =?UTF-8?q?=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/renderer/config/api-source.js | 24 ++++++ src/renderer/config/defaultSetting.js | 84 +++++++++++++++++++ src/renderer/config/index.js | 85 +++++++++++++++++-- src/renderer/utils/index.js | 90 ++------------------- src/renderer/utils/music/api-source.js | 82 ++++--------------- src/renderer/utils/music/bd/api-internal.js | 44 ---------- src/renderer/utils/music/kg/api-internal.js | 41 ---------- src/renderer/utils/music/kw/api-internal.js | 30 ------- src/renderer/utils/music/tx/api-internal.js | 24 ------ src/renderer/views/Setting.vue | 18 ++--- 10 files changed, 215 insertions(+), 307 deletions(-) create mode 100644 src/renderer/config/api-source.js create mode 100644 src/renderer/config/defaultSetting.js delete mode 100644 src/renderer/utils/music/bd/api-internal.js delete mode 100644 src/renderer/utils/music/kg/api-internal.js delete mode 100644 src/renderer/utils/music/kw/api-internal.js delete mode 100644 src/renderer/utils/music/tx/api-internal.js diff --git a/src/renderer/config/api-source.js b/src/renderer/config/api-source.js new file mode 100644 index 00000000..9f60779d --- /dev/null +++ b/src/renderer/config/api-source.js @@ -0,0 +1,24 @@ +export default [ + { + id: 'test', + name: '测试接口', + disabled: false, + supportQualitys: { + kw: ['128k'], + kg: ['128k'], + tx: ['128k'], + wy: ['128k'], + mg: ['128k'], + xm: ['128k'], + bd: ['128k'], + }, + }, + { + id: 'temp', + name: '临时接口', + disabled: false, + supportQualitys: { + kw: ['128k'], + }, + }, +] diff --git a/src/renderer/config/defaultSetting.js b/src/renderer/config/defaultSetting.js new file mode 100644 index 00000000..543e37db --- /dev/null +++ b/src/renderer/config/defaultSetting.js @@ -0,0 +1,84 @@ +import path from 'path' +import os from 'os' + +const defaultSetting = { + version: '1.0.24', + player: { + togglePlayMethod: 'listLoop', + highQuality: false, + isShowTaskProgess: true, + volume: 1, + mediaDeviceId: 'default', + isMediaDeviceRemovedStopPlay: false, + }, + list: { + isShowAlbumName: true, + isShowSource: true, + scroll: { + enable: true, + locations: {}, + }, + }, + download: { + savePath: path.join(os.homedir(), 'Desktop'), + fileName: '歌名 - 歌手', + maxDownloadNum: 3, + isDownloadLrc: false, + isEmbedPic: true, + }, + leaderboard: { + source: 'kw', + tabId: 'kwbiaosb', + }, + songList: { + source: 'kg', + sortId: '5', + tagInfo: { + name: '默认', + id: null, + }, + }, + odc: { + isAutoClearSearchInput: false, + isAutoClearSearchList: false, + }, + search: { + searchSource: 'kw', + tempSearchSource: 'kw', + isShowHotSearch: false, + isShowHistorySearch: false, + isFocusSearchBox: false, + }, + network: { + proxy: { + enable: false, + host: '', + port: '', + username: '', + password: '', + }, + }, + tray: { + isShow: false, + isToTray: false, + }, + windowSizeId: 2, + themeId: 0, + langId: 'cns', + sourceId: 'kw', + apiSource: 'temp', + sourceNameType: 'alias', + randomAnimate: true, + ignoreVersion: null, + isAgreePact: false, +} + +const overwriteSetting = { + +} + +// 使用新年皮肤 +if (new Date().getMonth() < 2) defaultSetting.themeId = 9 + + +export { defaultSetting, overwriteSetting } diff --git a/src/renderer/config/index.js b/src/renderer/config/index.js index 6001e6d2..543e37db 100644 --- a/src/renderer/config/index.js +++ b/src/renderer/config/index.js @@ -1,11 +1,84 @@ -module.exports = { - development: { +import path from 'path' +import os from 'os' +const defaultSetting = { + version: '1.0.24', + player: { + togglePlayMethod: 'listLoop', + highQuality: false, + isShowTaskProgess: true, + volume: 1, + mediaDeviceId: 'default', + isMediaDeviceRemovedStopPlay: false, }, - production: { - + list: { + isShowAlbumName: true, + isShowSource: true, + scroll: { + enable: true, + locations: {}, + }, }, - ajax: { - timeout: 15000, // ajax请求超时时间 + download: { + savePath: path.join(os.homedir(), 'Desktop'), + fileName: '歌名 - 歌手', + maxDownloadNum: 3, + isDownloadLrc: false, + isEmbedPic: true, }, + leaderboard: { + source: 'kw', + tabId: 'kwbiaosb', + }, + songList: { + source: 'kg', + sortId: '5', + tagInfo: { + name: '默认', + id: null, + }, + }, + odc: { + isAutoClearSearchInput: false, + isAutoClearSearchList: false, + }, + search: { + searchSource: 'kw', + tempSearchSource: 'kw', + isShowHotSearch: false, + isShowHistorySearch: false, + isFocusSearchBox: false, + }, + network: { + proxy: { + enable: false, + host: '', + port: '', + username: '', + password: '', + }, + }, + tray: { + isShow: false, + isToTray: false, + }, + windowSizeId: 2, + themeId: 0, + langId: 'cns', + sourceId: 'kw', + apiSource: 'temp', + sourceNameType: 'alias', + randomAnimate: true, + ignoreVersion: null, + isAgreePact: false, } + +const overwriteSetting = { + +} + +// 使用新年皮肤 +if (new Date().getMonth() < 2) defaultSetting.themeId = 9 + + +export { defaultSetting, overwriteSetting } diff --git a/src/renderer/utils/index.js b/src/renderer/utils/index.js index 6e9b7a2a..4ce5d718 100644 --- a/src/renderer/utils/index.js +++ b/src/renderer/utils/index.js @@ -1,9 +1,9 @@ import fs from 'fs' import { shell, clipboard } from 'electron' -import path from 'path' -import os from 'os' import crypto from 'crypto' import { rendererSend, rendererInvoke } from '../../common/ipc' +import { defaultSetting, overwriteSetting } from '../config' +import apiSource from '../config/api-source' /** * 获取两个数之间的随机整数,大于等于min,小于max @@ -184,91 +184,13 @@ export const objectDeepMerge = (target, source) => { * @param {*} setting */ export const updateSetting = (setting, version) => { - const defaultVersion = '1.0.24' + const defaultVersion = defaultSetting.version if (!version) { if (setting) { version = setting.version delete setting.version } } - const defaultSetting = { - player: { - togglePlayMethod: 'listLoop', - highQuality: false, - isShowTaskProgess: true, - volume: 1, - mediaDeviceId: 'default', - isMediaDeviceRemovedStopPlay: false, - }, - list: { - isShowAlbumName: true, - isShowSource: true, - scroll: { - enable: true, - locations: {}, - }, - }, - download: { - savePath: path.join(os.homedir(), 'Desktop'), - fileName: '歌名 - 歌手', - maxDownloadNum: 3, - isDownloadLrc: false, - isEmbedPic: true, - }, - leaderboard: { - source: 'kw', - tabId: 'kwbiaosb', - }, - songList: { - source: 'kg', - sortId: '5', - tagInfo: { - name: '默认', - id: null, - }, - }, - odc: { - isAutoClearSearchInput: false, - isAutoClearSearchList: false, - }, - search: { - searchSource: 'kw', - tempSearchSource: 'kw', - isShowHotSearch: false, - isShowHistorySearch: false, - isFocusSearchBox: false, - }, - network: { - proxy: { - enable: false, - host: '', - port: '', - username: '', - password: '', - }, - }, - tray: { - isShow: false, - isToTray: false, - }, - windowSizeId: 2, - themeId: 0, - langId: 'cns', - sourceId: 'kw', - apiSource: 'temp', - sourceNameType: 'alias', - randomAnimate: true, - ignoreVersion: null, - isAgreePact: false, - } - - // 使用新年皮肤 - if (new Date().getMonth() < 2) defaultSetting.themeId = 9 - - const overwriteSetting = { - - } - if (!setting) { setting = defaultSetting @@ -277,7 +199,11 @@ export const updateSetting = (setting, version) => { objectDeepMerge(defaultSetting, overwriteSetting) setting = defaultSetting } - if (setting.apiSource != 'temp') setting.apiSource = 'test' // 强制设置回 test 接口源 + + if (!apiSource.some(api => api.id === setting.apiSource && !api.disabled)) { + let api = apiSource.find(api => !api.disabled) + if (api) setting.apiSource = api.id + } return { setting, version: defaultVersion } } diff --git a/src/renderer/utils/music/api-source.js b/src/renderer/utils/music/api-source.js index b93a1e3e..6e0f8b01 100644 --- a/src/renderer/utils/music/api-source.js +++ b/src/renderer/utils/music/api-source.js @@ -1,75 +1,21 @@ -import kw_api_temp from './kw/api-temp' -import kw_api_test from './kw/api-test' -import tx_api_test from './tx/api-test' -import kg_api_test from './kg/api-test' -import wy_api_test from './wy/api-test' -import bd_api_test from './bd/api-test' -import mg_api_test from './mg/api-test' -import xm_api_test from './xm/api-test' -// import kw_api_internal from './kw/api-internal' -// import tx_api_internal from './tx/api-internal' -// import kg_api_internal from './kg/api-internal' -// import wy_api_internal from './wy/api-internal' -// import bd_api_internal from './bd/api-internal' +import apiSource from '../../config/api-source' -const apiList = { - kw_api_test, - tx_api_test, - kg_api_test, - wy_api_test, - bd_api_test, - mg_api_test, - xm_api_test, - // kw_api_internal, - // tx_api_internal, - // kg_api_internal, - // wy_api_internal, - // bd_api_internal, - kw_api_temp, -} +const apiList = {} +const supportQuality = {} - -const getAPI = source => { - switch (window.globalObj.apiSource) { - // case 'messoer': - // return apiList[`${source}_api_messoer`] - case 'test': - return apiList[`${source}_api_test`] - case 'temp': - return apiList[`${source}_api_temp`] +for (const api of apiSource) { + supportQuality[api.id] = api.supportQualitys + for (const source of Object.keys(api.supportQualitys)) { + apiList[`${api.id}_api_${source}`] = require(`./${source}/api-${api.id}`).default } } -export const apis = source => { - switch (source) { - case 'tx': - return getAPI('tx') - case 'kg': - return getAPI('kg') - case 'wy': - return getAPI('wy') - case 'bd': - return getAPI('bd') - case 'mg': - return getAPI('mg') - case 'xm': - return getAPI('xm') - default: - return getAPI('kw') - } +const getAPI = source => apiList[`${window.globalObj.apiSource}_api_${source}`] + +const apis = source => { + let api = getAPI(source) + if (api) return api + throw new Error('Api is not found') } -export const supportQuality = { - temp: { - kw: ['128k'], - }, - test: { - kw: ['128k'], - kg: ['128k'], - tx: ['128k'], - wy: ['128k'], - mg: ['128k'], - xm: ['128k'], - bd: ['128k'], - }, -} +export { apis, supportQuality } diff --git a/src/renderer/utils/music/bd/api-internal.js b/src/renderer/utils/music/bd/api-internal.js deleted file mode 100644 index 9ecc51b8..00000000 --- a/src/renderer/utils/music/bd/api-internal.js +++ /dev/null @@ -1,44 +0,0 @@ -import { httpFatch } from '../../request' -import { requestMsg } from '../../message' - -const api_internal = { - successCode: 2200, - // getMusicUrl(songInfo, type) { - // const requestObj = httpFatch(`http://play.taihe.com/data/music/songlink`, { - // method: 'post', - // headers: { - // Origin: 'http://play.taihe.com', - // }, - // formData: { - // songIds: songInfo.songmid, - // }, - // }) - // requestObj.promise = requestObj.promise.then(({ body }) => { - // console.log(body) - // return Promise.reject() - // // if (body.error_code !== this.successCode) return this.getMusicUrl(songInfo, type) - // // return body.code === 200 ? Promise.resolve({ type, url: body.result.bitrate.file_link }) : Promise.reject(new Error(requestMsg.fail)) - // }) - // return requestObj - // }, - getMusicInfo(songInfo, tryNum = 0) { - const requestObj = httpFatch(`http://tingapi.ting.baidu.com/v1/restserver/ting?from=webapp_music&method=baidu.ting.song.baseInfos&song_id=${songInfo.songmid}`) - requestObj.promise = requestObj.promise.then(({ body }) => { - if (body.error_code !== this.successCode) return tryNum > 5 ? Promise.reject(new Error(requestMsg.fail)) : this.getMusicInfo(songInfo, ++tryNum) - return body ? Promise.resolve(body.result.items[0]) : Promise.reject(new Error(requestMsg.fail)) - }) - return requestObj - }, - getPic(songInfo) { - const requestObj = this.getMusicInfo(songInfo) - requestObj.promise = requestObj.promise.then(info => info.pic_premium) - return requestObj - }, - getLyric(songInfo) { - const requestObj = this.getMusicInfo(songInfo) - requestObj.promise.then(info => httpFatch(info.lrclink).promise) - return requestObj - }, -} - -export default api_internal diff --git a/src/renderer/utils/music/kg/api-internal.js b/src/renderer/utils/music/kg/api-internal.js deleted file mode 100644 index fbdda9c9..00000000 --- a/src/renderer/utils/music/kg/api-internal.js +++ /dev/null @@ -1,41 +0,0 @@ -import { httpFatch } from '../../request' -import { requestMsg } from '../../message' -import { headers, timeout } from '../options' - -const api_messoer = { - getMusicUrl(songInfo, type) { - const requestObj = httpFatch(`https://v1.itooi.cn/kugou/url?id=${songInfo._types[type].hash}&quality=${type.replace(/k$/, '')}&isRedirect=0`, { - method: 'get', - timeout, - headers, - }) - requestObj.promise = requestObj.promise.then(({ body }) => { - return body.code === 200 ? Promise.resolve({ type, url: body.data }) : Promise.reject(new Error(requestMsg.fail)) - }) - return requestObj - }, - getPic(songInfo) { - const requestObj = httpFatch(`https://v1.itooi.cn/kugou/pic?id=${songInfo.hash}&isRedirect=0`, { - method: 'get', - timeout, - headers, - }) - requestObj.promise = requestObj.promise.then(({ body }) => { - return body.code === 200 ? Promise.resolve(body.data) : Promise.reject(new Error(requestMsg.fail)) - }) - return requestObj - }, - getLyric(songInfo) { - const requestObj = httpFatch(`https://v1.itooi.cn/kugou/lrc?id=${songInfo.hash}&isRedirect=0`, { - method: 'get', - timeout, - headers, - }) - requestObj.promise = requestObj.promise.then(({ body }) => { - return body ? Promise.resolve(body) : Promise.reject(new Error(requestMsg.fail)) - }) - return requestObj - }, -} - -export default api_messoer diff --git a/src/renderer/utils/music/kw/api-internal.js b/src/renderer/utils/music/kw/api-internal.js deleted file mode 100644 index 1964ee72..00000000 --- a/src/renderer/utils/music/kw/api-internal.js +++ /dev/null @@ -1,30 +0,0 @@ -import { httpFatch } from '../../request' -import { requestMsg } from '../../message' -import { headers, timeout } from '../options' - -const api_messoer = { - getMusicUrl(songInfo, type) { - const requestObj = httpFatch(`https://v1.itooi.cn/kuwo/url?id=${songInfo.songmid}&quality=${type.replace(/k$/, '')}&isRedirect=0`, { - method: 'get', - timeout, - headers, - }) - requestObj.promise = requestObj.promise.then(({ body }) => { - return body.code === 200 ? Promise.resolve({ type, url: body.data }) : Promise.reject(new Error(requestMsg.fail)) - }) - return requestObj - }, - getPic(songInfo) { - const requestObj = httpFatch(`https://v1.itooi.cn/kuwo/pic?id=${songInfo.songmid}&isRedirect=0`, { - method: 'get', - timeout, - headers, - }) - requestObj.promise = requestObj.promise.then(({ body }) => { - return body.code === 200 ? Promise.resolve(body.data) : Promise.reject(new Error(requestMsg.fail)) - }) - return requestObj - }, -} - -export default api_messoer diff --git a/src/renderer/utils/music/tx/api-internal.js b/src/renderer/utils/music/tx/api-internal.js deleted file mode 100644 index dd984fd8..00000000 --- a/src/renderer/utils/music/tx/api-internal.js +++ /dev/null @@ -1,24 +0,0 @@ -import { httpFatch } from '../../request' -import { requestMsg } from '../../message' -import { headers, timeout } from '../options' - -const api_messoer = { - getMusicUrl(songInfo, type) { - const requestObj = httpFatch(`https://v1.itooi.cn/tencent/url?id=${songInfo.strMediaMid}&quality=${type.replace(/k$/, '')}&isRedirect=0`, { - method: 'get', - timeout, - headers, - }) - requestObj.promise = requestObj.promise.then(({ body }) => { - return body.code === 200 ? Promise.resolve({ type, url: body.data }) : Promise.reject(new Error(requestMsg.fail)) - }) - return requestObj - }, - getPic(songInfo) { - return { - promise: Promise.resolve(`https://y.gtimg.cn/music/photo_new/T002R500x500M000${songInfo.albumId}.jpg`), - } - }, -} - -export default api_messoer diff --git a/src/renderer/views/Setting.vue b/src/renderer/views/Setting.vue index 6c46a6f2..9c24d4ee 100644 --- a/src/renderer/views/Setting.vue +++ b/src/renderer/views/Setting.vue @@ -230,6 +230,7 @@ import { setWindowSize, } from '../utils' import { rendererSend } from '../../common/ipc' +import apiSource from '../config/api-source' import fs from 'fs' import languageList from '@/lang/languages.json' @@ -267,18 +268,11 @@ export default { ] }, apiSources() { - return [ - { - id: 'test', - label: this.$t('view.setting.basic_source_test'), - disabled: false, - }, - { - id: 'temp', - label: this.$t('view.setting.basic_source_temp'), - disabled: false, - }, - ] + return apiSource.map(api => ({ + id: api.id, + label: this.$t('view.setting.basic_source_' + api.id) || api.name, + disabled: api.disabled, + })) }, sourceNameTypes() { return [