-
Notifications
You must be signed in to change notification settings - Fork 37
feat: 新增直接提交代码至lc功能 #4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
|
||
module.exports = { | ||
baseUrl: 'https://leetcode-cn.com', | ||
submitUrl: 'https://leetcode-cn.com/problems/$slug/submit/', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- slug?
- 写一个 url (就是你的baseURL)就够了,其他你可以自己拼接。
- baseURL 建议改名为 LEETCODE_URL 之类
@@ -0,0 +1,54 @@ | |||
// 用户上传账号名与密码 | |||
const router = require("koa-router")(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
建议不叫 account,目的和我的 user 区分。可以叫 LCAccount
|
||
// 设置响应头 | ||
router.options('/submitLcAccount', async (ctx, next) => { | ||
ctx.set("Access-Control-Allow-Origin", "http://localhost:8080"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
不需要自己设置跨域头,中间件设置好了
const {login, password} = ctx.request.body | ||
let result = await getLeetcodeCookie({login, password}) | ||
if(result.success){ | ||
// todo密码正确时,对密码进行加密 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
加密方法我写好了,直接调用 utils 下的即可
// 将加密后的密文 以及 sessionId、 csrftoken写入cookie中 | ||
LEETCODE_SESSION = result.LEETCODE_SESSION | ||
csrftoken = result.csrftoken | ||
set91Cookie({ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
使用 koa 内置的 setCookie 参考我的user 接口
router.post('/submitCode', async (ctx, next) => { | ||
const login = ctx.cookies.get('login') | ||
const password = ctx.cookies.get('password') | ||
let LEETCODE_SESSION = ctx.cookies.get('LEETCODE_SESSION') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cookie.get() 建议提出常量
opts.body = problem || {}; | ||
|
||
return new Promise(res => { | ||
request(opts, function(e, resp, body) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
建议用 fetch。这样一致一点,参考我的 middleware 代码
// 设置cookie | ||
function set91Cookie (data, ctx){ | ||
for(let key in data){ | ||
ctx.cookies.set(key, data[key], { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
为啥不直接 set 而是 for?
function getLeetcodeCookie({login, password}){ | ||
return new Promise((resolve, reject) => { | ||
// 先发前置请求获取csrftoken | ||
request(loginUrl, function(e, resp, body) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fetch
}; | ||
} | ||
|
||
function fail({ message, code = 10001 }) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
success 和 fail 我已经写了,你咋又写一遍
No description provided.