Skip to content

Conversation

threedayAAAAA
Copy link
Collaborator

No description provided.


module.exports = {
baseUrl: 'https://leetcode-cn.com',
submitUrl: 'https://leetcode-cn.com/problems/$slug/submit/',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. slug?
  2. 写一个 url (就是你的baseURL)就够了,其他你可以自己拼接。
  3. baseURL 建议改名为 LEETCODE_URL 之类

@@ -0,0 +1,54 @@
// 用户上传账号名与密码
const router = require("koa-router")();
Copy link
Contributor

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");
Copy link
Contributor

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密码正确时,对密码进行加密
Copy link
Contributor

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({
Copy link
Contributor

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')
Copy link
Contributor

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) {
Copy link
Contributor

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], {
Copy link
Contributor

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) {
Copy link
Contributor

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 }) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

success 和 fail 我已经写了,你咋又写一遍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants