import { AuthenticationClientOptions } from './types'; import { HttpClient } from '../common/HttpClient'; import { createFingerprint } from '../utils'; import { handleFingerprint, BrowserInfoFun, getBrowserInfo } from '../browserInfo'; import { BaseAuthenticationClient } from './BaseAuthenticationClient'; import { MyDevicesList } from './types'; export class BrowserFingerprintClient { options: AuthenticationClientOptions; httpClient: HttpClient; baseClient: BaseAuthenticationClient; constructor(options: AuthenticationClientOptions, httpClient: HttpClient) { this.baseClient = new BaseAuthenticationClient(options); this.options = options; this.httpClient = httpClient; } /** * *获取浏览器指纹信息 * @returns 返回浏览器的各种指纹信息 * @memberof BrowserFingerprintClient */ async getBrowserFingers() { let localBrowserId = localStorage.getItem('browserId'); const localDeviceFingers = localStorage.getItem('deviceFingers'); let localDeviceFingersObj = localDeviceFingers && JSON.parse(localDeviceFingers); if (!localBrowserId || !localDeviceFingers) { const { browserId, deviceFingers } = await createFingerprint(); localBrowserId = browserId; localDeviceFingersObj = deviceFingers; } return { browserId: localBrowserId, deviceFingers: handleFingerprint(localDeviceFingersObj) }; } /** * 获取设备信息,汇总指纹及Navigator上的信息 * @returns 返回带有设备信息的Promise */ async getDeviceInfo() { const browserFingers = await this.getBrowserFingers(); return { ...browserFingers }; } /** * 上报设备信息 * @returns */ async createDevice() { try { const { browserId, deviceFingers } = await this.getBrowserFingers(); const browserFingers = { browserId, ...deviceFingers } const BrowserInfo = await BrowserInfoFun() await this.httpClient.request({ method: 'POST', url: `${this.baseClient.appHost}/api/v3/create-device`, data: { deviceUniqueId: browserFingers.browserId, name: getBrowserInfo()?.toString(), ...BrowserInfo, ...browserFingers } }); return true; } catch (error) { return false; } } /** * 用户中心 - 列表 * @returns {Promise} */ async myDevicesList(): Promise { const data = await this.httpClient.request({ method: 'GET', url: `${this.baseClient.appHost}/api/v3/mydevices/list`, }); return data } /** * 用户中心 - 移除设备 * @param deviceId * @returns {Promise} */ async myDevicesUnbind(deviceId: string): Promise { try { await this.httpClient.request({ method: 'POST', url: `${this.baseClient.appHost}/api/v3/mydevices/unbind`, data: { deviceId } }); return true; } catch (error) { return false; } } /** * 用户中心 - 登出设备 * @param deviceId * @returns {Promise} */ async myDevicesLogout(deviceId: string): Promise { try { await this.httpClient.request({ method: 'POST', url: `${this.baseClient.appHost}/api/v3/mydevices/revoke-session`, data: { deviceId } }); return true; } catch (error) { return false; } } }