From 7e7112677409afd1c18017cb89c998fd56b7dbd3 Mon Sep 17 00:00:00 2001 From: huangzhenbao <17818024429@163.com> Date: Mon, 9 Mar 2026 15:38:50 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=95=86=E5=93=81=E5=88=86?= =?UTF-8?q?=E7=B1=BB=E6=96=87=E5=AD=97=E6=98=BE=E7=A4=BA=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../admin/docs/UNI_APP_X_PAGE_FIX_GUIDE.md | 41 ++++++++++++++++++- .../admin/product/classification/index.uvue | 3 ++ 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/pages/mall/admin/docs/UNI_APP_X_PAGE_FIX_GUIDE.md b/pages/mall/admin/docs/UNI_APP_X_PAGE_FIX_GUIDE.md index 89a5dc58..9dde7452 100644 --- a/pages/mall/admin/docs/UNI_APP_X_PAGE_FIX_GUIDE.md +++ b/pages/mall/admin/docs/UNI_APP_X_PAGE_FIX_GUIDE.md @@ -2115,4 +2115,43 @@ curl -i -X OPTIONS "http://192.168.1.61:9122/rest/v1/ml_coupon_templates?select= --- -这个指南现在涵盖了 uni-app-x 项目开发中最常见的 31 类问题,为后续开发提供了完整的故障排除和标准化指导。 🚀 +## 🎯 阶段十六: UI/UX与数据库集成深度打磨 (商品分类实战) + +### **背景与目标** + +本阶段以“商品分类 (`classification/index.uvue`)”页面为实战案例,彻底剔除前端 Mock 数据,深度结合真实 PostgreSQL 数据库(Supabase)结构进行前后端联调。同时打磨前端表单与自定义 UI 组件,以确保交互体验达到最佳标准。 + +### **核心技术栈与实战难点** + +#### **1. 前后端数据类型的精确映射** + +不同于简单的 JSON,前端必须严格适配数据库设计的各类约束: + +- **UUID 约束**: 分类的 `id` 及 `parent_id` 必须由普通整数转为字符串(UUID)格式。 +- **Array 数组类型**: 数据库中存储的 `path` 字段为 `text[]` (例如 `{"数码电器","智能手机"}`)。前端接收时需要正确将其反序列化为 UTS/JS 的 `string[]`。 +- **UTSJSONObject 转换红线**: Supabase 在请求/返回时强制声明类型检查,嵌套类型或者未知格式会导致 UTS 强转 `as UTSJSONObject` 失败。我们需要利用 `.replace('{', '[').replace('}', ']')` 与 `JSON.parse` 显式完成 postgres `text[]` 到 JS 数组格式的清洗。 + +#### **2. 分类层级 (Path & Level)的自动计算** + +对于具有树形结构的数据,新建或更新分类时: + +- 新节点的 `level` 动态等于其选定父节点的 `level + 1`,默认为 1。 +- 新节点的 `path` 必须完整继承父节点的 `path` 加当前节点自己的 `name`。 +- **抽屉组件联动 (Drawer Form)**:实现在 `picker` 表单选择上级分类时,前端自动提取对应父分类对象,用于准备组装上述关联属性,大幅降低人工误操作。 + +#### **3. 自定义组件样式交互调优 (UX 打磨)** + +对于模拟原生的 Switch 切换开关等手工 UI 元素 (`.switch-mock`),极易出现细微排版与手势体验不佳的问题。 + +- **失效的纵向居中**: `` 作为 Flex 的子元素使用 `display: flex; align-items: center` 时经常无法触发完美的纵轴对齐。**解决方案**: 为包裹单行文字的 `text` class 指定与父容器等高的 `line-height` (`line-height: 24px;` 对齐父元素 `height: 24px`)。 +- **热区与手势反馈**: 原生浏览器端缺失 pointer 样式会让用户认为该元素不可点击。需要在 `.switch-mock` 顶层增加 `cursor: pointer;`。 + +### **最佳实践总结 (最佳体验法则)** + +1. **统一数据源 (Single Source of Truth)**:一旦存在后台建表规范 (如 `CONSUMER_DB_DOC`),前端 `interface` 层应100%镜像字段名,规避繁琐的字段隐射。 +2. **渐进式数据增强 (Progressive Data Enhancement)**: 表格展示优先处理 `loadData`,并引入 `buildParentOptions` 把平铺的数据(Flat Data)映射到 `tree` 组件或下拉框(Select/Picker)。 +3. **细节决定体验**: 在处理 `div`/`view` 改写的组件时,必须补充缺失的交互态(鼠标悬浮、点击动效、内部绝对居中对齐)。 + +--- + +这个指南现在涵盖了 uni-app-x 项目开发中最常见的 31 类问题(包含数据源集成实战),为后续开发提供了完整的故障排除和标准化指导。 🚀 diff --git a/pages/mall/admin/product/classification/index.uvue b/pages/mall/admin/product/classification/index.uvue index 15d58b87..c45bd575 100644 --- a/pages/mall/admin/product/classification/index.uvue +++ b/pages/mall/admin/product/classification/index.uvue @@ -680,6 +680,7 @@ transition: all 0.3s; display: flex; align-items: center; padding: 0 6px; +cursor: pointer; } .switch-on { @@ -693,6 +694,7 @@ background-color: #fff; border-radius: 50%; position: absolute; left: 3px; +top: 3px; transition: all 0.3s; } @@ -704,6 +706,7 @@ left: 33px; font-size: 12px; color: #fff; margin-left: auto; +line-height: 24px; } .switch-on .switch-text {