Skip to content
This repository was archived by the owner on Aug 11, 2021. It is now read-only.

Commit 38ffdc9

Browse files
committed
增加mock支持
1 parent 39038d3 commit 38ffdc9

File tree

12 files changed

+168
-32
lines changed

12 files changed

+168
-32
lines changed

.env.development

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# 页面标题
22
VUE_APP_TITLE = 测试环境
33
# 接口请求地址,会设置到 axios 的 baseURL 参数上
4-
VUE_APP_API_ROOT = http://yigou.ketao.com/api/
4+
VUE_APP_API_ROOT = /
55
# 是否开启 CDN 支持,开启设置 ON,关闭设置 OFF
66
# 详情介绍请阅读 http://eoner.gitee.io/vue-automation/#/cdn
77
VUE_APP_CDN = OFF

.env.production

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# 页面标题
22
VUE_APP_TITLE = 网站标题
33
# 接口请求地址,会设置到 axios 的 baseURL 参数上
4-
VUE_APP_API_ROOT = http://yigou.ketao.com/api/
4+
VUE_APP_API_ROOT = /
55
# 是否开启 CDN 支持,开启设置 ON,关闭设置 OFF
66
# 详情介绍请阅读 http://eoner.gitee.io/vue-automation/#/cdn
77
VUE_APP_CDN = OFF

package.json

+2
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@
1616
"core-js": "^3.6.4",
1717
"dayjs": "^1.9.4",
1818
"js-cookie": "^2.2.1",
19+
"mockjs": "^1.1.0",
1920
"nprogress": "^0.2.0",
2021
"vue": "^2.6.12",
22+
"vue-cli-plugin-mock": "^1.0.2",
2123
"vue-meta": "^2.4.0",
2224
"vue-router": "^3.4.8",
2325
"vuex": "^3.5.1"

src/api/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const toLogin = () => {
1313
}
1414

1515
const api = axios.create({
16-
baseURL: process.env.NODE_ENV !== 'development' && process.env.VUE_APP_API_ROOT,
16+
baseURL: process.env.VUE_APP_API_ROOT,
1717
timeout: 10000,
1818
responseType: 'json'
1919
})

src/main.js

+2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ const req = require.context('./assets/icons', false, /\.svg$/)
2626
const requireAll = requireContext => requireContext.keys().map(requireContext)
2727
requireAll(req)
2828

29+
import './mock'
30+
2931
Vue.config.productionTip = false
3032

