Skip to content

Commit 30029a7

Browse files
author
lucifer
committed
feat: getDate 逻辑
1 parent e25c90c commit 30029a7

File tree

4 files changed

+57
-69
lines changed

4 files changed

+57
-69
lines changed

config/index.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
const startTime = new Date();
2+
3+
startTime.setMonth(5);
4+
startTime.setDate(1);
5+
startTime.setHours(0);
6+
startTime.setMinutes(0);
7+
startTime.setSeconds(0);
8+
9+
module.exports = {
10+
startTime: startTime.getTime(),
11+
secre: process.env.secret,
12+
clientId: "c16b80e7b58a5a007157",
13+
db: [
14+
{
15+
login: "azl397985856123",
16+
},
17+
{
18+
login: "Yueqi-19",
19+
},
20+
],
21+
};

middleware/mockUserInfo.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ module.exports = async function checkAuth(ctx, next) {
66
login: "azl397985856",
77
avatar_url: "https://avatars.githubusercontent.com/u/12479470?v=4",
88
name: "lucifer",
9-
pay: false,
9+
pay: true,
1010
};
1111

1212
await next();

middleware/passport.js

Lines changed: 22 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,7 @@
11
const fetch = require("node-fetch");
22
const { encrypt, decrypt } = require("../utils/crypto");
33
const { fail } = require("../utils/request");
4-
5-
const secret = process.env.secret;
6-
7-
const clientId = "c16b80e7b58a5a007157";
8-
const db = [
9-
{
10-
login: "azl397985856123",
11-
},
12-
{
13-
login: "Yueqi-19",
14-
},
15-
];
4+
const { secret, db, clientId } = require("../config/index");
165

