Skip to content

Commit

Permalink
Fix: request cookies not be refresh (#80)
Browse files Browse the repository at this point in the history
* fix: cookies not cache, just overwrite it, #49

* chore: remove useless console
  • Loading branch information
Rain120 authored Aug 15, 2022
1 parent da63e1f commit 715a941
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 74 deletions.
13 changes: 7 additions & 6 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require("@babel/register");
require('@babel/register');
const Koa = require('koa');
const app = new Koa();
const bodyParser = require('koa-bodyparser');
Expand All @@ -11,22 +11,23 @@ const cors = require('./middlewares/koa-cors');
const router = require('./routers/router');
const cookie = require('./util/cookie');
require('./util/colors');
const userInfo = require('./config/user-info')
const userInfo = require('./config/user-info');
const package = require('./package.json');
global = Object.assign({}, userInfo);

console.log(chalk.green('\n🥳🎉 We had supported config the user cookies. \n'));

if (!(global.loginUin || global.uin)) {
console.log(chalk.yellow(`😔 The configuration ${chalk.red('loginUin')} or your ${chalk.red('cookie')} in file ${chalk.green('config/user-info')} has not configured. \n`));
console.log(chalk.yellow(`😔 The configuration ${chalk.red('loginUin')} or your ${chalk.red('cookie')} in file ${chalk.green('config/user-info')} has not configured. \n`));
}

if (!global.cookie) {
console.log(chalk.yellow(`😔 The configuration ${chalk.red('cookie')} in file ${chalk.green('config/user-info')} has not configured. \n`));
console.log(chalk.yellow(`😔 The configuration ${chalk.red('cookie')} in file ${chalk.green('config/user-info')} has not configured. \n`));
}

exec('npm info QQ-Music-API version', (err, stdout, stderr) => {
if(!err){
let version = stdout.trim()
let version = stdout.trim();
if(package.version < version){
console.log(`Current Version: ${version}, Current Version: ${package.version}, Please update it.`.prompt);
}
Expand Down Expand Up @@ -70,5 +71,5 @@ app.use(router.routes())
const PORT = process.env.PORT || 3200;

app.listen(PORT, () => {
console.log(`server running @ http://localhost:${PORT}`.prompt)
console.log(`server running @ http://localhost:${PORT}`.prompt);
});
27 changes: 10 additions & 17 deletions config/user-info.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,21 @@
/*
* @Author: Rainy [https://github.com/rain120]
* @Date: 2021-01-23 15:38:31
* @LastEditors: Rainy
* @LastEditTime: 2021-06-19 22:25:49
*/

const userInfo = {
loginUin: '',
cookie: '',
}
loginUin: '',
cookie: '',
};

const cookieList = userInfo.cookie.split('; ').map(_ => _.trim());

const cookieObject = {};
cookieList.filter(Boolean).forEach(_ => {
if (_) {
const [key, value = ''] = _.split('=');
if (_) {
const [key, value = ''] = _.split('=');

cookieObject[key] = value;
}
cookieObject[key] = value;
}
});

module.exports = Object.assign(userInfo, {
uin: userInfo.loginUin || cookieObject.uin,
cookieList,
cookieObject,
uin: userInfo.loginUin || cookieObject.uin,
cookieList,
cookieObject,
});
39 changes: 16 additions & 23 deletions util/cookie.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,22 @@
/*
* @Author: Rainy [https://github.com/rain120]
* @Date: 2021-01-23 16:19:21
* @LastEditors: Rainy
* @LastEditTime: 2021-06-19 22:20:01
*/

module.exports = () => async (ctx, next) => {
if (global.cookie) {
ctx.request.cookie = global.cookie;
}
if (global.cookie) {
ctx.request.cookie = global.cookie;
}

const cookieHeader = ctx.request.headers;
const cookieHeader = ctx.request.headers;

if (cookieHeader) {
global.cookieList.forEach(cookie => {
const [key, value = ''] = cookie.split('=');
if (cookieHeader) {
global.cookieList.forEach(cookie => {
const [key, value = ''] = cookie.split('=');

if (value) {
ctx.cookies.set(key, value.trim(), {
maxAge: 24 * 60 * 60 * 1000,
// overwirte: true,
});
}
});
}
if (value) {
ctx.cookies.set(key, value.trim(), {
// maxAge: 24 * 60 * 60 * 1000,
overwirte: true,
});
}
});
}

await next();
await next();
};
68 changes: 40 additions & 28 deletions util/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,39 +7,51 @@ axios.defaults.timeout = 10000;
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8;text/plain;';
axios.defaults.responseType = 'json;text/plain;charset=utf-8;';

const setHeaders = headers => {
return {
...headers,
cookies: global.cookie
};
};

let yURL = 'https://y.qq.com';
let cURL = 'https://c.y.qq.com';
// let uURL = 'https:/u.y.qq.com/cgi-bin/musicu.fcg';

function request(url, method, options = {}, isUUrl = 'c') {
let baseURL = '';
switch (isUUrl) {
case 'y':
baseURL = yURL + url;
break;
case 'u':
baseURL = url;
break;
case 'c':
baseURL = cURL + url;
break;
default:
baseURL = cURL + url;
break;
}
return axios[method](baseURL, options).then(
response => {
if (!response) {
throw Error('response is null');
}
console.log(`${url} request success`.info);
return response;
},
error => {
console.log(`${url} request error`.error);
throw error;
},
);
let baseURL = '';
switch (isUUrl) {
case 'y':
baseURL = yURL + url;
break;
case 'u':
baseURL = url;
break;
case 'c':
baseURL = cURL + url;
break;
default:
baseURL = cURL + url;
break;
}

options = Object.assign(options, {
headers: setHeaders(options.headers || {}),
});

return axios[method](baseURL, options).then(
response => {
if (!response) {
throw Error('response is null');
}
console.log(`${url} request success`.info);
return response;
},
error => {
console.log(`${url} request error`.error);
throw error;
},
);
}

module.exports = request;

0 comments on commit 715a941

Please sign in to comment.