一、前提:
js本身是不能通過瀏覽器獲取設備唯一標識的,但是可以獲取其它的信息作為唯一標識,給用戶一個 visitorId,並且根據這個 visitorId 記錄一些用戶的習慣。總之就是根據瀏覽器的一些特性生成的唯一標識。
二、業務場景:
例如收藏或者關注功能,前端實現。
三、解決方案:FingerprintJS
FingerprintJS 是一個瀏覽器指紋庫,它查詢瀏覽器屬性(canvas、webgl、UserAgent、AudioContext等)並從中計算 hash 的訪問者標識符。因為指紋哈希依賴於所有瀏覽器屬性的精確匹配,使得它們在 > 2周的時間間隔內不穩定,所以自己要做下永久存儲 localStorage,只有第一次存下用戶Id就可以了。
1.github:https://github.com/fingerprintjs/fingerprintjs
2.使用方法:
npm i @fingerprintjs/fingerprintjs;
import FingerprintJS from '@fingerprintjs/fingerprintjs';
// Initialize an agent at application startup.
const fpPromise = FingerprintJS.load();
(async () => { // Get the visitor identifier when you need it. const fp = await fpPromise const result = await fp.get() // This is the visitor identifier: const visitorId = result.visitorId console.log(visitorId) })()