mirror of
https://github.com/BTMuli/TeyvatGuide.git
synced 2025-12-14 09:38:13 +08:00
🌱 UIGF 相关处理草创
This commit is contained in:
@@ -1,8 +1,8 @@
|
||||
/**
|
||||
* @file utils UIAF.ts
|
||||
* @description UIAF工具类
|
||||
* @author BTMuli<bt-muli@outlook.com>
|
||||
* @since Alpha v0.1.4
|
||||
* @author BTMuli <bt-muli@outlook.com>
|
||||
* @since Alpha v0.2.3
|
||||
*/
|
||||
|
||||
// tauri
|
||||
@@ -10,23 +10,6 @@ import { app, fs, path } from "@tauri-apps/api";
|
||||
// utils
|
||||
import TGSqlite from "../plugins/Sqlite";
|
||||
|
||||
/**
|
||||
* @description 时间戳转换为日期
|
||||
* @since Alpha v0.1.4
|
||||
* @param {number} timestamp - 时间戳
|
||||
* @returns {string} 日期 2021-01-01 00:00:00
|
||||
*/
|
||||
export function timestampToDate(timestamp: number): string {
|
||||
return new Date(timestamp * 1000).toLocaleString("zh", {
|
||||
year: "numeric",
|
||||
month: "2-digit",
|
||||
day: "2-digit",
|
||||
hour: "2-digit",
|
||||
minute: "2-digit",
|
||||
second: "2-digit",
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 根据 completed 跟 progress 获取 status
|
||||
* @since Alpha v0.1.4
|
||||
@@ -53,13 +36,9 @@ export function getUiafStatus(completed: boolean, progress: number): number {
|
||||
*/
|
||||
export async function getUiafHeader(): Promise<TGApp.Plugins.UIAF.Export> {
|
||||
return {
|
||||
// eslint-disable-next-line camelcase
|
||||
export_app: "Tauri.Genshin",
|
||||
// eslint-disable-next-line camelcase
|
||||
export_timestamp: Math.floor(Date.now() / 1000),
|
||||
// eslint-disable-next-line camelcase
|
||||
export_app_version: await app.getVersion(),
|
||||
// eslint-disable-next-line camelcase
|
||||
uiaf_version: "v1.1",
|
||||
};
|
||||
}
|
||||
|
||||
92
src/utils/UIGF.ts
Normal file
92
src/utils/UIGF.ts
Normal file
@@ -0,0 +1,92 @@
|
||||
/**
|
||||
* @file utils UIGF.ts
|
||||
* @description UIGF工具类
|
||||
* @author BTMuli <bt-muli@outlook.com>
|
||||
* @since Alpha v0.2.3
|
||||
*/
|
||||
|
||||
// tauri
|
||||
import { app, fs, path } from "@tauri-apps/api";
|
||||
// utils
|
||||
import TGSqlite from "../plugins/Sqlite";
|
||||
import { timestampToDate } from "./t2D";
|
||||
|
||||
/**
|
||||
* @description 获取 UIGF 头部信息
|
||||
* @since Alpha v0.2.3
|
||||
* @param {string} uid - UID
|
||||
* @returns {Promise<TGApp.Plugins.UIGF.Export>}
|
||||
*/
|
||||
export async function getUigfHeader(uid: string): Promise<TGApp.Plugins.UIGF.Export> {
|
||||
const stamp = Date.now();
|
||||
return {
|
||||
uid,
|
||||
lang: "zh-cn",
|
||||
uigf_version: "2.3.0",
|
||||
export_timestamp: Math.floor(stamp / 1000),
|
||||
export_time: timestampToDate(stamp),
|
||||
export_app: "Tauri.Genshin",
|
||||
export_app_version: await app.getVersion(),
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 检测是否存在 UIGF 数据
|
||||
* @description 粗略检测,不保证数据完整性
|
||||
* @since Alpha v0.2.3
|
||||
* @param {string} path - UIGF 数据路径
|
||||
* @returns {Promise<boolean>} 是否存在 UIGF 数据
|
||||
*/
|
||||
export async function verifyUigfData(path: string): Promise<boolean> {
|
||||
const fileData: string = await fs.readTextFile(path);
|
||||
const UigfData: TGApp.Plugins.UIGF.Export = JSON.parse(fileData).info;
|
||||
return UigfData.uigf_version !== undefined;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 读取 UIGF 数据
|
||||
* @since Alpha v0.2.3
|
||||
* @param {string} userPath - UIGF 数据路径
|
||||
* @returns {Promise<string|false>} UIGF 数据
|
||||
*/
|
||||
export async function readUigfData(userPath: string): Promise<string | false> {
|
||||
if (await fs.exists(userPath)) {
|
||||
const fileData = await fs.readTextFile(userPath);
|
||||
if (fileData !== undefined && fileData !== null && fileData !== "" && fileData !== "{}") {
|
||||
return fileData;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 备份 UIGF 数据
|
||||
* @since Alpha v0.2.3
|
||||
* @param {TGApp.Plugins.UIAF.GachaItem[]} gachaList - 祈愿列表
|
||||
* @param {string} uid - UID
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
export async function backupUigfData(
|
||||
gachaList: TGApp.Plugins.UIGF.GachaItem[],
|
||||
uid: string,
|
||||
): Promise<void> {
|
||||
const savePath = `${await path.appLocalDataDir()}\\userData\\UIGF_${uid}.json`;
|
||||
await fs.writeTextFile(savePath, JSON.stringify(gachaList, null, 2));
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 恢复 UIGF 数据
|
||||
* @since Alpha v0.2.3
|
||||
* @param {string} uid - UID
|
||||
* @returns {Promise<boolean>} UIGF 数据
|
||||
*/
|
||||
export async function restoreUigfData(uid: string): Promise<boolean> {
|
||||
const uigfPath = `${await path.appLocalDataDir()}\\userData\\UIGF_${uid}.json`;
|
||||
const uigfData = await readUigfData(uigfPath);
|
||||
if (uigfData === false) return false;
|
||||
await TGSqlite.mergeUIGF(uid, uigfData);
|
||||
return true;
|
||||
}
|
||||
23
src/utils/t2D.ts
Normal file
23
src/utils/t2D.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
/**
|
||||
* @file utils t2D.ts
|
||||
* @description time to date 时间戳转换为日期工具类
|
||||
* @author BTMuli<bt-muli@outlook.com>
|
||||
* @since Alpha v0.2.3
|
||||
*/
|
||||
|
||||
/**
|
||||
* @description 时间戳转换为日期
|
||||
* @since Alpha v0.2.3
|
||||
* @param {number} timestamp - 时间戳(毫秒)
|
||||
* @returns {string} 日期 2021-01-01 00:00:00
|
||||
*/
|
||||
export function timestampToDate(timestamp: number): string {
|
||||
return new Date(timestamp).toLocaleString("zh", {
|
||||
year: "numeric",
|
||||
month: "2-digit",
|
||||
day: "2-digit",
|
||||
hour: "2-digit",
|
||||
minute: "2-digit",
|
||||
second: "2-digit",
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user