Skip to content

Commit 6e4b837

Browse files
committed
chore: upgrade wx api
1 parent 673dee2 commit 6e4b837

File tree

15 files changed

+96
-74
lines changed

15 files changed

+96
-74
lines changed

script/test/setup.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,27 @@ const menuButtonBoundingClientRect = {
4747
width: 87,
4848
};
4949

50+
const appBaseInfo = {
51+
SDKVersion: '2.19.1',
52+
version: '8.0.5',
53+
};
54+
55+
const windowInfo = {
56+
pixelRatio: 2,
57+
screenWidth: 375,
58+
screenHeight: 667,
59+
windowWidth: 375,
60+
windowHeight: 667,
61+
statusBarHeight: 20,
62+
screenTop: 0,
63+
};
64+
5065
global.wx = {
5166
...similateApi,
5267
getSystemInfo: mockAsyncAndPromise('getSystemInfo', systemInfo),
5368
getSystemInfoSync: mockSync(systemInfo),
5469
getMenuButtonBoundingClientRect: mockSync(menuButtonBoundingClientRect),
70+
getAppBaseInfo: mockSync(appBaseInfo),
71+
getWindowInfo: mockSync(windowInfo),
5572
};
5673
global.VIRTUAL_HOST = canUseVirtualHost();

src/common/utils.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
1+
import exp from 'constants';
12
import { prefix } from './config';
3+
import { getWindowInfo, getAppBaseInfo, getDeviceInfo } from './wechat';
24

3-
export const systemInfo = wx.getSystemInfoSync();
5+
export const systemInfo: WechatMiniprogram.WindowInfo | WechatMiniprogram.SystemInfo = getWindowInfo();
6+
7+
export const appBaseInfo: WechatMiniprogram.AppBaseInfo | WechatMiniprogram.SystemInfo = getAppBaseInfo();
8+
9+
export const deviceInfo: WechatMiniprogram.DeviceInfo | WechatMiniprogram.SystemInfo = getDeviceInfo();
410

511
type Context = WechatMiniprogram.Page.TrivialInstance | WechatMiniprogram.Component.TrivialInstance;
612

src/common/version.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
1+
import { getAppBaseInfo } from './wechat';
2+
13
// 获取系统信息
2-
let systemInfo: WechatMiniprogram.SystemInfo;
4+
let systemInfo: WechatMiniprogram.AppBaseInfo;
35
function getSystemInfo() {
46
if (systemInfo == null) {
5-
systemInfo = wx.getSystemInfoSync();
7+
systemInfo = getAppBaseInfo();
68
}
79
return systemInfo;
810
}
911

1012
// 版本号比较, 参考:https://developers.weixin.qq.com/miniprogram/dev/framework/compatibility.html
11-
function compareVersion(v1, v2) {
13+
export function compareVersion(v1, v2) {
1214
v1 = v1.split('.');
1315
v2 = v2.split('.');
1416
const len = Math.max(v1.length, v2.length);
@@ -40,8 +42,7 @@ function judgeByVersion(version: string) {
4042
}
4143

4244
export function canIUseFormFieldButton() {
43-
const version: string = '2.10.3';
44-
return judgeByVersion(version);
45+
return judgeByVersion('2.10.3');
4546
}
4647

4748
export function canUseVirtualHost() {

src/common/wechat.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,15 @@ export const getObserver = (context, selector: string) => {
88
});
99
});
1010
};
11+
12+
export const getWindowInfo = () => {
13+
return wx.getWindowInfo ? wx.getWindowInfo() : wx.getSystemInfoSync();
14+
};
15+
16+
export const getAppBaseInfo = () => {
17+
return wx.getAppBaseInfo ? wx.getAppBaseInfo() : wx.getSystemInfoSync();
18+
};
19+
20+
export const getDeviceInfo = () => {
21+
return wx.getDeviceInfo ? wx.getDeviceInfo() : wx.getSystemInfoSync();
22+
};

src/fab/draggable/draggable.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
import { SuperComponent, wxComponent } from '../../common/src/index';
22
import config from '../../common/config';
33
import props from './props';
4-
import { getRect } from '../../common/utils';
4+
import { getRect, systemInfo } from '../../common/utils';
55
import type { TdDraggableProps } from './type';
66

7-
const systemInfo = wx.getSystemInfoSync();
87
const { prefix } = config;
98
const name = `${prefix}-draggable`;
109

src/fab/fab.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@ import { SuperComponent, wxComponent } from '../common/src/index';
22
import config from '../common/config';
33
import props from './props';
44
import useCustomNavbar from '../mixins/using-custom-navbar';
5-
import { unitConvert } from '../common/utils';
5+
import { unitConvert, systemInfo } from '../common/utils';
66

