feat: implement admin dashboard with full CRUD functionality

This commit is contained in:
2025-06-18 16:20:19 +08:00
parent 95ef4cd617
commit 0ca2d51669
6 changed files with 324 additions and 3 deletions

View File

@ -0,0 +1,39 @@
import { defineEventHandler, readBody, setResponseStatus, createError } from 'h3';
import { db, customers } from '~/server/db';
import bcrypt from 'bcryptjs';
export default defineEventHandler(async (event) => {
const body = await readBody(event);
const { name, gender, contact, idCard, password } = body;
if (!name || !gender || !contact || !idCard || !password) {
return createError({ statusCode: 400, statusMessage: '请填写完整信息' });
}
try {
const hashedPassword = bcrypt.hashSync(password, 10);
await db.insert(customers).values({
name,
gender,
contact,
idCard,
password: hashedPassword,
});
return { message: '用户添加成功!' };
} catch (error: any) {
if (error.message?.includes('UNIQUE constraint failed')) {
if (error.message.includes('customers.contact')) {
return createError({ statusCode: 409, statusMessage: '该手机号已被注册' });
}
if (error.message.includes('customers.id_card')) {
return createError({ statusCode: 409, statusMessage: '该身份证号已被注册' });
}
}
console.error('Add user error:', error);
return createError({ statusCode: 500, statusMessage: '添加用户失败,请稍后重试' });
}
});