// 获取设备唯一标识(优先 IMEI,退化到 uuid) // 仅在 APP-PLUS 生效;其他平台返回 null export function getDeviceImeiOrUuid() { return new Promise((resolve) => { try { // 规范化设备标识:处理逗号/分隔符/重复等,返回首个有效数字串 function normalizeDeviceCode(raw) { try { if (!raw) return null; const text = String(raw).trim(); // 拆分出所有数字片段(过滤空串) const parts = text.split(/[^0-9]+/).filter(Boolean); if (parts.length === 0) return null; // 去重并按长度排序,优先选择更长的数字串 const uniq = []; for (const p of parts) { if (!uniq.includes(p)) uniq.push(p); } uniq.sort((a, b) => b.length - a.length); // 选择第一个长度足够的候选(常见 IMEI 为15位,这里放宽到>=10) const candidate = uniq.find((v) => v.length >= 10) || uniq[0]; return candidate || null; } catch (e) { return raw ? String(raw) : null; } } // H5 平台:从 URL 参数读取 imei(用于调试或外部传入) // 不再依赖任何 debug 模块或本地开关 // #ifdef H5 try { if (typeof window !== 'undefined') { const params = new URLSearchParams(window.location.search || ''); const urlImei = params.get('imei'); if (urlImei) { const normalized = normalizeDeviceCode(urlImei); console.log('Device.js H5 从URL获取IMEI:', urlImei, '=>', normalized); resolve(normalized ? String(normalized) : null); return; } } } catch (e) {} // #endif // #ifdef APP-PLUS // 优先尝试异步获取 IMEI if (typeof plus !== 'undefined' && plus.device && typeof plus.device.getInfo === 'function') { plus.device.getInfo({ success: (info) => { // info 结构按 5+ 文档约定,可能包含 imei/uuid 等 if (info && info.imei) { const normalized = normalizeDeviceCode(info.imei); resolve(normalized ? String(normalized) : null); return; } // 退化到 uuid if (plus.device && plus.device.uuid) { resolve(String(plus.device.uuid)); return; } resolve(null); }, fail: () => { // 失败后退化到同步获取 try { if (plus.device && plus.device.imei) { const normalized = normalizeDeviceCode(plus.device.imei); resolve(normalized ? String(normalized) : null); return; } if (plus.device && plus.device.uuid) { resolve(String(plus.device.uuid)); return; } } catch (e) {} resolve(null); } }); return; } // 没有 getInfo,尝试直接读取 if (plus.device && plus.device.imei) { resolve(String(plus.device.imei)); return; } if (plus.device && plus.device.uuid) { resolve(String(plus.device.uuid)); return; } // #endif } catch (e) {} console.log('Device.js H5环境返回null (非APP-PLUS且非debug模式)'); resolve(null); }); }