mirror of
https://github.com/BTMuli/TeyvatGuide.git
synced 2026-03-27 05:49:45 +08:00
235
src/plugins/Sqlite/modules/userAccount.ts
Normal file
235
src/plugins/Sqlite/modules/userAccount.ts
Normal file
@@ -0,0 +1,235 @@
|
||||
/**
|
||||
* @file plugins/Sqlite/modules/userAccounts.ts
|
||||
* @description 用户账户模块
|
||||
* @since Beta v0.6.0
|
||||
*/
|
||||
|
||||
import { timestampToDate } from "../../../utils/toolFunc.js";
|
||||
import TGSqlite from "../index.js";
|
||||
|
||||
/**
|
||||
* @description 获取插入游戏账号数据的sql
|
||||
* @since Beta v0.6.0
|
||||
* @param {string} uid - 米社UID
|
||||
* @param {TGApp.BBS.Account.GameAccount} data - 游戏账号数据
|
||||
* @return {string}
|
||||
*/
|
||||
function getInsertGameAccountSql(uid: string, data: TGApp.BBS.Account.GameAccount): string {
|
||||
const isChosen = data.is_chosen ? 1 : 0;
|
||||
const isOfficial = data.is_official ? 1 : 0;
|
||||
const timeNow = timestampToDate(new Date().getTime());
|
||||
return `
|
||||
INSERT INTO GameAccount(uid, gameBiz, gameUid, isChosen, isOfficial, level, nickname, region, regionName, updated)
|
||||
VALUES ('${uid}', '${data.game_biz}', '${data.game_uid}', ${isChosen}, ${isOfficial}, ${data.level},
|
||||
'${data.nickname}', '${data.region}', '${data.region_name}', '${timeNow}')
|
||||
ON CONFLICT(uid, gameUid) DO UPDATE
|
||||
SET gameBiz = '${data.game_biz}',
|
||||
isChosen = ${isChosen},
|
||||
isOfficial = ${isOfficial},
|
||||
level = ${data.level},
|
||||
nickname = '${data.nickname}',
|
||||
region = '${data.region}',
|
||||
regionName = '${data.region_name}',
|
||||
updated = '${timeNow}';
|
||||
`;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 数据库转成可用数据
|
||||
* @since Beta v0.6.0
|
||||
* @param {TGApp.Sqlite.Account.User} data - 用户数据
|
||||
* @returns {TGApp.App.Account.User}
|
||||
*/
|
||||
function parseUser(data: TGApp.Sqlite.Account.User): TGApp.App.Account.User {
|
||||
return {
|
||||
uid: data.uid,
|
||||
brief: JSON.parse(data.brief),
|
||||
cookie: JSON.parse(data.cookie),
|
||||
updated: data.updated,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 转成数据库数据方便存储
|
||||
* @since Beta v0.6.0
|
||||
* @param {TGApp.App.Account.User} data - 用户数据
|
||||
* @returns {TGApp.Sqlite.Account.User}
|
||||
*/
|
||||
function transUser(data: TGApp.App.Account.User): TGApp.Sqlite.Account.User {
|
||||
return {
|
||||
uid: data.uid,
|
||||
brief: JSON.stringify(data.brief),
|
||||
cookie: JSON.stringify(data.cookie),
|
||||
updated: timestampToDate(new Date().getTime()),
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 获取所有用户数据
|
||||
* @since Beta v0.6.0
|
||||
* @returns {Promise<TGApp.App.Account.User[]>}
|
||||
*/
|
||||
async function getAllAccount(): Promise<TGApp.App.Account.User[]> {
|
||||
const db = await TGSqlite.getDB();
|
||||
const res = await db.select<TGApp.Sqlite.Account.User[]>("SELECT * FROM UserAccount;");
|
||||
return res.map((account) => parseUser(account));
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 获取所有UID
|
||||
* @since Beta v0.6.0
|
||||
* @returns {Promise<string[]>}
|
||||
*/
|
||||
async function getAllAccountId(): Promise<string[]> {
|
||||
const db = await TGSqlite.getDB();
|
||||
type resType = Array<{ uid: string }>;
|
||||
const res = await db.select<resType>("SELECT DISTINCT uid FROM GameAccount;");
|
||||
return res.map((account) => account.uid);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 获取指定用户数据
|
||||
* @since Beta v0.6.0
|
||||
* @param {string} uid - 用户UID
|
||||
* @returns {Promise<TGApp.App.Account.User|false>}
|
||||
*/
|
||||
async function getUserAccount(uid: string): Promise<TGApp.App.Account.User | false> {
|
||||
const db = await TGSqlite.getDB();
|
||||
const res = await db.select<TGApp.Sqlite.Account.User[]>(
|
||||
"SELECT * FROM UserAccount WHERE uid = ?",
|
||||
[uid],
|
||||
);
|
||||
if (res.length === 0) return false;
|
||||
return parseUser(res[0]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 更新用户数据
|
||||
* @since Beta v0.6.0
|
||||
* @param {TGApp.App.Account.User} data - 用户cookie
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
async function saveAccount(data: TGApp.App.Account.User): Promise<void> {
|
||||
const db = await TGSqlite.getDB();
|
||||
const table = transUser(data);
|
||||
const timeNow = timestampToDate(new Date().getTime());
|
||||
await db.execute(
|
||||
"INSERT INTO UserAccount(uid, cookie, brief, updated) VALUES \
|
||||
(?,?,?,?) ON CONFLICT (uid) DO UPDATE SET cookie=?,brief=?,updated=?",
|
||||
[table.uid, table.cookie, table.brief, timeNow, table.cookie, table.brief, timeNow],
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 复制cookie
|
||||
* @since Beta v0.6.0
|
||||
* @param {TGApp.App.Account.Cookie} cookie - cookie
|
||||
* @return {string}
|
||||
*/
|
||||
function copyCookie(cookie: TGApp.App.Account.Cookie): string {
|
||||
let res = "";
|
||||
if (cookie.ltuid && cookie.ltuid !== "") {
|
||||
res += `ltuid=${cookie.ltuid};`;
|
||||
}
|
||||
if (cookie.ltoken && cookie.ltoken !== "") {
|
||||
res += `ltoken=${cookie.ltoken};`;
|
||||
}
|
||||
if (cookie.mid && cookie.mid !== "") {
|
||||
res += `mid=${cookie.mid};`;
|
||||
}
|
||||
if (cookie.cookie_token && cookie.cookie_token !== "") {
|
||||
res += `cookie_token=${cookie.cookie_token};`;
|
||||
}
|
||||
if (cookie.stoken && cookie.stoken !== "") {
|
||||
res += `stoken=${cookie.stoken};`;
|
||||
}
|
||||
if (cookie.stuid && cookie.stuid !== "") {
|
||||
res += `stuid=${cookie.stuid};`;
|
||||
}
|
||||
if (cookie.account_id && cookie.account_id !== "") {
|
||||
res += `account_id=${cookie.account_id};`;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 获取指定用户账号
|
||||
* @since Beta v0.6.0
|
||||
* @param {string} uid - 用户UID
|
||||
* @returns {Promise<TGApp.Sqlite.Account.Game[]>}
|
||||
*/
|
||||
async function getGameAccount(uid: string): Promise<TGApp.Sqlite.Account.Game[]> {
|
||||
const db = await TGSqlite.getDB();
|
||||
return await db.select<TGApp.Sqlite.Account.Game[]>("SELECT * FROM GameAccount WHERE uid = ?;", [
|
||||
uid,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 切换到指定游戏账号
|
||||
* @since Beta v0.6.0
|
||||
* @param {string} uid - 米社UID
|
||||
* @param {string} gameUid - 游戏UID
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
async function switchGameAccount(uid: string, gameUid: string): Promise<void> {
|
||||
const db = await TGSqlite.getDB();
|
||||
await db.execute("UPDATE GameAccount SET isChosen = 0,updated=? WHERE uid = ?;", [
|
||||
timestampToDate(new Date().getTime()),
|
||||
uid,
|
||||
]);
|
||||
await db.execute("UPDATE GameAccount SET isChosen=1,updated=? WHERE uid = ? AND gameUid = ?;", [
|
||||
timestampToDate(new Date().getTime()),
|
||||
uid,
|
||||
gameUid,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 获取最近的游戏账户
|
||||
* @since Beta v0.6.0
|
||||
* @param {string} uid - 米社UID
|
||||
* @return {Promise<TGApp.Sqlite.Account.Game|false>}
|
||||
*/
|
||||
async function getCurGameAccount(uid: string): Promise<TGApp.Sqlite.Account.Game | false> {
|
||||
const gameAccounts = await getGameAccount(uid);
|
||||
if (gameAccounts.length === 0) return false;
|
||||
const curGameAccount = gameAccounts.find((account) => account.isChosen === 1);
|
||||
if (!curGameAccount) return gameAccounts[0];
|
||||
return curGameAccount;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 保存游戏账户数据
|
||||
* @since Beta v0.6.0
|
||||
* @param {string} uid - 米社UID
|
||||
* @param {TGApp.BBS.Account.GameAccount[]} accounts - 账户数据
|
||||
* @return {Promise<void>}
|
||||
*/
|
||||
async function saveGameAccount(
|
||||
uid: string,
|
||||
accounts: TGApp.BBS.Account.GameAccount[],
|
||||
): Promise<void> {
|
||||
const db = await TGSqlite.getDB();
|
||||
for (const account of accounts) {
|
||||
await db.execute(getInsertGameAccountSql(uid, account));
|
||||
}
|
||||
}
|
||||
|
||||
const TSUserAccount = {
|
||||
account: {
|
||||
getAllUid: getAllAccountId,
|
||||
getAllAccount,
|
||||
getAccount: getUserAccount,
|
||||
saveAccount,
|
||||
copy: copyCookie,
|
||||
},
|
||||
game: {
|
||||
getAccount: getGameAccount,
|
||||
switchAccount: switchGameAccount,
|
||||
getCurAccount: getCurGameAccount,
|
||||
saveAccounts: saveGameAccount,
|
||||
},
|
||||
};
|
||||
|
||||
export default TSUserAccount;
|
||||
@@ -1,22 +1,67 @@
|
||||
/**
|
||||
* @file plugins/Sqlite/modules/userAvatar.ts
|
||||
* @description 用户角色模块
|
||||
* @since Beta v0.5.3
|
||||
* @since Beta v0.6.0
|
||||
*/
|
||||
|
||||
import { AppCharacterData } from "../../../data/index.js";
|
||||
import { timestampToDate } from "../../../utils/toolFunc.js";
|
||||
import TGSqlite from "../index.js";
|
||||
import { insertRoleData } from "../sql/insertData.js";
|
||||
|
||||
/**
|
||||
* @description 获取角色插入Sql
|
||||
* @since Beta v0.6.0
|
||||
* @param {string} uid - 用户UID
|
||||
* @param {TGApp.Game.Avatar.DetailList} data - 角色数据
|
||||
* @return {string}
|
||||
*/
|
||||
function getInsertSql(uid: string, data: TGApp.Game.Avatar.DetailList): string {
|
||||
const role: TGApp.Sqlite.Character.UserRoleDB = {
|
||||
uid: Number(uid),
|
||||
cid: data.base.id,
|
||||
avatar: JSON.stringify(data.base),
|
||||
weapon: JSON.stringify(data.weapon),
|
||||
relics: JSON.stringify(data.relics),
|
||||
constellations: JSON.stringify(data.constellations),
|
||||
costumes: JSON.stringify(data.costumes),
|
||||
skills: JSON.stringify(data.skills),
|
||||
propSelected: JSON.stringify(data.selected_properties),
|
||||
propBase: JSON.stringify(data.base_properties),
|
||||
propExtra: JSON.stringify(data.extra_properties),
|
||||
propRecommend: JSON.stringify(data.recommend_relic_property),
|
||||
updated: timestampToDate(new Date().getTime()),
|
||||
};
|
||||
return `
|
||||
INSERT INTO UserCharacters (uid, cid, avatar, weapon, relics, constellations, costumes, skills,
|
||||
propSelected, propBase, propExtra, propRecommend, updated)
|
||||
VALUES (${uid}, ${role.cid}, '${role.avatar}', '${role.weapon}', '${role.relics}', '${role.constellations}',
|
||||
'${role.costumes}', '${role.skills}', '${role.propSelected}', '${role.propBase}', '${role.propExtra}',
|
||||
'${role.propRecommend}', '${role.updated}')
|
||||
ON CONFLICT(uid, cid) DO UPDATE
|
||||
SET avatar = '${role.avatar}',
|
||||
weapon = '${role.weapon}',
|
||||
relics = '${role.relics}',
|
||||
constellations = '${role.constellations}',
|
||||
costumes = '${role.costumes}',
|
||||
skills = '${role.skills}',
|
||||
propSelected = '${role.propSelected}',
|
||||
propBase = '${role.propBase}',
|
||||
propExtra = '${role.propExtra}',
|
||||
propRecommend = '${role.propRecommend}',
|
||||
updated = '${role.updated}';
|
||||
`;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 获取用户角色id列表
|
||||
* @since Beta v0.5.3
|
||||
* @returns {Promise<string[]>} 角色id列表
|
||||
*/
|
||||
async function getAllAvatarId(): Promise<string[]> {
|
||||
async function getAllUid(): Promise<string[]> {
|
||||
const db = await TGSqlite.getDB();
|
||||
type resType = Array<{ cid: string }>;
|
||||
type resType = Array<{ uid: string }>;
|
||||
const res = await db.select<resType>("SELECT DISTINCT cid FROM UserCharacters;");
|
||||
return res.map((i) => i.cid);
|
||||
return res.map((i) => i.uid);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -50,35 +95,32 @@ async function getAvatars(uid: string): Promise<TGApp.Sqlite.Character.UserRole[
|
||||
|
||||
/**
|
||||
* @description 保存用户角色数据
|
||||
* @since Beta v0.5.3
|
||||
* @since Beta v0.6.0
|
||||
* @param {string} uid 用户 uid
|
||||
* @param {TGApp.Game.Avatar.DetailList[]} data 角色数据
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
async function saveAvatars(uid: string, data: TGApp.Game.Avatar.DetailList[]): Promise<void> {
|
||||
const db = await TGSqlite.getDB();
|
||||
const sql = insertRoleData(uid, data);
|
||||
await db.execute(sql);
|
||||
for (const role of data) {
|
||||
await db.execute(getInsertSql(uid, role));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 获取角色名片
|
||||
* @since Beta v0.5.3
|
||||
* @since Beta v0.6.0
|
||||
* @param {number} id 角色 id
|
||||
* @returns {Promise<string|false>}
|
||||
* @returns {string|false}
|
||||
*/
|
||||
async function getAvatarCard(id: number): Promise<string | false> {
|
||||
const db = await TGSqlite.getDB();
|
||||
type resType = Array<{ card: string }>;
|
||||
const res = await db.select<resType>("SELECT nameCard as card FROM AppCharacters WHERE id = ?;", [
|
||||
id,
|
||||
]);
|
||||
if (res.length === 0) return false;
|
||||
return res[0].card;
|
||||
function getAvatarCard(id: number): string {
|
||||
const find = AppCharacterData.find((c) => c.id === id);
|
||||
if (!find) return "原神·印象";
|
||||
return find.nameCard;
|
||||
}
|
||||
|
||||
const TSUserAvatar = {
|
||||
getAllAvatarId,
|
||||
getAllUid,
|
||||
getAvatars,
|
||||
saveAvatars,
|
||||
getAvatarCard,
|
||||
|
||||
Reference in New Issue
Block a user