🐛 保存用户信息采用参数绑定,避免特殊数据导致的sql拼接异常

This commit is contained in:
BTMuli
2026-01-29 14:39:16 +08:00
parent 855ddace77
commit a78921a9b1
2 changed files with 13 additions and 22 deletions

View File

@@ -428,6 +428,7 @@ async function tryGetTokens(ck: TGApp.App.Account.Cookie): Promise<void> {
ck.cookie_token = cookieTokenRes;
await showLoading.update("正在获取用户信息");
const briefRes = await bbsReq.userInfo(ck);
console.debug(briefRes);
if ("retcode" in briefRes) {
await showLoading.end();
showSnackbar.error(`[${briefRes.retcode}]${briefRes.message}`);

View File

@@ -1,6 +1,6 @@
/**
* 用户账户模块
* @since Beta v0.9.2
* @since Beta v0.9.5
*/
import showLoading from "@comp/func/loading.js";
@@ -41,24 +41,6 @@ function getInsertGameAccountSql(uid: string, data: TGApp.BBS.Game.Account): str
`;
}
/**
* 获取插入账号数据的 sql
* @since Beta v0.6.1
* @param user - 账号
* @returns 插入Sql
*/
function getInsertAccountSql(user: TGApp.App.Account.User): string {
const table = transUser(user);
return `
INSERT INTO UserAccount(uid, cookie, brief, updated)
VALUES ('${table.uid}', '${table.cookie}', '${table.brief}', '${table.updated}')
ON CONFLICT(uid) DO UPDATE
SET cookie = '${table.cookie}',
brief = '${table.brief}',
updated = '${table.updated}';
`;
}
/**
* 数据库转成可用数据
* @since Beta v0.6.0
@@ -130,14 +112,22 @@ async function getUserAccount(uid: string): Promise<TGApp.App.Account.User | fal
/**
* 更新用户数据
* @since Beta v0.6.1
* @since Beta v0.9.5
* @param data - 用户cookie
* @returns 无返回值
*/
async function saveAccount(data: TGApp.App.Account.User): Promise<void> {
const db = await TGSqlite.getDB();
const sql = getInsertAccountSql(data);
await db.execute(sql);
const user = transUser(data);
await db.execute(
`INSERT INTO UserAccount(uid, cookie, brief, updated)
VALUES ($1, $2, $3, $4)
ON CONFLICT(uid) DO UPDATE
SET cookie = $2,
brief = $3,
updated = $4`,
[user.uid, user.cookie, user.brief, user.updated],
);
}
/**