176
module.exports = async function checkAuth(ctx, next) {
187
if (!ctx.session) {
@@ -21,6 +10,7 @@ module.exports = async function checkAuth(ctx, next) {
2110
if (ctx.session.user) {
2211
await next();
2312
} else {
13+
// 1. 如果有 token ,则说明是之前种植过的,直接解析(如果是别人伪造的则会解析失败)
2414
const token = ctx.cookies.get("token");
2515

2616
if (token) {
@@ -37,6 +27,7 @@ module.exports = async function checkAuth(ctx, next) {
3727
}
3828
}
3929
}
30+
// 2. 如果没有 token,就必须有 code,因此这个时候需要拿 code 去 github 登录,取用户的信息。
4031
const code = ctx.query.code;
4132
if (!code) {
4233
ctx.body = fail({ message: "请先登录~", code: 91 });
@@ -46,6 +37,7 @@ module.exports = async function checkAuth(ctx, next) {
4637
return;
4738
}
4839
try {
40+
// 3. 根据 code 获取用户信息
4941
const { access_token } = await fetch(
5042
`https://github.com/login/oauth/access_token?code=${code}&client_id=${clientId}&client_secret=${secret}`,
5143
{
@@ -66,59 +58,28 @@ module.exports = async function checkAuth(ctx, next) {
6658
// user.login 存在表示登录成功
6759
if (user.login) {
6860
// 付费用户
69-
if (db.find((q) => q.login === user.login)) {
70-
// TODO: 如果不在组织中,自动邀请进 Github 组织
71-
// see #1 https://octokit.github.io/rest.js/v18#orgs-check-membership
72-
// see #2 https://github.com/octokit/octokit.js
73-
// see #3 https://github.com/thundergolfer/automated-github-organization-invites/blob/bb1bb3d42a330716f4dd5c49256245e4bde27489/web_app.rb
74-
ctx.session.user = {
75-
...user,
76-
pay: true,
77-
};
78-
79-
ctx.cookies.set(
80-
"token",
81-
encrypt(
82-
Buffer.from(
83-
JSON.stringify({
84-
...user,
85-
pay: true,
86-
}),
87-
"utf8"
88-
)
89-
),
90-
{
91-
httpOnly: false,
92-
expires: new Date(24 * 60 * 60 * 1000 + Date.now()),
93-
}
94-
);
95-
} else {
96-
ctx.session.user = {
97-
...user,
98-
pay: false,
99-
};
100-
101-
ctx.cookies.set(
102-
"token",
103-
encrypt(
104-
Buffer.from(
105-
JSON.stringify({
106-
...user,
107-
pay: false,
108-
}),
109-
"utf8"
110-
)
111-
),
112-
{
113-
httpOnly: false,
114-
expires: new Date(24 * 60 * 60 * 1000 + Date.now()),
115-
}
116-
);
117-
}
61+
const u = {
62+
...user,
63+
pay: !!db.find((q) => q.login === user.login),
64+
};
65+
// TODO: 如果不在组织中,自动邀请进 Github 组织
66+
// see #1 https://octokit.github.io/rest.js/v18#orgs-check-membership
67+
// see #2 https://github.com/octokit/octokit.js
68+
// see #3 https://github.com/thundergolfer/automated-github-organization-invites/blob/bb1bb3d42a330716f4dd5c49256245e4bde27489/web_app.rb
69+
ctx.session.user = u;
70+
ctx.cookies.set(
71+
"token",
72+
encrypt(Buffer.from(JSON.stringify(u), "utf8")),
73+
{
74+
httpOnly: false,
75+
expires: new Date(24 * 60 * 60 * 1000 + Date.now()), // 一天后过期,后期考虑延长时间
76+
}
77+
);
11878
}
11979

12080
await next();
12181
} catch (err) {
82+
// 4. 登录过程中出错,会跳转至此
12283
ctx.body = fail({ message: "登录失败, code 码已失效~", code: 93 });
12384
}
12485
}

routes/dailyProblem.js

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@ const solutions = require("../static/solution/solutions.json");
33
const { decrypt } = require("../utils/crypto");
44

55
const { success, fail } = require("../utils/request");
6+
const { startTime } = require("../config/index");
67

7-
function getCurrentDay(date) {
8-
return 1;
8+
const MS_PER_DAY = 24 * 60 * 60 * 1000;
9+
10+
function getDay(date) {
11+
return ((date - startTime + MS_PER_DAY - 1) / MS_PER_DAY) >> 0;
912
}
1013

1114
router.get("/api/v1/daily-problem", async (ctx) => {
@@ -14,9 +17,8 @@ router.get("/api/v1/daily-problem", async (ctx) => {
1417
// 3. 根据 Day 几 计算出具体返回哪一个题目
1518
// !!注意: 如果用户指定的时间大于今天,则返回”题目不存在,仅支持查询历史每日一题“
1619

17-
const date = ctx.query.date; // 用户指定的实际
18-
19-
if (date) {
20+
const date = getDay(ctx.query.date || new Date().getTime()); // 用户指定的实际
21+
if (date === 2) {
2022
ctx.body = success({
2123
day: 2,
2224
title: "821. 字符的最短距离",
@@ -38,7 +40,7 @@ router.get("/api/v1/daily-problem", async (ctx) => {
3840
3941
`,
4042
});
41-
} else {
43+
} else if (date <= 1) {
4244
ctx.body = success({
4345
day: 1,
4446
title: "66. 加一",
@@ -67,6 +69,10 @@ router.get("/api/v1/daily-problem", async (ctx) => {
6769
解释: 输入数组表示数字 4321。
6870
`,
6971
});
72+
} else {
73+
ctx.body = fail({
74+
message: "当前暂时没有每日一题,请联系当前讲师进行处理~",
75+
});
7076
}
7177
});
7278

@@ -75,7 +81,7 @@ router.get("/api/v1/daily-problem/solution", async (ctx) => {
7581

7682
// !!注意: 如果用户指定的 day 大于今天对应的day,则返回”题解不存在,仅支持查询历史官方题解“
7783

78-
const day = ctx.query.day || getCurrentDay(); // 用户指定的实际第几天(注意这里的 day 是数字,含义为第 day 天)
84+
const day = ctx.query.day || getDay(new Date().getTime()); // 用户指定的实际第几天(注意这里的 day 是数字,含义为第 day 天)
7985
if (day in solutions) {
8086
ctx.body = success({
8187
content: decrypt(solutions[day].content),

0 commit comments

Comments
 (0)