This commit is contained in:
2026-10-25 23:20:48 +08:00
parent 4e9f3dcb13
commit c178ea5160
5 changed files with 3492 additions and 37 deletions
+30 -29
View File
@@ -1,10 +1,10 @@
import { dashboardSummary, menuRoutes, USER_STATUS, users } from './data'
import { dashboardSummary, menuRoutes, USER_STATUS, users, type MockUser } from './data'
export interface MockContext {
url: string
params: string[]
query: Record<string, string>
body: Record<string, any>
body: Record<string, unknown>
}
export interface MockRule {
@@ -18,22 +18,19 @@ const toNumber = (value: unknown, fallback = 0) => {
return Number.isFinite(n) ? n : fallback
}
const pickString = (value: unknown, fallback = '') => (typeof value === 'string' ? value : fallback)
export const mockRules: MockRule[] = [
{
method: 'POST',
pattern: /^\/api\/v1\/auth\/login$/,
handler: (ctx) => {
const { username, password } = ctx.body
if (!username || !password) {
return { code: 'A0120', msg: '用户名和密码不能为空' }
}
return {
accessToken: 'mock-access-token',
refreshToken: 'mock-refresh-token',
tokenType: 'Bearer',
expiresIn: 7200,
}
},
handler: (ctx) => ({
accessToken: 'mock-access-token',
refreshToken: 'mock-refresh-token',
tokenType: 'Bearer',
expiresIn: 7200,
username: pickString(ctx.body.username, 'admin'),
}),
},
{
method: 'POST',
@@ -61,17 +58,18 @@ export const mockRules: MockRule[] = [
method: 'GET',
pattern: /^\/api\/v1\/users\/page$/,
handler: (ctx) => {
const { username, status, pageNum = '1', pageSize = '10' } = ctx.query
const { username, status } = ctx.query
const pageNum = toNumber(ctx.query.pageNum, 1)
const pageSize = toNumber(ctx.query.pageSize, 10)
const filtered = users.filter((item) => {
const matchName = !username || item.username.includes(username)
const matchStatus = !status || String(item.status) === status
return matchName && matchStatus
})
const start = (toNumber(pageNum, 1) - 1) * toNumber(pageSize, 10)
return {
list: filtered.slice(start, start + toNumber(pageSize, 10)),
total: filtered.length,
}
const start = (pageNum - 1) * pageSize
return { list: filtered.slice(start, start + pageSize), total: filtered.length }
},
},
{
@@ -84,17 +82,20 @@ export const mockRules: MockRule[] = [
pattern: /^\/api\/v1\/users$/,
handler: (ctx) => {
const id = users.length + 1
users.unshift({
const created: MockUser = {
id,
username: ctx.body.username ?? `user${id}`,
nickname: ctx.body.nickname ?? '',
mobile: ctx.body.mobile ?? '',
email: ctx.body.email ?? '',
status: ctx.body.status ?? USER_STATUS.ENABLED,
deptName: ctx.body.deptName ?? '技术部',
roleNames: ctx.body.roleNames ?? ['普通用户'],
username: pickString(ctx.body.username, `user${id}`),
nickname: pickString(ctx.body.nickname),
mobile: pickString(ctx.body.mobile),
email: pickString(ctx.body.email),
status: toNumber(ctx.body.status, USER_STATUS.ENABLED) === 0 ? 0 : 1,
deptName: pickString(ctx.body.deptName, '技术部'),
roleNames: Array.isArray(ctx.body.roleNames)
? (ctx.body.roleNames as string[])
: ['普通用户'],
createTime: new Date().toISOString(),
})
}
users.unshift(created)
return id
},
},