refactor(utils&form): 优化设备信息获取与表单组件样式配置

1.  为设备信息相关方法添加可选链操作符,避免空值报错
2.  重构getDeviceInfo和getWindowInfo的异常处理逻辑,简化代码
3.  重构ns-form表单组件,新增全局样式配置支持,兼容多端样式自定义
4.  优化表单布局样式、必填项和选中态颜色,修复鸿蒙元服务TextArea适配问题
5.  新增标签宽度自动计算功能,优化表单排版对齐效果
This commit is contained in:
2026-07-17 10:43:49 +08:00
parent 4044070867
commit 61495b5e90
2 changed files with 427 additions and 86 deletions

View File

@@ -988,14 +988,14 @@ export default {
*/
isIPhoneX() {
let res = this.getDeviceInfo();
if (res.model.search('iPhone X') != -1) {
if (res?.model?.search('iPhone X') != -1) {
return true;
}
return false;
},
//判断安卓还是iOS
isAndroid() {
let platform = this.getDeviceInfo().platform;
let platform = this.getDeviceInfo()?.platform;
if (platform == 'ios') {
return false;
} else if (platform == 'android') {
@@ -1006,21 +1006,21 @@ export default {
* 获取设备信息(包含降级处理)
*/
getDeviceInfo() {
try {
return uni.getDeviceInfo();
} catch (e) {
return uni.getSystemInfoSync();
let info = {};
try { info = uni.getDeviceInfo(); } catch (e) {
try { info = uni.getSystemInfoSync(); } catch (e) {}
}
return info;
},
/**
* 获取窗口信息(包含降级处理)
*/
getWindowInfo() {
try {
return uni.getWindowInfo();
} catch (e) {
return uni.getSystemInfoSync();
let info;
try {info = uni.getWindowInfo();} catch (e) {
try {info = uni.getSystemInfoSync();} catch (e) {}
}
return info;
},
/**
* 深度拷贝对象
@@ -1118,10 +1118,10 @@ export default {
// auto: host 完全一致才还原
const appHosts = [];
// #ifdef H5
try { if (typeof location !== 'undefined' && location.hostname) appHosts.push(location.hostname); } catch (e) {}
try { if (typeof location !== 'undefined' && location.hostname) appHosts.push(location.hostname); } catch (e) { }
// #endif
try { if (Config.h5Domain) appHosts.push(Config.h5Domain); } catch (e) {}
try { if (Config.baseUrl) appHosts.push(Config.baseUrl); } catch (e) {}
try { if (Config.h5Domain) appHosts.push(Config.h5Domain); } catch (e) { }
try { if (Config.baseUrl) appHosts.push(Config.baseUrl); } catch (e) { }
internalPath = _extractInternalAppPath(link.wap_url, appHosts);
}
if (internalPath) {
@@ -1152,7 +1152,7 @@ export default {
// #endif
// #ifdef MP
console.log(`外部链接,跳转到webview`, url);
console.log(`外部链接,跳转到webview`, url);
this.redirectTo(PAGE_KEY_MAP.PAGE_WEBVIEW, {
...link,
src: url
@@ -1312,7 +1312,7 @@ export default {
*/
uniappIsIPhone11() {
let isIphone11 = false;
let systemInfo = this.getDeviceInfo();
let systemInfo = this?.getDeviceInfo();
// #ifdef MP
if (systemInfo.model.search('iPhone 11') != -1) {
isIphone11 = true;
@@ -1323,8 +1323,7 @@ export default {
// #ifdef H5
//判断该浏览器是否为safaria浏览器
isSafari() {
let res = this.getDeviceInfo();
var ua = navigator.userAgent.toLowerCase();
var ua = navigator?.userAgent?.toLowerCase();
if (ua.indexOf('applewebkit') > -1 && ua.indexOf('mobile') > -1 && ua.indexOf('safari') > -1 &&
ua.indexOf('linux') === -1 && ua.indexOf('android') === -1 && ua.indexOf('chrome') === -1 &&
ua.indexOf('ios') === -1 && ua.indexOf('browser') === -1) {