7-
const systemInfo = wx.getSystemInfoSync();
87
const { prefix } = config;
98
const name = `${prefix}-fab`;
109
const baseButtonProps = {

src/image-viewer/image-viewer.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { styles, calcIcon } from '../common/utils';
1+
import { styles, calcIcon, systemInfo } from '../common/utils';
22
import { SuperComponent, wxComponent } from '../common/src/index';
33
import config from '../common/config';
44
import props from './props';
@@ -67,7 +67,7 @@ export default class ImageViewer extends SuperComponent {
6767
calcMaskTop() {
6868
if (this.data.usingCustomNavbar) {
6969
const rect = wx?.getMenuButtonBoundingClientRect() || null;
70-
const { statusBarHeight } = wx.getSystemInfoSync();
70+
const { statusBarHeight } = systemInfo;
7171

7272
if (rect && statusBarHeight) {
7373
this.setData({
@@ -77,7 +77,7 @@ export default class ImageViewer extends SuperComponent {
7777
}
7878
},
7979
saveScreenSize() {
80-
const { windowHeight, windowWidth } = wx.getSystemInfoSync();
80+
const { windowHeight, windowWidth } = systemInfo;
8181
this.setData({
8282
windowHeight,
8383
windowWidth,

src/image/__test__/index.test.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,16 @@ import path from 'path';
22
import simulate from 'miniprogram-simulate';
33

44
describe('image', () => {
5+
jest.resetModules();
6+
7+
jest.mock('../../common/version', () => {
8+
const originalModule = jest.requireActual('../../common/version');
9+
return {
10+
...originalModule,
11+
compareVersion: () => -1,
12+
};
13+
});
14+
515
const image = load(path.resolve(__dirname, `../image`), 't-image');
616

717
it(`: style && customStyle`, async () => {
@@ -68,17 +78,12 @@ describe('image', () => {
6878
await simulate.sleep();
6979

7080
expect(handleLoad).toHaveBeenCalled();
71-
7281
// height fixed
73-
const mock = jest.spyOn(wx, 'getSystemInfoSync');
74-
mock.mockImplementation(() => ({ SDKVersion: '2.10.2' }));
7582

7683
comp.setData({ mode: 'heightFix' });
7784
$image.dispatchEvent('load', { detail: { width: 100, height: 100 } });
7885

7986
await simulate.sleep();
8087
expect(comp.toJSON()).toMatchSnapshot();
81-
82-
mock.mockRestore();
8388
});
8489
});

src/image/image.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { SuperComponent, wxComponent } from '../common/src/index';
22
import ImageProps from './props';
33
import config from '../common/config';
4-
import { addUnit, getRect } from '../common/utils';
4+
import { addUnit, getRect, appBaseInfo } from '../common/utils';
5+
import { compareVersion } from '../common/version';
56

67
const { prefix } = config;
78
const name = `${prefix}-image`;
@@ -37,13 +38,9 @@ export default class Image extends SuperComponent {
3738

3839
methods = {
3940
onLoaded(e: any) {
40-
const sdkVersion = wx.getSystemInfoSync().SDKVersion;
41-
const versionArray = sdkVersion.split('.').map((v) => parseInt(v, 10));
41+
const sdkVersion = appBaseInfo.SDKVersion;
4242
const { mode, tId } = this.properties;
43-
const isInCompatible =
44-
versionArray[0] < 2 ||
45-
(versionArray[0] === 2 && versionArray[1] < 10) ||
46-
(versionArray[0] === 2 && versionArray[1] === 10 && versionArray[2] < 3);
43+
const isInCompatible = compareVersion(sdkVersion, '2.10.3') < 0;
4744
// 版本号低于2.10.3时组件内部实现heightFix模式
4845
if (mode === 'heightFix' && isInCompatible) {
4946
// 实现heightFix模式,保持高度和宽高比,设置对应的宽度

src/indexes/indexes.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { RelationsOptions, SuperComponent, wxComponent } from '../common/src/index';
22
import config from '../common/config';
33
import props from './props';
4-
import { getRect, throttle } from '../common/utils';
4+
import { getRect, throttle, systemInfo } from '../common/utils';
55
import pageScrollMixin from '../mixins/page-scroll';
66

77
const { prefix } = config;
@@ -66,7 +66,7 @@ export default class Indexes extends SuperComponent {
6666
methods = {
6767
setHeight(height: string | number) {
6868
if (!height) {
69-
const { windowHeight } = wx.getSystemInfoSync();
69+
const { windowHeight } = systemInfo;
7070
height = windowHeight;
7171
}
7272
this.setData(

0 commit comments

Comments
 (0)