import md5 from './md5.js' /** 生成 uuid */ export function guid() { return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => { const r = (Math.random() * 16) | 0 const v = c === 'x' ? r : (r & 0x3) | 0x8 return v.toString(16) }) } /** * 生成接口签名用的 nonce(沿用旧版约定) * @returns {{ timeStr: number, nonce: string }} */ export function getNonce() { const timeStr = Date.now() const str = md5(timeStr.toString()) const nonce = str.substring(4, 14) return { timeStr, nonce } } /** 获取版本信息(沿用旧项目:默认 0.0.0 兜底,避免后端“version必填”报错) */ export function getVersion() { let version = '0.0.0' let version_code = 0 // #ifdef APP-PLUS version = plus.runtime.version version_code = plus.runtime.versionCode // #endif // #ifdef MP-WEIXIN version = uni.getAccountInfoSync().miniProgram.version || '0.0.0' // #endif return { version, version_code } } /** 判断当前栈顶是否为登录页,避免重复跳转登录 */ export function isLoginTop() { const pages = getCurrentPages() if (!pages.length) return false const top = pages[pages.length - 1] return (top.route || '').includes('pages/login/login') } /** 获取 UserAgent(登录上报用,沿用旧项目逻辑) */ export function getUA() { // #ifdef APP-PLUS const info = uni.getSystemInfoSync() const ua = plus.navigator.getUserAgent() return `${ua} ${info.osName}/${info.osVersion} ${info.deviceBrand}/${info.deviceModel} ${info.appVersion}/${info.appVersionCode}` // #endif // #ifdef H5 return navigator.userAgent // #endif // #ifndef APP-PLUS || H5 const sys = uni.getSystemInfoSync() return `miniprogram ${sys.osName}/${sys.osVersion} ${sys.deviceBrand || ''}/${sys.deviceModel || ''}` // #endif } /** H5 是否在微信内置浏览器中 */ export function isInWechat() { // #ifdef H5 return /MicroMessenger/i.test(navigator.userAgent) // #endif // eslint-disable-next-line no-unreachable return false } /** 客户端类型:1.pc 2.h5 3.ios 4.android 5.微信小程序 */ export function getClient() { let client = 2 // #ifdef APP-PLUS client = uni.getSystemInfoSync().platform === 'ios' ? 3 : 4 // #endif // #ifdef MP-WEIXIN client = 5 // #endif return client }