3133
new Vue({

src/mock/index.js

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
const Mock = require('mockjs')
2+
3+
export function param2Obj(url) {
4+
const search = decodeURIComponent(url.split('?')[1]).replace(/\+/g, ' ')
5+
if (!search) {
6+
return {}
7+
}
8+
const obj = {}
9+
const searchArr = search.split('&')
10+
searchArr.forEach(v => {
11+
const index = v.indexOf('=')
12+
if (index !== -1) {
13+
const name = v.substring(0, index)
14+
const val = v.substring(index + 1, v.length)
15+
obj[name] = val
16+
}
17+
})
18+
return obj
19+
}
20+
21+
function XHR2ExpressReqWrap(respond) {
22+
return function(options) {
23+
let result = null
24+
if (respond instanceof Function) {
25+
const { body, type, url } = options
26+
// https://expressjs.com/en/4x/api.html#req
27+
result = respond({
28+
method: type,
29+
body: JSON.parse(body),
30+
query: param2Obj(url)
31+
})
32+
} else {
33+
result = respond
34+
}
35+
return Mock.mock(result)
36+
}
37+
}
38+
const mocksContext = require.context('./modules/', true, /.js$/)
39+
mocksContext.keys().forEach(file_name => {
40+
// 获取文件中的 default 模块
41+
const mocks = mocksContext(file_name)
42+
for (const mock of mocks) {
43+
Mock.mock(
44+
new RegExp(`${process.env.VUE_APP_API_ROOT}mock/${mock.url}`),
45+
mock.type || 'get',
46+
XHR2ExpressReqWrap(mock.result)
47+
)
48+
}
49+
})

src/mock/modules/news.js

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
module.exports = [
2+
{
3+
url: 'news/list',
4+
type: 'get',
5+
result: () => {
6+
return {
7+
error: '',
8+
status: 1,
9+
data: {
10+
'list|5-10': [
11+
{
12+
'title': '@ctitle'
13+
}
14+
]
15+
}
16+
}
17+
}
18+
}
19+
]

src/mock/server-modules/news.js

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
const Mock = require('mockjs')
2+
3+
export default {
4+
'GET /mock/news/list': (req, res) => {
5+
return res.json({
6+
error: '',
7+
status: 1,
8+
data: Mock.mock({
9+
'list|5-10': [
10+
{
11+
'title': '@ctitle'
12+
}
13+
]
14+
})
15+
})
16+
}
17+
}

src/mock/server.js

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
let mockMap = {}
2+
3+
const mocksContext = require.context('./server-modules/', false, /.js$/)
4+
mocksContext.keys().forEach(file_name => {
5+
mockMap = Object.assign(mockMap, mocksContext(file_name).default)
6+
})
7+
8+
export default mockMap

src/views/example/axios.vue

+9-15
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,27 @@
11
<template>
22
<div>
3-
<button type="button" @click="getInfo">获取数据</button>
4-
<img v-for="(item, index) in banner" :key="index" :src="item.image">
3+
<button type="button" @click="getInfo">获取 mock 数据</button>
4+
<div>
5+
<p v-for="(item, index) in news" :key="index">{{ item.title }}</p>
6+
</div>
57
</div>
68
</template>
79

810
<script>
911
export default {
1012
data() {
1113
return {
12-
banner: []
14+
news: []
1315
}
1416
},
1517
methods: {
1618
getInfo() {
1719
Promise.all([
18-
this.$api.get('banner/list', {
19-
params: {
20-
categoryid: 1
21-
}
22-
}),
23-
this.$api.get('banner/list', {
24-
params: {
25-
categoryid: 2
26-
}
27-
})
20+
this.$api.get('mock/news/list'),
21+
this.$api.get('mock/news/list')
2822
]).then(res => {
29-
this.banner = res[0].data.banner.concat(
30-
res[1].data.banner
23+
this.news = res[0].data.list.concat(
24+
res[1].data.list
3125
)
3226
})
3327
}

vue.config.js

+22-7
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,23 @@ module.exports = {
5757
productionSourceMap: false,
5858
devServer: {
5959
open: true,
60-
// 开发环境默认开启反向代理,如果不需要请自行注释
61-
proxy: {
62-
'/': {
63-
target: process.env.VUE_APP_API_ROOT,
64-
changeOrigin: true
65-
}
66-
}
60+
// proxy: {
61+
// '/': {
62+
// target: process.env.VUE_APP_API_ROOT,
63+
// changeOrigin: true
64+
// }
65+
// },
66+
// 用于 mock-server
67+
// proxy: {
68+
// '/mock': {
69+
// target: '/',
70+
// changeOrigin: true
71+
// },
72+
// '/': {
73+
// target: process.env.VUE_APP_API_ROOT,
74+
// changeOrigin: true
75+
// }
76+
// },
6777
},
6878
configureWebpack: config => {
6979
config.resolve.modules = ['node_modules', 'assets/sprites']
@@ -103,6 +113,11 @@ module.exports = {
103113
lintStyleOnBuild: true,
104114
stylelint: {
105115
fix: true
116+
},
117+
mock: {
118+
entry: './src/mock/server.js',
119+
debug: true,
120+
disable: true
106121
}
107122
},
108123
chainWebpack: config => {

yarn.lock

+37-7
Original file line numberDiff line numberDiff line change
@@ -2131,7 +2131,7 @@ bn.js@^5.0.0, bn.js@^5.1.1:
21312131
resolved "https://registry.npm.taobao.org/bn.js/download/bn.js-5.1.3.tgz#beca005408f642ebebea80b042b4d18d2ac0ee6b"
21322132
integrity sha1-vsoAVAj2Quvr6oCwQrTRjSrA7ms=
21332133

2134-
2134+
[email protected], body-parser@^1.18.3:
21352135
version "1.19.0"
21362136
resolved "https://registry.npm.taobao.org/body-parser/download/body-parser-1.19.0.tgz#96b2709e57c9c4e09a6fd66a8fd979844f69f08a"
21372137
integrity sha1-lrJwnlfJxOCab9Zqj9l5hE9p8Io=
@@ -2838,6 +2838,11 @@ combined-stream@^1.0.6, combined-stream@~1.0.6:
28382838
dependencies:
28392839
delayed-stream "~1.0.0"
28402840

2841+
commander@*, commander@^6.2.0:
2842+
version "6.2.0"
2843+
resolved "https://registry.npm.taobao.org/commander/download/commander-6.2.0.tgz?cache=0&sync_timestamp=1605992729751&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2Fcommander%2Fdownload%2Fcommander-6.2.0.tgz#b990bfb8ac030aedc6d11bc04d1488ffef56db75"
2844+
integrity sha1-uZC/uKwDCu3G0RvATRSI/+9W23U=
2845+
28412846
28422847
version "2.17.1"
28432848
resolved "https://registry.npm.taobao.org/commander/download/commander-2.17.1.tgz?cache=0&sync_timestamp=1605992729751&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2Fcommander%2Fdownload%2Fcommander-2.17.1.tgz#bd77ab7de6de94205ceacc72f1716d29f20a77bf"
@@ -2853,11 +2858,6 @@ commander@^4.1.1:
28532858
resolved "https://registry.npm.taobao.org/commander/download/commander-4.1.1.tgz?cache=0&sync_timestamp=1605992729751&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2Fcommander%2Fdownload%2Fcommander-4.1.1.tgz#9fd602bd936294e9e9ef46a3f4d6964044b18068"
28542859
integrity sha1-n9YCvZNilOnp70aj9NaWQESxgGg=
28552860

2856-
commander@^6.2.0:
2857-
version "6.2.0"
2858-
resolved "https://registry.npm.taobao.org/commander/download/commander-6.2.0.tgz?cache=0&sync_timestamp=1605992729751&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2Fcommander%2Fdownload%2Fcommander-6.2.0.tgz#b990bfb8ac030aedc6d11bc04d1488ffef56db75"
2859-
integrity sha1-uZC/uKwDCu3G0RvATRSI/+9W23U=
2860-
28612861
commander@~2.19.0:
28622862
version "2.19.0"
28632863
resolved "https://registry.npm.taobao.org/commander/download/commander-2.19.0.tgz?cache=0&sync_timestamp=1605992729751&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2Fcommander%2Fdownload%2Fcommander-2.19.0.tgz#f6198aa84e5b83c46054b94ddedbfed5ee9ff12a"
@@ -3359,7 +3359,7 @@ [email protected], debug@^2.2.0, debug@^2.3.3:
33593359
dependencies:
33603360
ms "2.0.0"
33613361

3362-
debug@^3.1.1, debug@^3.2.5:
3362+
debug@^3.1.0, debug@^3.1.1, debug@^3.2.5:
33633363
version "3.2.7"
33643364
resolved "https://registry.npm.taobao.org/debug/download/debug-3.2.7.tgz?cache=0&sync_timestamp=1606566568533&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2Fdebug%2Fdownload%2Fdebug-3.2.7.tgz#72580b7e9145fb39b6676f9c5e5fb100b934179a"
33653365
integrity sha1-clgLfpFF+zm2Z2+cXl+xALk0F5o=
@@ -4141,6 +4141,16 @@ expand-tilde@^2.0.0, expand-tilde@^2.0.2:
41414141
dependencies:
41424142
homedir-polyfill "^1.0.1"
41434143

4144+
express-mock-restful@^1.1.2:
4145+
version "1.1.2"
4146+
resolved "https://registry.npm.taobao.org/express-mock-restful/download/express-mock-restful-1.1.2.tgz#7c78bd61811ec00d20c6a36d4058044c79d4a839"
4147+
integrity sha1-fHi9YYEewA0gxqNtQFgETHnUqDk=
4148+
dependencies:
4149+
body-parser "^1.18.3"
4150+
chalk "^2.4.1"
4151+
debug "^3.1.0"
4152+
path-to-regexp "^2.2.1"
4153+
41444154
express@^4.16.3, express@^4.17.1:
41454155
version "4.17.1"
41464156
resolved "https://registry.npm.taobao.org/express/download/express-4.17.1.tgz?cache=0&sync_timestamp=1592843208199&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2Fexpress%2Fdownload%2Fexpress-4.17.1.tgz#4491fc38605cf51f8629d39c2b5d026f98a4c134"
@@ -6713,6 +6723,13 @@ mkdirp@^1.0.3, mkdirp@^1.0.4:
67136723
resolved "https://registry.npm.taobao.org/mkdirp/download/mkdirp-1.0.4.tgz#3eb5ed62622756d79a5f0e2a221dfebad75c2f7e"
67146724
integrity sha1-PrXtYmInVteaXw4qIh3+utdcL34=
67156725

6726+
mockjs@^1.1.0:
6727+
version "1.1.0"
6728+
resolved "https://registry.npm.taobao.org/mockjs/download/mockjs-1.1.0.tgz#e6a0c378e91906dbaff20911cc0273b3c7d75b06"
6729+
integrity sha1-5qDDeOkZBtuv8gkRzAJzs8fXWwY=
6730+
dependencies:
6731+
commander "*"
6732+
67166733
move-concurrently@^1.0.1:
67176734
version "1.0.1"
67186735
resolved "https://registry.npm.taobao.org/move-concurrently/download/move-concurrently-1.0.1.tgz#be2c005fda32e0b29af1f05d7c4b33214c701f92"
@@ -7505,6 +7522,11 @@ [email protected]:
75057522
resolved "https://registry.npm.taobao.org/path-to-regexp/download/path-to-regexp-0.1.7.tgz?cache=0&sync_timestamp=1601400328354&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2Fpath-to-regexp%2Fdownload%2Fpath-to-regexp-0.1.7.tgz#df604178005f522f15eb4490e7247a1bfaa67f8c"
75067523
integrity sha1-32BBeABfUi8V60SQ5yR6G/qmf4w=
75077524

7525+
path-to-regexp@^2.2.1:
7526+
version "2.4.0"
7527+
resolved "https://registry.npm.taobao.org/path-to-regexp/download/path-to-regexp-2.4.0.tgz?cache=0&sync_timestamp=1601400328354&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2Fpath-to-regexp%2Fdownload%2Fpath-to-regexp-2.4.0.tgz#35ce7f333d5616f1c1e1bfe266c3aba2e5b2e704"
7528+
integrity sha1-Nc5/Mz1WFvHB4b/iZsOrouWy5wQ=
7529+
75087530
path-type@^3.0.0:
75097531
version "3.0.0"
75107532
resolved "https://registry.npm.taobao.org/path-type/download/path-type-3.0.0.tgz#cef31dc8e0a1a3bb0d105c0cd97cf3bf47f4e36f"
@@ -10377,6 +10399,14 @@ vm-browserify@^1.0.1:
1037710399
resolved "https://registry.npm.taobao.org/vm-browserify/download/vm-browserify-1.1.2.tgz#78641c488b8e6ca91a75f511e7a3b32a86e5dda0"
1037810400
integrity sha1-eGQcSIuObKkadfUR56OzKobl3aA=
1037910401

10402+
vue-cli-plugin-mock@^1.0.2:
10403+
version "1.0.2"
10404+
resolved "https://registry.npm.taobao.org/vue-cli-plugin-mock/download/vue-cli-plugin-mock-1.0.2.tgz#67520e43313baae9195c3399ba669232583621f8"
10405+
integrity sha1-Z1IOQzE7qukZXDOZumaSMlg2Ifg=
10406+
dependencies:
10407+
express-mock-restful "^1.1.2"
10408+
memory-fs "^0.4.1"
10409+
1038010410
vue-eslint-parser@^7.1.1:
1038110411
version "7.1.1"
1038210412
resolved "https://registry.npm.taobao.org/vue-eslint-parser/download/vue-eslint-parser-7.1.1.tgz?cache=0&sync_timestamp=1602499032728&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2Fvue-eslint-parser%2Fdownload%2Fvue-eslint-parser-7.1.1.tgz#c43c1c715ff50778b9a7e9a4e16921185f3425d3"

0 commit comments

Comments
 (0)