嗨,我是新来的.现在我试图在数据库表中插入购物车数据..。我想在我的手推车里储存itemid,qty,deviceuniqueid ..。为什么这个设备唯一的意思是,在我的webapp中没有登录用户...i想要在购物车页面显示购物车的详细信息。那只是我需要设备.那么如何使设备在角度上是唯一的..。我搜索更多,但我找不到解决方案,...so,请支持我开发webapp。如果有任何其他在本地存储中存储数据的新方法,请帮助我完成this...thank
发布于 2022-06-27 12:53:55
您不可能从浏览器中获得设备唯一的id;但是您可以在启动时自己生成一个id,并将其保存到localStorage中供以后重用!
function generateUUIDV4(): string {
// http://www.ietf.org/rfc/rfc4122.txt
const s = []
const hexDigits = '0123456789abcdef'
for (let i = 0; i < 36; i++) {
s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1)
}
s[14] = '4' // bits 12-15 of the time_hi_and_version field to 0010
// eslint-disable-next-line no-bitwise
s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1) // bits 6-7 of the clock_seq_hi_and_reserved to 01
s[8] = s[13] = s[18] = s[23] = '-'
const uuid = s.join('')
return uuid
}function getDeviceId() {
let deviceId = localStorage.getItem('deviceId')
if(!deviceId) {
deviceId = generateUUIDV4()
localStorage.setItem('deviceId', deviceId)
}
return deviceId
}https://stackoverflow.com/questions/72772289
复制相似问题