实装 getDeviceFp

close #58
This commit is contained in:
BTMuli
2023-11-16 14:19:18 +08:00
parent 13e9440c6f
commit 712a09131e
11 changed files with 274 additions and 46 deletions

View File

@@ -1,14 +1,14 @@
/**
* @file utils/TGClient.ts
* @desc 负责米游社客户端的 callback 处理
* @since Beta v0.3.5
* @since Beta v0.3.6
*/
import { event, invoke, path } from "@tauri-apps/api";
import type { Event } from "@tauri-apps/api/event";
import { WebviewWindow } from "@tauri-apps/api/window";
import { getDeviceID } from "./toolFunc";
import { getDeviceInfo } from "./toolFunc";
import { useUserStore } from "../store/modules/user";
import TGConstant from "../web/constant/TGConstant";
import TGRequest from "../web/request/TGRequest";
@@ -321,16 +321,20 @@ class TGClient {
/**
* @func getHTTPRequestHeaders
* @since Beta v0.3.4
* @since Beta v0.3.6
* @desc 获取米游社客户端的 HTTP 请求头
* @param {string} callback - 回调函数名
* @returns {void} - 无返回值
*/
async getHTTPRequestHeaders(callback: string): Promise<void> {
const localFp = getDeviceInfo("device_fp");
if (localFp === "0000000000000") await TGRequest.Device.getFp();
const data = {
"user-agent": TGConstant.BBS.UA_MOBILE,
"x-rpc-client_type": "5",
"x-rpc-device_id": getDeviceID(),
"x-rpc-device_id": getDeviceInfo("device_id"),
"x-rpc-app_version": TGConstant.BBS.VERSION,
"x-rpc-device_fp": getDeviceInfo("device_fp"),
};
await this.callback(callback, data);
}

View File

@@ -1,7 +1,7 @@
/**
* @file utils toolFunc.ts
* @file utils/toolFunc.ts
* @description 一些工具函数
* @since Beta v0.3.5
* @since Beta v0.3.6
*/
import { os, path } from "@tauri-apps/api";
@@ -40,17 +40,36 @@ export function timestampToDate(timestamp: number): string {
}
/**
* @description 获取 deviceID
* @since Beta v0.3.4
* @returns {string} deviceID
* @description 获取设备信息(初始化时)
* @since Beta v0.3.6
* @returns {TGApp.App.Device.DeviceInfo} 设备信息
*/
export function getDeviceID(): string {
let deviceID = localStorage.getItem("deviceID");
if (deviceID === null) {
deviceID = v4();
localStorage.setItem("deviceID", deviceID);
export function getInitDeviceInfo(): TGApp.App.Device.DeviceInfo {
return {
device_id: v4(),
model: getRandomString(6),
seed_id: v4(),
seed_time: Date.now().toString(),
device_fp: "0000000000000",
};
}
/**
* @description 获取设备信息(登录时)
* @since Beta v0.3.6
* @param {string} key - 设备信息 key
* @returns {string} 设备信息
*/
export function getDeviceInfo(key: "device_id" | "device_fp"): string {
const localDevice = localStorage.getItem("deviceInfo");
let deviceInfo: TGApp.App.Device.DeviceInfo;
if (localDevice === null) {
deviceInfo = getInitDeviceInfo();
localStorage.setItem("deviceInfo", JSON.stringify({ deviceInfo }));
} else {
deviceInfo = JSON.parse(localDevice).deviceInfo;
}
return deviceID;
return deviceInfo[key];
}
/**
@@ -84,3 +103,43 @@ export async function getCacheDir(): Promise<string[] | false> {
}
return false;
}
/**
* @description 获取随机字符串
* @since Beta v0.3.6
* @param {number} length 字符串长度
* @param {string} type
* @returns {string} 随机字符串
*/
export function getRandomString(length: number, type: string = "all"): string {
const char = "abcdefghijklmnopqrstuvwxyz";
const num = "0123456789";
let str = "";
switch (type) {
case "all":
str = char + char.toUpperCase() + num;
break;
case "number":
str = num;
break;
case "lower":
str = char;
break;
case "upper":
str = char.toUpperCase();
break;
case "letter":
str = char + char.toUpperCase();
break;
case "hex":
str = num + "abcdef";
break;
default:
str = char + char.toUpperCase() + num;
}
let res = "";
for (let i = 0; i < length; i++) {
res += str.charAt(Math.floor(Math.random() * str.length));
}
return res;
}