Skip to content

Commit 24ba414

Browse files
authored
Merge pull request #1 from XPoet/dev
Dev
2 parents 93287d1 + 084532c commit 24ba414

File tree

9 files changed

+217
-24
lines changed

9 files changed

+217
-24
lines changed

.eslintrc.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ module.exports = {
2222
'import/no-absolute-path': 'off',
2323
'import/no-extraneous-dependencies': 'off',
2424
'vue/no-multiple-template-root': 'off',
25+
'no-console': 'off',
2526
'no-param-reassign': [
2627
'error',
2728
{

mock/test.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { MockMethod } from 'vite-plugin-mock'
2+
3+
const mockArr = [
4+
{
5+
url: '/api/get',
6+
method: 'get',
7+
response: (res: { query: any }) => {
8+
return {
9+
code: 0,
10+
data: {
11+
name: res.query
12+
}
13+
}
14+
}
15+
},
16+
{
17+
url: '/api/post',
18+
method: 'post',
19+
timeout: 2000,
20+
response: {
21+
code: 0,
22+
data: {
23+
name: 'vben'
24+
}
25+
}
26+
},
27+
{
28+
url: '/api/test',
29+
method: 'get',
30+
response: (res: { body: any }) => {
31+
console.log('mock funciton: /api/test/ ----', res)
32+
return {
33+
code: 0,
34+
message: 'ok',
35+
data: {
36+
name: 'Mock'
37+
}
38+
}
39+
}
40+
}
41+
]
42+
43+
export default mockArr as MockMethod[]

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
"dependencies": {
4242
"axios": "^0.21.1",
4343
"element-plus": "^1.0.2-beta.36",
44+
"mockjs": "^1.1.0",
4445
"vue": "^3.0.5",
4546
"vue-router": "^4.0.6",
4647
"vuex": "^4.0.0"
@@ -72,6 +73,7 @@
7273
"ts-jest": "^26.5.4",
7374
"typescript": "^4.1.3",
7475
"vite": "^2.1.5",
76+
"vite-plugin-mock": "^2.5.0",
7577
"vue-jest": "^5.0.0-alpha.7",
7678
"vue-tsc": "^0.0.15"
7779
}

src/components/Nav.vue

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ export default defineComponent({
4242
isActive: false,
4343
path: '/axios'
4444
},
45+
{
46+
name: 'Mock',
47+
isActive: false,
48+
path: '/mock'
49+
},
4550
{
4651
name: 'Test',
4752
isActive: false,

src/mockProdServer.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { createProdMockServer } from 'vite-plugin-mock/es/createProdMockServer'
2+
3+
// import your mock.ts
4+
// you can import only one file if you use vite.mock.config.ts
5+
// aslo can import.meta.glob to import all files
6+
import testModule from '../mock/test'
7+
8+
// eslint-disable-next-line import/prefer-default-export
9+
export function setupProdMockServer() {
10+
createProdMockServer([...testModule])
11+
}

src/router/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ const routes: Array<RouteRecordRaw> = [
2323
path: '/test',
2424
name: 'Test',
2525
component: Test
26+
},
27+
{
28+
path: '/mock',
29+
name: 'Mock',
30+
component: () => import('@/views/Mock.vue')
2631
}
2732
]
2833

src/utils/axios.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,25 @@ const axios = Axios.create({
88
timeout: 20000 // 请求超时 20s
99
})
1010

11+
declare module 'axios' {
12+
// eslint-disable-next-line no-unused-vars
13+
interface AxiosRequestConfig {
14+
// add mock switch
15+
isMock?: boolean
16+
}
17+
}
18+
1119
// 前置拦截器(发起请求之前的拦截)
1220
axios.interceptors.request.use(
1321
(response) => {
1422
/**
1523
* 根据你的项目实际情况来对 config 做处理
16-
* 这里对 config 不做任何处理,直接返回
1724
*/
25+
// mock switch
26+
if (response.isMock) {
27+
// eslint-disable-next-line no-param-reassign
28+
response.baseURL = ''
29+
}
1830
return response
1931
},
2032
(error) => {

src/views/Mock.vue

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
<template>
2+
<div class="mock-container page-container">
3+
<div class="page-title">Axios Test Page</div>
4+
<div class="user-info-container">
5+
<el-card class="box-card">
6+
<template #header>
7+
<div class="card-header">
8+
<span>Mock</span>
9+
<el-button class="button" type="text" @click="getTestInfo"
10+
>Click to get mock
11+
</el-button>
12+
</div>
13+
</template>
14+
<div class="info-list-box" v-loading="loading">
15+
<div class="text item" v-if="mockInfo?.code">code: {{ mockInfo?.code }}</div>
16+
<div class="text item" v-if="mockInfo?.data">data: {{ mockInfo?.data }}</div>
17+
<div class="text item" v-if="mockInfo?.message">
18+
msg: {{ mockInfo?.message }}
19+
</div>
20+
</div>
21+
</el-card>
22+
</div>
23+
</div>
24+
</template>
25+
26+
<script lang="ts">
27+
import { defineComponent, ref, Ref } from 'vue'
28+
import axios from '../utils/axios'
29+
30+
export default defineComponent({
31+
name: 'Mock',
32+
setup() {
33+
const mockInfo: Ref = ref(null)
34+
const loading = ref(false)
35+
36+
const getTestInfo = () => {
37+
loading.value = true
38+
axios({
39+
method: 'get',
40+
url: '/api/test',
41+
isMock: true
42+
})
43+
.then((response) => {
44+
mockInfo.value = response.data
45+
loading.value = false
46+
})
47+
.catch((error) => {
48+
loading.value = false
49+
console.error(error)
50+
})
51+
}
52+
53+
return {
54+
mockInfo,
55+
loading,
56+
getTestInfo
57+
}
58+
}
59+
})
60+
</script>
61+
62+
<style scoped lang="stylus">
63+
64+
.axios-container {
65+
66+
.user-info-container {
67+
display flex
68+
justify-content center
69+
width 100%
70+
71+
.info-list-box {
72+
padding 10px
73+
74+
.text {
75+
font-size: 14px;
76+
}
77+
78+
.item {
79+
margin-bottom: 18px;
80+
}
81+
82+
}
83+
84+
.card-header {
85+
display: flex;
86+
justify-content: space-between;
87+
align-items: center;
88+
}
89+
90+
.box-card {
91+
width: 480px;
92+
}
93+
94+
}
95+
96+
}
97+
</style>

vite.config.ts

Lines changed: 40 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,47 @@
1-
import { defineConfig } from 'vite'
1+
import { UserConfigExport, ConfigEnv } from 'vite'
22
import vue from '@vitejs/plugin-vue'
3+
import { viteMockServe } from 'vite-plugin-mock'
34
// 如果编辑器提示 path 模块找不到,则可以安装一下 @types/node -> npm i @types/node -D
45
import { resolve } from 'path'
56

67
// https://vitejs.dev/config/
7-
export default defineConfig({
8-
plugins: [vue()],
9-
resolve: {
10-
alias: {
11-
'@': resolve(__dirname, 'src') // 设置 `@` 指向 `src` 目录
12-
}
13-
},
14-
base: './', // 设置打包路径
15-
server: {
16-
port: 4500, // 设置服务启动端口号
17-
open: true, // 设置服务启动时是否自动打开浏览器
18-
cors: true // 允许跨域
8+
export default ({ command }: ConfigEnv): UserConfigExport => {
9+
const prodMock = true
10+
return {
11+
plugins: [
12+
vue(),
13+
viteMockServe({
14+
mockPath: 'mock',
15+
logger: true,
16+
localEnabled: command === 'serve',
17+
prodEnabled: command !== 'serve' && prodMock,
18+
// 这样可以控制关闭mock的时候不让mock打包到最终代码内
19+
injectCode: `
20+
import { setupProdMockServer } from './mockProdServer';
21+
setupProdMockServer();
22+
`
23+
})
24+
],
25+
resolve: {
26+
alias: {
27+
'@': resolve(__dirname, 'src') // 设置 `@` 指向 `src` 目录
28+
}
29+
},
30+
base: './', // 设置打包路径
31+
server: {
32+
port: 4500, // 设置服务启动端口号
33+
open: true, // 设置服务启动时是否自动打开浏览器
34+
cors: true // 允许跨域
1935

20-
// 设置代理,根据我们项目实际情况配置
21-
// proxy: {
22-
// '/api': {
23-
// target: 'http://xxx.xxx.xxx.xxx:x000',
24-
// changeOrigin: true,
25-
// secure: false,
26-
// rewrite: (path) => path.replace('/api/', '/')
27-
// }
28-
// },
36+
// 设置代理,根据我们项目实际情况配置
37+
// proxy: {
38+
// '/api': {
39+
// target: 'http://xxx.xxx.xxx.xxx:x000',
40+
// changeOrigin: true,
41+
// secure: false,
42+
// rewrite: (path) => path.replace('/api/', '/')
43+
// }
44+
// },
45+
}
2946
}
30-
})
47+
}

0 commit comments

Comments
 (0)