Skip to content

Commit 54e039e

Browse files
author
develop202
committed
可追加新账号,手机号登录简化
1 parent 4560a6a commit 54e039e

5 files changed

Lines changed: 118 additions & 35 deletions

File tree

.github/workflows/phoneLogin.yml

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
name: phoneLogin
22
on:
33
workflow_dispatch:
4-
# push:
5-
# schedule:
6-
# - cron: 1 16 * * *
4+
inputs:
5+
append_user:
6+
description: "是否追加新账号?(不删除已存在账号信息)"
7+
required: true
8+
default: ""
9+
type: choice
10+
options:
11+
-
12+
-
713
jobs:
814
run:
915
runs-on: ubuntu-latest
@@ -17,6 +23,20 @@ jobs:
1723
node-version: 24
1824
cache: 'npm'
1925
cache-dependency-path: ./api/package-lock.json
26+
- name: 🔧 安装GitHub CLI,并使用PAT登录
27+
env:
28+
PAT: ${{ secrets.PAT }}
29+
run: |
30+
if [ -n "${PAT}" ]; then
31+
curl -fsSL https://cli.github.com/packages/githubcli-archive-keyring.gpg | sudo dd of=/usr/share/keyrings/githubcli-archive-keyring.gpg
32+
sudo chmod go+r /usr/share/keyrings/githubcli-archive-keyring.gpg
33+
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/githubcli-archive-keyring.gpg] https://cli.github.com/packages stable main" | sudo tee /etc/apt/sources.list.d/github-cli.list > /dev/null
34+
sudo apt update
35+
sudo apt install gh -y
36+
gh --version
37+
echo "${{ secrets.PAT }}" | gh auth login --with-token
38+
gh auth status
39+
fi
2040
- name: Cache node_modules in api directory
2141
id: cache-npm
2242
uses: actions/cache@v5
@@ -33,3 +53,7 @@ jobs:
3353
env:
3454
PHONE: ${{ secrets.PHONE }}
3555
CODE: ${{ secrets.CODE }}
56+
PAT: ${{ secrets.PAT }}
57+
GITHUB_REPOSITORY: ${{ github.repository }}
58+
USERINFO: ${{ secrets.USERINFO }}
59+
APPEND_USER: ${{ github.event.inputs.append_user }}

.github/workflows/qrcodeLogin.yml

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,17 @@ on:
33
workflow_dispatch:
44
inputs:
55
number:
6-
description: "需要登录几个账号?"
6+
description: "需要登录几个账号"
77
required: true
88
default: 1
9+
append_user:
10+
description: "是否追加新账号?(不删除已存在账号信息)"
11+
required: true
12+
default: ""
13+
type: choice
14+
options:
15+
-
16+
-
917
jobs:
1018
run:
1119
runs-on: ubuntu-latest
@@ -49,3 +57,5 @@ jobs:
4957
NUMBER: ${{ github.event.inputs.number }}
5058
PAT: ${{ secrets.PAT }}
5159
GITHUB_REPOSITORY: ${{ github.repository }}
60+
USERINFO: ${{ secrets.USERINFO }}
61+
APPEND_USER: ${{ github.event.inputs.append_user }}

