mirror of
https://github.com/zuiidea/antd-admin
synced 2026-10-28 03:04:48 +00:00
local mock data
This commit is contained in:
+12
-8
@@ -61,15 +61,19 @@ module.exports = {
|
||||
total: tableListData.page.total
|
||||
}
|
||||
}
|
||||
res.json({
|
||||
success: true,
|
||||
data,
|
||||
page: newPage
|
||||
})
|
||||
|
||||
|
||||
setTimeout(function () {
|
||||
res.json({
|
||||
success: true,
|
||||
data,
|
||||
page: newPage
|
||||
})
|
||||
}, 500)
|
||||
// setTimeout(function () {
|
||||
// res.json({
|
||||
// success: true,
|
||||
// data,
|
||||
// page: newPage
|
||||
// })
|
||||
// }, 500)
|
||||
},
|
||||
|
||||
'POST /api/users' (req, res) {
|
||||
|
||||
+2
-2
@@ -29,7 +29,7 @@
|
||||
"eslint-plugin-jsx-a11y": "^1.4.2",
|
||||
"eslint-plugin-react": "^5.1.1",
|
||||
"glob": "^7.0.5",
|
||||
"mockjs": "^1.0.1-beta2",
|
||||
"mockjs": "^1.0.1-beta3",
|
||||
"redbox-react": "^1.2.10"
|
||||
},
|
||||
"scripts": {
|
||||
@@ -38,4 +38,4 @@
|
||||
"lint": "eslint --fix --ext .js,.jsx .",
|
||||
"build": "atool-build"
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,136 +0,0 @@
|
||||
'use strict'
|
||||
|
||||
const qs = require('qs')
|
||||
const mockjs = require('mockjs')
|
||||
|
||||
// 数据持久
|
||||
let tableListData = {}
|
||||
if (!global.tableListData) {
|
||||
const data = mockjs.mock({
|
||||
'data|100': [{
|
||||
'id|+1': 1,
|
||||
name: '@cname',
|
||||
nickName:'@name',
|
||||
phone:/^1[34578]\d{9}$/,
|
||||
'age|11-99': 1,
|
||||
address: '@county(true)',
|
||||
isMale:'@boolean',
|
||||
email:'@email',
|
||||
createTime:'@datetime',
|
||||
avatar:mockjs.Random.image('100x100',mockjs.Random.color()),
|
||||
}],
|
||||
page: {
|
||||
total: 100,
|
||||
current: 1
|
||||
}
|
||||
})
|
||||
tableListData = data
|
||||
global.tableListData = tableListData
|
||||
} else {
|
||||
tableListData = global.tableListData
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
|
||||
'GET /api/users' (req, res) {
|
||||
const page = qs.parse(req.query)
|
||||
const pageSize = page.pageSize || 20
|
||||
const currentPage = page.page || 1
|
||||
|
||||
let data
|
||||
let newPage
|
||||
|
||||
let newData = tableListData.data.concat()
|
||||
|
||||
if (page.field) {
|
||||
const d = newData.filter(function (item) {
|
||||
return item[page.field].indexOf(page.keyword) > -1
|
||||
})
|
||||
|
||||
data = d.slice((currentPage - 1) * pageSize, currentPage * pageSize)
|
||||
|
||||
newPage = {
|
||||
current: currentPage * 1,
|
||||
total: d.length
|
||||
}
|
||||
} else {
|
||||
data = tableListData.data.slice((currentPage - 1) * pageSize, currentPage * pageSize)
|
||||
tableListData.page.current = currentPage * 1
|
||||
newPage = {
|
||||
current: tableListData.page.current,
|
||||
total: tableListData.page.total
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
setTimeout(function () {
|
||||
res.json({
|
||||
success: true,
|
||||
data,
|
||||
page: newPage
|
||||
})
|
||||
}, 500)
|
||||
},
|
||||
|
||||
'POST /api/users' (req, res) {
|
||||
setTimeout(function () {
|
||||
const newData = qs.parse(req.body)
|
||||
|
||||
newData.id = tableListData.data.length + 1
|
||||
tableListData.data.unshift(newData)
|
||||
|
||||
tableListData.page.total = tableListData.data.length
|
||||
tableListData.page.current = 1
|
||||
|
||||
global.tableListData = tableListData
|
||||
res.json({
|
||||
success: true,
|
||||
data: tableListData.data,
|
||||
page: tableListData.page
|
||||
})
|
||||
}, 500)
|
||||
},
|
||||
|
||||
'DELETE /api/users' (req, res) {
|
||||
setTimeout(function () {
|
||||
const deleteItem = qs.parse(req.body)
|
||||
|
||||
tableListData.data = tableListData.data.filter(function (item) {
|
||||
if (item.id == deleteItem.id) {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
})
|
||||
|
||||
tableListData.page.total = tableListData.data.length
|
||||
|
||||
global.tableListData = tableListData
|
||||
res.json({
|
||||
success: true,
|
||||
data: tableListData.data,
|
||||
page: tableListData.page
|
||||
})
|
||||
}, 500)
|
||||
},
|
||||
|
||||
'PUT /api/users' (req, res) {
|
||||
setTimeout(function () {
|
||||
const editItem = qs.parse(req.body)
|
||||
|
||||
tableListData.data = tableListData.data.map(function (item) {
|
||||
if (item.id == editItem.id) {
|
||||
return editItem
|
||||
}
|
||||
return item
|
||||
})
|
||||
|
||||
global.tableListData = tableListData
|
||||
res.json({
|
||||
success: true,
|
||||
data: tableListData.data,
|
||||
page: tableListData.page
|
||||
})
|
||||
}, 500)
|
||||
}
|
||||
|
||||
}
|
||||
+2
-1
@@ -36,7 +36,8 @@ export default {
|
||||
effects: {
|
||||
*query({ payload }, { call, put }) {
|
||||
yield put({ type: 'showLoading' })
|
||||
const { data } = yield call(query, parse(payload))
|
||||
const data = yield call(query, parse(payload))
|
||||
console.log("modae",data);
|
||||
if (data) {
|
||||
yield put({
|
||||
type: 'querySuccess',
|
||||
|
||||
@@ -2,13 +2,19 @@ import { request } from '../utils'
|
||||
import qs from 'qs'
|
||||
|
||||
export async function query(params) {
|
||||
return request(`/api/users?${qs.stringify(params)}`)
|
||||
return request('/api/users', {
|
||||
method: 'get',
|
||||
body: qs.stringify(params),
|
||||
data:params,
|
||||
})
|
||||
// return request(`/api/users?${qs.stringify(params)}`)
|
||||
}
|
||||
|
||||
export async function create(params) {
|
||||
return request('/api/users', {
|
||||
method: 'post',
|
||||
body: qs.stringify(params),
|
||||
data:params,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -16,6 +22,7 @@ export async function remove(params) {
|
||||
return request('/api/users', {
|
||||
method: 'delete',
|
||||
body: qs.stringify(params),
|
||||
data:params,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -23,5 +30,6 @@ export async function update(params) {
|
||||
return request('/api/users', {
|
||||
method: 'put',
|
||||
body: qs.stringify(params),
|
||||
data:params,
|
||||
})
|
||||
}
|
||||
|
||||
+24
-22
@@ -1,27 +1,29 @@
|
||||
'use strict'
|
||||
|
||||
const qs = require('qs')
|
||||
const fs = require('fs')
|
||||
const Mock = require('mockjs')
|
||||
const Ajax = require("robe-ajax")
|
||||
const mockUser = require("../../mock/users")
|
||||
|
||||
Mock.mock(/\.json/, function (options) {
|
||||
console.log(options);
|
||||
return options
|
||||
})
|
||||
function serialize(str) {
|
||||
let paramArray = str.split("&")
|
||||
let query = {}
|
||||
for (let i in paramArray) {
|
||||
query[paramArray[i].split("=")[0]] = paramArray[i].split("=")[1]
|
||||
}
|
||||
return query
|
||||
}
|
||||
|
||||
const mock = {};
|
||||
console.log(fs);
|
||||
|
||||
|
||||
Ajax.ajax({
|
||||
url: 'hello.json',
|
||||
dataType: 'json',
|
||||
data: {
|
||||
foo: 1,
|
||||
bar: 2,
|
||||
faz: 3
|
||||
for (let key in mockUser) {
|
||||
Mock.mock(eval("/" + key.split(" ")[1]), key.split(" ")[0].toLowerCase(), function (options) {
|
||||
options.query = !!options.url.split("?")[1]
|
||||
? serialize(options.url.split("?")[1])
|
||||
: {}
|
||||
let res = {}
|
||||
let result = {}
|
||||
res.json = function (data) {
|
||||
result = data
|
||||
}
|
||||
}).done(function(data, status, jqXHR) {
|
||||
console.log(data)
|
||||
})
|
||||
mockUser[key](options, res)
|
||||
return result
|
||||
})
|
||||
}
|
||||
|
||||
Mock.setup({timeout: 400})
|
||||
|
||||
+20
-6
@@ -1,4 +1,5 @@
|
||||
import fetch from 'dva/fetch'
|
||||
const Ajax = require("robe-ajax")
|
||||
|
||||
function parseJSON(response) {
|
||||
return response.json()
|
||||
@@ -22,10 +23,23 @@ function checkStatus(response) {
|
||||
* @return {object} An object containing either "data" or "err"
|
||||
*/
|
||||
export default function request(url, options) {
|
||||
console.log(url, options);
|
||||
return fetch(url, options)
|
||||
.then(checkStatus)
|
||||
.then(parseJSON)
|
||||
.then((data) => ({ data }))
|
||||
.catch((err) => ({ err }))
|
||||
// return fetch(url, options)
|
||||
// .then(checkStatus)
|
||||
// .then(parseJSON)
|
||||
// .then((data) => {
|
||||
// console.log(data);
|
||||
// return data
|
||||
// })
|
||||
// .catch((err) => ({ err }))
|
||||
|
||||
return Ajax.ajax({
|
||||
url: url,
|
||||
method:options.method||'GET',
|
||||
data:options.data||{},
|
||||
dataType:'json',
|
||||
})
|
||||
.done((data) => {
|
||||
console.log(data);
|
||||
return data
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user