From 83e19d44e3f032e7e2d2b77e8d66f9ee8a36d966 Mon Sep 17 00:00:00 2001 From: shanmite Date: Tue, 29 Aug 2023 08:41:32 +0800 Subject: [PATCH] =?UTF-8?q?docs:=20=E6=89=AB=E7=A0=81=E7=99=BB=E9=99=86?= =?UTF-8?q?=E4=BD=BF=E7=94=A8=E8=AF=B4=E6=98=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 10 ++++++++++ lib/login.js | 43 +++++++++++++++++++------------------------ 2 files changed, 29 insertions(+), 24 deletions(-) diff --git a/README.md b/README.md index 1f96b81..2a3c925 100644 --- a/README.md +++ b/README.md @@ -3,6 +3,8 @@ - [AutoScript](#autoscript) - [操作步骤](#操作步骤) - [获取COOKIE](#获取cookie) + - [扫码登陆](#扫码登陆) + - [手动获取](#手动获取) - [本地运行](#本地运行) - [可执行文件](#可执行文件) - [以源码方式运行](#以源码方式运行) @@ -54,6 +56,14 @@ ### 获取COOKIE +#### 扫码登陆 + +在`env.js`文件填`COOKIE`的对应位置写入`"DedeUserID=你的UID"`即可使用`lottery login`扫码自动获取Cookie + +`COOKIE`中包含`DedeUserID=你的UID`的都会被自动替换 + +#### 手动获取 + 进入[B站主页](https://www.bilibili.com/)获取Cookie用于登录 Chrome浏览器: diff --git a/lib/login.js b/lib/login.js index cb59900..b55b331 100644 --- a/lib/login.js +++ b/lib/login.js @@ -1,44 +1,39 @@ const { readFileSync, writeFileSync } = require('fs'); -const { log, env_file} = require("./utils"); -const env_path = env_file +const { log, env_file } = require("./utils"); -/* -* 扫码登陆 -* @param {string} num -*/ +/** + * 扫码登陆 + * @param {string} num + */ async function login(num) { try { - const { pcLogin } = await require('@catlair/blogin'); + const { pcLogin } = require('@catlair/blogin'); const loginInfo = await pcLogin(); if (!loginInfo) { - log.error("登录状态","失败/取消"); + log.error("扫码登陆", "失败/取消"); return; } - log.info("登录状态","登录成功"); + log.info("扫码登陆", "登录成功"); JSON.stringify(loginInfo, null, 2); const uid = `${loginInfo.mid}`; const cookie = `${loginInfo.cookie}`; log.info("账号UID", uid); - log.info("cookie", cookie); - if (await replaceCookie(env_path, uid, cookie)) { - log.info("说明",`账号${num}已进行cookie自动更新,如未能生效请手动复制在env.js内替换。路径:${env_path}`); + log.info("Cookie", cookie); + if (replaceCookie(env_file, uid, cookie)) { + log.info("扫码登陆", `账号${num}已进行cookie自动更新,如未能生效请手动复制在env.js内替换。路径:${env_file}`); } } catch (error) { - if (error?.message?.includes('Cannot find module')) { - log.error('请先运行 npm add @catlair/blogin'); - return; - } log.error(error); } } -/* -* 正则检索uid更新cookie -* @param {string} filePath -* @param {string} uid -* @param {string} oldCK -*/ -async function replaceCookie(filePath,uid,oldCK) { +/** + * 正则检索uid更新cookie + * @param {string} filePath + * @param {string} uid + * @param {string} oldCK + */ +function replaceCookie(filePath, uid, oldCK) { try { const content = readFileSync(filePath, 'utf-8'); const DedeUserID = `DedeUserID=${uid}`; @@ -55,7 +50,7 @@ async function replaceCookie(filePath,uid,oldCK) { writeFileSync(filePath, newCK); return true; } catch (error) { - log.error("替换错误",error); + log.error("扫码登陆", error); } return false; }