tweak mock

This commit is contained in:
Pan 2019-03-18 15:51:48 +08:00
parent 05ca5cae6d
commit 686d0acd6d
11 changed files with 171 additions and 114 deletions

View File

@ -27,8 +27,11 @@ for (let i = 0; i < count; i++) {
})) }))
} }
export default { export default [
'/article/list': config => { {
url: '/article/list',
type: 'get',
response: config => {
const { importance, type, title, page = 1, limit = 20, sort } = config.query const { importance, type, title, page = 1, limit = 20, sort } = config.query
let mockList = List.filter(item => { let mockList = List.filter(item => {
@ -48,27 +51,54 @@ export default {
total: mockList.length, total: mockList.length,
items: pageList items: pageList
} }
}
}, },
'/article/detail': config => {
{
url: '/article/detail',
type: 'get',
response: config => {
const { id } = config.query const { id } = config.query
for (const article of List) { for (const article of List) {
if (article.id === +id) { if (article.id === +id) {
return article return article
} }
} }
}
}, },
'/article/pv': {
pvData: [ {
url: '/article/pv',
type: 'get',
response: _ => {
return { pvData: [
{ key: 'PC', pv: 1024 }, { key: 'PC', pv: 1024 },
{ key: 'mobile', pv: 1024 }, { key: 'mobile', pv: 1024 },
{ key: 'ios', pv: 1024 }, { key: 'ios', pv: 1024 },
{ key: 'android', pv: 1024 } { key: 'android', pv: 1024 }
] ]
}
}
}, },
'/article/create': {
data: 'success' {
}, url: '/article/create',
'/article/update': { type: 'post',
response: _ => {
return {
data: 'success' data: 'success'
} }
} }
},
{
url: '/article/update',
type: 'post',
response: _ => {
return {
data: 'success'
}
}
}
]

View File

@ -33,18 +33,21 @@ export function mockXHR() {
} }
} }
for (const [route, respond] of Object.entries(mocks)) { for (const i of mocks) {
Mock.mock(new RegExp(`${route}`), XHR2ExpressReqWrap(respond)) Mock.mock(new RegExp(i.url), i.type || 'get', XHR2ExpressReqWrap(i.response))
} }
} }
const responseFake = (route, respond) => ( const responseFake = (url, type, respond) => {
{ return {
route: new RegExp(`${MOCK_API_BASE}${route}`), url: new RegExp(`${MOCK_API_BASE}${url}`),
type: type || 'get',
response(req, res) { response(req, res) {
res.json(Mock.mock(respond instanceof Function ? respond(req, res) : respond)) res.json(Mock.mock(respond instanceof Function ? respond(req, res) : respond))
} }
} }
) }
export default Object.keys(mocks).map(route => responseFake(route, mocks[route])) export default mocks.map(route => {
return responseFake(route.url, route.type, route.response)
})

View File

@ -15,13 +15,28 @@ const userMap = {
} }
} }
export default { export default [
'/login/login': config => { {
url: '/login/login',
type: 'post',
response: config => {
const { username } = config.body const { username } = config.body
return userMap[username] return userMap[username]
}
}, },
'/login/logout': 'success', {
'/user/info': config => { url: '/login/logout',
type: 'post',
response: _ => {
return {
data: 'success'
}
}
},
{
url: '/user/info\.*',
type: 'get',
response: config => {
const { token } = config.query const { token } = config.query
if (userMap[token]) { if (userMap[token]) {
return userMap[token] return userMap[token]
@ -30,4 +45,4 @@ export default {
} }
} }
} }
]

View File

@ -1,14 +1,12 @@
import login from './login' import login from './login'
import role from './role'
import article from './article' import article from './article'
import search from './remoteSearch' import search from './remoteSearch'
import transaction from './transaction'
import role from './role'
export default { export default [
...login, ...login,
...role,
...article, ...article,
...search, ...search
...transaction, ]
...role
}

View File

@ -10,8 +10,11 @@ for (let i = 0; i < count; i++) {
} }
NameList.push({ name: 'mockPan' }) NameList.push({ name: 'mockPan' })
export default { export default [
'/search/user': config => { {
url: '/search/user',
type: 'get',
response: config => {
const { name } = config.query const { name } = config.query
const mockNameList = NameList.filter(item => { const mockNameList = NameList.filter(item => {
const lowerCaseName = item.name.toLowerCase() const lowerCaseName = item.name.toLowerCase()
@ -19,4 +22,22 @@ export default {
}) })
return { items: mockNameList } return { items: mockNameList }
} }
},
{
url: '/transaction/list',
type: 'get',
response: _ => {
const count = 20
return {
total: count,
[`items|${count}`]: [{
order_no: '@guid()',
timestamp: +Mock.Random.date('T'),
username: '@name()',
price: '@float(1000, 15000, 0, 2)',
'status|1': ['success', 'pending']
}]
} }
}
}
]

View File

@ -35,26 +35,34 @@ const roles = [
} }
] ]
export default { export default [
'/routes': () => { {
return routes url: '/routes',
type: 'get',
response: routes
}, },
'/roles': () => { {
return roles url: '/roles',
type: 'get',
response: roles
}, },
'/roles/add': () => { {
return Mock.mock('@integer(300, 5000)') url: '/roles/add',
type: 'post',
response: Mock.mock('@integer(300, 5000)')
}, },
'/roles/update/\/[A-Za-z0-9]': () => { {
const res = { url: '/roles/update/\/[A-Za-z0-9]',
type: 'put',
response: {
data: 'success' data: 'success'
} }
return res
}, },
'/roles/delete/\/[A-Za-z0-9]'() { {
const res = { url: '/roles/delete/\/[A-Za-z0-9]',
type: 'delete',
response: {
data: 'success' data: 'success'
} }
return res
}
} }
]

View File

@ -1,16 +0,0 @@
import Mock from 'mockjs'
const count = 20
export default {
'/transaction/list': {
total: count,
[`items|${count}`]: [{
order_no: '@guid()',
timestamp: +Mock.Random.date('T'),
username: '@name()',
price: '@float(1000, 15000, 0, 2)',
'status|1': ['success', 'pending']
}]
}
}

View File

@ -7,3 +7,11 @@ export function userSearch(name) {
params: { name } params: { name }
}) })
} }
export function transactionList(query) {
return request({
url: '/transaction/list',
method: 'get',
params: query
})
}

View File

@ -1,9 +0,0 @@
import request from '@/utils/request'
export function fetchList(query) {
return request({
url: '/transaction/list',
method: 'get',
params: query
})
}

View File

@ -21,7 +21,7 @@
</template> </template>
<script> <script>
import { fetchList } from '@/api/transaction' import { transactionList } from '@/api/remoteSearch'
export default { export default {
filters: { filters: {
@ -46,7 +46,7 @@ export default {
}, },
methods: { methods: {
fetchData() { fetchData() {
fetchList().then(response => { transactionList().then(response => {
this.list = response.data.items.slice(0, 8) this.list = response.data.items.slice(0, 8)
}) })
} }

View File

@ -51,10 +51,9 @@ module.exports = {
extended: true extended: true
})) }))
// import ES2015 module from common.js module
const { default: mocks } = require('./mock') const { default: mocks } = require('./mock')
for (const mock of mocks) { for (const mock of mocks) {
app.all(mock.route, mock.response) app[mock.type](mock.url, mock.response)
} }
} }
}, },