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

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

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