const express = require('express'); const router = express.Router(); const { getDB } = require('../database'); const { auth } = require('../middleware/auth'); // 获取地址标签列表(包含系统默认和用户自定义) router.get('/', auth, async (req, res) => { try { const userId = req.user.id; const [labels] = await getDB().execute( `SELECT * FROM address_labels WHERE user_id IS NULL OR user_id = ? ORDER BY is_system DESC, created_at ASC`, [userId] ); res.json({ success: true, data: labels }); } catch (error) { console.error('获取地址标签列表错误:', error); res.status(500).json({ message: '获取地址标签列表失败' }); } }); // 创建自定义地址标签 router.post('/', auth, async (req, res) => { try { const userId = req.user.id; const { name, color = '#1890ff' } = req.body; if (!name || name.trim() === '') { return res.status(400).json({ message: '标签名称不能为空' }); } // 检查标签名称是否已存在(系统标签或用户自定义标签) const [existing] = await getDB().execute( `SELECT id FROM address_labels WHERE name = ? AND (user_id IS NULL OR user_id = ?)`, [name.trim(), userId] ); if (existing.length > 0) { return res.status(400).json({ message: '标签名称已存在' }); } const [result] = await getDB().execute( `INSERT INTO address_labels (name, color, user_id, is_system, created_at, updated_at) VALUES (?, ?, ?, false, NOW(), NOW())`, [name.trim(), color, userId] ); res.status(201).json({ success: true, message: '地址标签创建成功', data: { labelId: result.insertId } }); } catch (error) { console.error('创建地址标签错误:', error); res.status(500).json({ message: '创建地址标签失败' }); } }); // 更新自定义地址标签 router.put('/:id', auth, async (req, res) => { try { const labelId = req.params.id; const userId = req.user.id; const { name, color } = req.body; // 检查标签是否存在且属于当前用户(不能修改系统标签) const [existing] = await getDB().execute( 'SELECT id, is_system FROM address_labels WHERE id = ? AND user_id = ?', [labelId, userId] ); if (existing.length === 0) { return res.status(404).json({ message: '地址标签不存在或无权限修改' }); } if (existing[0].is_system) { return res.status(403).json({ message: '系统标签不能修改' }); } if (name && name.trim() !== '') { // 检查新名称是否已存在 const [nameExists] = await getDB().execute( `SELECT id FROM address_labels WHERE name = ? AND id != ? AND (user_id IS NULL OR user_id = ?)`, [name.trim(), labelId, userId] ); if (nameExists.length > 0) { return res.status(400).json({ message: '标签名称已存在' }); } } const updateFields = []; const updateValues = []; if (name && name.trim() !== '') { updateFields.push('name = ?'); updateValues.push(name.trim()); } if (color) { updateFields.push('color = ?'); updateValues.push(color); } if (updateFields.length === 0) { return res.status(400).json({ message: '没有需要更新的字段' }); } updateFields.push('updated_at = NOW()'); updateValues.push(labelId, userId); await getDB().execute( `UPDATE address_labels SET ${updateFields.join(', ')} WHERE id = ? AND user_id = ?`, updateValues ); res.json({ success: true, message: '地址标签更新成功' }); } catch (error) { console.error('更新地址标签错误:', error); res.status(500).json({ message: '更新地址标签失败' }); } }); // 删除自定义地址标签 router.delete('/:id', auth, async (req, res) => { try { const labelId = req.params.id; const userId = req.user.id; // 检查标签是否存在且属于当前用户(不能删除系统标签) const [existing] = await getDB().execute( 'SELECT id, is_system FROM address_labels WHERE id = ? AND user_id = ?', [labelId, userId] ); if (existing.length === 0) { return res.status(404).json({ message: '地址标签不存在或无权限删除' }); } if (existing[0].is_system) { return res.status(403).json({ message: '系统标签不能删除' }); } // 检查是否有地址正在使用该标签 const [addressesUsingLabel] = await getDB().execute( 'SELECT COUNT(*) as count FROM user_addresses WHERE label_id = ? AND deleted_at IS NULL', [labelId] ); if (addressesUsingLabel[0].count > 0) { return res.status(400).json({ message: '该标签正在被使用,无法删除' }); } await getDB().execute( 'DELETE FROM address_labels WHERE id = ? AND user_id = ?', [labelId, userId] ); res.json({ success: true, message: '地址标签删除成功' }); } catch (error) { console.error('删除地址标签错误:', error); res.status(500).json({ message: '删除地址标签失败' }); } }); module.exports = router;