如何在Angular中获取每个设备的唯一ID

发布于 2025-02-11 01:43:03 字数 227 浏览 0 评论 0原文

嗨,我是Angular的新手...现在我试图将购物车数据插入数据库表...在我的购物车中,我想存储itemID,QTY,DeviceuniqueID ...为什么此DeviceuniqueId表示,在我的WebApp中没有用于用户的登录...我想在购物车页面中显示购物车详细信息。找到解决方案...因此,请支持我开发WebApp ..如果在LocalStorage中有其他新方法意味着帮助我做到这一点...谢谢

Hi i am new to angular ... now i am trying to insert cart data in database table ... in my cart i want to store itemid,qty,deviceuniqueid ... why this deviceuniqueid means , in my webapp there is no login for user ...i want to show cart details in cart page .. thats only i need deviceuniqueid.... so how to get deviceuniqueid in angular... i search more but i can't find solution ...so please support me to develop webapp .. if any other new way to storedata in localstorage means help me to do this...thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

憧憬巴黎街头的黎明 2025-02-18 01:43:03

您无法从浏览器中获得设备唯一的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
}

There's no way you can get a device unique id from the browser; but you can generate one yourself upon startup and save it into localStorage for later re-use!

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
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文