Skip to content

Commit 5617ba8

Browse files
committed
♻️ 重构战绩页面数据刷新逻辑
1 parent fcd6a61 commit 5617ba8

2 files changed

Lines changed: 73 additions & 37 deletions

File tree

src/pages/User/Record.vue

Lines changed: 51 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ import TurOverviewGrid from "@comp/userRecord/tur-overview-grid.vue";
8181
import TurRoleInfo from "@comp/userRecord/tur-role-info.vue";
8282
import TurWorldGrid from "@comp/userRecord/tur-world-grid.vue";
8383
import recordReq from "@req/recordReq.js";
84+
import TSUserAccount from "@Sqlm/userAccount.js";
8485
import TSUserRecord from "@Sqlm/userRecord.js";
8586
import useUserStore from "@store/user.js";
8687
import { getVersion } from "@tauri-apps/api/app";
@@ -105,13 +106,20 @@ onMounted(async () => {
105106
});
106107
107108
watch(() => uidCur.value, loadRecord);
109+
watch(
110+
() => account.value,
111+
async () => await loadUid(),
112+
);
108113
109-
async function loadUid(): Promise<void> {
114+
async function loadUid(uid?: string): Promise<void> {
110115
uidList.value = await TSUserRecord.getAllUid();
111116
if (uidList.value.length === 0) uidList.value = [Number(account.value.gameUid)];
112117
if (uidList.value.includes(Number(account.value.gameUid))) {
113-
uidCur.value = Number(account.value.gameUid);
114-
} else uidCur.value = uidList.value[0];
118+
if (uid === undefined) uidCur.value = Number(account.value.gameUid);
119+
} else {
120+
uidList.value = [Number(account.value.gameUid), ...uidList.value];
121+
if (uid === undefined) uidCur.value = uidList.value[0];
122+
}
115123
}
116124
117125
async function loadRecord(): Promise<void> {
@@ -124,70 +132,77 @@ async function loadRecord(): Promise<void> {
124132
}
125133
126134
async function refreshRecord(): Promise<void> {
127-
if (!cookie.value) {
128-
showSnackbar.warn("请先登录");
129-
await TGLogger.Warn(`[UserRecord][refresh][${account.value.gameUid}] 未登录`);
130-
return;
131-
}
132-
if (uidCur.value && uidCur.value.toString() !== account.value.gameUid) {
133-
const switchCheck = await showDialog.check(
134-
"是否切换游戏账户",
135-
`确认则尝试切换至${uidCur.value}`,
136-
);
137-
if (switchCheck) {
138-
await userStore.switchGameAccount(uidCur.value.toString());
139-
await refreshRecord();
135+
let rfAccount = account.value;
136+
let rfCk = cookie.value;
137+
if (!uidCur.value) {
138+
if (!rfCk) {
139+
showSnackbar.warn("请先登录");
140+
await TGLogger.Warn(`[UserRecord][refresh][${rfAccount.gameUid}] 未登录`);
140141
return;
141142
}
142-
const freshCheck = await showDialog.check(
143-
"是否刷新战绩数据",
144-
`用户${account.value.gameUid}与当前UID${uidCur.value}不一致`,
145-
);
146-
if (!freshCheck) {
147-
showSnackbar.cancel("已取消战绩数据刷新");
148-
return;
143+
} else {
144+
const gcFind = await TSUserAccount.game.getAccountByGid(uidCur.value.toString());
145+
console.log(uidCur.value, gcFind);
146+
if (!gcFind) {
147+
const check = await showDialog.check(
148+
`确定刷新?`,
149+
`未找到 ${uidCur.value} 对应 UID,将刷新 ${rfAccount.gameUid} 数据`,
150+
);
151+
if (!check) return;
152+
} else {
153+
const acFind = await TSUserAccount.account.getAccount(gcFind.uid);
154+
if (!acFind) {
155+
const check = await showDialog.check(
156+
`确定刷新?`,
157+
`未找到 ${uidCur.value} 对应 CK,将刷新 ${rfAccount.gameUid} 数据`,
158+
);
159+
if (!check) return;
160+
} else {
161+
rfAccount = gcFind;
162+
rfCk = acFind.cookie;
163+
}
149164
}
150165
}
151-
await showLoading.start(`正在刷新${account.value.gameUid}的战绩数据`);
152-
await TGLogger.Info(`[UserRecord][refresh][${account.value.gameUid}] 刷新战绩数据`);
153-
const resp = await recordReq.index(cookie.value, account.value);
166+
await showLoading.start(`正在刷新${rfAccount.gameUid}的战绩数据`);
167+
await TGLogger.Info(`[UserRecord][refresh][${rfAccount.gameUid}] 刷新战绩数据`);
168+
const resp = await recordReq.index(rfCk!, rfAccount);
154169
console.log(resp);
155170
if ("retcode" in resp) {
156171
await showLoading.end();
157172
showSnackbar.error(`[${resp.retcode}] ${resp.message}`);
158-
await TGLogger.Error(`[UserRecord][refresh][${account.value.gameUid}] 获取战绩数据失败`);
173+
await TGLogger.Error(`[UserRecord][refresh][${rfAccount.gameUid}] 获取战绩数据失败`);
159174
await TGLogger.Error(
160-
`[UserRecord][refresh][${account.value.gameUid}] ${resp.retcode} ${resp.message}`,
175+
`[UserRecord][refresh][${rfAccount.gameUid}] ${resp.retcode} ${resp.message}`,
161176
);
162177
return;
163178
}
164-
await TGLogger.Info(`[UserRecord][refresh][${account.value.gameUid}] 获取战绩数据成功`);
165-
await TGLogger.Info(`[UserRecord][refresh][${account.value.gameUid}]`, false);
179+
await TGLogger.Info(`[UserRecord][refresh][${rfAccount.gameUid}] 获取战绩数据成功`);
180+
await TGLogger.Info(`[UserRecord][refresh][${rfAccount.gameUid}]`, false);
166181
await showLoading.update("正在保存战绩数据");
167-
await TSUserRecord.saveRecord(Number(account.value.gameUid), resp);
182+
await TSUserRecord.saveRecord(Number(rfAccount.gameUid), resp);
168183
await showLoading.update("正在加载战绩数据");
169-
await loadUid();
184+
await loadUid(rfAccount.gameUid);
170185
await loadRecord();
171186
await showLoading.end();
172-
showSnackbar.success(`成功刷新${account.value.gameUid}的战绩数据`);
187+
showSnackbar.success(`成功刷新${rfAccount.gameUid}的战绩数据`);
173188
}
174189
175190
async function shareRecord(): Promise<void> {
176191
if (!recordData.value) {
177192
showSnackbar.warn("未找到战绩数据,请尝试刷新");
178193
return;
179194
}
180-
await TGLogger.Info(`[UserRecord][shareRecord][${account.value.gameUid}] 生成分享图片`);
195+
await TGLogger.Info(`[UserRecord][shareRecord][${uidCur.value}] 生成分享图片`);
181196
const recordBox = document.querySelector<HTMLElement>(".ur-box");
182197
if (recordBox === null) {
183198
showSnackbar.error("未找到战绩数据,请尝试刷新");
184199
return;
185200
}
186-
const fileName = `【原神战绩】-${account.value.gameUid}.png`;
201+
const fileName = `【原神战绩】-${uidCur.value}.png`;
187202
await showLoading.start("正在生成图片", fileName);
188203
await generateShareImg(fileName, recordBox);
189204
await showLoading.end();
190-
await TGLogger.Info(`[UserRecord][shareRecord][${account.value.gameUid}] 生成分享图片成功`);
205+
await TGLogger.Info(`[UserRecord][shareRecord][${uidCur.value}] 生成分享图片成功`);
191206
}
192207
193208
async function deleteRecord(): Promise<void> {

src/plugins/Sqlite/modules/userAccount.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
* 用户账户模块
3-
* @since Beta v0.9.5
3+
* @since Beta v0.9.6
44
*/
55

66
import showLoading from "@comp/func/loading.js";
@@ -307,6 +307,26 @@ async function getGameAccount(uid: string): Promise<Array<TGApp.Sqlite.Account.G
307307
);
308308
}
309309

310+
/**
311+
* 获取指定 GameUid 的 uid
312+
* @since Beta v0.9.6
313+
* @param gid - 游戏 UID
314+
* @param biz - 游戏 BIZ
315+
* @returns UID或false
316+
*/
317+
async function getGameAccountByGid(
318+
gid: string,
319+
biz: string = "hk4e_cn",
320+
): Promise<TGApp.Sqlite.Account.Game | false> {
321+
const db = await TGSqlite.getDB();
322+
const res = await db.select<Array<TGApp.Sqlite.Account.Game>>(
323+
"SELECT * FROM GameAccount WHERE gameUid = $1 AND gameBiz = $2",
324+
[gid, biz],
325+
);
326+
if (res.length === 0) return false;
327+
return res[0];
328+
}
329+
310330
/**
311331
* 切换到指定游戏账号
312332
* @since Beta v0.6.0
@@ -399,6 +419,7 @@ const TSUserAccount = {
399419
},
400420
game: {
401421
getAccount: getGameAccount,
422+
getAccountByGid: getGameAccountByGid,
402423
switchAccount: switchGameAccount,
403424
getCurAccount: getCurGameAccount,
404425
saveAccounts: saveGameAccount,

0 commit comments

Comments
 (0)