From e11a227feb8f08a2e923e0a822f004fc976fd5b1 Mon Sep 17 00:00:00 2001 From: DCloud_LXH <283700113@qq.com> Date: Thu, 26 May 2022 21:16:20 +0800 Subject: [PATCH] fix: systemInfo ssr --- packages/uni-h5/dist/uni-h5.cjs.js | 200 +----------------- .../service/api/device/getSystemInfoSync.ts | 10 +- 2 files changed, 11 insertions(+), 199 deletions(-) diff --git a/packages/uni-h5/dist/uni-h5.cjs.js b/packages/uni-h5/dist/uni-h5.cjs.js index bbd5ad6f7..f827275ff 100644 --- a/packages/uni-h5/dist/uni-h5.cjs.js +++ b/packages/uni-h5/dist/uni-h5.cjs.js @@ -905,13 +905,6 @@ function getRealPath(filePath) { } return filePath; } -const ua = navigator.userAgent; -const isAndroid = /* @__PURE__ */ /android/i.test(ua); -const isIOS = /* @__PURE__ */ /iphone|ipad|ipod/i.test(ua); -const isWindows = /* @__PURE__ */ ua.match(/Windows NT ([\d|\d.\d]*)/i); -const isMac = /* @__PURE__ */ /Macintosh|Mac/i.test(ua); -const isLinux = /* @__PURE__ */ /Linux|X11/i.test(ua); -const isIPadOS = isMac && navigator.maxTouchPoints > 0; const HTTP_METHODS = [ "GET", "OPTIONS", @@ -10028,198 +10021,11 @@ const getStorageInfoSync = /* @__PURE__ */ defineSyncApi("getStorageInfoSync", ( const getStorageInfo = /* @__PURE__ */ defineAsyncApi("getStorageInfo", (_, { resolve }) => { resolve(getStorageInfoSync()); }); -function IEVersion() { - const userAgent = navigator.userAgent; - const isIE = userAgent.indexOf("compatible") > -1 && userAgent.indexOf("MSIE") > -1; - const isEdge = userAgent.indexOf("Edge") > -1 && !isIE; - const isIE11 = userAgent.indexOf("Trident") > -1 && userAgent.indexOf("rv:11.0") > -1; - if (isIE) { - const reIE = new RegExp("MSIE (\\d+\\.\\d+);"); - reIE.test(userAgent); - const fIEVersion = parseFloat(RegExp.$1); - if (fIEVersion > 6) { - return fIEVersion; - } else { - return 6; - } - } else if (isEdge) { - return -1; - } else if (isIE11) { - return 11; - } else { - return -1; - } -} -function getDeviceBrand(model) { - if (/iphone/gi.test(model) || /ipad/gi.test(model) || /mac/gi.test(model)) { - return "apple"; - } - if (/windows/gi.test(model)) { - return "microsoft"; - } -} -function getBrowserInfo() { - let osname; - let osversion = "0"; - let model = ""; - let deviceType = "phone"; - const language = navigator.language; - if (isIOS) { - osname = "iOS"; - const osversionFind = ua.match(/OS\s([\w_]+)\slike/); - if (osversionFind) { - osversion = osversionFind[1].replace(/_/g, "."); - } - const modelFind = ua.match(/\(([a-zA-Z]+);/); - if (modelFind) { - model = modelFind[1]; - } - } else if (isAndroid) { - osname = "Android"; - const osversionFind = ua.match(/Android[\s/]([\w\.]+)[;\s]/); - if (osversionFind) { - osversion = osversionFind[1]; - } - const infoFind = ua.match(/\((.+?)\)/); - const infos = infoFind ? infoFind[1].split(";") : ua.split(" "); - const otherInfo = [ - /\bAndroid\b/i, - /\bLinux\b/i, - /\bU\b/i, - /^\s?[a-z][a-z]$/i, - /^\s?[a-z][a-z]-[a-z][a-z]$/i, - /\bwv\b/i, - /\/[\d\.,]+$/, - /^\s?[\d\.,]+$/, - /\bBrowser\b/i, - /\bMobile\b/i - ]; - for (let i = 0; i < infos.length; i++) { - const info = infos[i]; - if (info.indexOf("Build") > 0) { - model = info.split("Build")[0].trim(); - break; - } - let other; - for (let o = 0; o < otherInfo.length; o++) { - if (otherInfo[o].test(info)) { - other = true; - break; - } - } - if (!other) { - model = info.trim(); - break; - } - } - } else if (isIPadOS) { - model = "iPad"; - osname = "iOS"; - deviceType = "pad"; - osversion = typeof window.BigInt === "function" ? "14.0" : "13.0"; - } else if (isWindows || isMac || isLinux) { - model = "PC"; - osname = "PC"; - deviceType = "pc"; - osversion = "0"; - let osversionFind = ua.match(/\((.+?)\)/)[1]; - if (isWindows) { - osname = "Windows"; - switch (isWindows[1]) { - case "5.1": - osversion = "XP"; - break; - case "6.0": - osversion = "Vista"; - break; - case "6.1": - osversion = "7"; - break; - case "6.2": - osversion = "8"; - break; - case "6.3": - osversion = "8.1"; - break; - case "10.0": - osversion = "10"; - break; - } - const framework = osversionFind && osversionFind.match(/[Win|WOW]([\d]+)/); - if (framework) { - osversion += ` x${framework[1]}`; - } - } else if (isMac) { - osname = "Mac"; - const _osversion = osversionFind && osversionFind.match(/Mac OS X (.+)/) || ""; - if (osversion) { - osversion = _osversion[1].replace(/_/g, "."); - if (osversion.indexOf(";") !== -1) { - osversion = osversion.split(";")[0]; - } - } - } else if (isLinux) { - osname = "Linux"; - const _osversion = osversionFind && osversionFind.match(/Linux (.*)/) || ""; - if (_osversion) { - osversion = _osversion[1]; - if (osversion.indexOf(";") !== -1) { - osversion = osversion.split(";")[0]; - } - } - } - } else { - osname = "Other"; - osversion = "0"; - deviceType = "other"; - } - const system = `${osname} ${osversion}`; - const platform = osname.toLocaleLowerCase(); - let browserName = ""; - let browseVersion = String(IEVersion()); - if (browseVersion !== "-1") { - browserName = "IE"; - } else { - const browseVendors = ["Version", "Firefox", "Chrome", "Edge{0,1}"]; - const vendors = ["Safari", "Firefox", "Chrome", "Edge"]; - for (let index2 = 0; index2 < browseVendors.length; index2++) { - const vendor = browseVendors[index2]; - const reg = new RegExp(`(${vendor})/(\\S*)\\b`); - if (reg.test(ua)) { - browserName = vendors[index2]; - browseVersion = ua.match(reg)[2]; - } - } - } - let deviceBrand = ""; - if (model) { - const _model = model.toLocaleLowerCase(); - deviceBrand = getDeviceBrand(_model) || getDeviceBrand(osname.toLocaleLowerCase()) || _model.split(" ")[0]; - } - let deviceOrientation = "portrait"; - const orientation = typeof window.screen.orientation === "undefined" ? window.orientation : window.screen.orientation.angle; - deviceOrientation = Math.abs(orientation) === 90 ? "landscape" : "portrait"; - return { - deviceBrand, - deviceModel: model, - deviceOrientation, - brand: deviceBrand, - model, - system, - platform, - browserName: browserName.toLocaleLowerCase(), - browseVersion, - language, - deviceType, - ua, - osname, - osversion, - theme: "" - }; -} let browserInfo; function initBrowserInfo() { - browserInfo = getBrowserInfo(); + { + return browserInfo = {}; + } } const getDeviceInfo = /* @__PURE__ */ defineSyncApi("getDeviceInfo", () => { initBrowserInfo(); diff --git a/packages/uni-h5/src/service/api/device/getSystemInfoSync.ts b/packages/uni-h5/src/service/api/device/getSystemInfoSync.ts index 46ae96554..08dc3ff2c 100644 --- a/packages/uni-h5/src/service/api/device/getSystemInfoSync.ts +++ b/packages/uni-h5/src/service/api/device/getSystemInfoSync.ts @@ -4,10 +4,16 @@ import { getBrowserInfo } from '../base/getBrowserInfo' import { getWindowInfo } from './getWindowInfo' import { extend } from '@vue/shared' -let browserInfo: ReturnType +type BrowserInfo = ReturnType + +let browserInfo: BrowserInfo let _initBrowserInfo = true function initBrowserInfo() { + if (__NODE_JS__) { + //TODO 临时搞一下配合 uniCloud 测试 + return (browserInfo = {} as BrowserInfo) + } if (!_initBrowserInfo) return browserInfo = getBrowserInfo() } @@ -67,7 +73,7 @@ export const getSystemInfoSync = defineSyncApi( return { deviceId: Date.now() + '' + Math.floor(Math.random() * 1e7), platform: 'nodejs', - } as unknown as UniApp.GetSystemInfoResult + } as UniApp.GetSystemInfoResult } _initBrowserInfo = true -- GitLab