Skip to content

Commit

Permalink
main: migrate login api to eapi (#183)
Browse files Browse the repository at this point in the history
Co-authored-by: Rocka <[email protected]>
  • Loading branch information
hatateaya and rocka authored Oct 29, 2024
1 parent 8c9de85 commit 8c64726
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 19 deletions.
27 changes: 18 additions & 9 deletions src/main/api/httpClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,25 +190,34 @@ export default class HttpClient {
return this.post('https://music.163.com/api/linux/forward', init);
}

static EapiDefaultCookies = {
os: 'android',
osver: '10.0.0',
appver: '8.20.30',
mobilename: 'linux'
};
static EapiDefaultCookieString = Object.entries(HttpClient.EapiDefaultCookies)
.map(([k, v]) => `${k}=${v}`).join('; ');

/**
* eapi request
* @param {string} url
* @param {object} data
* @param {boolean} putCacheKey
* @param {boolean} useInterfaceUrl
*/
async postE(url, data = {}, putCacheKey = false) {
url = `https://music.163.com/eapi${url}`;
async postE(url, data = {}, putCacheKey = false, useInterfaceUrl = false) {
if (useInterfaceUrl) {
url = `https://interface.music.163.com/eapi${url}`;
} else {
url = `https://music.163.com/eapi${url}`;
}
let body = Object.assign({ e_r: 'true' }, data);
if (putCacheKey) {
body['cache_key'] = getCacheKey(body);
}
// default eapi cookies
body.header = Object.assign({
os: 'android',
osver: '10.0.0',
appver: '8.10.20',
mobilename: 'linux'
}, this.getCookie());
body['header'] = Object.assign({}, HttpClient.EapiDefaultCookies, this.getCookie());
/** @type {import('electron-fetch').RequestInit} */
let init = {
method: 'POST',
Expand All @@ -218,7 +227,7 @@ export default class HttpClient {
// encrypt request payload
init.body = qs.stringify(encodeEApi(new URL(url).pathname, body));
init.headers = this.mergeHeaders({
'Cookie': 'os=android; osver=10.0.0; appver=2.0.3.131777; mobilename=linux',
'Cookie': HttpClient.EapiDefaultCookieString,
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': Buffer.byteLength(init.body)
});
Expand Down
24 changes: 14 additions & 10 deletions src/main/api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,16 @@ export function login(acc, pwd, countrycode = '86') {
const password = crypto.createHash('md5').update(pwd).digest('hex');
const postBody = {
password,
rememberLogin: true,
remember: true,
type: 0,
https: true
};
if (/^\d*$/.test(acc)) {
return client.postW('/login/cellphone', { phone: acc, countrycode, ...postBody });
postBody.type = 1;
return client.postE('/w/login/cellphone', { phone: acc, countrycode, ...postBody }, false, true);
} else {
return client.postW('/login', { username: acc, ...postBody });
postBody.type = 0;
return client.postE('/w/login', { username: acc, ...postBody }, false, true);
}
}

Expand All @@ -72,8 +76,8 @@ export function login(acc, pwd, countrycode = '86') {
* @param {number} [type = 1]
* @returns {Promise<Types.QRCodeUnikeyRes>}
*/
export function getQRLoginKey(type = 1) {
return client.postW('/login/qrcode/unikey', { type });
export function getQRLoginKey(type = 3) {
return client.postE('/login/qrcode/unikey', { type });
}

/**
Expand All @@ -86,18 +90,18 @@ export function getQRLoginKey(type = 1) {
* @param {number} [type = 1]
*/
export function checkQRLoginStatus(key, type = 1) {
return client.postW('/login/qrcode/client/login', { key, type });
return client.postE('/login/qrcode/client/login', { key, type });
}

/**
* @returns {Promise<Types.ApiRes>}
*/
export function refreshLogin() {
return client.postW('/login/token/refresh');
return client.postE('/login/token/refresh');
}

export async function logout() {
const resp = await client.postW('/logout');
const resp = await client.postE('/logout');
if (resp.code === 200) {
client.updateCookie();
}
Expand All @@ -117,7 +121,7 @@ export function verifyCaptcha(id, captcha) {
* @returns {Promise<Types.MyProfileRes>}
*/
export function getMyProfile() {
return client.postW('/nuser/account/get');
return client.postE('/nuser/account/get');
}

/**
Expand Down Expand Up @@ -635,7 +639,7 @@ export function postDailyTaskE(type, adid = 0) {
* @returns {Promise<Types.GetDailyTaskRes>}
*/
export function getDailyTask() {
return client.postW('/point/getDailyTask');
return client.postE('/point/getDailyTask');
}

/**
Expand Down

0 comments on commit 8c64726

Please sign in to comment.