Skip to content

Commit 6db53bc

Browse files
authored
refactor: use compatible systemInfo util API (#2995)
1 parent d4533e4 commit 6db53bc

10 files changed

Lines changed: 51 additions & 68 deletions

File tree

src/packages/avatarcropper/avatarcropper.taro.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import React, {
88
import Taro, { useReady, createSelectorQuery } from '@tarojs/taro'
99
import classNames from 'classnames'
1010
import { Canvas } from '@tarojs/components'
11+
import { getWindowInfo } from '@/utils/get-system-info'
1112
import { Button } from '@/packages/button/button.taro'
1213
import { useConfig } from '@/packages/configprovider/configprovider.taro'
1314

@@ -118,7 +119,7 @@ export const AvatarCropper: FunctionComponent<Partial<AvatarCropperProps>> = (
118119
const [moving, setMoving] = useState(false)
119120
const [zooming, setZooming] = useState(false)
120121

121-
const systemInfo: Taro.getSystemInfoSync.Result = Taro.getSystemInfoSync()
122+
const systemInfo = getWindowInfo()
122123
// 支付宝基础库2.7.0以上支持,需要开启支付宝小程序canvas2d
123124
const showAlipayCanvas2D = useMemo(() => {
124125
return (

src/packages/calendar/__tests__/__snapshots__/calendar.spec.tsx.snap

Lines changed: 2 additions & 2 deletions
Large diffs are not rendered by default.

src/packages/drag/demos/taro/demo4.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import React from 'react'
22
import { Drag, Button } from '@nutui/nutui-react-taro'
3-
import { getSystemInfoSync } from '@tarojs/taro'
3+
import { getWindowInfo } from '@/utils/get-system-info'
44

55
const Demo4 = () => {
6-
const { screenWidth, windowHeight, screenHeight } = getSystemInfoSync()
6+
const { screenWidth, windowHeight, screenHeight } = getWindowInfo()
77

88
console.log(windowHeight, screenHeight)
99

src/packages/drag/drag.taro.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import React, { FunctionComponent, useState, useEffect, useRef } from 'react'
2-
import { getSystemInfoSync } from '@tarojs/taro'
32
import { BasicComponent, ComponentDefaults } from '@/utils/typings'
43
import { getRectByTaro } from '@/utils/get-rect-by-taro'
54
import { DragState } from './drag'
5+
import { getWindowInfo } from '@/utils/get-system-info'
66

77
export interface DragProps extends BasicComponent {
88
attract: boolean
@@ -64,7 +64,7 @@ export const Drag: FunctionComponent<
6464
const el = myDrag.current
6565
if (el) {
6666
const { top, left, bottom, right } = boundary
67-
const { screenWidth, windowHeight } = getSystemInfoSync()
67+
const { screenWidth, windowHeight } = getWindowInfo()
6868

6969
const {
7070
width,

src/packages/menuitem/menuitem.taro.tsx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,11 @@ import React, {
99
useRef,
1010
} from 'react'
1111
import classNames from 'classnames'
12-
import { getSystemInfoSync, usePageScroll } from '@tarojs/taro'
12+
import { usePageScroll } from '@tarojs/taro'
1313
import { View } from '@tarojs/components'
1414
import { CSSTransition } from 'react-transition-group'
1515
import { Check } from '@nutui/icons-react-taro'
16+
import { getWindowInfo } from '@/utils/get-system-info'
1617
import { Overlay } from '@/packages/overlay/overlay.taro'
1718
import { getRectByTaro } from '@/utils/get-rect-by-taro'
1819
import { BasicComponent, ComponentDefaults } from '@/utils/typings'
@@ -106,7 +107,7 @@ export const MenuItem = forwardRef((props: Partial<MenuItemProps>, ref) => {
106107
getParentOffset()
107108
}, [showPopup, getParentOffset])
108109

109-
const windowHeight = useMemo(() => getSystemInfoSync().windowHeight, [])
110+
const windowHeight = useMemo(() => getWindowInfo().windowHeight, [])
110111
const updateItemOffset = useCallback(() => {
111112
if (!parent.lockScroll) return
112113
const p = parent.menuRef.current
@@ -163,7 +164,7 @@ export const MenuItem = forwardRef((props: Partial<MenuItemProps>, ref) => {
163164
height: 'initial',
164165
}
165166
: {
166-
bottom: `${getSystemInfoSync().windowHeight - position.top}px`,
167+
bottom: `${getWindowInfo().windowHeight - position.top}px`,
167168
top: '0',
168169
height: 'initial',
169170
}
@@ -177,7 +178,7 @@ export const MenuItem = forwardRef((props: Partial<MenuItemProps>, ref) => {
177178
}
178179
}
179180
return {
180-
height: `${getSystemInfoSync().windowHeight - position.top}px`,
181+
height: `${getWindowInfo().windowHeight - position.top}px`,
181182
top: 'auto',
182183
...isShow(),
183184
}

src/packages/pulltorefresh/pulltorefresh.taro.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { rubberbandIfOutOfBounds } from '@/utils/rubberband'
99
import { sleep } from '@/utils/sleep'
1010
import { BasicComponent, ComponentDefaults } from '@/utils/typings'
1111
import { PullToRefreshType } from './types'
12+
import { getDeviceInfo } from '@/utils/get-system-info'
1213

1314
export type PullStatus = 'pulling' | 'canRelease' | 'refreshing' | 'complete'
1415

@@ -153,7 +154,7 @@ export const PullToRefresh: FunctionComponent<Partial<PullToRefreshProps>> = (
153154
}
154155
// 安卓微信小程序onTouchMove回调次数少导致下拉卡顿,增加动效会更顺畅
155156
const isAndroidWeApp =
156-
Taro.getSystemInfoSync().platform === 'android' && Taro.getEnv() === 'WEAPP'
157+
getDeviceInfo().platform === 'android' && Taro.getEnv() === 'WEAPP'
157158
const springStyles = {
158159
height: `${height}px`,
159160
...(!pullingRef.current || isAndroidWeApp

src/packages/range/__test__/__snapshots__/range.spec.tsx.snap

Lines changed: 0 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -41,55 +41,8 @@ exports[`range props test 1`] = `
4141
</div>
4242
`;
4343

44-
exports[`range props test 2`] = `
45-
<div>
46-
<div
47-
class="nut-range-container test-range"
48-
>
49-
<div
50-
class="min"
51-
>
52-
0
53-
</div>
54-
<div
55-
class="nut-range"
56-
>
57-
<div
58-
class="nut-range-bar"
59-
style="width: 40%; left: 0%; transition: none;"
60-
>
61-
<div
62-
class="nut-range-button-wrapper"
63-
>
64-
<div
65-
class="nut-range-button"
66-
>
67-
<div
68-
class="number"
69-
>
70-
40
71-
</div>
72-
</div>
73-
</div>
74-
</div>
75-
</div>
76-
<div
77-
class="max"
78-
>
79-
100
80-
</div>
81-
</div>
82-
</div>
83-
`;
84-
8544
exports[`range test 1`] = `
8645
"<div><div class="nut-range-container"><div class="min">0</div><div class="nut-range"><div class="nut-range-bar" style="width: 30%; left: 30%; transition: none;"><div class="nut-range-button-wrapper-left
8746
"><div class="nut-range-button"><div class="number">30</div></div></div><div class="
8847
nut-range-button-wrapper-right"><div class="nut-range-button"><div class="number">60</div></div></div></div></div><div class="max">100</div></div></div>"
8948
`;
90-
91-
exports[`range test 2`] = `
92-
"<div><div class="nut-range-container"><div class="min">0</div><div class="nut-range"><div class="nut-range-bar" style="width: 30%; left: 30%; transition: none;"><div class="nut-range-button-wrapper-left
93-
"><div class="nut-range-button"><div class="number">30</div></div></div><div class="
94-
nut-range-button-wrapper-right"><div class="nut-range-button"><div class="number">60</div></div></div></div></div><div class="max">100</div></div></div>"
95-
`;

src/packages/sticky/sticky.taro.tsx

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,9 @@ import React, {
88
useState,
99
} from 'react'
1010
import classNames from 'classnames'
11-
import {
12-
getEnv,
13-
getSystemInfoSync,
14-
PageScrollObject,
15-
usePageScroll,
16-
} from '@tarojs/taro'
11+
import { getEnv, PageScrollObject, usePageScroll } from '@tarojs/taro'
1712
import { BasicComponent, ComponentDefaults } from '@/utils/typings'
13+
import { getWindowInfo } from '@/utils/get-system-info'
1814
import useWatch from '@/utils/use-watch'
1915
import { getRectByTaro } from '@/utils/get-rect-by-taro'
2016
import { getScrollParent } from '@/utils/get-scroll-parent'
@@ -128,7 +124,7 @@ export const Sticky: FunctionComponent<Partial<StickyProps>> = (props) => {
128124
setFixed(threshold > curRootRect.top)
129125
}
130126
} else {
131-
const windowHeight = getSystemInfoSync().windowHeight
127+
const windowHeight = getWindowInfo().windowHeight
132128
setFixed(windowHeight - threshold < curRootRect.bottom)
133129
}
134130
}

src/packages/virtuallist/virtuallist.taro.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ import React, {
88
useState,
99
} from 'react'
1010
import { ScrollView, View } from '@tarojs/components'
11-
import { getSystemInfoSync } from '@tarojs/taro'
1211
import classNames from 'classnames'
1312
import { Data, PositionType } from './types'
1413
import { initPositinoCache, updateItemSize } from './utils'
14+
import { getWindowInfo } from '@/utils/get-system-info'
1515
import { BasicComponent, ComponentDefaults } from '@/utils/typings'
1616

1717
export interface VirtualListProps extends BasicComponent {
@@ -56,7 +56,7 @@ export const VirtualList: FunctionComponent<Partial<VirtualListProps>> = (
5656
}
5757

5858
const clientHeight = useMemo(
59-
() => getSystemInfoSync().windowHeight - 5 || 667,
59+
() => getWindowInfo().windowHeight - 5 || 667,
6060
[]
6161
)
6262

src/utils/get-system-info.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import Taro from '@tarojs/taro'
2+
3+
interface IDeviceInfo
4+
extends Omit<Taro.getDeviceInfo.Result, 'deviceAbi' | 'CPUType'> {}
5+
/**
6+
* 获取设备基础信息,兼容新旧 API
7+
* @returns {IDeviceInfo} 设备基础信息
8+
*/
9+
export const getDeviceInfo = (): IDeviceInfo => {
10+
return Taro.canIUse('getDeviceInfo')
11+
? Taro.getDeviceInfo()
12+
: Taro.getSystemInfoSync()
13+
}
14+
/**
15+
* 获取窗口信息,兼容新旧 API
16+
* @returns {Taro.getWindowInfo.Result} 窗口信息
17+
*/
18+
export const getWindowInfo = (): Taro.getWindowInfo.Result => {
19+
return Taro.canIUse('getWindowInfo')
20+
? Taro.getWindowInfo()
21+
: Taro.getSystemInfoSync()
22+
}
23+
/**
24+
* 获取应用基础信息,兼容新旧 API
25+
* @returns {Taro.getAppBaseInfo.Result} 应用基础信息
26+
*/
27+
export const getAppBaseInfo = (): Taro.getAppBaseInfo.Result => {
28+
return Taro.canIUse('getAppBaseInfo')
29+
? Taro.getAppBaseInfo()
30+
: Taro.getSystemInfoSync()
31+
}

0 commit comments

Comments
 (0)