README.md

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,7 @@ GitHub Actions 实现 `酷狗概念VIP` 自动签到,每天领取总计 `两
5050

5151
3.2 手机号
5252

53-
添加手机号到 Secret `PHONE`,运行 Actions `sent` 获取验证码,把验证码添加到 Secret `CODE`;运行Actions `phoneLogin`,复制 `token``userid` 添加到 Secret `USERINFO`,格式如下, 注意删去换行,否则会多出一些字符,但是不影响签到
54-
55-
> ```json
56-
> [
57-
> { "userid": "写入id", "token": "写入token" },
58-
> // 其他账号信息 注意删掉此行
59-
> { "userid": "写入id", "token": "写入token" }
60-
> ]
61-
> ```
53+
添加手机号到 Secret `PHONE`,运行 Actions `sent` 获取验证码,把验证码添加到 Secret `CODE`;运行Actions `phoneLogin` ,成功即可
6254

6355
1. 启用 Actions `main` , 每天北京时间 00:01 自动签到
6456

phoneLogin.js

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
1+
import { execSync } from "child_process";
2+
import { printBlue, printGreen, printRed, printYellow } from "./utils/colorOut.js";
13
import { close_api, delay, send, startService } from "./utils/utils.js";
24

35
async function login() {
46

57
const phone = process.env.PHONE
68
const code = process.env.CODE
9+
const PAT = process.env.PAT
10+
const USERINFO = process.env.USERINFO
11+
const APPEND_USER = process.env.APPEND_USER
12+
const userinfo = (USERINFO && APPEND_USER == "是") ? JSON.parse(USERINFO) : []
713

814
// 不使用二维码登录并且没有手机号或验证码
915
if (!phone || !code) {
@@ -17,14 +23,49 @@ async function login() {
1723
// 手机号登录请求
1824
const result = await send(`/login/cellphone?mobile=${phone}&code=${code}`, "GET", {})
1925
if (result.status === 1) {
20-
console.log("登录成功!")
21-
console.log("第一行是token,第二行是userid")
22-
console.log(result.data.token)
23-
console.log(result.data.userid)
26+
27+
let userAlreadyExist = false
28+
printGreen("登录成功!")
29+
if (PAT) {
30+
if (APPEND_USER == "是") {
31+
for (let i = 0; i < userinfo.length; i++) {
32+
33+
if (userinfo[i].userid == res.data.userid) {
34+
userAlreadyExist = true
35+
printYellow(`userid: ${userinfo[i].userid} 此账号已存在, 仅更新登录信息`)
36+
userinfo[i].token = res.data.token
37+
}
38+
}
39+
}
40+
if (!userAlreadyExist) {
41+
userinfo.push({
42+
userid: res.data.userid,
43+
token: res.data.token
44+
})
45+
}
46+
if (userinfo.length) {
47+
const userinfoJSON = JSON.stringify(userinfo)
48+
if (PAT) {
49+
try {
50+
execSync(`gh secret set USERINFO -b'${userinfoJSON}' --repo ${process.env.GITHUB_REPOSITORY}`);
51+
printGreen("secret <USERINFO> 更改成功")
52+
} catch (error) {
53+
printRed("自动写入出错,登录信息如下,请手动添加到secret USERINFO")
54+
printRed(userinfoJSON)
55+
}
56+
} else {
57+
printGreen("登录信息如下,把它添加到secret USERINFO 即可")
58+
printBlue(userinfoJSON)
59+
}
60+
}
61+
} else {
62+
printRed("PAT变量缺失")
63+
64+
}
2465
} else if (result.error_code === 34175) {
2566
throw new Error("暂不支持多账号绑定手机登录")
2667
} else {
27-
console.log("响应内容")
68+
printRed("响应内容")
2869
console.dir(result, { depth: null })
2970
throw new Error("登录失败!请检查")
3071
}

qrcodeLogin.js

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
import { execSync } from "child_process";
22
import { close_api, delay, send, startService } from "./utils/utils.js";
3+
import { printBlue, printGreen, printMagenta, printRed, printYellow } from "./utils/colorOut.js";
34

45
async function qrcode() {
56

67
// 启动服务
78
const api = startService()
89
await delay(2000)
910
let qrcode = ""
10-
const userinfo = []
11+
const USERINFO = process.env.USERINFO
12+
const APPEND_USER = process.env.APPEND_USER
13+
const userinfo = (USERINFO && APPEND_USER == "是") ? JSON.parse(USERINFO) : []
1114
const args = process.argv.slice(2);
1215
const number = parseInt(process.env.NUMBER || args[0] || "1")
1316
const pat = process.env.PAT
@@ -19,24 +22,24 @@ async function qrcode() {
1922
qrcode = result.data.qrcode
2023
const img_base64 = result.data.qrcode_img;
2124
const chunkSize = 1000;
22-
console.log("二维码链接如下, 请在浏览器打开使用APP扫描并确认登录")
25+
printMagenta("二维码链接如下, 请在浏览器打开使用APP扫描并确认登录")
2326
for (let i = 0; i < img_base64.length; i += chunkSize) {
2427
console.log(img_base64.slice(i, i + chunkSize));
2528
}
2629
} else {
27-
console.log("响应内容")
30+
printRed("响应内容")
2831
console.dir(result, { depth: null })
2932
throw new Error("请求出错")
3033
}
31-
console.log("正在等待,请扫描二维码并确定登录")
34+
printMagenta("正在等待,请扫描二维码并确定登录")
3235
// 登录
3336
for (let i = 0; i < 25; i++) {
3437
const timestrap = Date.now();
3538
const res = await send(`/login/qr/check?key=${qrcode}&timestrap=${timestrap}`, "GET", {})
3639
const status = res?.data?.status
3740
switch (status) {
3841
case 0:
39-
console.log("二维码已过期")
42+
printYellow("二维码已过期")
4043
break
4144

4245
case 1:
@@ -47,21 +50,34 @@ async function qrcode() {
4750
// console.log("二维码未确认,请点击确认登录")
4851
break
4952
case 4:
50-
console.log("登录成功!")
51-
userinfo.push({
52-
userid: res.data.userid,
53-
token: res.data.token
54-
})
53+
let userAlreadyExist = false
54+
printGreen("登录成功!")
55+
if (APPEND_USER == "是") {
56+
for (let i = 0; i < userinfo.length; i++) {
57+
58+
if (userinfo[i].userid == res.data.userid) {
59+
userAlreadyExist = true
60+
printYellow(`userid: ${userinfo[i].userid} 此账号已存在, 仅更新登录信息`)
61+
userinfo[i].token = res.data.token
62+
}
63+
}
64+
}
65+
if (!userAlreadyExist) {
66+
userinfo.push({
67+
userid: res.data.userid,
68+
token: res.data.token
69+
})
70+
}
5571
break;
5672
default:
57-
console.log("请求出错")
73+
printRed("请求出错")
5874
console.dir(res, { depth: null })
5975
}
6076
if (status == 4 || status == 0) {
6177
break
6278
}
6379
if (i == 24) {
64-
console.log("等待超时\n")
80+
printRed("等待超时\n")
6581
break
6682
}
6783
await delay(5000)
@@ -72,14 +88,14 @@ async function qrcode() {
7288
if (pat) {
7389
try {
7490
execSync(`gh secret set USERINFO -b'${userinfoJSON}' --repo ${process.env.GITHUB_REPOSITORY}`);
75-
console.log("secret <USERINFO> 更改成功")
91+
printGreen("secret <USERINFO> 更改成功")
7692
} catch (error) {
77-
console.log("自动写入出错,登录信息如下,请手动添加到secret USERINFO")
78-
console.log(userinfoJSON)
93+
printRed("自动写入出错,登录信息如下,请手动添加到secret USERINFO")
94+
printRed(userinfoJSON)
7995
}
8096
} else {
81-
console.log("登录信息如下,把它添加到secret USERINFO 即可")
82-
console.log(userinfoJSON)
97+
printGreen("登录信息如下,把它添加到secret USERINFO 即可")
98+
printBlue(userinfoJSON)
8399
}
84100
}
85101
} finally {

0 commit comments

Comments
 (0)