feat: dida365, close #6486

This commit is contained in:
DIYgod
2020-12-21 20:21:14 +08:00
parent 18adc60d16
commit c5f8dd19c6
3 changed files with 76 additions and 0 deletions

View File

@@ -177,6 +177,10 @@ const calculateValue = () => {
game4399: {
cookie: envs.GAME_4399,
},
dida365: {
username: envs.DIDA365_USERNAME,
password: envs.DIDA365_PASSWORD,
},
};
};
calculateValue();

View File

@@ -3727,4 +3727,7 @@ router.get('/mathunion/fields-medal', require('./routes/mathunion/fields-medal')
// ACM
router.get('/acm/amturingaward', require('./routes/acm/amturingaward'));
// 滴答清单
router.get('/dida365/habit/checkins', require('./routes/dida365/habit-checkins'));
module.exports = router;

View File

@@ -0,0 +1,69 @@
const got = require('@/utils/got');
const { CookieJar } = require('tough-cookie');
const config = require('@/config').value;
const logger = require('@/utils/logger');
let loginFlag;
const cookieJar = new CookieJar();
if (config.dida365.username && config.dida365.password) {
got({
method: 'post',
url: 'https://api.dida365.com/api/v2/user/signon?wc=true&remember=true',
cookieJar,
json: {
username: config.dida365.username,
password: config.dida365.password,
},
})
.then(() => {
loginFlag = true;
logger.info('Dida365 login success.');
})
.catch(() => {
logger.error('Dida365 login fail.');
});
}
module.exports = async (ctx) => {
if (loginFlag) {
const habitsListRes = await got.get({
url: 'https://api.dida365.com/api/v2/habits',
cookieJar,
});
const habitsList = habitsListRes.data;
const paramsList = [];
habitsList.forEach((item) => {
paramsList.push(['habitIds', item.id]);
});
const searchParams = new URLSearchParams(paramsList);
const checkinsRes = await got.get({
url: 'https://api.dida365.com/api/v2/habitCheckins',
cookieJar,
searchParams,
});
const checkins = checkinsRes.data;
let list = [];
for (const habitId in checkins.checkins) {
const info = habitsList.find((item) => item.id === habitId);
list = list.concat(
checkins.checkins[habitId].map((checkin, index) => ({
title: `${index + 1}${info.name}打卡`,
pubDate: checkin.checkinTime,
link: `https://dida365.com/webapp/#q/all/habit/${checkin.habitId}`,
guid: checkin.id,
}))
);
}
ctx.state.data = {
title: '滴答清单打卡',
link: 'https://dida365.com/webapp/#q/all/habit/',
item: list,
};
} else {
throw Error('Login required');
}
};