Skip to content

Commit 5de627c

Browse files
author
zhaolongfei
committed
feat: update api of system info
1 parent 482e39c commit 5de627c

File tree

3 files changed

+68
-3
lines changed

3 files changed

+68
-3
lines changed

packages/mars-api/api/system/info.js

Lines changed: 63 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,72 @@
55

66
import {callback} from '../utils';
77

8+
const NAVIGATION_BAR_HEIGHT = 38;
9+
810
function getSystemInfoSync() {
11+
const userAgent = window.navigator.userAgent;
12+
const ua = userAgent.toLowerCase();
13+
let isIos = false;
14+
let isAndroid = false;
15+
let system = undefined;
16+
let platform = undefined;
17+
let model = undefined;
18+
const screenHeight = document.documentElement.clientHeight;
19+
const screenWidth = document.documentElement.clientWidth;
20+
const windowWidth = screenWidth;
21+
const windowHeight = screenHeight - NAVIGATION_BAR_HEIGHT;
22+
23+
if (userAgent.indexOf('Mac OS') > -1 || userAgent.indexOf('iPhone OS') > -1) {
24+
isIos = true;
25+
}
26+
else if (userAgent.indexOf('Android') > -1) {
27+
isAndroid = true;
28+
}
29+
30+
if (isIos) {
31+
let match = userAgent.match('iPhone OS ([0-9,_]*) ');
32+
system = match && match[1] && `iOS ${match[1].replace(/_/g, '.')}` || undefined;
33+
platform = 'ios';
34+
35+
if (screenHeight === 736 && screenWidth === 414) {
36+
model = 'iPhone6P, iPhone7P, iPhone8P';
37+
}
38+
else if (screenHeight === 812 && screenWidth === 375) {
39+
model = 'iPhoneX';
40+
}
41+
else if (screenHeight === 667 && screenWidth === 375) {
42+
model = 'iPhone6, iPhone7, iPhone8';
43+
}
44+
else if (screenWidth > 320) {
45+
model = 'iPhone';
46+
}
47+
else if (screenHeight === 568 && screenWidth === 320) {
48+
model = 'iPhone5, iPhoneSE, iPhone5s';
49+
}
50+
else {
51+
model = 'iPhone4s, iPhone4';
52+
}
53+
}
54+
else if (isAndroid) {
55+
let match = userAgent.match('Android ([0-9,.]*)');
56+
system = match && match[0] || undefined;
57+
platform = 'android';
58+
const buildIndex = ua.indexOf('build');
59+
const fullResult = userAgent.substring(0, buildIndex);
60+
const fullArr = fullResult.split(';');
61+
model = fullArr[fullArr.length - 1].trim();
62+
}
963

1064
const info = {
11-
system: navigator.userAgent,
12-
screenWidth: document.body.clientWidth,
13-
windowWidth: document.body.clientWidth
65+
system,
66+
platform,
67+
model,
68+
screenWidth,
69+
screenHeight,
70+
windowWidth,
71+
windowHeight,
72+
pixelRatio: window.devicePixelRatio,
73+
language: navigator.language
1474
};
1575

1676
return info;

packages/mars-api/initApi.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import * as other from './api/other';
1313
import * as open from './api/open';
1414
import * as interactive from './api/interactive';
1515
import * as canvas from './api/canvas';
16+
import * as pageInfo from './api/pageInfo';
1617
import request from './api/request';
1718
import {createAnimation, animationDirective} from './api/createAnimation';
1819

@@ -33,6 +34,7 @@ export default function initNativeApi(mars) {
3334
selector,
3435
socket,
3536
canvas,
37+
pageInfo,
3638
{
3739
request,
3840
createAnimation

packages/mars-api/jest.config.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ module.exports = {
1414
'<rootDir>/__test__/**/*.js'
1515
],
1616
testEnvironment: 'jest-environment-jsdom-global',
17+
testEnvironmentOptions: {
18+
userAgent: 'Mozilla/5.0 (Linux; Android 7.1.1; Nexus 6 Build/N6F26U) AppleWebKit/537.36 (KHTML, like Gecko)'
19+
},
1720
testPathIgnorePatterns: [
1821
'/node_modules/'
1922
],

0 commit comments

Comments
 (0)