device.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /**
  2. * 设备信息工具
  3. */
  4. const DEVICE_ID_KEY = 'marketing_device_id'
  5. /**
  6. * 生成设备ID
  7. */
  8. function generateDeviceId(): string {
  9. return `device_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`
  10. }
  11. /**
  12. * 获取设备ID
  13. */
  14. export function getDeviceId(): string {
  15. let deviceId = uni.getStorageSync(DEVICE_ID_KEY) || ''
  16. if (!deviceId) {
  17. deviceId = generateDeviceId()
  18. uni.setStorageSync(DEVICE_ID_KEY, deviceId)
  19. }
  20. return deviceId
  21. }
  22. /**
  23. * 获取平台类型
  24. */
  25. export function getPlatform(): 'android' | 'ios' | 'h5' | 'mp-weixin' | 'mp-alipay' {
  26. // #ifdef APP-PLUS
  27. // @ts-ignore
  28. const platform = plus.os.name.toLowerCase()
  29. return platform === 'ios' ? 'ios' : 'android'
  30. // #endif
  31. // #ifdef H5
  32. return 'h5'
  33. // #endif
  34. // #ifdef MP-WEIXIN
  35. return 'mp-weixin'
  36. // #endif
  37. // #ifdef MP-ALIPAY
  38. return 'mp-alipay'
  39. // #endif
  40. return 'h5'
  41. }
  42. /**
  43. * 获取APP类型
  44. */
  45. export function getAppType(): string {
  46. const platform = getPlatform()
  47. return platform === 'android' || platform === 'ios' ? 'app' : platform
  48. }
  49. /**
  50. * 获取运行时小程序 appid
  51. */
  52. export function getRuntimeMiniAppId(): string {
  53. // #ifdef MP-WEIXIN
  54. try {
  55. const accountInfo = uni.getAccountInfoSync()
  56. return accountInfo?.miniProgram?.appId || ''
  57. } catch {
  58. return ''
  59. }
  60. // #endif
  61. return ''
  62. }
  63. /**
  64. * 与《移动端接口规范》对齐的请求头:X-Platform、X-Version(上传/普通请求统一带上,避免网关或统计侧拦截)。
  65. */
  66. export function getMobileClientHeaders(): Record<string, string> {
  67. const platform = getPlatform()
  68. let xPlatform = 'H5'
  69. if (platform === 'mp-weixin' || platform === 'mp-alipay') {
  70. xPlatform = 'MINI'
  71. } else if (platform === 'android' || platform === 'ios') {
  72. xPlatform = 'APP'
  73. }
  74. const { version } = getRuntimeClientVersion()
  75. const headers: Record<string, string> = {
  76. 'X-Platform': xPlatform,
  77. 'X-Version': version || '1.0.0'
  78. }
  79. const miniAppId = getRuntimeMiniAppId()
  80. if (miniAppId) {
  81. headers.APPID = miniAppId
  82. }
  83. return headers
  84. }
  85. /**
  86. * 获取系统信息
  87. */
  88. export function getSystemInfo(): UniApp.GetSystemInfoResult {
  89. return uni.getSystemInfoSync()
  90. }
  91. /**
  92. * 获取应用版本
  93. */
  94. export function getAppVersion(): string {
  95. // #ifdef APP-PLUS
  96. // @ts-ignore
  97. return plus.runtime.version
  98. // #endif
  99. // #ifndef APP-PLUS
  100. return ''
  101. // #endif
  102. }
  103. /**
  104. * 小程序 / App 运行时版本号与整型构建号(非 H5 静态写死;与 manifest 发布配置、微信审核版本一致)。
  105. */
  106. export function getRuntimeClientVersion(): { version: string; versionCode: number } {
  107. let version = ''
  108. let versionCode = 0
  109. // #ifdef MP-WEIXIN
  110. try {
  111. const ac = uni.getAccountInfoSync()
  112. const mp = ac?.miniProgram as { version?: string; versionCode?: number | string } | undefined
  113. if (mp?.version) version = String(mp.version).trim()
  114. const raw = mp?.versionCode
  115. if (raw !== undefined && raw !== null && raw !== '') {
  116. const n = typeof raw === 'number' ? raw : parseInt(String(raw), 10)
  117. if (!Number.isNaN(n)) versionCode = n
  118. }
  119. } catch {
  120. /* ignore */
  121. }
  122. // #endif
  123. // #ifdef APP-PLUS
  124. try {
  125. // @ts-ignore
  126. const v = plus.runtime.version
  127. if (v) version = String(v).trim()
  128. // @ts-ignore
  129. const c = plus.runtime.versionCode
  130. if (typeof c === 'number' && !Number.isNaN(c)) versionCode = c
  131. } catch {
  132. /* ignore */
  133. }
  134. // #endif
  135. return { version, versionCode }
  136. }
  137. /**
  138. * 获取网络类型
  139. */
  140. export function getNetworkType(): Promise<string> {
  141. return new Promise((resolve, reject) => {
  142. uni.getNetworkType({
  143. success: (res) => {
  144. resolve(res.networkType)
  145. },
  146. fail: reject
  147. })
  148. })
  149. }