更新cookie管理,文件保存

This commit is contained in:
whyour
2021-03-17 20:43:03 +08:00
parent 8526a57caf
commit 361963711e
6 changed files with 156 additions and 89 deletions
+24
View File
@@ -3,6 +3,8 @@ import { Router, Request, Response, NextFunction } from 'express';
import { Container } from 'typedi';
import { Logger } from 'winston';
import config from '../config';
import * as fs from 'fs';
import { celebrate, Joi } from 'celebrate';
const route = Router();
export default (app: Router) => {
@@ -40,4 +42,26 @@ export default (app: Router) => {
}
},
);
route.post(
'/save',
celebrate({
body: Joi.object({
name: Joi.string().required(),
content: Joi.string().required(),
}),
}),
async (req: Request, res: Response, next: NextFunction) => {
const logger: Logger = Container.get('logger');
try {
const { name, content } = req.body;
const path = (config.fileMap as any)[name];
fs.writeFileSync(path, content);
res.send({ code: 200, msg: '保存成功' });
} catch (e) {
logger.error('🔥 error: %o', e);
return next(e);
}
},
);
};
+17 -4
View File
@@ -1,10 +1,7 @@
import { Router, Request, Response, NextFunction } from 'express';
import { Container } from 'typedi';
import CookieService from '../services/cookie';
import { celebrate, Joi } from 'celebrate';
import { Logger } from 'winston';
import { getFileContentByName } from '../config/util';
import config from '../config';
const route = Router();
export default (app: Router) => {
@@ -49,7 +46,23 @@ export default (app: Router) => {
const logger: Logger = Container.get('logger');
try {
const cookieService = Container.get(CookieService);
const data = await cookieService.addCookie();
const data = await cookieService.addCookie(req.query.cookie as string);
return res.send({ code: 200, data });
} catch (e) {
logger.error('🔥 error: %o', e);
return next(e);
}
},
);
route.post(
'/cookie/refresh',
async (req: Request, res: Response, next: NextFunction) => {
const logger: Logger = Container.get('logger');
try {
const cookieService = Container.get(CookieService);
console.log(req);
const data = await cookieService.refreshCookie(req.body);
return res.send({ code: 200, data });
} catch (e) {
logger.error('🔥 error: %o', e);
+5
View File
@@ -41,4 +41,9 @@ export default {
crontabFile,
sampleFile,
confFile,
fileMap: {
'config.sh': confFile,
'crontab.list': crontabFile,
'diy.sh': diyFile,
},
};
+13 -3
View File
@@ -159,13 +159,13 @@ export default class CookieService {
return userCookie;
}
public async addCookie() {
public async addCookie(oldCookie: string) {
const res: any = await this.checkLogin();
if (res.body.errcode === 0) {
let ucookie = this.getCookie(res);
let content = getFileContentByName(config.confFile);
const regx = /.*Cookie[0-9]{1}\=\"(.+?)\"/g;
const lastCookie = (content.match(regx) as any[]).pop();
const lastCookie = oldCookie || (content.match(regx) as any[]).pop();
const cookieRegx = /Cookie([0-9]+)\=.+?/.exec(lastCookie);
if (cookieRegx) {
const num = parseInt(cookieRegx[1]) + 1;
@@ -246,6 +246,17 @@ export default class CookieService {
return result;
}
public async refreshCookie(body: any) {
const { cookie } = body;
const { nickname, status } = await this.getJdInfo(cookie);
return {
pin: cookie.match(/pt_pin=(.+?);/)[1],
cookie,
status,
nickname: nickname,
};
}
private getJdInfo(cookie: string) {
return fetch(
`https://me-api.jd.com/user_new/info/GetJDUserInfoUnion?orgFlag=JD_PinGou_New&callSource=mainorder&channel=4&isHomewhite=0&sceneval=2&_=${Date.now()}&sceneval=2&g_login_type=1&g_ty=ls`,
@@ -266,7 +277,6 @@ export default class CookieService {
)
.then((x) => x.json())
.then((x) => {
console.log(x.data.userInfo);
if (x.retcode === '0' && x.data && x.data.userInfo) {
return { nickname: x.data.userInfo.baseInfo.nickname, status: 0 };
} else if (x.retcode === 13) {