From 61495b5e90e971421cb6918944fbc22c5a28b084 Mon Sep 17 00:00:00 2001 From: ZF sun <34314687@qq.com> Date: Fri, 17 Jul 2026 10:43:49 +0800 Subject: [PATCH] =?UTF-8?q?refactor(utils&form):=20=E4=BC=98=E5=8C=96?= =?UTF-8?q?=E8=AE=BE=E5=A4=87=E4=BF=A1=E6=81=AF=E8=8E=B7=E5=8F=96=E4=B8=8E?= =?UTF-8?q?=E8=A1=A8=E5=8D=95=E7=BB=84=E4=BB=B6=E6=A0=B7=E5=BC=8F=E9=85=8D?= =?UTF-8?q?=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. 为设备信息相关方法添加可选链操作符,避免空值报错 2. 重构getDeviceInfo和getWindowInfo的异常处理逻辑,简化代码 3. 重构ns-form表单组件,新增全局样式配置支持,兼容多端样式自定义 4. 优化表单布局样式、必填项和选中态颜色,修复鸿蒙元服务TextArea适配问题 5. 新增标签宽度自动计算功能,优化表单排版对齐效果 --- common/js/util.js | 33 ++- components/ns-form/ns-form.vue | 480 ++++++++++++++++++++++++++++----- 2 files changed, 427 insertions(+), 86 deletions(-) diff --git a/common/js/util.js b/common/js/util.js index 3279c14..0875518 100644 --- a/common/js/util.js +++ b/common/js/util.js @@ -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) { diff --git a/components/ns-form/ns-form.vue b/components/ns-form/ns-form.vue index 2a3840d..ca053f6 100644 --- a/components/ns-form/ns-form.vue +++ b/components/ns-form/ns-form.vue @@ -1,36 +1,59 @@