diff --git a/pages/API/get-system-info/get-system-info.uvue b/pages/API/get-system-info/get-system-info.uvue index 022c54d58170858ff4f2312d7775f93532578f67..a064c1ac8314f4c5eeda691172fada70b1ae5a9b 100644 --- a/pages/API/get-system-info/get-system-info.uvue +++ b/pages/API/get-system-info/get-system-info.uvue @@ -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) } } }