52 lines
1.2 KiB
TypeScript
52 lines
1.2 KiB
TypeScript
/**
|
|
* 全局搜索路由 — 员工、页面、功能搜索
|
|
*/
|
|
import { Router } from 'express'
|
|
import prisma from '../lib/prisma'
|
|
import { authMiddleware, AuthRequest } from '../middleware/auth'
|
|
|
|
const router = Router()
|
|
|
|
/**
|
|
* GET /search?q=keyword
|
|
* 全局搜索:员工、部门等
|
|
*/
|
|
router.get('/', authMiddleware, async (req: AuthRequest, res, next) => {
|
|
try {
|
|
const q = (req.query.q as string || '').trim()
|
|
if (!q || q.length < 1) {
|
|
return res.json({ success: true, data: { employees: [] } })
|
|
}
|
|
|
|
if (!req.user) {
|
|
return res.status(401).json({ success: false, message: '未授权' })
|
|
}
|
|
const orgId = req.user.orgId
|
|
|
|
// 搜索员工(按姓名、工号、手机号)
|
|
const employees = await prisma.employee.findMany({
|
|
where: {
|
|
orgId,
|
|
OR: [
|
|
{ name: { contains: q } },
|
|
{ employeeNo: { contains: q } },
|
|
{ phone: { contains: q } },
|
|
],
|
|
status: { notIn: ['TERMINATED'] },
|
|
},
|
|
select: {
|
|
id: true,
|
|
name: true,
|
|
department: true,
|
|
position: true,
|
|
employeeNo: true,
|
|
},
|
|
take: 10,
|
|
})
|
|
|
|
res.json({ success: true, data: { employees } })
|
|
} catch (err) { next(err) }
|
|
})
|
|
|
|
export default router
|