重构后端,使其更加权责分明

This commit is contained in:
2026-06-26 09:44:54 +08:00
parent 764919f69e
commit 89d8505260
11 changed files with 1426 additions and 761 deletions

View File

@@ -52,7 +52,8 @@ function check(label, res, expectStatus) {
} else {
failCount++
console.log(`${label} 预期 HTTP ${expectStatus}, 实际 HTTP ${res.status}`)
console.log(` 响应: ${JSON.stringify(res.body).substring(0, 300)}`)
const preview = JSON.stringify(res.body).substring(0, 300)
if (preview) console.log(` 响应: ${preview}`)
}
return res
}
@@ -70,7 +71,7 @@ function checkValue(label, actual, expected) {
// ========== 主流程 ==========
async function main() {
console.log('╔══════════════════════════════════════════════════╗')
console.log('║ 蜜雪冰城企业管理系统 — 全量接口测试 ║')
console.log('║ 蜜雪冰城企业管理系统 — 全量接口测试 V2 ║')
console.log('╚══════════════════════════════════════════════════╝')
console.log(`服务地址: ${BASE}\n`)
@@ -82,12 +83,12 @@ async function main() {
console.log('━'.repeat(55))
const accounts = [
{ username: 'admin', password: '123456', label: '系统管理员' },
{ username: 'liming', password: '123456', label: '招商经理' },
{ username: 'wangli', password: '123456', label: '运营经理' },
{ username: 'zhaoqiang', password: '123456', label: '采购经理' },
{ username: 'chenfang', password: '123456', label: '财务人员' },
{ username: 'zhangchao', password: '123456', label: '总经理' },
{ username: 'admin', password: '123456', label: '信息技术部' },
{ username: 'zhangchao', password: '123456', label: '总经理办公室' },
{ username: 'liming', password: '123456', label: '招商部' },
{ username: 'wangli', password: '123456', label: '运营部' },
{ username: 'zhaoqiang', password: '123456', label: '采购部' },
{ username: 'chenfang', password: '123456', label: '财务部' },
]
for (const acct of accounts) {
@@ -98,49 +99,56 @@ async function main() {
}
}
// 错误密码
let r = await req('POST', '/api/user/login', { username: 'admin', password: 'wrong' })
check('POST /api/user/login (错误密码)', r, 400)
check('POST /api/user/login (错误密码 → 400)', r, 400)
r = await req('POST', '/api/user/login', { username: '', password: '' })
check('POST /api/user/login (空参数)', r, 400)
check('POST /api/user/login (空参数 → 400)', r, 400)
// ══════════════════════════════════════════
// 【2】 JWT payload 验证
// 【2】 JWT payload 验证(新字段名)
// ══════════════════════════════════════════
console.log('\n' + '━'.repeat(55))
console.log('【2】 JWT payload 验证(新字段')
console.log('【2】 JWT payload 验证(departmentName / departmentDesc / permissions')
console.log('━'.repeat(55))
// 验证 admin 登录返回的 userInfo 结构
const adminLogin = await req('POST', '/api/user/login', { username: 'admin', password: '123456' })
const adminInfo = adminLogin.body?.data?.userInfo
if (adminInfo) {
checkValue('admin userInfo.name', adminInfo.name, '系统管理员')
checkValue('admin userInfo.roleName', adminInfo.roleName, 'admin')
checkValue('admin userInfo.department', adminInfo.department, '信息技术部')
check('admin userInfo.permissions 是数组', { status: Array.isArray(adminInfo.permissions) ? 200 : 500 }, 200)
checkValue('admin name', adminInfo.name, '系统管理员')
checkValue('admin departmentName', adminInfo.departmentName, 'admin')
checkValue('admin departmentDesc', adminInfo.departmentDesc, '信息技术部')
check('admin permissions 是数组', { status: Array.isArray(adminInfo.permissions) ? 200 : 500 }, 200)
checkValue('admin permissions 数量', adminInfo.permissions?.length, 25)
}
// 验证 liming招商经理的权限
const limingLogin = await req('POST', '/api/user/login', { username: 'liming', password: '123456' })
const limingInfo = limingLogin.body?.data?.userInfo
if (limingInfo) {
checkValue('liming userInfo.name', limingInfo.name, '李明')
checkValue('liming userInfo.roleName', limingInfo.roleName, 'franchise_manager')
checkValue('liming userInfo.department', limingInfo.department, '招商部')
checkValue('liming name', limingInfo.name, '李明')
checkValue('liming departmentName', limingInfo.departmentName, 'franchise_manager')
checkValue('liming departmentDesc', limingInfo.departmentDesc, '招商部')
check('liming 有 customer:read', { status: limingInfo.permissions?.includes('customer:read') ? 200 : 500 }, 200)
check('liming 有 customer:create', { status: limingInfo.permissions?.includes('customer:create') ? 200 : 500 }, 200)
check('liming 无 product:read', { status: !limingInfo.permissions?.includes('product:read') ? 200 : 500 }, 200)
check('liming 无 supplier:read', { status: !limingInfo.permissions?.includes('supplier:read') ? 200 : 500 }, 200)
check('liming 无 after_sale:read', { status: !limingInfo.permissions?.includes('after_sale:read') ? 200 : 500 }, 200)
}
// 验证 /api/user/info 返回新字段
const zhaoqiangLogin = await req('POST', '/api/user/login', { username: 'zhaoqiang', password: '123456' })
const zhaoqiangInfo = zhaoqiangLogin.body?.data?.userInfo
if (zhaoqiangInfo) {
checkValue('zhaoqiang name', zhaoqiangInfo.name, '赵强')
check('zhaoqiang 有 customer:read新增权限', { status: zhaoqiangInfo.permissions?.includes('customer:read') ? 200 : 500 }, 200)
check('zhaoqiang 有 supplier:create', { status: zhaoqiangInfo.permissions?.includes('supplier:create') ? 200 : 500 }, 200)
}
// GET /api/user/info 返回新字段
r = await req('GET', '/api/user/info', null, tokens.admin)
check('GET /api/user/info (admin)', r, 200)
if (r.body?.data) {
check('info 包含 role_name 字段', { status: r.body.data.role_name ? 200 : 500 }, 200)
check('info 包含 department 字段', { status: r.body.data.department ? 200 : 500 }, 200)
check('info 有 dept_name', { status: r.body.data.dept_name ? 200 : 500 }, 200)
check('info dept_desc', { status: r.body.data.dept_desc ? 200 : 500 }, 200)
}
// ══════════════════════════════════════════
@@ -150,222 +158,214 @@ async function main() {
console.log('【3】 权限隔离测试(无权限应返回 403')
console.log('━'.repeat(55))
// 招商经理 → products无权限
// 招商 → products无权限
r = await req('GET', '/api/products', null, tokens.liming)
check('liming → GET /api/products → 403', r, 403)
r = await req('POST', '/api/products', { name: 'x' }, tokens.liming)
check('liming → POST /api/products → 403', r, 403)
// 招商经理 → suppliers无权限
// 招商 → suppliers无权限
r = await req('GET', '/api/suppliers', null, tokens.liming)
check('liming → GET /api/suppliers → 403', r, 403)
// 招商经理 → after-sales无权限
// 招商 → after-sales无权限
r = await req('GET', '/api/after-sales', null, tokens.liming)
check('liming → GET /api/after-sales → 403', r, 403)
// 采购经理 → customers无权限
r = await req('GET', '/api/customers', null, tokens.zhaoqiang)
check('zhaoqiang → GET /api/customers → 403', r, 403)
// 运营部 → products无权限
r = await req('GET', '/api/products', null, tokens.wangli)
check('wangli → GET /api/products → 403', r, 403)
// 采购经理 → after-sales无权限
// 运营部 → suppliers无权限
r = await req('GET', '/api/suppliers', null, tokens.wangli)
check('wangli → GET /api/suppliers → 403', r, 403)
// 运营部 → contracts无权限
r = await req('GET', '/api/contracts', null, tokens.wangli)
check('wangli → GET /api/contracts → 403 (运营部无合同权限)', r, 403)
// 采购部 → after-sales无权限
r = await req('GET', '/api/after-sales', null, tokens.zhaoqiang)
check('zhaoqiang → GET /api/after-sales → 403', r, 403)
// 财务人员products无权限
r = await req('GET', '/api/products', null, tokens.chenfang)
check('chenfang → GET /api/products → 403', r, 403)
// 财务人员 → suppliers无权限
r = await req('GET', '/api/suppliers', null, tokens.chenfang)
check('chenfang → GET /api/suppliers → 403', r, 403)
// 财务人员 → 创建 customers只有 read 权限,无 create
// 财务POST customers只有 read
r = await req('POST', '/api/customers', { name: '财务创建' }, tokens.chenfang)
check('chenfang → POST /api/customers → 403', r, 403)
check('chenfang → POST /api/customers → 403 (仅可读)', r, 403)
// 财务人员创建 employees只有 read,无 create
// 财务POST employees只有 read
r = await req('POST', '/api/employees', { name: '财务创建' }, tokens.chenfang)
check('chenfang → POST /api/employees → 403', r, 403)
check('chenfang → POST /api/employees → 403 (仅可读)', r, 403)
// 总经理 → 创建 customers只有 read,无 create
// 总经理 → POST customers只有 read
r = await req('POST', '/api/customers', { name: '总经理创建' }, tokens.zhangchao)
check('zhangchao → POST /api/customers → 403', r, 403)
check('zhangchao → POST /api/customers → 403 (仅可读)', r, 403)
// 总经理 → 创建 contracts只有 read,无 create
// 总经理 → POST contracts只有 read
r = await req('POST', '/api/contracts', { contract_name: 'x', customer_id: 1 }, tokens.zhangchao)
check('zhangchao → POST /api/contracts → 403', r, 403)
check('zhangchao → POST /api/contracts → 403 (仅可读)', r, 403)
// 非管理员 → 用户管理
r = await req('GET', '/api/users', null, tokens.liming)
check('liming → GET /api/users → 403', r, 403)
check('liming → GET /api/users → 403 (非管理员)', r, 403)
// 无 token
r = await req('GET', '/api/customers', null, null)
check('无 token → GET /api/customers → 401', r, 401)
// ══════════════════════════════════════════
// 【4】 数据范围隔离测试
// 【4】 简易列表接口
// ══════════════════════════════════════════
console.log('\n' + '━'.repeat(55))
console.log('【4】 数据范围隔离测试')
console.log('【4】 简易列表接口(下拉选择用)')
console.log('━'.repeat(55))
// 4.1 加盟商范围隔离admin 创建 2 个加盟商,分别分配给 liming 和 wangli
// 获取 liming 和 wangli 的 user ID
const allUsers = await req('GET', '/api/users', null, tokens.admin)
const limingUserId = allUsers.body?.data?.list?.find(u => u.username === 'liming')?.id
const wangliUserId = allUsers.body?.data?.list?.find(u => u.username === 'wangli')?.id
r = await req('POST', '/api/customers', { name: '加盟商A-归属liming', phone: '13811111111', responsible_user_id: limingUserId }, tokens.admin)
check('admin 创建加盟商A (归属liming)', r, 200)
const custAId = r.body?.data?.id
if (custAId) created.customers.push(custAId)
r = await req('POST', '/api/customers', { name: '加盟商B-归属wangli', phone: '13822222222', responsible_user_id: wangliUserId }, tokens.admin)
check('admin 创建加盟商B (归属wangli)', r, 200)
const custBId = r.body?.data?.id
if (custBId) created.customers.push(custBId)
// liming 只能看到自己负责的
r = await req('GET', '/api/customers', null, tokens.liming)
check('liming GET /api/customers (应只看到自己的)', r, 200)
r = await req('GET', '/api/user/list', null, tokens.admin)
check('GET /api/user/list (admin)', r, 200)
if (r.body?.data) {
const limingCusts = r.body.data.list || []
const allBelongToLiming = limingCusts.every(c => c.responsible_user_id === limingUserId)
checkValue('liming 只看到自己的加盟商', allBelongToLiming ? 'YES' : 'NO', 'YES')
checkValue('liming 加盟商数量 >= 1', limingCusts.length >= 1 ? 'YES' : 'NO', 'YES')
checkValue('user/list 数量 >= 6', r.body.data.length >= 6 ? 'YES' : 'NO', 'YES')
}
// wangli 只能看到自己负责的
r = await req('GET', '/api/customers', null, tokens.wangli)
check('wangli GET /api/customers (应只看到自己的)', r, 200)
// 放宽为 auth 后,非管理员也能调
r = await req('GET', '/api/user/list', null, tokens.liming)
check('GET /api/user/list (liming auth即可)', r, 200)
r = await req('GET', '/api/employees/simple', null, tokens.liming)
check('GET /api/employees/simple (liming)', r, 200)
r = await req('GET', '/api/customers/simple', null, tokens.liming)
check('GET /api/customers/simple (liming)', r, 200)
if (r.body?.data) {
const wangliCusts = r.body.data.list || []
const allBelongToWangli = wangliCusts.every(c => c.responsible_user_id === wangliUserId)
checkValue('wangli 只看到自己的加盟商', allBelongToWangli ? 'YES' : 'NO', 'YES')
check('customers/simple 有 id+name', { status: r.body.data[0]?.id && r.body.data[0]?.name ? 200 : 500 }, 200)
}
// admin 能看到全部
r = await req('GET', '/api/suppliers/simple', null, tokens.zhaoqiang)
check('GET /api/suppliers/simple (zhaoqiang)', r, 200)
// 无 supplier:read 的看不到供应商简易列表
r = await req('GET', '/api/suppliers/simple', null, tokens.liming)
check('GET /api/suppliers/simple (liming 无权限 → 403)', r, 403)
// ══════════════════════════════════════════
// 【5】 数据范围隔离测试(过渡期规则)
// ══════════════════════════════════════════
console.log('\n' + '━'.repeat(55))
console.log('【5】 数据范围隔离测试')
console.log('━'.repeat(55))
// 5.1 加盟商:招商部看全量(过渡期)
r = await req('GET', '/api/customers', null, tokens.admin)
check('admin GET /api/customers (应看到全部)', r, 200)
check('admin GET /api/customers (全量)', r, 200)
const adminCustCount = r.body?.data?.total || 0
r = await req('GET', '/api/customers', null, tokens.liming)
check('liming GET /api/customers (全量-过渡期)', r, 200)
if (r.body?.data) {
checkValue('admin 加盟商>= 2', r.body.data.total >= 2 ? 'YES' : 'NO', 'YES')
checkValue('liming 看到加盟商数 = admin', r.body.data.total >= adminCustCount ? 'YES' : 'NO', 'YES')
}
// liming 不能看 wangli 的加盟商详情
if (custBId) {
r = await req('GET', '/api/customers/' + custBId, null, tokens.liming)
check('liming GET wangli的加盟商详情 → 404', r, 404)
r = await req('GET', '/api/customers', null, tokens.wangli)
check('wangli GET /api/customers (全量-过渡期)', r, 200)
if (r.body?.data) {
checkValue('wangli 看到加盟商数 = admin', r.body.data.total >= adminCustCount ? 'YES' : 'NO', 'YES')
}
// liming 不能修改 wangli 的加盟商
if (custBId) {
r = await req('PUT', '/api/customers/' + custBId, { name: '越权修改' }, tokens.liming)
check('liming PUT wangli的加盟商 → 404', r, 404)
// 采购部现在也能看加盟商
r = await req('GET', '/api/customers', null, tokens.zhaoqiang)
check('zhaoqiang GET /api/customers (采购部可看)', r, 200)
// 5.2 合同类型隔离
// 全量看
r = await req('GET', '/api/contracts', null, tokens.admin)
check('admin GET /api/contracts (全量)', r, 200)
// 招商部只看 franchise
r = await req('GET', '/api/contracts', null, tokens.liming)
check('liming GET /api/contracts (只看加盟合同)', r, 200)
if (r.body?.data) {
const allFranchise = (r.body.data.list || []).every(c => c.type === 'franchise')
checkValue('liming 合同全是 franchise', allFranchise ? 'YES' : 'NO', 'YES')
check('liming 有加盟合同', { status: r.body.data.total > 0 ? 200 : 500 }, 200)
}
// 4.2 员工部门隔离admin 创建 2 个不同部门的员工
r = await req('POST', '/api/employees', { name: '招商部测试员工', department: '招商部', position: '专员', status: 1 }, tokens.admin)
check('admin 创建招商部员工', r, 200)
const empLmId = r.body?.data?.id
if (empLmId) created.employees.push(empLmId)
// 采购部只看 supply
r = await req('GET', '/api/contracts', null, tokens.zhaoqiang)
check('zhaoqiang GET /api/contracts (只看采购合同)', r, 200)
if (r.body?.data) {
const allSupply = (r.body.data.list || []).every(c => c.type === 'supply')
checkValue('zhaoqiang 合同全是 supply', allSupply ? 'YES' : 'NO', 'YES')
}
r = await req('POST', '/api/employees', { name: '运营部测试员工', department: '运营部', position: '专员', status: 1 }, tokens.admin)
check('admin 创建运营部员工', r, 200)
const empWlId = r.body?.data?.id
if (empWlId) created.employees.push(empWlId)
// 运营部无合同权限(走不到 dataScope
r = await req('GET', '/api/contracts', null, tokens.wangli)
check('wangli → GET /api/contracts → 403', r, 403)
// liming 只能看到招商部员工
// 5.3 售后:运营部看全量(过渡期)
r = await req('GET', '/api/after-sales', null, tokens.wangli)
check('wangli GET /api/after-sales (全量-过渡期)', r, 200)
if (r.body?.data) {
check('wangli 看到售后数据', { status: r.body.data.total > 0 ? 200 : 500 }, 200)
}
r = await req('GET', '/api/after-sales', null, tokens.chenfang)
check('chenfang GET /api/after-sales (财务全量)', r, 200)
// 5.4 员工部门隔离
r = await req('GET', '/api/employees', null, tokens.liming)
check('liming GET /api/employees (部门隔离)', r, 200)
if (r.body?.data) {
const limingEmps = r.body.data.list || []
const allInDept = limingEmps.every(e => e.department === '招商部')
checkValue('liming 只看到招商部员工', allInDept ? 'YES' : 'NO', 'YES')
const allInDept = (r.body.data.list || []).every(e => e.department === '招商部')
checkValue('liming 只看招商部员工', allInDept ? 'YES' : 'NO', 'YES')
}
// wangli 只能看到运营部员工
r = await req('GET', '/api/employees', null, tokens.wangli)
check('wangli GET /api/employees (部门隔离)', r, 200)
if (r.body?.data) {
const wangliEmps = r.body.data.list || []
const allInDept = wangliEmps.every(e => e.department === '运营部')
checkValue('wangli 只看到运营部员工', allInDept ? 'YES' : 'NO', 'YES')
const allInDept = (r.body.data.list || []).every(e => e.department === '运营部')
checkValue('wangli 只看运营部员工', allInDept ? 'YES' : 'NO', 'YES')
}
// admin 看到全部
r = await req('GET', '/api/employees', null, tokens.admin)
check('admin GET /api/employees (应看到全部)', r, 200)
if (r.body?.data) {
checkValue('admin 员工总数 >= 2', r.body.data.total >= 2 ? 'YES' : 'NO', 'YES')
}
// 4.3 合同范围隔离
// admin 创建加盟合同(归属 liming和采购合同
r = await req('POST', '/api/contracts', {
customer_id: custAId, contract_name: '加盟合同A', amount: 100000,
effective_date: '2026-01-01', expiry_date: '2027-01-01', status: '生效',
type: 'franchise',
}, tokens.admin)
check('admin 创建加盟合同 (归属liming的客户)', r, 200)
const franchiseConId = r.body?.data?.id
if (franchiseConId) created.contracts.push(franchiseConId)
r = await req('POST', '/api/contracts', {
customer_id: custAId, contract_name: '采购合同X', amount: 200000,
effective_date: '2026-01-01', expiry_date: '2027-01-01', status: '生效',
type: 'supply',
}, tokens.admin)
check('admin 创建采购合同', r, 200)
const supplyConId = r.body?.data?.id
if (supplyConId) created.contracts.push(supplyConId)
// 采购经理只看 supply 合同
r = await req('GET', '/api/contracts', null, tokens.zhaoqiang)
check('zhaoqiang GET /api/contracts (只看supply)', r, 200)
if (r.body?.data) {
const zhaoContracts = r.body.data.list || []
const allSupply = zhaoContracts.every(c => c.type === 'supply')
checkValue('zhaoqiang 只看到supply合同', allSupply ? 'YES' : 'NO', 'YES')
}
// 4.4 after_sales 范围隔离admin 创建售后记录
r = await req('POST', '/api/after-sales', {
customer_id: custAId, feedback: '设备故障归属wangli处理',
responsible_user_id: wangliUserId, handle_status: '待处理',
}, tokens.admin)
check('admin 创建售后记录 (归属wangli)', r, 200)
const asScopeId = r.body?.data?.id
if (asScopeId) created.afterSales.push(asScopeId)
r = await req('POST', '/api/after-sales', {
customer_id: custBId, feedback: '原料质量问题,归属当前用户处理',
handle_status: '待处理',
}, tokens.wangli)
check('wangli 创建售后记录 (默认归属自己)', r, 200)
const asScopeId2 = r.body?.data?.id
if (asScopeId2) created.afterSales.push(asScopeId2)
// wangli 应该能看到 2 条售后(都是自己负责的)
r = await req('GET', '/api/after-sales', null, tokens.wangli)
check('wangli GET /api/after-sales (应看到自己负责的)', r, 200)
if (r.body?.data) {
const wangliAS = r.body.data.list || []
const allBelong = wangliAS.every(a => a.responsible_user_id === wangliUserId)
checkValue('wangli 只看到自己的售后', allBelong ? 'YES' : 'NO', 'YES')
}
// liming 没有 after_sale:read 权限,直接 403
r = await req('GET', '/api/after-sales', null, tokens.liming)
check('liming GET /api/after-sales → 403 (无权限)', r, 403)
check('admin GET /api/employees (全量)', r, 200)
// ══════════════════════════════════════════
// 【5suppliers CRUD 测试
// 【6合同类型校验中间件
// ══════════════════════════════════════════
console.log('\n' + '━'.repeat(55))
console.log('【5suppliers CRUD采购经理操作')
console.log('【6validateContractType 中间件测试')
console.log('━'.repeat(55))
// 获取一个加盟商 ID 用于测试
const custList = await req('GET', '/api/customers?pageSize=1', null, tokens.admin)
const testCustId = custList.body?.data?.list?.[0]?.id
// 招商部不能创建采购合同
r = await req('POST', '/api/contracts', {
customer_id: testCustId, contract_name: '越权采购合同',
type: 'supply', effective_date: '2026-01-01', expiry_date: '2027-01-01',
}, tokens.liming)
check('liming → POST supply合同 → 403', r, 403)
// 采购部不能创建加盟合同
r = await req('POST', '/api/contracts', {
customer_id: testCustId, contract_name: '越权加盟合同',
type: 'franchise', effective_date: '2026-01-01', expiry_date: '2027-01-01',
}, tokens.zhaoqiang)
check('zhaoqiang → POST franchise合同 → 403', r, 403)
// admin 可以创建任意类型
r = await req('POST', '/api/contracts', {
customer_id: testCustId, contract_name: 'admin测试合同',
amount: 100000, effective_date: '2026-01-01', expiry_date: '2027-01-01',
type: 'franchise', status: '生效',
}, tokens.admin)
check('admin → POST franchise合同 → 200', r, 200)
if (r.body?.data?.id) created.contracts.push(r.body.data.id)
// ══════════════════════════════════════════
// 【7】 suppliers CRUD
// ══════════════════════════════════════════
console.log('\n' + '━'.repeat(55))
console.log('【7】 suppliers CRUD采购部操作')
console.log('━'.repeat(55))
r = await req('POST', '/api/suppliers', {
@@ -377,53 +377,56 @@ async function main() {
if (supId) created.suppliers.push(supId)
r = await req('POST', '/api/suppliers', { name: '' }, tokens.zhaoqiang)
check('zhaoqiang POST /api/suppliers (缺名)', r, 400)
check('zhaoqiang POST /api/suppliers (缺名 → 400)', r, 400)
if (supId) {
r = await req('GET', '/api/suppliers/' + supId, null, tokens.zhaoqiang)
check('zhaoqiang GET /api/suppliers/:id (详情)', r, 200)
if (r.body?.data) {
checkValue('供应商名称', r.body.data.name, '柠檬供应商-测试')
}
check('GET /api/suppliers/:id (详情)', r, 200)
if (r.body?.data) checkValue('供应商名称', r.body.data.name, '柠檬供应商-测试')
r = await req('PUT', '/api/suppliers/' + supId, { phone: '13800008888', remark: '更新联系方式' }, tokens.zhaoqiang)
check('zhaoqiang PUT /api/suppliers/:id (更新)', r, 200)
r = await req('GET', '/api/suppliers', null, tokens.zhaoqiang)
check('zhaoqiang GET /api/suppliers (列表)', r, 200)
if (r.body?.data) console.log(` ↳ 供应商总数: ${r.body.data.total}`)
r = await req('PUT', '/api/suppliers/' + supId, { phone: '13800008888' }, tokens.zhaoqiang)
check('PUT /api/suppliers/:id (更新)', r, 200)
r = await req('GET', '/api/suppliers?name=柠檬', null, tokens.zhaoqiang)
check('zhaoqiang GET /api/suppliers?name=柠檬 (搜索)', r, 200)
check('GET /api/suppliers?name=柠檬 (搜索)', r, 200)
}
// ══════════════════════════════════════════
// 【6】 users CRUD 测试(适配新字段
// 【8】 users CRUD(新字段 department_id
// ══════════════════════════════════════════
console.log('\n' + '━'.repeat(55))
console.log('【6】 users CRUD管理员操作')
console.log('【8】 users CRUD管理员操作,新字段 department_id')
console.log('━'.repeat(55))
r = await req('GET', '/api/users', null, tokens.admin)
check('admin GET /api/users (列表)', r, 200)
if (r.body?.data) console.log(` ↳ 用户总数: ${r.body.data.total}`)
if (r.body?.data) {
console.log(` ↳ 用户总数: ${r.body.data.total}`)
// 验证新字段
const sample = r.body.data.list[0]
check('用户有 dept_name', { status: sample?.dept_name ? 200 : 500 }, 200)
check('用户有 dept_desc', { status: sample?.dept_desc ? 200 : 500 }, 200)
check('用户有 real_name (employees联查)', { status: sample?.real_name ? 200 : 500 }, 200)
}
// 创建新用户,关联到已有员工
// 管理员查自己 ID
const allUsers = await req('GET', '/api/users', null, tokens.admin)
const adminUser = allUsers.body?.data?.list?.find(u => u.username === 'admin')
const adminUserId = adminUser?.id
// 创建新用户(用新字段 department_id
r = await req('POST', '/api/users', {
username: 'testuser_' + Date.now(),
password: 'pass123456',
role_id: 3, // franchise_manager
employee_id: empLmId,
department: '招商部',
department_id: 3, // franchise_manager 的 ID
}, tokens.admin)
check('admin POST /api/users (创建带role_id+employee_id)', r, 200)
check('admin POST /api/users (创建,新字段)', r, 200)
const newUserId = r.body?.data?.id
if (newUserId) {
created.users.push(newUserId)
if (r.body?.data) {
check('新用户有 role_name', { status: r.body.data.role_name ? 200 : 500 }, 200)
check('新用户有 real_name (从employees)', { status: r.body.data.real_name ? 200 : 500 }, 200)
console.log(` ↳ 新用户: ${r.body.data.username}, 角色: ${r.body.data.role_name}, 姓名: ${r.body.data.real_name}`)
check('新用户有 dept_name', { status: r.body.data.dept_name ? 200 : 500 }, 200)
check('新用户有 dept_desc', { status: r.body.data.dept_desc ? 200 : 500 }, 200)
}
r = await req('GET', '/api/users/' + newUserId, null, tokens.admin)
@@ -431,28 +434,71 @@ async function main() {
r = await req('PUT', '/api/users/' + newUserId, { is_active: 0 }, tokens.admin)
check('admin PUT /api/users/:id (禁用)', r, 200)
if (r.body?.data) checkValue('is_active 已禁用', r.body.data.is_active, 0)
r = await req('PUT', '/api/users/' + newUserId, { is_active: 1, password: 'newpass123' }, tokens.admin)
check('admin PUT /api/users/:id (启用+重置密码)', r, 200)
check('admin PUT /api/users/:id (启用+改密)', r, 200)
}
// admin 不能删除自己(先获取 admin 的真实 ID
const adminUser = allUsers.body?.data?.list?.find(u => u.username === 'admin')
const adminUserId = adminUser?.id
// admin 不能删除自己
r = await req('DELETE', '/api/users/' + adminUserId, null, tokens.admin)
check('admin DELETE /api/users/' + adminUserId + ' (删自己) → 400', r, 400)
check('admin DELETE 自己 → 400', r, 400)
// admin 不能禁用自己
r = await req('PUT', '/api/users/' + adminUserId, { is_active: 0 }, tokens.admin)
check('admin PUT /api/users/' + adminUserId + ' (禁用自己) → 400', r, 400)
check('admin PUT 禁用自己 → 400', r, 400)
// admin 不能改自己部门
r = await req('PUT', '/api/users/' + adminUserId, { department_id: 3 }, tokens.admin)
check('admin PUT 改自己部门 → 400', r, 400)
// 按部门筛选用户
r = await req('GET', '/api/users?department_id=1', null, tokens.admin)
check('GET /api/users?department_id=1 (筛选admin部门)', r, 200)
// ══════════════════════════════════════════
// 【7customers CRUD 测试
// 【9allowUserCreation创建员工同时建用户
// ══════════════════════════════════════════
console.log('\n' + '━'.repeat(55))
console.log('【7customers CRUD')
console.log('【9allowUserCreation 中间件测试')
console.log('━'.repeat(55))
// 管理员创建员工同时建账号
r = await req('POST', '/api/employees', {
name: '测试员工-带账号', gender: '男', department: '招商部',
position: '专员', status: 1,
username: 'testemp_' + Date.now(),
password: 'pass123456',
department_id: 3, // franchise_manager
}, tokens.admin)
check('admin POST /api/employees (同步建用户)', r, 200)
if (r.body?.data?.id) created.employees.push(r.body.data.id)
// 非管理员创建员工不能带用户字段
r = await req('POST', '/api/employees', {
name: '测试员工-越权', department: '招商部',
username: 'hackuser', password: 'pass123', department_id: 3,
}, tokens.liming)
check('liming POST /api/employees 带 user 字段 → 403', r, 403)
// 管理员不带用户字段 → 只创建员工
r = await req('POST', '/api/employees', {
name: '测试员工-纯员工', department: '招商部', position: '专员', status: 1,
}, tokens.admin)
check('admin POST /api/employees (不建用户)', r, 200)
if (r.body?.data?.id) created.employees.push(r.body.data.id)
// 管理员建用户但缺字段
r = await req('POST', '/api/employees', {
name: '测试员工-缺字段', department: '招商部',
username: 'testuser2', // 缺 password 和 department_id
}, tokens.admin)
check('admin POST /api/employees (建用户缺字段 → 400)', r, 400)
// ══════════════════════════════════════════
// 【10】 customers CRUD
// ══════════════════════════════════════════
console.log('\n' + '━'.repeat(55))
console.log('【10】 customers CRUD')
console.log('━'.repeat(55))
r = await req('POST', '/api/customers', {
@@ -463,17 +509,18 @@ async function main() {
const custCrudId = r.body?.data?.id
if (custCrudId) {
created.customers.push(custCrudId)
checkValue('默认 responsible_user_id = admin', r.body.data.responsible_user_id, adminUserId)
check('客户创建成功', { status: r.body.data.id ? 200 : 500 }, 200)
}
r = await req('POST', '/api/customers', { name: '' }, tokens.admin)
check('POST /api/customers (缺名)', r, 400)
check('POST /api/customers (缺名 → 400)', r, 400)
if (custCrudId) {
r = await req('GET', '/api/customers/' + custCrudId, null, tokens.admin)
check('GET /api/customers/:id (详情)', r, 200)
if (r.body?.data) checkValue('加盟商名', r.body.data.name, 'CRUD测试加盟商')
r = await req('PUT', '/api/customers/' + custCrudId, { phone: '13900006666', remark: '更新备注' }, tokens.admin)
r = await req('PUT', '/api/customers/' + custCrudId, { phone: '13900006666', remark: '更新' }, tokens.admin)
check('PUT /api/customers/:id (更新)', r, 200)
r = await req('GET', '/api/customers?name=CRUD', null, tokens.admin)
@@ -481,13 +528,13 @@ async function main() {
}
r = await req('GET', '/api/customers/99999', null, tokens.admin)
check('GET /api/customers/99999 (不存在)', r, 404)
check('GET /api/customers/99999 (不存在 → 404)', r, 404)
// ══════════════════════════════════════════
// 【8】 employees CRUD 测试
// 【11】 employees CRUD
// ══════════════════════════════════════════
console.log('\n' + '━'.repeat(55))
console.log('【8】 employees CRUD')
console.log('【11】 employees CRUD')
console.log('━'.repeat(55))
r = await req('POST', '/api/employees', {
@@ -503,21 +550,18 @@ async function main() {
r = await req('GET', '/api/employees/' + empCrudId, null, tokens.admin)
check('GET /api/employees/:id (详情)', r, 200)
r = await req('PUT', '/api/employees/' + empCrudId, { salary: 15000, position: '高级品控专员' }, tokens.admin)
r = await req('PUT', '/api/employees/' + empCrudId, { salary: 15000, position: '高级专员' }, tokens.admin)
check('PUT /api/employees/:id (更新)', r, 200)
r = await req('GET', '/api/employees?status=1', null, tokens.admin)
check('GET /api/employees?status=1 (在职筛选)', r, 200)
r = await req('GET', '/api/employees?department=品控部', null, tokens.admin)
check('GET /api/employees?department=品控部 (部门筛选)', r, 200)
check('GET /api/employees?department=品控部 (筛选)', r, 200)
}
// ══════════════════════════════════════════
// 【9】 products CRUD 测试
// 【12】 products CRUD
// ══════════════════════════════════════════
console.log('\n' + '━'.repeat(55))
console.log('【9】 products CRUD采购经理操作)')
console.log('【12】 products CRUD采购操作)')
console.log('━'.repeat(55))
r = await req('POST', '/api/products', {
@@ -529,7 +573,7 @@ async function main() {
if (prodId) created.products.push(prodId)
r = await req('POST', '/api/products', { name: '' }, tokens.zhaoqiang)
check('POST /api/products (缺名)', r, 400)
check('POST /api/products (缺名 → 400)', r, 400)
if (prodId) {
r = await req('GET', '/api/products/' + prodId, null, tokens.zhaoqiang)
@@ -540,72 +584,66 @@ async function main() {
r = await req('GET', '/api/products?name=柠檬', null, tokens.zhaoqiang)
check('GET /api/products?name=柠檬 (搜索)', r, 200)
r = await req('GET', '/api/products?type=原材料', null, tokens.zhaoqiang)
check('GET /api/products?type=原材料 (筛选)', r, 200)
}
// ══════════════════════════════════════════
// 【10】 contracts CRUD 测试
// 【13】 contracts CRUD
// ══════════════════════════════════════════
console.log('\n' + '━'.repeat(55))
console.log('【10】 contracts CRUD(含 type 字段)')
console.log('【13】 contracts CRUD')
console.log('━'.repeat(55))
// 采购经理创建合同 → 自动 type='supply'
// 招商部正确创建加盟合同
r = await req('POST', '/api/contracts', {
customer_id: custAId, contract_name: '原材料采购合同', amount: 500000,
customer_id: custCrudId, contract_name: '加盟协议-测试', amount: 300000,
effective_date: '2026-06-01', expiry_date: '2027-05-31', status: '生效',
}, tokens.zhaoqiang)
check('zhaoqiang POST /api/contracts (自动type=supply)', r, 200)
}, tokens.liming)
check('liming POST /api/contracts (加盟合同)', r, 200)
if (r.body?.data) {
checkValue('合同type自动设为supply', r.body.data.type, 'supply')
created.contracts.push(r.body.data.id)
}
// 管理员创建合同 → 默认 type='franchise'
r = await req('POST', '/api/contracts', {
customer_id: custAId, contract_name: '加盟协议-测试', amount: 300000,
effective_date: '2026-06-01', expiry_date: '2027-05-31', status: '生效',
employee_id: empLmId,
}, tokens.admin)
check('admin POST /api/contracts (默认type=franchise)', r, 200)
if (r.body?.data) {
checkValue('合同type默认franchise', r.body.data.type, 'franchise')
checkValue('合同type自动=franchise', r.body.data.type, 'franchise')
check('合同有 customer_name 联查', { status: r.body.data.customer_name ? 200 : 500 }, 200)
created.contracts.push(r.body.data.id)
}
r = await req('POST', '/api/contracts', { customer_id: 99999, contract_name: '无效' }, tokens.admin)
check('POST /api/contracts (客户不存在)', r, 400)
// 采购部正确创建采购合同(需要 supplier_id
r = await req('POST', '/api/contracts', {
customer_id: custCrudId, contract_name: '采购合同-测试', amount: 200000,
effective_date: '2026-06-01', expiry_date: '2027-05-31',
type: 'supply', status: '生效', supplier_id: 1,
}, tokens.zhaoqiang)
check('zhaoqiang POST /api/contracts (采购合同)', r, 200)
if (r.body?.data) {
checkValue('合同type=supply', r.body.data.type, 'supply')
created.contracts.push(r.body.data.id)
}
r = await req('GET', '/api/contracts', null, tokens.admin)
check('GET /api/contracts (列表)', r, 200)
r = await req('POST', '/api/contracts', { customer_id: 99999, contract_name: '无效' }, tokens.admin)
check('POST /api/contracts (客户不存在 → 400)', r, 400)
// ══════════════════════════════════════════
// 【11】 after-sales CRUD 测试
// 【14】 after-sales CRUD
// ══════════════════════════════════════════
console.log('\n' + '━'.repeat(55))
console.log('【11】 after-sales CRUD')
console.log('【14】 after-sales CRUD')
console.log('━'.repeat(55))
r = await req('POST', '/api/after-sales', {
customer_id: custAId, feedback: '冰淇淋机不出料',
employee_id: empWlId, handle_method: '派工程师上门检修',
handle_status: '处理中', service_date: '2026-06-20',
customer_id: custCrudId, feedback: '冰淇淋机不出料',
handle_method: '派工程师上门', handle_status: '处理中',
service_date: '2026-06-20',
}, tokens.wangli)
check('wangli POST /api/after-sales (创建)', r, 200)
const asId = r.body?.data?.id
if (asId) {
created.afterSales.push(asId)
check('售后有 customer_name 联查', { status: r.body.data.customer_name ? 200 : 500 }, 200)
check('售后有 customer_name', { status: r.body.data.customer_name ? 200 : 500 }, 200)
}
r = await req('POST', '/api/after-sales', { customer_id: 99999, feedback: '测试' }, tokens.wangli)
check('POST /api/after-sales (客户不存在)', r, 400)
check('POST /api/after-sales (客户不存在 → 400)', r, 400)
r = await req('POST', '/api/after-sales', { customer_id: custAId }, tokens.wangli)
check('POST /api/after-sales (缺feedback)', r, 400)
r = await req('POST', '/api/after-sales', { customer_id: custCrudId }, tokens.wangli)
check('POST /api/after-sales (缺feedback → 400)', r, 400)
if (asId) {
r = await req('GET', '/api/after-sales/' + asId, null, tokens.wangli)
@@ -619,32 +657,31 @@ async function main() {
}
// ══════════════════════════════════════════
// 【12】 个人信息 & 改密
// 【15】 个人信息 & 改密
// ══════════════════════════════════════════
console.log('\n' + '━'.repeat(55))
console.log('【12】 个人信息 & 改密')
console.log('【15】 个人信息 & 改密')
console.log('━'.repeat(55))
r = await req('POST', '/api/user/logout', null, tokens.admin)
check('POST /api/user/logout', r, 200)
r = await req('PUT', '/api/user/password', { oldPassword: 'wrong', newPassword: 'newpwd999' }, tokens.admin)
check('PUT /api/user/password (旧密码错)', r, 400)
check('PUT /api/user/password (旧密码错 → 400)', r, 400)
r = await req('PUT', '/api/user/password', { oldPassword: '123456', newPassword: '123456' }, tokens.admin)
check('PUT /api/user/password (新旧相同)', r, 400)
check('PUT /api/user/password (新旧相同 → 400)', r, 400)
r = await req('PUT', '/api/user/password', { oldPassword: '123456', newPassword: '123' }, tokens.admin)
check('PUT /api/user/password (新密码太短)', r, 400)
check('PUT /api/user/password (新密码太短 → 400)', r, 400)
// ══════════════════════════════════════════
// 【13】 数据清理
// 【16】 数据清理
// ══════════════════════════════════════════
console.log('\n' + '━'.repeat(55))
console.log('【13】 测试数据清理')
console.log('【16】 测试数据清理')
console.log('━'.repeat(55))
// 按依赖顺序删除
for (const id of created.afterSales) {
await req('DELETE', '/api/after-sales/' + id, null, tokens.admin)
}