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,48 +27,78 @@ for (let i = 0; i < count; i++) {
})) }))
} }
export default { export default [
'/article/list': config => { {
const { importance, type, title, page = 1, limit = 20, sort } = config.query url: '/article/list',
type: 'get',
response: config => {
const { importance, type, title, page = 1, limit = 20, sort } = config.query
let mockList = List.filter(item => { let mockList = List.filter(item => {
if (importance && item.importance !== +importance) return false if (importance && item.importance !== +importance) return false
if (type && item.type !== type) return false if (type && item.type !== type) return false
if (title && item.title.indexOf(title) < 0) return false if (title && item.title.indexOf(title) < 0) return false
return true return true
}) })
if (sort === '-id') { if (sort === '-id') {
mockList = mockList.reverse() mockList = mockList.reverse()
} }
const pageList = mockList.filter((item, index) => index < limit * page && index >= limit * (page - 1)) const pageList = mockList.filter((item, index) => index < limit * page && index >= limit * (page - 1))
return { return {
total: mockList.length, total: mockList.length,
items: pageList items: pageList
}
},
'/article/detail': config => {
const { id } = config.query
for (const article of List) {
if (article.id === +id) {
return article
} }
} }
}, },
'/article/pv': {
pvData: [ {
{ key: 'PC', pv: 1024 }, url: '/article/detail',
{ key: 'mobile', pv: 1024 }, type: 'get',
{ key: 'ios', pv: 1024 }, response: config => {
{ key: 'android', pv: 1024 } const { id } = config.query
] for (const article of List) {
if (article.id === +id) {
return article
}
}
}
}, },
'/article/create': {
data: 'success' {
url: '/article/pv',
type: 'get',
response: _ => {
return { pvData: [
{ key: 'PC', pv: 1024 },
{ key: 'mobile', pv: 1024 },
{ key: 'ios', pv: 1024 },
{ key: 'android', pv: 1024 }
]
}
}
}, },
'/article/update': {
data: 'success' {
url: '/article/create',
type: 'post',
response: _ => {
return {
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,19 +15,34 @@ const userMap = {
} }
} }
export default { export default [
'/login/login': config => { {
const { username } = config.body url: '/login/login',
return userMap[username] type: 'post',
response: config => {
const { username } = config.body
return userMap[username]
}
}, },
'/login/logout': 'success', {
'/user/info': config => { url: '/login/logout',
const { token } = config.query type: 'post',
if (userMap[token]) { response: _ => {
return userMap[token] return {
} else { data: 'success'
return false }
}
},
{
url: '/user/info\.*',
type: 'get',
response: config => {
const { token } = config.query
if (userMap[token]) {
return userMap[token]
} else {
return false
}
} }
} }
} ]

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,13 +10,34 @@ for (let i = 0; i < count; i++) {
} }
NameList.push({ name: 'mockPan' }) NameList.push({ name: 'mockPan' })
export default { export default [
'/search/user': config => { {
const { name } = config.query url: '/search/user',
const mockNameList = NameList.filter(item => { type: 'get',
const lowerCaseName = item.name.toLowerCase() response: config => {
return !(name && lowerCaseName.indexOf(name.toLowerCase()) < 0) const { name } = config.query
}) const mockNameList = NameList.filter(item => {
return { items: mockNameList } const lowerCaseName = item.name.toLowerCase()
return !(name && lowerCaseName.indexOf(name.toLowerCase()) < 0)
})
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)
} }
} }
}, },