重构Cookie管理逻辑和交互

This commit is contained in:
whyour 2021-03-22 17:59:21 +08:00
parent ad513d1eae
commit ad30e6bbeb
12 changed files with 255 additions and 64 deletions

View File

@ -46,7 +46,9 @@ export default (app: Router) => {
const logger: Logger = Container.get('logger');
try {
const cookieService = Container.get(CookieService);
const data = await cookieService.addCookie(req.query.cookie as string);
const data = await cookieService.addQrCookie(
req.query.cookie as string,
);
return res.send({ code: 200, data });
} catch (e) {
logger.error('🔥 error: %o', e);
@ -55,6 +57,65 @@ export default (app: Router) => {
},
);
route.post(
'/cookie',
async (req: Request, res: Response, next: NextFunction) => {
const logger: Logger = Container.get('logger');
try {
const cookieService = Container.get(CookieService);
const data = await cookieService.addCookie(req.body.cookies);
if (data) {
return res.send({ code: 500, data });
} else {
return res.send({ code: 200, data: '新建成功' });
}
} catch (e) {
logger.error('🔥 error: %o', e);
return next(e);
}
},
);
route.put(
'/cookie',
async (req: Request, res: Response, next: NextFunction) => {
const logger: Logger = Container.get('logger');
try {
const cookieService = Container.get(CookieService);
const data = await cookieService.updateCookie(req.body);
if (data) {
return res.send({ code: 500, data });
} else {
return res.send({ code: 200, data: '新建成功' });
}
} catch (e) {
logger.error('🔥 error: %o', e);
return next(e);
}
},
);
route.delete(
'/cookie',
async (req: Request, res: Response, next: NextFunction) => {
const logger: Logger = Container.get('logger');
try {
const cookieService = Container.get(CookieService);
const data = await cookieService.deleteCookie(
req.body.cookie as string,
);
if (data) {
return res.send({ code: 500, data });
} else {
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) => {

View File

@ -5,6 +5,7 @@ process.env.NODE_ENV = process.env.NODE_ENV || 'development';
const envFound = dotenv.config();
const rootPath = path.resolve(__dirname, '../../');
const cookieFile = path.join(rootPath, 'config/cookie.sh');
const confFile = path.join(rootPath, 'config/config.sh');
const sampleFile = path.join(rootPath, 'sample/config.sh.sample');
const crontabFile = path.join(rootPath, 'config/crontab.list');
@ -41,6 +42,7 @@ export default {
crontabFile,
sampleFile,
confFile,
cookieFile,
fileMap: {
'config.sh': confFile,
'crontab.list': crontabFile,

View File

@ -155,14 +155,14 @@ export default class CookieService {
return userCookie;
}
public async addCookie(oldCookie: string) {
public async addQrCookie(cookie: 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;
if (content.match(regx)) {
const lastCookie = oldCookie || (content.match(regx) as any[]).pop();
const lastCookie = cookie || (content.match(regx) as any[]).pop();
const cookieRegx = /Cookie([0-9]+)\=.+?/.exec(lastCookie);
if (cookieRegx) {
const num = parseInt(cookieRegx[1]) + 1;
@ -181,6 +181,40 @@ export default class CookieService {
}
}
public async addCookie(cookies: string[]) {
let content = getFileContentByName(config.cookieFile);
const originCookies = content.split('\n').filter((x) => !!x);
const result = originCookies.concat(cookies);
fs.writeFileSync(config.cookieFile, result.join('\n'));
return '';
}
public async updateCookie({ cookie, oldCookie }) {
let content = getFileContentByName(config.cookieFile);
const cookies = content.split('\n');
const index = cookies.findIndex((x) => x === oldCookie);
if (index !== -1) {
cookies[index] = cookie;
fs.writeFileSync(config.cookieFile, cookies.join('\n'));
return '';
} else {
return '未找到要原有Cookie';
}
}
public async deleteCookie(cookie: string) {
let content = getFileContentByName(config.cookieFile);
const cookies = content.split('\n');
const index = cookies.findIndex((x) => x === cookie);
if (index !== -1) {
cookies.splice(index, 1);
fs.writeFileSync(config.cookieFile, cookies.join('\n'));
return '';
} else {
return '未找到要删除的Cookie';
}
}
private async checkLogin() {
try {
if (this.cookies == '') {
@ -224,14 +258,8 @@ export default class CookieService {
}
public async getCookies() {
const content = getFileContentByName(config.confFile);
const regx = /Cookie[0-9]{1}\=\"(.+?)\"/g;
let m,
data = [];
while ((m = regx.exec(content)) !== null) {
data.push(m[1]);
}
return this.formatCookie(data);
const content = getFileContentByName(config.cookieFile);
return this.formatCookie(content.split('\n').filter((x) => !!x));
}
private async formatCookie(data: any[]) {

View File

@ -15,6 +15,12 @@ crond
crontab ${JD_DIR}/config/crontab.list
echo -e "成功添加定时任务...\n"
if [ ! -s ${JD_DIR}/config/cookie.sh ]; then
echo -e "检测到config配置目录下不存在config.sh从示例文件复制一份用于初始化...\n"
touch ${JD_DIR}/config/cookie.sh
echo
fi
if [ ! -s ${JD_DIR}/config/config.sh ]; then
echo -e "检测到config配置目录下不存在config.sh从示例文件复制一份用于初始化...\n"
cp -fv ${JD_DIR}/sample/config.sh.sample ${JD_DIR}/config/config.sh
@ -38,12 +44,14 @@ bash ${JD_DIR}/shell/git_pull.sh
echo
echo -e "======================4. 启动挂机程序========================\n"
CookieConf=${JD_DIR}/config/cookie.sh
. ${JD_DIR}/config/config.sh
if [ -n "${Cookie1}" ]; then
. ${CookieConf}
if [ -s ${CookieConf} ]; then
bash ${JD_DIR}/shell/jd.sh hangup 2>/dev/null
echo -e "挂机程序启动成功...\n"
else
echo -e "config.sh中还未填入有效的Cookie可能是首次部署容器因此不启动挂机程序...\n"
echo -e "尚未在Cookie管理中添加一条Cookie可能是首次部署容器因此不启动挂机程序...\n"
fi
echo -e "======================5. 启动控制面板========================\n"

View File

@ -19,20 +19,6 @@
## 判断条件 -eq -ne -gt -ge -lt -le ,具体含义可百度一下
################################## 定义Cookie必填 ##################################
## 请依次填入每个用户的CookieCookie的具体形式只有pt_key字段和pt_pin字段没有其他字段pt_key=xxxxxxxxxx;pt_pin=xxxx;
## 1. 如果是通过控制面板编辑本文件点击页面上方“扫码获取Cookie”即可获取此方式获取的Cookie有效期为3个月
## 2. 还可以通过浏览器开发工具获取此方式获得的Cookie只有1个月有效期教程https://github.com/LXK9301/jd_scripts/wiki/GetCookies
## 必须按数字顺序1、2、3、4...依次编号下去例子只有6个超出6个你继续往下编号即可
## 不允许有汉字如果ID有汉字请在PC浏览器上获取Cookie会自动将汉字转换为URL编码
Cookie1=""
Cookie2=""
Cookie3=""
Cookie4=""
Cookie5=""
Cookie6=""
################################## 临时屏蔽某个Cookie选填 ##################################
## 如果某些Cookie已经失效了但暂时还没法更新可以使用此功能在不删除该Cookie和重新修改Cookie编号的前提下临时屏蔽掉某些编号的Cookie
## 多个Cookie编号以半角的空格分隔两侧一对半角双引号使用此功能后在运行js脚本时账户编号将发生变化

View File

@ -19,13 +19,14 @@ Name3=(Fruit Pet Bean DreamFactory JdFactory Joy Jdzz Jxnc BookShop Cash Sgmh Cf
function Import_Conf {
if [ -f ${FileConf} ]
then
. ${CookieConf}
. ${FileConf}
if [ -z "${Cookie1}" ]; then
echo -e "请先在 config.sh 中配置好 Cookie\n"
if [ ! -s ${CookieConf} ]; then
echo -e "请先在Cookie管理中添加一条Cookie...\n"
exit 1
fi
else
echo -e "配置文件 ${FileConf} 不存在,请先按教程配置好该文件\n"
echo -e "配置文件 ${FileConf} 不存在,请先按教程配置好该文件...\n"
exit 1
fi
}

View File

@ -8,6 +8,7 @@ LogDir=${ShellDir}/log
ScriptsDir=${ShellDir}/scripts
ConfigDir=${ShellDir}/config
FileConf=${ConfigDir}/config.sh
CookieConf=${ConfigDir}/cookie.sh
FileDiy=${ConfigDir}/diy.sh
FileConfSample=${ShellDir}/sample/config.sh.sample
ListCron=${ConfigDir}/crontab.list
@ -27,6 +28,7 @@ ScriptsURL=https://github.com.cnpmjs.org/RikudouPatrickstar/jd_scripts
## 导入配置文件
function Import_Conf {
if [ -f ${FileConf} ]; then
. ${CookieConf}
. ${FileConf}
fi
}
@ -108,17 +110,6 @@ function Update_Entrypoint {
fi
}
## 用户数量UserSum
function Count_UserSum {
i=1
while [ $i -le 1000 ]; do
Tmp=Cookie$i
CookieTmp=${!Tmp}
[[ ${CookieTmp} ]] && UserSum=$i || break
let i++
done
}
## 检测文件LXK9301/jd_scripts 仓库中的 docker/crontab_list.sh
## 检测定时任务是否有变化此函数会在Log文件夹下生成四个文件分别为
## task.list crontab.list中的所有任务清单仅保留脚本名

View File

@ -7,6 +7,7 @@ ShellDir=${JD_DIR:-$(cd $(dirname $0); pwd)}
ScriptsDir=${ShellDir}/scripts
ConfigDir=${ShellDir}/config
FileConf=${ConfigDir}/config.sh
CookieConf=${ConfigDir}/cookie.sh
FileConfSample=${ShellDir}/sample/config.sh.sample
LogDir=${ShellDir}/log
ListScripts=($(cd ${ScriptsDir}; ls *.js | grep -E "j[drx]_"))
@ -18,9 +19,10 @@ ListJs=${LogDir}/js.list
function Import_Conf {
if [ -f ${FileConf} ]
then
. ${CookieConf}
. ${FileConf}
if [ -z "${Cookie1}" ]; then
echo -e "请先在config.sh中配置好Cookie...\n"
if [ ! -s ${CookieConf} ]; then
echo -e "请先在Cookie管理中添加一条Cookie...\n"
exit 1
fi
else

View File

@ -1,6 +1,7 @@
#!/bin/bash
#author:spark thanks to: https://github.com/sparkssssssss/scripts
. /jd/config/cookie.sh
. /jd/config/config.sh
title=$(echo $1|sed 's/-/_/g')
msg=$(echo -e $2)

View File

@ -5,6 +5,7 @@ ShellDir=${JD_DIR:-$(cd $(dirname $0); pwd)}
LogDir=${ShellDir}/log
## 导入配置文件
. ${ShellDir}/config/cookie.sh
. ${ShellDir}/config/config.sh
## 删除运行js脚本的旧日志

View File

@ -1,9 +1,11 @@
import React, { PureComponent, Fragment, useState, useEffect } from 'react';
import { Button, notification, Modal, Table, Tag, Space } from 'antd';
import { EditOutlined, DeleteOutlined } from '@ant-design/icons';
import config from '@/utils/config';
import { PageContainer } from '@ant-design/pro-layout';
import { request } from '@/utils/http';
import QRCode from 'qrcode.react';
import CookieModal from './modal';
enum Status {
'正常',
@ -74,13 +76,8 @@ const Config = () => {
align: 'center' as const,
render: (text: string, record: any, index: number) => (
<Space size="middle">
{record.status === 0 && <span>-</span>}
{record.status === 1 && (
<a onClick={() => reacquire(record)}></a>
)}
{record.status === 2 && (
<a onClick={() => refreshStatus(record, index)}></a>
)}
<EditOutlined onClick={() => editCookie(record, index)} />
<DeleteOutlined onClick={() => deleteCookie(record, index)} />
</Space>
),
},
@ -90,29 +87,19 @@ const Config = () => {
const [marginTop, setMarginTop] = useState(-72);
const [value, setValue] = useState();
const [loading, setLoading] = useState(true);
const [isModalVisible, setIsModalVisible] = useState(false);
const [editedCookie, setEditedCookie] = useState();
const getCookies = () => {
setLoading(true);
request
.get(`${config.apiPrefix}cookies`)
.then((data) => {
.then((data: any) => {
setValue(data.data);
})
.finally(() => setLoading(false));
};
const updateConfig = () => {
request
.post(`${config.apiPrefix}save`, {
data: { content: value, name: 'config.sh' },
})
.then((data) => {
notification.success({
message: data.msg,
});
});
};
function sleep(time: number) {
return new Promise((resolve) => setTimeout(resolve, time));
}
@ -198,6 +185,50 @@ const Config = () => {
});
};
const addCookie = () => {
setIsModalVisible(true);
};
const editCookie = (record: any, index: number) => {
setEditedCookie(record.cookie);
setIsModalVisible(true);
};
const deleteCookie = (record: any, index: number) => {
Modal.confirm({
title: '确认删除',
content: `确认删除Cookie ${record.cookie}`,
onOk() {
request
.delete(`${config.apiPrefix}cookie`, {
data: { cookie: record.cookie },
})
.then((data: any) => {
if (data.code === 200) {
notification.success({
message: '删除成功',
});
getCookies();
} else {
notification.error({
message: data,
});
}
});
},
onCancel() {
console.log('Cancel');
},
});
};
const handleCancel = (needUpdate?: boolean) => {
setIsModalVisible(false);
if (needUpdate) {
getCookies();
}
};
useEffect(() => {
if (document.body.clientWidth < 768) {
setWdith('auto');
@ -217,7 +248,7 @@ const Config = () => {
title="Cookie管理"
loading={loading}
extra={[
<Button key="2" type="primary" onClick={() => showQrCode()}>
<Button key="2" type="primary" onClick={() => addCookie()}>
Cookie
</Button>,
]}
@ -246,6 +277,11 @@ const Config = () => {
bordered
scroll={{ x: '100%' }}
/>
<CookieModal
visible={isModalVisible}
handleCancel={handleCancel}
cookie={editedCookie}
/>
</PageContainer>
);
};

View File

@ -0,0 +1,74 @@
import React, { useEffect, useState } from 'react';
import { Modal, notification, Input, Form } from 'antd';
import { request } from '@/utils/http';
import config from '@/utils/config';
const CookieModal = ({
cookie,
handleCancel,
visible,
}: {
cookie?: string;
visible: boolean;
handleCancel: (needUpdate?: boolean) => void;
}) => {
const [form] = Form.useForm();
const handleOk = async (values: any) => {
const method = cookie ? 'put' : 'post';
const payload = cookie
? { cookie: values.cookie, oldCookie: cookie }
: { cookies: values.cookie.split('\n') };
const { code, data } = await request[method](`${config.apiPrefix}cookie`, {
data: payload,
});
if (code === 200) {
notification.success({
message: cookie ? '更新Cookie成功' : '添加Cookie成功',
});
} else {
notification.error({
message: data,
});
}
handleCancel(true);
};
useEffect(() => {
if (cookie) {
form.setFieldsValue({ cookie });
}
}, [cookie]);
return (
<Modal
title={cookie ? '编辑Cookie' : '新建Cookie'}
visible={visible}
onOk={() => {
form
.validateFields()
.then((values) => {
handleOk(values);
})
.catch((info) => {
console.log('Validate Failed:', info);
});
}}
onCancel={() => handleCancel()}
>
<Form form={form} layout="vertical" name="form_in_modal">
<Form.Item
name="cookie"
rules={[{ required: true, message: '请输入Cookie' }]}
>
<Input.TextArea
rows={4}
placeholder="请输入cookie多个cookie换行输入"
/>
</Form.Item>
</Form>
</Modal>
);
};
export default CookieModal;