/** * @file 样例业务逻辑服务 * @author hotok * @date 2025-06-29 * @lastEditor hotok * @lastEditTime 2025-06-29 * @description 样例接口的业务逻辑实现 * * 设计思路: * 1. 接收用户名参数,对参数进行基础校验 * 2. 使用Drizzle ORM查询数据库中的用户信息 * 3. 处理查询结果:用户存在则返回用户信息,不存在则抛出业务异常 * 4. 对敏感信息进行过滤,不返回密码哈希等敏感字段 * 5. 统一异常处理,确保返回标准的错误响应格式 * 6. 记录操作日志,便于系统监控和问题排查 * * 安全考虑: * - 严格过滤敏感信息,不向客户端返回密码哈希 * - 对查询参数进行SQL注入防护(Drizzle ORM自带防护) * - 记录查询日志,便于安全审计 */ import { eq } from 'drizzle-orm'; import { db } from '@/plugins/drizzle/drizzle.service'; import { users } from '@/eneities/users'; import { ERROR_CODES } from '@/validators/global.response'; import { type GetUserByUsernameType } from './example.schema'; import type { JwtUserType } from '@/type/jwt.type'; /** * 样例服务类 * @description 提供用户相关的业务逻辑处理 */ export class ExampleService { async findUserByUsername({ params, user }: { params: GetUserByUsernameType; user: JwtUserType }) { const { username } = params; user; // 使用Drizzle ORM查询用户信息 const userList = await db() .select({ id: users.id, username: users.username, email: users.email, nickname: users.nickname, avatar: users.avatar, status: users.status, createdAt: users.createdAt, updatedAt: users.updatedAt, }) .from(users) .where(eq(users.username, username)) .limit(1); // 检查查询结果 if (!userList || userList.length === 0) { return { code: 400 as const, message: '用户不存在', data: null, }; } const userInfo = userList[0]!; // 返回成功响应 return { code: ERROR_CODES.SUCCESS, message: '查询用户成功', data: { id: userInfo.id, username: userInfo.username, email: userInfo.email, nickname: userInfo.nickname || undefined, avatar: userInfo.avatar || undefined, status: userInfo.status, createdAt: userInfo.createdAt.toISOString(), updatedAt: userInfo.updatedAt.toISOString(), }, }; } } /** * 导出样例服务实例 */ export const exampleService = new ExampleService();