feat(mock): Add mock

This commit is contained in:
kailong321200875
2022-01-08 18:38:20 +08:00
parent 357fc44e51
commit 3fc7d4d39a
20 changed files with 354 additions and 67 deletions

View File

@@ -0,0 +1,15 @@
import { createProdMockServer } from 'vite-plugin-mock/es/createProdMockServer'
const modules = import.meta.globEager('./**/*.ts')
const mockModules: any[] = []
Object.keys(modules).forEach((key) => {
if (key.includes('_')) {
return
}
mockModules.push(...modules[key].default)
})
export function setupProdMockServer() {
createProdMockServer(mockModules)
}

51
mock/user/index.ts Normal file
View File

@@ -0,0 +1,51 @@
import { config } from '@/config/axios'
import { MockMethod } from 'vite-plugin-mock'
const { result_code } = config
const List: {
username: string
password: string
role: string
roleId: string
}[] = [
{
username: 'admin',
password: 'admin',
role: 'admin',
roleId: '1'
},
{
username: 'test',
password: 'test',
role: 'test',
roleId: '2'
}
]
export default [
// 登录接口
{
url: '/user/login',
method: 'post',
response: ({ body }) => {
const data = body
let hasUser = false
for (const user of List) {
if (user.username === data.username && user.password === data.password) {
hasUser = true
return {
code: result_code,
data: user
}
}
}
if (!hasUser) {
return {
code: '500',
message: '账号或密码错误'
}
}
}
}
] as MockMethod[]