加入商品分类

This commit is contained in:
2026-03-09 14:44:00 +08:00
parent e5ad13d6f5
commit 7ae92fea00
2 changed files with 473 additions and 55 deletions

View File

@@ -29,6 +29,7 @@
<text class="th-cell flex-1">ID</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-1">排序</text>
<text class="th-cell flex-2 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 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">
<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>
</view>
<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>
<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="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>
<text class="switch-text">{{ item.status ? '开启' : '关闭' }}</text>
<text class="switch-text">{{ item.is_active ? '开启' : '关闭' }}</text>
</view>
</view>
<view class="td-cell flex-3 row-center">
@@ -62,19 +64,20 @@
<!-- 子级 -->
<view v-if="item.expanded">
<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">
<text class="child-line"></text>
<text>{{ child.name }}</text>
</view>
<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>
<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="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>
<text class="switch-text">{{ child.status ? '开启' : '关闭' }}</text>
<text class="switch-text">{{ child.is_active ? '开启' : '关闭' }}</text>
</view>
</view>
<view class="td-cell flex-3 row-center">
@@ -100,10 +103,12 @@
<view class="form-item">
<view class="form-label-box"><text class="form-label">上级分类:</text></view>
<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">
<text>{{ form.parentName || '顶级分类' }}</text>
<text class="arrow-down"></text>
</view>
</picker>
</view>
</view>
<view class="form-item">
@@ -112,28 +117,46 @@
<input class="drawer-input" v-model="form.name" placeholder="请输入分类名称" />
</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-label-box"><text class="form-label">分类图标:</text></view>
<view class="form-input-box">
<view class="upload-box">
<text class="plus">+</text>
<text class="upload-text">上传图片</text>
<view class="upload-box" style="position: relative; overflow: hidden;">
<template v-if="form.icon_url">
<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>
<input class="drawer-input" style="margin-top: 10px;" v-model="form.icon_url" placeholder="或者直接输入图片外链" />
<text class="form-tip">建议尺寸180*180</text>
</view>
</view>
<view class="form-item">
<view class="form-label-box"><text class="form-label">排序:</text></view>
<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 class="form-item">
<view class="form-label-box"><text class="form-label">状态:</text></view>
<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>
<text class="switch-text">{{ form.status ? '开启' : '关闭' }}</text>
<text class="switch-text">{{ form.is_active ? '开启' : '关闭' }}</text>
</view>
</view>
</view>
@@ -149,67 +172,199 @@
</template>
<script setup lang="uts">
import { ref, reactive } from 'vue'
import { ref, reactive, onMounted } from 'vue'
import supa from '@/components/supadb/aksupainstance.uts'
interface CateItem {
id: number;
id: string; // Updated to match UUID string type per DB documentation
name: string;
icon: string;
sort: number;
status: boolean;
slug?: string;
icon_url: string;
description?: string;
sort_order: number;
is_active: boolean;
level: number;
path: string[];
expanded?: boolean;
children?: CateItem[];
parentId?: number;
parent_id?: string; // Updated to match UUID string type per DB documentation
}
const list = reactive<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 list = ref<CateItem[]>([])
const allCategoriesFlat = ref<CateItem[]>([])
const showDrawerMask = ref(false)
const showDrawer = ref(false)
const isEdit = ref(false)
const currentEditId = ref('')
const form = reactive({
name: '',
slug: '',
parentName: '',
sort: 0,
status: true
parent_id: '' as string | null,
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) {
buildParentOptions()
if (item != null) {
isEdit.value = true
currentEditId.value = item.id
form.name = item.name
form.sort = item.sort
form.status = item.status
form.parentName = item.parentId != null ? '子分类' : '顶级分类'
form.slug = item.slug ?? ''
form.description = item.description ?? ''
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 {
isEdit.value = false
currentEditId.value = ''
form.name = ''
form.sort = 0
form.status = true
form.slug = ''
form.description = ''
form.icon_url = ''
form.sort_order = 0
form.is_active = true
form.parent_id = null
form.parentName = '顶级分类'
form.parentLevel = 0
form.parentPath = []
}
parentOptionsIndex.value = Math.max(0, parentOptions.value.findIndex(o => o.id == (form.parent_id ?? '')))
showDrawerMask.value = true
setTimeout(() => {
showDrawer.value = true
}, 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() {
showDrawer.value = false
setTimeout(() => {
@@ -217,22 +372,93 @@ showDrawerMask.value = false
}, 300)
}
function saveCate() {
uni.showToast({ title: '保存成功', icon: 'success' })
closeDrawer()
async function saveCate() {
if (form.name.trim() == '') {
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) {
item.status = !item.status
async function toggleStatus(item: CateItem) {
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) {
uni.showModal({
title: '提示',
content: '确定删除该分类吗?',
success: (res) => {
content: '确定删除该分类吗?如果是父分类,其子分类可能也受影响。',
success: async (res) => {
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()
}
}
})