Skip to content

Commit 3fc7d4d

Browse files
feat(mock): Add mock
1 parent 357fc44 commit 3fc7d4d

File tree

20 files changed

+354
-67
lines changed

20 files changed

+354
-67
lines changed

mock/_createProductionServer.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { createProdMockServer } from 'vite-plugin-mock/es/createProdMockServer'
2+
3+
const modules = import.meta.globEager('./**/*.ts')
4+
5+
const mockModules: any[] = []
6+
Object.keys(modules).forEach((key) => {
7+
if (key.includes('_')) {
8+
return
9+
}
10+
mockModules.push(...modules[key].default)
11+
})
12+
13+
export function setupProdMockServer() {
14+
createProdMockServer(mockModules)
15+
}

mock/user/index.ts

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import { config } from '@/config/axios'
2+
import { MockMethod } from 'vite-plugin-mock'
3+
4+
const { result_code } = config
5+
6+
const List: {
7+
username: string
8+
password: string
9+
role: string
10+
roleId: string
11+
}[] = [
12+
{
13+
username: 'admin',
14+
password: 'admin',
15+
role: 'admin',
16+
roleId: '1'
17+
},
18+
{
19+
username: 'test',
20+
password: 'test',
21+
role: 'test',
22+
roleId: '2'
23+
}
24+
]
25+
26+
export default [
27+
// 登录接口
28+
{
29+
url: '/user/login',
30+
method: 'post',
31+
response: ({ body }) => {
32+
const data = body
33+
let hasUser = false
34+
for (const user of List) {
35+
if (user.username === data.username && user.password === data.password) {
36+
hasUser = true
37+
return {
38+
code: result_code,
39+
data: user
40+
}
41+
}
42+
}
43+
if (!hasUser) {
44+
return {
45+
code: '500',
46+
message: '账号或密码错误'
47+
}
48+
}
49+
}
50+
}
51+
] as MockMethod[]

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,12 @@
2929
"@vueuse/core": "^7.5.1",
3030
"@zxcvbn-ts/core": "^1.2.0",
3131
"animate.css": "^4.1.1",
32+
"axios": "^0.24.0",
3233
"element-plus": "1.3.0-beta.1",
3334
"lodash-es": "^4.17.21",
3435
"mockjs": "^1.1.0",
3536
"pinia": "^2.0.9",
37+
"qs": "^6.10.2",
3638
"vue": "3.2.26",
3739
"vue-i18n": "9.1.9",
3840
"vue-router": "^4.0.12",

pnpm-lock.yaml

Lines changed: 83 additions & 45 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)