Skip to content

Commit

Permalink
perf: 优化扫码登录
Browse files Browse the repository at this point in the history
  • Loading branch information
XasYer committed Dec 24, 2024
1 parent c6009fc commit f018be2
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 30 deletions.
13 changes: 12 additions & 1 deletion apps/QRLogin.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const rule = {
QRLoginTips: {
reg: App.getReg(baseReg),
fnc: async (e) => {
const tips = '将使用steamApp扫码二维码进行登录, 登录完成后机器人可获得对应账号的access_token并保存, 不会同时绑定对应的steamId, 拥有access_token后可执行各种隐私操作, 请在**特别信任**的机器人上进行扫码登录, 如果确认需要扫码登录, 请先打开steamApp进入扫码界面并继续发送\n#steam确认扫码登录'
const tips = '将使用steamApp扫码二维码进行登录, 登录完成后机器人可获得对应账号的access_token并保存, 拥有access_token后可执行各种隐私操作, 请在**特别信任**的机器人上进行扫码登录, 如果确认需要扫码登录, 请先打开steamApp进入扫码界面并继续发送(不支持扫描相册二维码)\n#steam确认扫码登录'
await e.reply(tips)
verify[e.user_id] = true
return true
Expand All @@ -35,6 +35,17 @@ const rule = {
const qrcodeRes = await api.IAuthenticationService.PollAuthSessionStatus(session.client_id, session.request_id).catch(() => {})
if (qrcodeRes.access_token) {
const dbRes = await db.TokenTableAddData(e.user_id, qrcodeRes.access_token, qrcodeRes.refresh_token)
const user = await db.UserTableGetDataBySteamId(dbRes.steamId)
if (user.userId) {
if (user.userId != e.user_id) {
await db.UserTableDelSteamIdByUserId(user.userId, dbRes.steamId)
await db.UserTableAddSteamIdByUserId(e.user_id, dbRes.steamId)
} else {
await db.UserTableBindSteamIdByUserId(e.user_id, dbRes.steamId, true)
}
} else {
await db.UserTableAddSteamIdByUserId(e.user_id, dbRes.steamId)
}
await e.reply(`登录成功\nsteamId: ${dbRes.steamId}\n登录名: ${qrcodeRes.account_name.replace(/^(.)(.*)(.)$/, '$1***$3')}\n需要切换到对应的steamId才会生效`)
return true
}
Expand Down
5 changes: 4 additions & 1 deletion models/bind/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export async function getBindSteamIdsImg (bid, uid, gid, userBindAll = []) {
if (!userBindAll.length) {
return '没有绑定任何steamId, 请使用#steam绑定steamId或好友码 进行绑定'
}
const accessTokenList = await db.TokenTableGetByUserId(uid)
const enablePush = (() => {
if (!Config.push.enable) {
return false
Expand Down Expand Up @@ -47,14 +48,16 @@ export async function getBindSteamIdsImg (bid, uid, gid, userBindAll = []) {
} catch { }
let index = 1
for (const item of userBindAll) {
const accessToken = accessTokenList.find(i => i.steamId == item.steamId)
const i = allSteamIdInfo[item.steamId] || {}
const avatar = Config.other.steamAvatar ? i.avatarfull : await utils.bot.getUserAvatar(bid, uid, gid)
const info = {
steamId: item.steamId,
isBind: item.isBind,
name: i.personaname || await utils.bot.getUserName(bid, uid, gid),
avatar: avatar || await utils.bot.getUserAvatar(bid, uid, gid),
index
index,
type: accessToken ? 'ck' : 'reg'
}
if (enablePushSteamIdList.includes(item.steamId)) {
pushSteamId.push(info)
Expand Down
11 changes: 0 additions & 11 deletions models/db/history.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,17 +52,6 @@ const HistoryTable = sequelize.define('history', {

await HistoryTable.sync()

HistoryTable.findAll().then(res => {
if (res.length) {
// 在这之前统计的记录有误差 全部删除重新统计
const time = 1734495420
const data = res[0].dataValues
if (data.start < time && data.end < time) {
HistoryTable.truncate().catch(() => {})
}
}
}).catch(() => {})

/**
* 添加一条记录
* @param {string} userId
Expand Down
16 changes: 15 additions & 1 deletion models/db/token.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ const TokenTable = sequelize.define('token', {
await TokenTable.sync()

/**
* 添加steamId到userId
* 添加accessToken到userId
* @param {string} userId
* @param {string} accessToken
* @param {string?} refreshToken
Expand Down Expand Up @@ -112,6 +112,20 @@ export async function TokenTableGetByUserIdAndSteamId (userId, steamId) {
}).then(res => res?.dataValues)
}

/**
* 根据userId查询所有信息
* @param {string} userId
* @returns {Promise<TokenColumns[]|null>}
*/
export async function TokenTableGetByUserId (userId) {
userId = String(userId)
return await TokenTable.findAll({
where: {
userId
}
}).then(res => res.map(item => item.dataValues))
}

/**
* 删除accessToken
* @param {string} userId
Expand Down
28 changes: 15 additions & 13 deletions models/db/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,19 +64,21 @@ export async function UserTableBindSteamIdByUserId (userId, steamId, isBind = tr
// 开启一个事务
const transaction = await sequelize.transaction()
try {
// 先将之前绑定的steamId解除绑定
const data = await UserTableGetDataByUserId(userId)
const bind = data.find(item => item.isBind)
if (bind) {
await UserTable.update({
isBind: false
}, {
transaction,
where: {
userId: bind.userId,
steamId: bind.steamId
}
})
if (isBind) {
// 先将之前绑定的steamId解除绑定
const data = await UserTableGetDataByUserId(userId)
const bind = data.find(item => item.isBind)
if (bind) {
await UserTable.update({
isBind: false
}, {
transaction,
where: {
userId: bind.userId,
steamId: bind.steamId
}
})
}
}
const res = await UserTable.update({
isBind
Expand Down
28 changes: 25 additions & 3 deletions resources/user/index.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions resources/user/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
<div class="info">
<div class="uid-info">
<div class="uid shadow">{{uid.steamId}}<span>{{uid.steamId}}</span></div>
<div class="type {{uid.type}}">{{uid.type==='ck'?'扫码':'绑定'}}</div>
</div>
<div class="detail">
<div class="name">{{uid.name}}</div>
Expand Down

0 comments on commit f018be2

Please sign in to comment.