提交 4e956ff2 编写于 作者: 辛宝Otto's avatar 辛宝Otto 🥊

refactor: 全局获取 systemInfoSync 兼容本地测试环境

上级 31bd7c5e
...@@ -37,76 +37,83 @@ ...@@ -37,76 +37,83 @@
type Item = { type Item = {
label : string, label : string,
value : string, value : string,
} }
const globalScreenHeight = uni.getSystemInfoSync().screenHeight
let globalScreenHeight = 0
try {
globalScreenHeight = uni.getSystemInfoSync().screenHeight
} catch (e) {
// 兼容本地测试
console.error(e)
}
export default { export default {
data() { data() {
return { return {
title: 'getSystemInfo', title: 'getSystemInfo',
items: [] as Item[], items: [] as Item[],
screenHeightAtReady: 0, screenHeightAtReady: 0,
jest_result: false, jest_result: false,
} }
}, },
onUnload: function () { onUnload: function () {
}, },
onReady() {
onReady() { this.screenHeightAtReady = uni.getSystemInfoSync().screenHeight
this.screenHeightAtReady = uni.getSystemInfoSync().screenHeight console.log(`全局获取屏幕高度: ${globalScreenHeight} onReady内获取屏幕高度: ${this.screenHeightAtReady}`);
console.log(`全局获取屏幕高度: ${globalScreenHeight} onReady内获取屏幕高度: ${this.screenHeightAtReady}`);
}, },
methods: { methods: {
getSystemInfo: function () { getSystemInfo: function () {
uni.getSystemInfo({ uni.getSystemInfo({
success: (res) => { success: (res) => {
this.items = [] as Item[]; this.items = [] as Item[];
const res_str = JSON.stringify(res); const res_str = JSON.stringify(res);
const res_obj = JSON.parseObject(res_str); const res_obj = JSON.parseObject(res_str);
const res_map = res_obj!.toMap(); const res_map = res_obj!.toMap();
let keys = [] as string[] let keys = [] as string[]
res_map.forEach((_, key) => { res_map.forEach((_, key) => {
keys.push(key); keys.push(key);
}); });
keys.sort().forEach( key => { keys.sort().forEach(key => {
const value = res[key]; const value = res[key];
if(value != null){ if (value != null) {
const item = { const item = {
label: key, label: key,
value: "" + ((typeof value == "object")? JSON.stringify(value) : value) value: "" + ((typeof value == "object") ? JSON.stringify(value) : value)
} as Item; } as Item;
this.items.push(item); this.items.push(item);
} }
}); });
}, },
}) })
}, },
getSystemInfoSync: function () { getSystemInfoSync: function () {
this.items = [] as Item[]; this.items = [] as Item[];
const res = uni.getSystemInfoSync() const res = uni.getSystemInfoSync()
const res_str = JSON.stringify(res); const res_str = JSON.stringify(res);
const res_obj = JSON.parseObject(res_str); const res_obj = JSON.parseObject(res_str);
const res_map = res_obj!.toMap(); const res_map = res_obj!.toMap();
let keys = [] as string[] let keys = [] as string[]
res_map.forEach((_, key) => { res_map.forEach((_, key) => {
keys.push(key); keys.push(key);
}); });
keys.sort().forEach( key => { keys.sort().forEach(key => {
const value = res[key]; const value = res[key];
if(value != null){ if (value != null) {
const item = { const item = {
label: key, label: key,
value: "" + ((typeof value == "object")? JSON.stringify(value) : value) value: "" + ((typeof value == "object") ? JSON.stringify(value) : value)
} as Item; } as Item;
this.items.push(item); this.items.push(item);
} }
}); });
}, },
//自动化测试例专用 //自动化测试例专用
jest_getSystemInfo() : GetSystemInfoResult { jest_getSystemInfo() : GetSystemInfoResult {
return uni.getSystemInfoSync(); return uni.getSystemInfoSync();
}, },
jest_getScreenHeight_at_different_stages(){ jest_getScreenHeight_at_different_stages() {
this.jest_result = (globalScreenHeight == this.screenHeightAtReady) this.jest_result = (globalScreenHeight == this.screenHeightAtReady)
} }
} }
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册