25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 

81 satır
2.3 KiB

  1. import md5 from './md5.js'
  2. /** 生成 uuid */
  3. export function guid() {
  4. return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => {
  5. const r = (Math.random() * 16) | 0
  6. const v = c === 'x' ? r : (r & 0x3) | 0x8
  7. return v.toString(16)
  8. })
  9. }
  10. /**
  11. * 生成接口签名用的 nonce(沿用旧版约定)
  12. * @returns {{ timeStr: number, nonce: string }}
  13. */
  14. export function getNonce() {
  15. const timeStr = Date.now()
  16. const str = md5(timeStr.toString())
  17. const nonce = str.substring(4, 14)
  18. return { timeStr, nonce }
  19. }
  20. /** 获取版本信息(沿用旧项目:默认 0.0.0 兜底,避免后端“version必填”报错) */
  21. export function getVersion() {
  22. let version = '0.0.0'
  23. let version_code = 0
  24. // #ifdef APP-PLUS
  25. version = plus.runtime.version
  26. version_code = plus.runtime.versionCode
  27. // #endif
  28. // #ifdef MP-WEIXIN
  29. version = uni.getAccountInfoSync().miniProgram.version || '0.0.0'
  30. // #endif
  31. return { version, version_code }
  32. }
  33. /** 判断当前栈顶是否为登录页,避免重复跳转登录 */
  34. export function isLoginTop() {
  35. const pages = getCurrentPages()
  36. if (!pages.length) return false
  37. const top = pages[pages.length - 1]
  38. return (top.route || '').includes('pages/login/login')
  39. }
  40. /** 获取 UserAgent(登录上报用,沿用旧项目逻辑) */
  41. export function getUA() {
  42. // #ifdef APP-PLUS
  43. const info = uni.getSystemInfoSync()
  44. const ua = plus.navigator.getUserAgent()
  45. return `${ua} ${info.osName}/${info.osVersion} ${info.deviceBrand}/${info.deviceModel} ${info.appVersion}/${info.appVersionCode}`
  46. // #endif
  47. // #ifdef H5
  48. return navigator.userAgent
  49. // #endif
  50. // #ifndef APP-PLUS || H5
  51. const sys = uni.getSystemInfoSync()
  52. return `miniprogram ${sys.osName}/${sys.osVersion} ${sys.deviceBrand || ''}/${sys.deviceModel || ''}`
  53. // #endif
  54. }
  55. /** H5 是否在微信内置浏览器中 */
  56. export function isInWechat() {
  57. // #ifdef H5
  58. return /MicroMessenger/i.test(navigator.userAgent)
  59. // #endif
  60. // eslint-disable-next-line no-unreachable
  61. return false
  62. }
  63. /** 客户端类型:1.pc 2.h5 3.ios 4.android 5.微信小程序 */
  64. export function getClient() {
  65. let client = 2
  66. // #ifdef APP-PLUS
  67. client = uni.getSystemInfoSync().platform === 'ios' ? 3 : 4
  68. // #endif
  69. // #ifdef MP-WEIXIN
  70. client = 5
  71. // #endif
  72. return client
  73. }