feat: 使用 axios 扩展的方式实现 mock 数据

This commit is contained in:
FairyEver
2020-05-07 23:02:12 +08:00
parent 28acfdbb04
commit ac9b1ea26d
14 changed files with 161 additions and 41 deletions

View File

@@ -1,9 +1,23 @@
export default ({ request }) => ({
import { find, map, random } from 'lodash'
const businessIssue142Db = [
{ id: '1', name: '用户 1', address: '上海市普陀区金沙江路 1518 弄' },
{ id: '2', name: '用户 2', address: '上海市普陀区金沙江路 1517 弄' },
{ id: '3', name: '用户 3', address: '上海市普陀区金沙江路 1519 弄' },
{ id: '4', name: '用户 4', address: '上海市普陀区金沙江路 1516 弄' }
]
export default ({ request, faker, mock, tools }) => ({
/**
* @description https://d2.pub/d2-admin/preview/#/demo/business/issues/142
* @description 列表
*/
DEMO_BUSINESS_ISSUE_142_LIST () {
// 模拟数据
mock
.onAny('/demo/business/issues/142/fetch')
.reply(...tools.responseSuccess({ list: businessIssue142Db }))
// 接口请求
return request({
url: '/demo/business/issues/142/fetch',
method: 'get'
@@ -12,8 +26,14 @@ export default ({ request }) => ({
/**
* @description https://d2.pub/d2-admin/preview/#/demo/business/issues/142
* @description 详情
* @param {String} id 项目 ID
*/
DEMO_BUSINESS_ISSUE_142_DETAIL ({ id } = {}) {
DEMO_BUSINESS_ISSUE_142_DETAIL (id) {
// 模拟数据
mock
.onAny('/demo/business/issues/142/detail')
.reply(config => tools.responseSuccess(find(businessIssue142Db, { id: config.params.id })))
// 接口请求
return request({
url: '/demo/business/issues/142/detail',
method: 'get',
@@ -26,16 +46,37 @@ export default ({ request }) => ({
* @description https://d2.pub/d2-admin/preview/#/demo/business/table/1
* @description 列表
*/
DEMO_BUSINESS_TABLE_1_LIST () {
DEMO_BUSINESS_TABLE_1_LIST (params = {}) {
// 模拟数据
mock
.onAny('/demo/business/table/1/fetch')
.reply(config => tools.responseSuccess({
page: {
total: 1000
},
list: map(Array(config.params.pageSize), () => ({
key: faker.random.uuid(),
value: [10, 100, 200, 500][random(0, 3)],
type: faker.random.boolean(),
admin: faker.name.firstName() + faker.name.lastName(),
adminNote: faker.random.words(),
dateTimeCreat: faker.date.past(),
used: faker.random.boolean(),
dateTimeUse: faker.date.past()
}))
}))
// 接口请求
return request({
url: '/demo/business/table/1/fetch',
method: 'get'
method: 'get',
params
})
},
/**
* @description 错误日志示例 请求一个不存在的地址
*/
DEMO_LOG_AJAX () {
// 接口请求
return request({
url: '/invalid-url',
method: 'get'