加入商品分类
This commit is contained in:
192
pages/mall/admin/CONSUMER_DB_DOC(1).md
Normal file
192
pages/mall/admin/CONSUMER_DB_DOC(1).md
Normal file
@@ -0,0 +1,192 @@
|
|||||||
|
# 消费者端前端数据库文档 (Consumer App DB Schema)
|
||||||
|
|
||||||
|
本文档基于现有消费者前端 (`mall/pages/mall/consumer`) 和 Supabase 服务层 (`mall/utils/supabaseService.uts`) 的调用逻辑生成。旨在协助商家端前端开发进行数据库对接。
|
||||||
|
|
||||||
|
## 1. 核心业务表 (Core Business Tables)
|
||||||
|
|
||||||
|
### 1.1 商品分类表 (`ml_categories`)
|
||||||
|
用于展示商品的一级/二级分类。
|
||||||
|
|
||||||
|
| 字段名 | 类型 | 描述 | 备注 |
|
||||||
|
| :--- | :--- | :--- | :--- |
|
||||||
|
| `id` | UUID | 主键 | |
|
||||||
|
| `name` | Text | 分类名称 | |
|
||||||
|
| `icon_url` | Text | 图标 URL | 前端可能回退到 Emoji |
|
||||||
|
| `description` | Text | 描述 | |
|
||||||
|
| `parent_id` | UUID | 父分类 ID | 用于树形结构 |
|
||||||
|
| `sort_order` | Integer | 排序权重 | |
|
||||||
|
| `is_active` | Boolean | 是否启用 | |
|
||||||
|
|
||||||
|
### 1.2 品牌表 (`ml_brands`)
|
||||||
|
商品所属品牌信息。
|
||||||
|
|
||||||
|
| 字段名 | 类型 | 描述 | 备注 |
|
||||||
|
| :--- | :--- | :--- | :--- |
|
||||||
|
| `id` | UUID | 主键 | |
|
||||||
|
| `name` | Text | 品牌名称 | |
|
||||||
|
| `logo_url` | Text | 品牌 Logo URL | |
|
||||||
|
| `description` | Text | 品牌描述 | |
|
||||||
|
| `country` | Text | 所属国家 | 可选 |
|
||||||
|
| `is_active` | Boolean | 是否启用 | |
|
||||||
|
|
||||||
|
### 1.3 商家/店铺表 (`ml_shops`)
|
||||||
|
商家端主要管理的店铺信息实体。
|
||||||
|
|
||||||
|
| 字段名 | 类型 | 描述 | 备注 |
|
||||||
|
| :--- | :--- | :--- | :--- |
|
||||||
|
| `id` | UUID | 主键 | |
|
||||||
|
| `merchant_id` | UUID | 关联的商户账号 ID | 对应 auth.users 或 merchants 表 |
|
||||||
|
| `shop_name` | Text | 店铺名称 | |
|
||||||
|
| `shop_logo` | Text | 店铺 Logo | |
|
||||||
|
| `shop_banner` | Text | 店铺背景图 | |
|
||||||
|
| `description` | Text | 店铺简介 | |
|
||||||
|
| `contact_name` | Text | 联系人 | |
|
||||||
|
| `contact_phone` | Text | 联系电话 | |
|
||||||
|
| `rating_avg` | Numeric | 平均评分 | |
|
||||||
|
| `total_sales` | Integer | 总销量 | |
|
||||||
|
| `status` | Integer | 状态 | 1: 正常, 0: 停用 |
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 2. 商品系统 (Product System)
|
||||||
|
|
||||||
|
### 2.1 商品主表 (`ml_products`)
|
||||||
|
商家发布的核心商品数据。
|
||||||
|
|
||||||
|
| 字段名 | 类型 | 描述 | 备注 |
|
||||||
|
| :--- | :--- | :--- | :--- |
|
||||||
|
| `id` | UUID | 主键 | |
|
||||||
|
| `merchant_id` | UUID | 所属商家 ID | |
|
||||||
|
| `category_id` | UUID | 所属分类 ID | |
|
||||||
|
| `brand_id` | UUID | 所属品牌 ID | |
|
||||||
|
| `name` | Text | 商品名称 | |
|
||||||
|
| `subtitle` | Text | 副标题 | 简短描述 |
|
||||||
|
| `description` | Text | 商品详情 | HTML 或 Markdown |
|
||||||
|
| `main_image_url` | Text | 主图 URL | |
|
||||||
|
| `image_urls` | JSON/Array | 轮播图列表 | `['url1', 'url2']` |
|
||||||
|
| `video_urls` | JSON/Array | 视频列表 | |
|
||||||
|
| `base_price` | Numeric | 基础售价 | 列表页展示价格 |
|
||||||
|
| `market_price` | Numeric | 市场价/划线价 | |
|
||||||
|
| `cost_price` | Numeric | 成本价 | 敏感字段,仅商家可见 |
|
||||||
|
| `total_stock` | Integer | 总库存 | |
|
||||||
|
| `status` | Integer | 状态 | 1: 上架, 0: 下架, 2: 审核中 |
|
||||||
|
| `is_hot` | Boolean | 是否热销 | |
|
||||||
|
| `is_new` | Boolean | 是否新品 | |
|
||||||
|
| `is_featured` | Boolean | 是否推荐 | |
|
||||||
|
| `attributes` | JSONB | 商品属性 | `{ "材质": "纯棉", "季节": "夏季" }` |
|
||||||
|
| `tags` | Text[] | 标签 | |
|
||||||
|
| `sale_count` | Integer | 销量 | 统计字段 |
|
||||||
|
|
||||||
|
### 2.2 商品 SKU 表 (`ml_product_skus`)
|
||||||
|
商品的多规格定义(如颜色、尺寸)。
|
||||||
|
|
||||||
|
| 字段名 | 类型 | 描述 | 备注 |
|
||||||
|
| :--- | :--- | :--- | :--- |
|
||||||
|
| `id` | UUID | 主键 | |
|
||||||
|
| `product_id` | UUID | 关联商品 ID | |
|
||||||
|
| `sku_code` | Text | SKU 编码 | 商家自定义编码 |
|
||||||
|
| `specifications` | JSONB | 规格键值对 | `{ "颜色": "红", "尺寸": "L" }` |
|
||||||
|
| `price` | Numeric | SKU 售价 | 特殊规格价格 |
|
||||||
|
| `market_price` | Numeric | SKU 市场价 | |
|
||||||
|
| `stock` | Integer | 当前库存 | |
|
||||||
|
| `image_url` | Text | 规格对应图片 | 如红色款对应红色的图 |
|
||||||
|
| `status` | Integer | 状态 | 1: 启用, 0: 禁用 |
|
||||||
|
|
||||||
|
### 2.3 商品详情视图 (`ml_products_detail_view`)
|
||||||
|
**重要**: 消费者端主要通过此视图查询商品,商家在维护数据时应确保这些关联字段能正确生成。
|
||||||
|
* 该视图通常 `JOIN` 了 `ml_shops` (获取 `shop_name`), `ml_brands` (获取 `brand_name`), `ml_categories` (获取 `category_name`)。
|
||||||
|
* **商家端操作**: 不需要直接操作视图,只需维护上述基础表。
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 3. 交易系统 (Transaction System)
|
||||||
|
|
||||||
|
### 3.1 购物车 (`ml_shopping_cart`)
|
||||||
|
|
||||||
|
| 字段名 | 类型 | 描述 | 备注 |
|
||||||
|
| :--- | :--- | :--- | :--- |
|
||||||
|
| `id` | UUID | 主键 | |
|
||||||
|
| `user_id` | UUID | 用户 ID | |
|
||||||
|
| `product_id` | UUID | 商品 ID | |
|
||||||
|
| `sku_id` | UUID | SKU ID | 可空(若商品无多规格) |
|
||||||
|
| `quantity` | Integer |数量 | |
|
||||||
|
| `selected` | Boolean | 是否勾选 | 购物车状态 |
|
||||||
|
| `created_at` | Timestamp | 创建时间 | |
|
||||||
|
|
||||||
|
### 3.2 订单主表 (`ml_orders`) (推测结构)
|
||||||
|
商家端处理订单的核心表。
|
||||||
|
|
||||||
|
| 字段名 | 类型 | 描述 | 备注 |
|
||||||
|
| :--- | :--- | :--- | :--- |
|
||||||
|
| `id` | UUID | 主键 | |
|
||||||
|
| `user_id` | UUID | 用户 ID | |
|
||||||
|
| `merchant_id` | UUID | 商家 ID | |
|
||||||
|
| `order_no` | Text | 订单号 | 唯一业务单号 |
|
||||||
|
| `total_amount` | Numeric | 订单总金额 | |
|
||||||
|
| `pay_amount` | Numeric | 实付金额 | |
|
||||||
|
| `status` | Integer | 订单状态 | 0: 待付款, 1: 待发货, 2: 待收货, 3: 已完成, -1: 已取消 |
|
||||||
|
| `address_snapshot` | JSONB | 收货地址快照 | 下单时的地址信息 |
|
||||||
|
| `remark` | Text | 订单备注 | |
|
||||||
|
| `created_at` | Timestamp | 下单时间 | |
|
||||||
|
|
||||||
|
### 3.3 订单项表 (`ml_order_items`) (推测结构)
|
||||||
|
|
||||||
|
| 字段名 | 类型 | 描述 | 备注 |
|
||||||
|
| :--- | :--- | :--- | :--- |
|
||||||
|
| `id` | UUID | 主键 | |
|
||||||
|
| `order_id` | UUID | 订单 ID | |
|
||||||
|
| `product_id` | UUID | 商品 ID | |
|
||||||
|
| `sku_id` | UUID | SKU ID | |
|
||||||
|
| `product_name` | Text | 商品名称快照 | |
|
||||||
|
| `price` | Numeric | 成交单价 | |
|
||||||
|
| `quantity` | Integer | 购买数量 | |
|
||||||
|
| `sku_snapshot` | JSONB | 规格快照 | |
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 4. 用户相关 (User Relations)
|
||||||
|
|
||||||
|
### 4.1 用户地址 (`ml_user_addresses`)
|
||||||
|
|
||||||
|
| 字段名 | 类型 | 描述 | 备注 |
|
||||||
|
| :--- | :--- | :--- | :--- |
|
||||||
|
| `id` | UUID | 主键 | |
|
||||||
|
| `user_id` | UUID | 用户 ID | |
|
||||||
|
| `recipient_name` | Text | 收货人姓名 | |
|
||||||
|
| `phone` | Text | 手机号 | |
|
||||||
|
| `province` | Text | 省 | |
|
||||||
|
| `city` | Text | 市 | |
|
||||||
|
| `district` | Text | 区 | |
|
||||||
|
| `detail_address` | Text | 详细地址 | |
|
||||||
|
| `is_default` | Boolean | 是否默认地址 | |
|
||||||
|
|
||||||
|
### 4.2 聊天消息 (`ml_chat_messages`)
|
||||||
|
用于客服系统。
|
||||||
|
|
||||||
|
| 字段名 | 类型 | 描述 | 备注 |
|
||||||
|
| :--- | :--- | :--- | :--- |
|
||||||
|
| `id` | UUID | 主键 | |
|
||||||
|
| `session_id` | UUID | 会话 ID | 可选,或通过收发人聚合 |
|
||||||
|
| `sender_id` | UUID | 发送者 ID | |
|
||||||
|
| `receiver_id` | UUID | 接收者 ID | |
|
||||||
|
| `content` | Text | 消息内容 | |
|
||||||
|
| `msg_type` | Text | 消息类型 | 'text', 'image', 'product' |
|
||||||
|
| `is_read` | Boolean | 是否已读 | |
|
||||||
|
| `created_at` | Timestamp | 发送时间 | |
|
||||||
|
|
||||||
|
## 5. 对接建议 (Integration Tips for Merchant Frontend)
|
||||||
|
|
||||||
|
1. **商品管理**:
|
||||||
|
* 在创建商品时,必须先选择 `category_id` 和 `merchant_id`。
|
||||||
|
* 如果有 `specifications` (多规格),请同时向 `ml_product_skus` 插入数据。
|
||||||
|
* 更新库存时,请优先更新 `ml_product_skus` 中的 `stock`,并同步总库存到 `ml_products`.`total_stock`。
|
||||||
|
|
||||||
|
2. **图片处理**:
|
||||||
|
* 消费者端支持 `main_image_url` (字符串) 和 `image_urls` (JSON 数组) 两种格式,请确保都正确填充。
|
||||||
|
|
||||||
|
3. **状态管理**:
|
||||||
|
* 上架商品请将 `status` 设为 `1`。
|
||||||
|
* 如需在首页 "推荐/热销" 板块显示,请设置 `is_featured` 或 `is_hot` 为 `true`。
|
||||||
|
|
||||||
|
4. **Supabase 安全策略 (RLS)**:
|
||||||
|
* 请确保商家端账号有权限写入 `ml_products` 和 `ml_shops` 表,但只能修改 `merchant_id` 等于自己账号的数据。
|
||||||
@@ -29,6 +29,7 @@
|
|||||||
<text class="th-cell flex-1">ID</text>
|
<text class="th-cell flex-1">ID</text>
|
||||||
<text class="th-cell flex-3">分类名称</text>
|
<text class="th-cell flex-3">分类名称</text>
|
||||||
<text class="th-cell flex-2">分类图标</text>
|
<text class="th-cell flex-2">分类图标</text>
|
||||||
|
<text class="th-cell flex-2">描述</text>
|
||||||
<text class="th-cell flex-1">排序</text>
|
<text class="th-cell flex-1">排序</text>
|
||||||
<text class="th-cell flex-2 text-center">状态</text>
|
<text class="th-cell flex-2 text-center">状态</text>
|
||||||
<text class="th-cell flex-3 text-center">操作</text>
|
<text class="th-cell flex-3 text-center">操作</text>
|
||||||
@@ -38,19 +39,20 @@
|
|||||||
<view v-for="(item, index) in list" :key="index" class="table-row-group">
|
<view v-for="(item, index) in list" :key="index" class="table-row-group">
|
||||||
<!-- 父级 -->
|
<!-- 父级 -->
|
||||||
<view class="table-row">
|
<view class="table-row">
|
||||||
<text class="td-cell flex-1 color-9">{{ item.id }}</text>
|
<text class="td-cell flex-1 color-9" style="overflow: hidden; text-overflow: ellipsis; white-space: nowrap;" :title="item.id">{{ item.id.substring(0, 8) }}</text>
|
||||||
<view class="td-cell flex-3 row-layout">
|
<view class="td-cell flex-3 row-layout">
|
||||||
<text class="expand-icon" @click="item.expanded = !item.expanded">{{ item.expanded ? '' : '' }}</text>
|
<text class="expand-icon" @click="item.expanded = !item.expanded">{{ item.expanded ? '▼' : '▶' }}</text>
|
||||||
<text>{{ item.name }}</text>
|
<text>{{ item.name }}</text>
|
||||||
</view>
|
</view>
|
||||||
<view class="td-cell flex-2">
|
<view class="td-cell flex-2">
|
||||||
<image class="cate-icon" :src="item.icon" mode="aspectFit"></image>
|
<image class="cate-icon" :src="item.icon_url || '/static/logo.png'" mode="aspectFit"></image>
|
||||||
</view>
|
</view>
|
||||||
<text class="td-cell flex-1">{{ item.sort }}</text>
|
<text class="td-cell flex-2" style="overflow: hidden; text-overflow: ellipsis; white-space: nowrap;">{{ item.description || '-' }}</text>
|
||||||
|
<text class="td-cell flex-1">{{ item.sort_order }}</text>
|
||||||
<view class="td-cell flex-2 row-center">
|
<view class="td-cell flex-2 row-center">
|
||||||
<view class="switch-mock" :class="item.status ? 'switch-on' : ''" @click="toggleStatus(item)">
|
<view class="switch-mock" :class="item.is_active ? 'switch-on' : ''" @click="toggleStatus(item)">
|
||||||
<view class="switch-dot"></view>
|
<view class="switch-dot"></view>
|
||||||
<text class="switch-text">{{ item.status ? '开启' : '关闭' }}</text>
|
<text class="switch-text">{{ item.is_active ? '开启' : '关闭' }}</text>
|
||||||
</view>
|
</view>
|
||||||
</view>
|
</view>
|
||||||
<view class="td-cell flex-3 row-center">
|
<view class="td-cell flex-3 row-center">
|
||||||
@@ -62,19 +64,20 @@
|
|||||||
<!-- 子级 -->
|
<!-- 子级 -->
|
||||||
<view v-if="item.expanded">
|
<view v-if="item.expanded">
|
||||||
<view v-for="(child, cIndex) in item.children" :key="cIndex" class="table-row sub-row">
|
<view v-for="(child, cIndex) in item.children" :key="cIndex" class="table-row sub-row">
|
||||||
<text class="td-cell flex-1 color-9">{{ child.id }}</text>
|
<text class="td-cell flex-1 color-9" style="overflow: hidden; text-overflow: ellipsis; white-space: nowrap;" :title="child.id">{{ child.id.substring(0, 8) }}</text>
|
||||||
<view class="td-cell flex-3 row-layout pl-20">
|
<view class="td-cell flex-3 row-layout pl-20">
|
||||||
<text class="child-line"></text>
|
<text class="child-line"></text>
|
||||||
<text>{{ child.name }}</text>
|
<text>{{ child.name }}</text>
|
||||||
</view>
|
</view>
|
||||||
<view class="td-cell flex-2">
|
<view class="td-cell flex-2">
|
||||||
<image class="cate-icon" :src="child.icon" mode="aspectFit"></image>
|
<image class="cate-icon" :src="child.icon_url || '/static/logo.png'" mode="aspectFit"></image>
|
||||||
</view>
|
</view>
|
||||||
<text class="td-cell flex-1">{{ child.sort }}</text>
|
<text class="td-cell flex-2" style="overflow: hidden; text-overflow: ellipsis; white-space: nowrap;">{{ child.description || '-' }}</text>
|
||||||
|
<text class="td-cell flex-1">{{ child.sort_order }}</text>
|
||||||
<view class="td-cell flex-2 row-center">
|
<view class="td-cell flex-2 row-center">
|
||||||
<view class="switch-mock" :class="child.status ? 'switch-on' : ''" @click="toggleStatus(child)">
|
<view class="switch-mock" :class="child.is_active ? 'switch-on' : ''" @click="toggleStatus(child)">
|
||||||
<view class="switch-dot"></view>
|
<view class="switch-dot"></view>
|
||||||
<text class="switch-text">{{ child.status ? '开启' : '关闭' }}</text>
|
<text class="switch-text">{{ child.is_active ? '开启' : '关闭' }}</text>
|
||||||
</view>
|
</view>
|
||||||
</view>
|
</view>
|
||||||
<view class="td-cell flex-3 row-center">
|
<view class="td-cell flex-3 row-center">
|
||||||
@@ -100,10 +103,12 @@
|
|||||||
<view class="form-item">
|
<view class="form-item">
|
||||||
<view class="form-label-box"><text class="form-label">上级分类:</text></view>
|
<view class="form-label-box"><text class="form-label">上级分类:</text></view>
|
||||||
<view class="form-input-box">
|
<view class="form-input-box">
|
||||||
|
<picker mode="selector" :range="parentOptions" range-key="name" :value="parentOptionsIndex" @change="onParentChange" class="picker-box">
|
||||||
<view class="mock-select-full">
|
<view class="mock-select-full">
|
||||||
<text>{{ form.parentName || '顶级分类' }}</text>
|
<text>{{ form.parentName || '顶级分类' }}</text>
|
||||||
<text class="arrow-down"></text>
|
<text class="arrow-down"></text>
|
||||||
</view>
|
</view>
|
||||||
|
</picker>
|
||||||
</view>
|
</view>
|
||||||
</view>
|
</view>
|
||||||
<view class="form-item">
|
<view class="form-item">
|
||||||
@@ -112,28 +117,46 @@
|
|||||||
<input class="drawer-input" v-model="form.name" placeholder="请输入分类名称" />
|
<input class="drawer-input" v-model="form.name" placeholder="请输入分类名称" />
|
||||||
</view>
|
</view>
|
||||||
</view>
|
</view>
|
||||||
|
<view class="form-item">
|
||||||
|
<view class="form-label-box"><text class="form-label">标识 (Slug):</text></view>
|
||||||
|
<view class="form-input-box">
|
||||||
|
<input class="drawer-input" v-model="form.slug" placeholder="可选,分类英文/拼音标识" />
|
||||||
|
</view>
|
||||||
|
</view>
|
||||||
|
<view class="form-item align-start">
|
||||||
|
<view class="form-label-box"><text class="form-label">分类描述:</text></view>
|
||||||
|
<view class="form-input-box">
|
||||||
|
<textarea class="drawer-textarea" v-model="form.description" placeholder="请输入分类描述" style="width: 100%; height: 80px; border: 1px solid #dcdfe6; border-radius: 4px; padding: 8px 12px; font-size: 14px; box-sizing: border-box;"></textarea>
|
||||||
|
</view>
|
||||||
|
</view>
|
||||||
<view class="form-item align-start">
|
<view class="form-item align-start">
|
||||||
<view class="form-label-box"><text class="form-label">分类图标:</text></view>
|
<view class="form-label-box"><text class="form-label">分类图标:</text></view>
|
||||||
<view class="form-input-box">
|
<view class="form-input-box">
|
||||||
<view class="upload-box">
|
<view class="upload-box" style="position: relative; overflow: hidden;">
|
||||||
<text class="plus">+</text>
|
<template v-if="form.icon_url">
|
||||||
<text class="upload-text">上传图片</text>
|
<image :src="form.icon_url" mode="aspectFit" style="width: 100%; height: 100%;"></image>
|
||||||
|
</template>
|
||||||
|
<template v-else>
|
||||||
|
<text class="plus">+</text>
|
||||||
|
<text class="upload-text">上传图片</text>
|
||||||
|
</template>
|
||||||
</view>
|
</view>
|
||||||
|
<input class="drawer-input" style="margin-top: 10px;" v-model="form.icon_url" placeholder="或者直接输入图片外链" />
|
||||||
<text class="form-tip">建议尺寸:180*180</text>
|
<text class="form-tip">建议尺寸:180*180</text>
|
||||||
</view>
|
</view>
|
||||||
</view>
|
</view>
|
||||||
<view class="form-item">
|
<view class="form-item">
|
||||||
<view class="form-label-box"><text class="form-label">排序:</text></view>
|
<view class="form-label-box"><text class="form-label">排序:</text></view>
|
||||||
<view class="form-input-box">
|
<view class="form-input-box">
|
||||||
<input type="number" class="drawer-input" v-model="form.sort" />
|
<input type="number" class="drawer-input" v-model="form.sort_order" />
|
||||||
</view>
|
</view>
|
||||||
</view>
|
</view>
|
||||||
<view class="form-item">
|
<view class="form-item">
|
||||||
<view class="form-label-box"><text class="form-label">状态:</text></view>
|
<view class="form-label-box"><text class="form-label">状态:</text></view>
|
||||||
<view class="form-input-box">
|
<view class="form-input-box">
|
||||||
<view class="switch-mock" :class="form.status ? 'switch-on' : ''" @click="form.status = !form.status">
|
<view class="switch-mock" :class="form.is_active ? 'switch-on' : ''" @click="form.is_active = !form.is_active">
|
||||||
<view class="switch-dot"></view>
|
<view class="switch-dot"></view>
|
||||||
<text class="switch-text">{{ form.status ? '开启' : '关闭' }}</text>
|
<text class="switch-text">{{ form.is_active ? '开启' : '关闭' }}</text>
|
||||||
</view>
|
</view>
|
||||||
</view>
|
</view>
|
||||||
</view>
|
</view>
|
||||||
@@ -149,67 +172,199 @@
|
|||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script setup lang="uts">
|
<script setup lang="uts">
|
||||||
import { ref, reactive } from 'vue'
|
import { ref, reactive, onMounted } from 'vue'
|
||||||
|
import supa from '@/components/supadb/aksupainstance.uts'
|
||||||
|
|
||||||
interface CateItem {
|
interface CateItem {
|
||||||
id: number;
|
id: string; // Updated to match UUID string type per DB documentation
|
||||||
name: string;
|
name: string;
|
||||||
icon: string;
|
slug?: string;
|
||||||
sort: number;
|
icon_url: string;
|
||||||
status: boolean;
|
description?: string;
|
||||||
|
sort_order: number;
|
||||||
|
is_active: boolean;
|
||||||
|
level: number;
|
||||||
|
path: string[];
|
||||||
expanded?: boolean;
|
expanded?: boolean;
|
||||||
children?: CateItem[];
|
children?: CateItem[];
|
||||||
parentId?: number;
|
parent_id?: string; // Updated to match UUID string type per DB documentation
|
||||||
}
|
}
|
||||||
|
|
||||||
const list = reactive<CateItem[]>([
|
const list = ref<CateItem[]>([])
|
||||||
{
|
const allCategoriesFlat = ref<CateItem[]>([])
|
||||||
id: 100, name: '家用电器', icon: '/static/logo.png', sort: 1, status: true, expanded: true,
|
|
||||||
children: [
|
|
||||||
{ id: 101, name: '电视机', icon: '/static/logo.png', sort: 1, status: true, parentId: 100 },
|
|
||||||
{ id: 102, name: '电冰箱', icon: '/static/logo.png', sort: 2, status: true, parentId: 100 }
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
id: 200, name: '手机数码', icon: '/static/logo.png', sort: 2, status: true, expanded: false,
|
|
||||||
children: [
|
|
||||||
{ id: 201, name: '手机', icon: '/static/logo.png', sort: 1, status: true, parentId: 200 },
|
|
||||||
{ id: 202, name: '耳机', icon: '/static/logo.png', sort: 2, status: true, parentId: 200 }
|
|
||||||
]
|
|
||||||
}
|
|
||||||
])
|
|
||||||
|
|
||||||
const showDrawerMask = ref(false)
|
const showDrawerMask = ref(false)
|
||||||
const showDrawer = ref(false)
|
const showDrawer = ref(false)
|
||||||
const isEdit = ref(false)
|
const isEdit = ref(false)
|
||||||
|
const currentEditId = ref('')
|
||||||
|
|
||||||
const form = reactive({
|
const form = reactive({
|
||||||
name: '',
|
name: '',
|
||||||
|
slug: '',
|
||||||
parentName: '',
|
parentName: '',
|
||||||
sort: 0,
|
parent_id: '' as string | null,
|
||||||
status: true
|
parentLevel: 0,
|
||||||
|
parentPath: [] as string[],
|
||||||
|
description: '',
|
||||||
|
icon_url: '',
|
||||||
|
sort_order: 0,
|
||||||
|
is_active: true
|
||||||
})
|
})
|
||||||
|
|
||||||
|
const parentOptions = ref<{id: string, name: string, level: number, path: string[]}[]>([])
|
||||||
|
const parentOptionsIndex = ref(0)
|
||||||
|
|
||||||
|
async function loadData() {
|
||||||
|
uni.showLoading({ title: '加载中' })
|
||||||
|
try {
|
||||||
|
const res = await supa.from('ml_categories').select('*').order('sort_order', { ascending: true }).execute()
|
||||||
|
if (res.error != null) {
|
||||||
|
uni.showToast({ title: '获取数据失败', icon: 'none' })
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!Array.isArray(res.data)) {
|
||||||
|
list.value = []
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
const allItems = res.data as Array<UTSJSONObject>
|
||||||
|
const formatted = allItems.map((item: UTSJSONObject): CateItem => {
|
||||||
|
let rawPath = item.get('path')
|
||||||
|
let parsedPath: string[] = []
|
||||||
|
if (Array.isArray(rawPath)) {
|
||||||
|
parsedPath = rawPath as string[]
|
||||||
|
} else if (typeof rawPath === 'string') {
|
||||||
|
// just in case it returns a string like '{"name"}'
|
||||||
|
try {
|
||||||
|
parsedPath = JSON.parse(rawPath.replace('{','[').replace('}',']')) as string[]
|
||||||
|
} catch(e) {}
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
id: (item.get('id') as string | null) ?? '',
|
||||||
|
name: (item.get('name') as string | null) ?? '',
|
||||||
|
slug: (item.get('slug') as string | null) ?? '',
|
||||||
|
icon_url: (item.get('icon_url') as string | null) ?? '',
|
||||||
|
description: (item.get('description') as string | null) ?? '',
|
||||||
|
sort_order: (item.get('sort_order') as number | null) ?? 0,
|
||||||
|
is_active: (item.get('is_active') as boolean | null) ?? true,
|
||||||
|
parent_id: (item.get('parent_id') as string | null),
|
||||||
|
level: (item.get('level') as number | null) ?? 1,
|
||||||
|
path: parsedPath,
|
||||||
|
expanded: false,
|
||||||
|
children: [] as Array<CateItem>
|
||||||
|
} as CateItem
|
||||||
|
})
|
||||||
|
|
||||||
|
allCategoriesFlat.value = formatted
|
||||||
|
|
||||||
|
const topLevel = formatted.filter((f: CateItem): boolean => f.parent_id == null || f.parent_id == '')
|
||||||
|
topLevel.forEach((top: CateItem) => {
|
||||||
|
top.children = formatted.filter((f: CateItem): boolean => f.parent_id == top.id)
|
||||||
|
})
|
||||||
|
list.value = topLevel
|
||||||
|
} catch (e) {
|
||||||
|
console.error(e)
|
||||||
|
} finally {
|
||||||
|
uni.hideLoading()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
onMounted(() => {
|
||||||
|
loadData()
|
||||||
|
})
|
||||||
|
|
||||||
|
function buildParentOptions() {
|
||||||
|
const options = [{id: '', name: '顶级分类', level: 0, path: [] as string[]}]
|
||||||
|
|
||||||
|
function traverse(items: CateItem[], prefix: string) {
|
||||||
|
items.forEach(item => {
|
||||||
|
options.push({
|
||||||
|
id: item.id,
|
||||||
|
name: prefix + item.name,
|
||||||
|
level: item.level,
|
||||||
|
path: item.path
|
||||||
|
})
|
||||||
|
if (item.children && item.children!.length > 0) {
|
||||||
|
traverse(item.children!, prefix + '├─ ')
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
traverse(list.value, '')
|
||||||
|
parentOptions.value = options
|
||||||
|
}
|
||||||
|
|
||||||
function openDrawer(item: CateItem | null = null) {
|
function openDrawer(item: CateItem | null = null) {
|
||||||
|
buildParentOptions()
|
||||||
|
|
||||||
if (item != null) {
|
if (item != null) {
|
||||||
isEdit.value = true
|
isEdit.value = true
|
||||||
|
currentEditId.value = item.id
|
||||||
form.name = item.name
|
form.name = item.name
|
||||||
form.sort = item.sort
|
form.slug = item.slug ?? ''
|
||||||
form.status = item.status
|
form.description = item.description ?? ''
|
||||||
form.parentName = item.parentId != null ? '子分类' : '顶级分类'
|
form.icon_url = item.icon_url
|
||||||
|
form.sort_order = item.sort_order
|
||||||
|
form.is_active = item.is_active
|
||||||
|
form.parent_id = item.parent_id
|
||||||
|
|
||||||
|
if (item.parent_id) {
|
||||||
|
const p = allCategoriesFlat.value.find(c => c.id == item.parent_id)
|
||||||
|
if (p != null) {
|
||||||
|
form.parentName = p.name
|
||||||
|
form.parentLevel = p.level
|
||||||
|
form.parentPath = p.path
|
||||||
|
} else {
|
||||||
|
form.parentName = '顶级分类'
|
||||||
|
form.parentLevel = 0
|
||||||
|
form.parentPath = []
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
form.parentName = '顶级分类'
|
||||||
|
form.parentLevel = 0
|
||||||
|
form.parentPath = []
|
||||||
|
}
|
||||||
|
|
||||||
|
// remove self from parent options to avoid loop
|
||||||
|
parentOptions.value = parentOptions.value.filter(o => o.id != item.id)
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
isEdit.value = false
|
isEdit.value = false
|
||||||
|
currentEditId.value = ''
|
||||||
form.name = ''
|
form.name = ''
|
||||||
form.sort = 0
|
form.slug = ''
|
||||||
form.status = true
|
form.description = ''
|
||||||
|
form.icon_url = ''
|
||||||
|
form.sort_order = 0
|
||||||
|
form.is_active = true
|
||||||
|
form.parent_id = null
|
||||||
form.parentName = '顶级分类'
|
form.parentName = '顶级分类'
|
||||||
|
form.parentLevel = 0
|
||||||
|
form.parentPath = []
|
||||||
}
|
}
|
||||||
|
|
||||||
|
parentOptionsIndex.value = Math.max(0, parentOptions.value.findIndex(o => o.id == (form.parent_id ?? '')))
|
||||||
|
|
||||||
showDrawerMask.value = true
|
showDrawerMask.value = true
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
showDrawer.value = true
|
showDrawer.value = true
|
||||||
}, 50)
|
}, 50)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function onParentChange(e: any) {
|
||||||
|
const index = e.detail.value as number
|
||||||
|
parentOptionsIndex.value = index
|
||||||
|
const selected = parentOptions.value[index]
|
||||||
|
if (selected.id == '') {
|
||||||
|
form.parent_id = null
|
||||||
|
} else {
|
||||||
|
form.parent_id = selected.id
|
||||||
|
}
|
||||||
|
form.parentName = selected.name.replace(/├─ /g, '')
|
||||||
|
form.parentLevel = selected.level
|
||||||
|
form.parentPath = selected.path
|
||||||
|
}
|
||||||
|
|
||||||
function closeDrawer() {
|
function closeDrawer() {
|
||||||
showDrawer.value = false
|
showDrawer.value = false
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
@@ -217,22 +372,93 @@ showDrawerMask.value = false
|
|||||||
}, 300)
|
}, 300)
|
||||||
}
|
}
|
||||||
|
|
||||||
function saveCate() {
|
async function saveCate() {
|
||||||
uni.showToast({ title: '保存成功', icon: 'success' })
|
if (form.name.trim() == '') {
|
||||||
closeDrawer()
|
uni.showToast({ title: '请输入分类名称', icon: 'none' })
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
uni.showLoading({ title: '保存中' })
|
||||||
|
|
||||||
|
const currentLevel = form.parent_id ? form.parentLevel + 1 : 1
|
||||||
|
// DB stores text array: ['顶级名称', '子级名称']
|
||||||
|
// Since parentPath is already an array of parent names, we just append current name.
|
||||||
|
let currentPath: string[] = []
|
||||||
|
if (form.parentPath.length > 0) {
|
||||||
|
currentPath = [...form.parentPath, form.name]
|
||||||
|
} else {
|
||||||
|
currentPath = [form.name]
|
||||||
|
}
|
||||||
|
|
||||||
|
// UTS expects explicitly casting arrays or passing them properly to Supabase
|
||||||
|
// In UTS Supabase client arrays are handled natively if typed correctly.
|
||||||
|
const payload = {
|
||||||
|
name: form.name,
|
||||||
|
slug: form.slug.length > 0 ? form.slug : null,
|
||||||
|
icon_url: form.icon_url,
|
||||||
|
description: form.description,
|
||||||
|
sort_order: form.sort_order,
|
||||||
|
is_active: form.is_active,
|
||||||
|
parent_id: form.parent_id,
|
||||||
|
level: currentLevel,
|
||||||
|
path: currentPath
|
||||||
|
} as UTSJSONObject
|
||||||
|
|
||||||
|
try {
|
||||||
|
if (isEdit.value) {
|
||||||
|
const res = await supa.from('ml_categories').update(payload).eq('id', currentEditId.value).execute()
|
||||||
|
if (res.error != null) {
|
||||||
|
uni.showToast({ title: '保存失败: ' + res.error!.message, icon: 'none' })
|
||||||
|
return
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
const res = await supa.from('ml_categories').insert(payload).execute()
|
||||||
|
if (res.error != null) {
|
||||||
|
uni.showToast({ title: '保存失败: ' + res.error!.message, icon: 'none' })
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
uni.showToast({ title: '保存成功', icon: 'success' })
|
||||||
|
closeDrawer()
|
||||||
|
loadData()
|
||||||
|
} catch (e) {
|
||||||
|
uni.showToast({ title: '发生异常', icon: 'none' })
|
||||||
|
} finally {
|
||||||
|
uni.hideLoading()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function toggleStatus(item: CateItem) {
|
async function toggleStatus(item: CateItem) {
|
||||||
item.status = !item.status
|
const newStatus = !item.is_active
|
||||||
|
const oldStatus = item.is_active
|
||||||
|
item.is_active = newStatus
|
||||||
|
|
||||||
|
const payload = { is_active: newStatus } as UTSJSONObject
|
||||||
|
const res = await supa.from('ml_categories').update(payload).eq('id', item.id).execute()
|
||||||
|
|
||||||
|
if (res.error != null) {
|
||||||
|
item.is_active = oldStatus
|
||||||
|
uni.showToast({ title: '状态更新失败', icon: 'none' })
|
||||||
|
} else {
|
||||||
|
uni.showToast({ title: '已更新状态', icon: 'success' })
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function deleteItem(item: CateItem) {
|
function deleteItem(item: CateItem) {
|
||||||
uni.showModal({
|
uni.showModal({
|
||||||
title: '提示',
|
title: '提示',
|
||||||
content: '确定删除该分类吗?',
|
content: '确定删除该分类吗?如果是父分类,其子分类可能也受影响。',
|
||||||
success: (res) => {
|
success: async (res) => {
|
||||||
if (res.confirm) {
|
if (res.confirm) {
|
||||||
uni.showToast({ title: '已模拟删除', icon: 'none' })
|
uni.showLoading({ title: '删除中' })
|
||||||
|
const delRes = await supa.from('ml_categories').delete().eq('id', item.id).execute()
|
||||||
|
if (delRes.error != null) {
|
||||||
|
uni.showToast({ title: '删除失败: ' + delRes.error!.message, icon: 'none' })
|
||||||
|
} else {
|
||||||
|
uni.showToast({ title: '删除成功', icon: 'success' })
|
||||||
|
loadData()
|
||||||
|
}
|
||||||
|
uni.hideLoading()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user