-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathCharacters.vue
480 lines (455 loc) · 14.7 KB
/
Characters.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
<template>
<v-app-bar>
<template #prepend>
<div class="uc-top-title">
<img alt="icon" src="/source/UI/userAvatar.webp" />
<span>我的角色</span>
<v-btn variant="outlined" @click="showSelect = true">筛选角色</v-btn>
<v-btn variant="outlined" @click="resetSelect = true">重置筛选</v-btn>
</div>
</template>
<template #append>
<div class="uc-top-btns">
<v-btn
prepend-icon="mdi-refresh"
@click="refresh()"
:rounded="true"
variant="outlined"
v-model:loading="loadData"
>
刷新
</v-btn>
<v-btn
prepend-icon="mdi-share"
:disabled="enableShare"
@click="share()"
:rounded="true"
variant="outlined"
v-model:loading="loadShare"
>
分享
</v-btn>
<v-btn
prepend-icon="mdi-delete"
@click="deleteUid()"
:rounded="true"
variant="outlined"
:disabled="uidCur === undefined"
v-model:loading="loadDel"
>
删除
</v-btn>
</div>
</template>
<template #extension>
<div class="uc-select">
<v-select
v-model="showMode"
:items="modeList"
label="详情视图模式"
:hide-details="true"
item-title="label"
item-value="value"
variant="outlined"
class="uc-select-btn"
density="compact"
/>
<v-select
v-model="uidCur"
:items="uidList"
label="当前UID"
:hide-details="true"
variant="outlined"
class="uc-select-btn"
density="compact"
/>
</div>
</template>
</v-app-bar>
<div class="uc-box">
<div class="uc-top">
<div class="uc-top-title">UID:{{ uidCur }}</div>
<div class="uc-top-info">
<span>角色详情</span>
<span>|Render by TeyvatGuide v{{ version }}|</span>
<span>更新于 {{ getUpdateTime() }}</span>
</div>
</div>
<div class="uc-grid" v-if="!isEmpty">
<TuaAvatarBox
v-for="(role, index) in selectedList"
:key="index"
:model-value="role"
@click="selectRole(role)"
/>
</div>
<div class="uc-empty" v-else>
<img src="/source/UI/empty.webp" alt="empty" />
</div>
</div>
<TuaDetailOverlay
v-if="dataVal"
v-model="showOverlay"
:avatar="dataVal"
:avatars="selectedList"
v-model:mode="showMode"
@to-next="handleSwitch"
@to-avatar="selectRole"
/>
<TwoSelectC v-model="showSelect" @select-c="handleSelect" v-model:reset="resetSelect" />
</template>
<script lang="ts" setup>
import showDialog from "@comp/func/dialog.js";
import showLoading from "@comp/func/loading.js";
import showSnackbar from "@comp/func/snackbar.js";
import TwoSelectC, { type SelectedCValue } from "@comp/pageWiki/two-select-c.vue";
import TuaAvatarBox from "@comp/userAvatar/tua-avatar-box.vue";
import TuaDetailOverlay from "@comp/userAvatar/tua-detail-overlay.vue";
import TSUserAvatar from "@Sqlite/modules/userAvatar.js";
import { getVersion } from "@tauri-apps/api/app";
import { storeToRefs } from "pinia";
import { computed, onMounted, ref, shallowRef, watch } from "vue";
import { AppCharacterData } from "@/data/index.js";
import { useUserStore } from "@/store/modules/user.js";
import TGLogger from "@/utils/TGLogger.js";
import { generateShareImg } from "@/utils/TGShare.js";
import { timestampToDate } from "@/utils/toolFunc.js";
import TakumiRecordGenshinApi from "@/web/request/recordReq.js";
type TabItem = { label: string; value: string };
const modeList: Readonly<Array<TabItem>> = [
{ label: "经典视图", value: "classic" },
{ label: "卡片视图(简略)", value: "card" },
{ label: "卡片视图(详细)", value: "dev" },
];
const { cookie, account, propMap } = storeToRefs(useUserStore());
const loadData = ref<boolean>(false);
const loadShare = ref<boolean>(false);
const loadDel = ref<boolean>(false);
const version = ref<string>();
const isEmpty = ref<boolean>(true);
const showOverlay = ref<boolean>(false);
const selectIndex = ref<number>(0);
const showSelect = ref<boolean>(false);
const showMode = ref<"classic" | "card" | "dev">("dev");
const resetSelect = ref<boolean>(false);
const uidCur = ref<string>();
const uidList = shallowRef<Array<string>>([]);
const roleList = shallowRef<Array<TGApp.Sqlite.Character.UserRole>>([]);
const selectedList = shallowRef<Array<TGApp.Sqlite.Character.UserRole>>([]);
const dataVal = shallowRef<TGApp.Sqlite.Character.UserRole>();
const enableShare = computed<boolean>(
() => showOverlay.value || showSelect.value || loadData.value,
);
onMounted(async () => {
await showLoading.start("正在获取角色数据");
await TGLogger.Info("[Character][onMounted] 进入角色页面");
version.value = await getVersion();
await showLoading.update("正在加载UID列表");
await loadUid();
loadData.value = false;
await showLoading.end();
});
watch(
() => resetSelect.value,
() => {
if (resetSelect.value) {
selectedList.value = getOrderedList(roleList.value);
showSnackbar.success("已重置筛选条件");
if (!dataVal.value) return;
selectIndex.value = selectedList.value.indexOf(dataVal.value);
if (selectIndex.value === -1) {
dataVal.value = selectedList.value[0];
selectIndex.value = 0;
}
}
},
);
watch(
() => showMode.value,
() => {
switch (showMode.value) {
case "classic":
showSnackbar.success("已切换至经典视图");
break;
case "card":
showSnackbar.success("已切换至卡片视图(简略)");
break;
case "dev":
showSnackbar.success("已切换至卡片视图(详细)");
break;
}
},
);
watch(() => uidCur.value, loadRole);
function getOrderedList(
data: Array<TGApp.Sqlite.Character.UserRole>,
): Array<TGApp.Sqlite.Character.UserRole> {
return data.sort((a, b) => {
if (a.avatar.rarity !== b.avatar.rarity) return b.avatar.rarity - a.avatar.rarity;
if (a.avatar.element === b.avatar.element) return a.cid - b.cid;
return a.avatar.element.localeCompare(b.avatar.element);
});
}
async function loadUid(): Promise<void> {
uidList.value = await TSUserAvatar.getAllUid();
if (uidList.value.length === 0) uidList.value = [account.value.gameUid];
if (uidList.value.includes(account.value.gameUid)) uidCur.value = account.value.gameUid;
else uidCur.value = uidList.value[0];
}
async function loadRole(): Promise<void> {
if (!uidCur.value) {
isEmpty.value = true;
return;
}
roleList.value = [];
const roleData = await TSUserAvatar.getAvatars(Number(uidCur.value));
roleList.value = getOrderedList(roleData);
selectedList.value = roleList.value;
dataVal.value = roleData[selectIndex.value];
isEmpty.value = roleList.value.length === 0;
await TGLogger.Info(`[Character][loadRole][${uidCur.value}] 成功加载角色数据`);
await TGLogger.Info(`[Character][loadRole][${uidCur.value}] 共获取到${roleData.length}个角色`);
showSnackbar.success(`成功加载${roleData.length}个角色`);
}
async function refresh(): Promise<void> {
if (!account.value) {
showSnackbar.warn("未获取到游戏账户");
return;
}
if (showSelect.value) {
showSelect.value = false;
await new Promise<void>((resolve) => setTimeout(resolve, 500));
}
if (showOverlay.value) {
showOverlay.value = false;
await new Promise<void>((resolve) => setTimeout(resolve, 500));
}
if (uidCur.value && uidCur.value !== account.value.gameUid) {
const switchCheck = await showDialog.check(
"是否切换游戏账户",
`确认则尝试切换至${uidCur.value}`,
);
if (switchCheck) {
await useUserStore().switchGameAccount(uidCur.value);
await refresh();
return;
}
const freshCheck = await showDialog.check(
"是否刷新角色数据",
`用户${account.value.gameUid}与当前UID${uidCur.value}不一致`,
);
if (!freshCheck) {
showSnackbar.cancel("已取消角色数据刷新");
return;
}
}
await TGLogger.Info(`[Character][refreshRoles][${account.value.gameUid}] 正在更新角色数据`);
if (!cookie.value) {
showSnackbar.warn("请先登录");
loadData.value = false;
return;
}
await showLoading.start(`正在更新${account.value.gameUid}的角色数据`);
loadData.value = true;
await showLoading.update("正在刷新首页数据");
const indexRes = await TakumiRecordGenshinApi.index(cookie.value, account.value, 1);
if ("retcode" in indexRes) {
showSnackbar.error(`[${indexRes.retcode}] ${indexRes.message}`);
await TGLogger.Error(JSON.stringify(indexRes.message));
await showLoading.end();
loadData.value = false;
return;
}
await showLoading.update("正在获取角色列表");
const listRes = await TakumiRecordGenshinApi.character.list(cookie.value, account.value);
if (!Array.isArray(listRes)) {
showSnackbar.error(`[${listRes.retcode}] ${listRes.message}`);
await TGLogger.Error(`[Character][refreshRoles][${account.value.gameUid}] 获取角色列表失败`);
await TGLogger.Error(
`[Character][refreshRoles][${account.value.gameUid}] ${listRes.retcode} ${listRes.message}`,
);
await showLoading.end();
loadData.value = false;
return;
}
const idList = listRes.map((i) => i.id.toString());
await showLoading.update(`共${idList.length}个角色`);
const res = await TakumiRecordGenshinApi.character.detail(cookie.value, account.value, idList);
if ("retcode" in res) {
showSnackbar.error(`[${res.retcode}] ${res.message}`);
await TGLogger.Error(`[Character][refreshRoles][${account.value.gameUid}] 获取角色数据失败`);
await TGLogger.Error(
`[Character][refreshRoles][${account.value.gameUid}] ${res.retcode} ${res.message}`,
);
await showLoading.end();
loadData.value = false;
return;
}
propMap.value = res.property_map;
await showLoading.update("正在保存角色数据");
await TSUserAvatar.saveAvatars(account.value.gameUid, res.list);
await TGLogger.Info(`[Character][refreshRoles][${account.value.gameUid}] 成功更新角色数据`);
await TGLogger.Info(
`[Character][refreshRoles][${account.value.gameUid}] 共更新${res.list.length}个角色`,
);
await showLoading.update("正在加载角色数据");
await loadUid();
await loadRole();
await showLoading.end();
loadData.value = false;
}
async function share(): Promise<void> {
if (!account.value || isEmpty.value) {
showSnackbar.warn("暂无数据");
return;
}
await TGLogger.Info(`[Character][shareRoles][${account.value.gameUid}] 正在生成分享图片`);
const rolesBox = document.querySelector<HTMLElement>(".uc-box");
if (rolesBox === null) {
showSnackbar.error("未找到角色列表");
return;
}
const fileName = `【角色列表】-${account.value.gameUid}.png`;
await showLoading.start("正在生成图片", fileName);
loadShare.value = true;
await generateShareImg(fileName, rolesBox);
loadShare.value = false;
await showLoading.end();
await TGLogger.Info(`[Character][shareRoles][${account.value.gameUid}] 生成分享图片成功`);
}
async function deleteUid(): Promise<void> {
if (!uidCur.value) {
showSnackbar.warn("未找到当前UID");
return;
}
const delCheck = await showDialog.check("确定删除?", `将删除${uidCur.value}对应的角色数据`);
if (!delCheck) {
showSnackbar.cancel("已取消删除");
return;
}
await TSUserAvatar.deleteUid(uidCur.value);
showSnackbar.success(`成功删除${uidCur.value}的角色数据`);
await loadUid();
await loadRole();
}
function getUpdateTime(): string {
if (roleList.value.length === 0) return "";
let lastUpdateTime = 0;
for (const role of roleList.value) {
const updateTime = new Date(role.updated).getTime();
if (updateTime > lastUpdateTime) lastUpdateTime = updateTime;
}
return timestampToDate(lastUpdateTime);
}
function selectRole(role: TGApp.Sqlite.Character.UserRole): void {
dataVal.value = role;
selectIndex.value = roleList.value.indexOf(role);
if (!showOverlay.value) showOverlay.value = true;
}
function handleSelect(val: SelectedCValue) {
showSelect.value = false;
const filterC = AppCharacterData.filter((avatar) => {
if (!roleList.value.find((role) => role.avatar.id === avatar.id)) return false;
if (!val.star.includes(avatar.star)) return false;
if (!val.weapon.includes(avatar.weapon)) return false;
if (!val.elements.includes(avatar.element)) return false;
return val.area.includes(avatar.area);
});
if (filterC.length === 0) {
showSnackbar.warn("未找到符合条件的角色");
return;
}
showSnackbar.success(`筛选出符合条件的角色 ${filterC.length} 个`);
const selectedId = filterC.map((item) => item.id);
selectedList.value = roleList.value.filter((role) => selectedId.includes(role.cid));
if (!dataVal.value) return;
if (!selectedList.value.includes(dataVal.value)) {
dataVal.value = selectedList.value[0];
selectIndex.value = 0;
} else selectIndex.value = selectedList.value.indexOf(dataVal.value);
}
function handleSwitch(next: boolean): void {
if (next) {
selectIndex.value += 1;
if (selectIndex.value >= selectedList.value.length) selectIndex.value = 0;
} else {
selectIndex.value -= 1;
if (selectIndex.value < 0) selectIndex.value = selectedList.value.length - 1;
}
dataVal.value = selectedList.value[selectIndex.value];
}
</script>
<style lang="css" scoped>
.uc-select {
display: flex;
align-items: center;
justify-content: flex-start;
margin: 0 10px;
gap: 10px;
}
.uc-select-btn {
position: relative;
display: flex;
width: 200px;
height: 40px;
align-items: center;
justify-content: flex-start;
font-size: 14px;
}
.uc-box {
display: flex;
flex-direction: column;
padding: 10px;
border: 1px solid var(--common-shadow-2);
border-radius: 5px;
background: var(--box-bg-1);
gap: 10px;
}
.uc-top {
display: flex;
width: 100%;
height: 50px;
align-items: center;
justify-content: space-between;
padding: 10px;
border-bottom: 1px solid var(--common-shadow-2);
}
.uc-top-title {
display: flex;
align-items: center;
justify-content: center;
padding: 10px;
gap: 10px;
img {
width: 32px;
height: 32px;
}
span {
color: var(--common-text-title);
font-family: var(--font-title);
font-size: 20px;
}
}
.uc-top-info {
z-index: -1;
font-size: 14px;
opacity: 0.8;
}
.uc-top-btns {
display: flex;
align-content: center;
margin: 0 10px;
column-gap: 10px;
}
.uc-grid {
display: grid;
grid-gap: 15px;
grid-template-columns: repeat(auto-fill, minmax(210px, 0.2fr));
}
.uc-empty {
display: flex;
height: 100%;
align-items: center;
justify-content: center;
}
</style>