local mock data

This commit is contained in:
zuiidea
2016-10-19 17:25:46 +08:00
parent 1918e132e7
commit 6307e31366
6 changed files with 62 additions and 99 deletions
+42 -70
View File
@@ -7,18 +7,20 @@ 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()),
}],
'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
@@ -44,7 +46,7 @@ module.exports = {
if (page.field) {
const d = newData.filter(function (item) {
return item[page.field].indexOf(page.keyword) > -1
return item[page.field].indexOf(decodeURI(page.keyword)) > -1
})
data = d.slice((currentPage - 1) * pageSize, currentPage * pageSize)
@@ -61,80 +63,50 @@ module.exports = {
total: tableListData.page.total
}
}
res.json({
success: true,
data,
page: newPage
})
// setTimeout(function () {
// res.json({
// success: true,
// data,
// page: newPage
// })
// }, 500)
res.json({success: true, data, page: newPage})
},
'POST /api/users' (req, res) {
setTimeout(function () {
const newData = qs.parse(req.body)
const newData = qs.parse(req.body)
newData.id = tableListData.data.length + 1
tableListData.data.unshift(newData)
newData.id = tableListData.data.length + 1
tableListData.data.unshift(newData)
tableListData.page.total = tableListData.data.length
tableListData.page.current = 1
tableListData.page.total = tableListData.data.length
tableListData.page.current = 1
global.tableListData = tableListData
res.json({
success: true,
data: tableListData.data,
page: tableListData.page
})
}, 500)
global.tableListData = tableListData
res.json({success: true, data: tableListData.data, page: tableListData.page})
},
'DELETE /api/users' (req, res) {
setTimeout(function () {
const deleteItem = qs.parse(req.body)
const deleteItem = qs.parse(req.body)
tableListData.data = tableListData.data.filter(function (item) {
if (item.id == deleteItem.id) {
return false
}
return true
})
tableListData.data = tableListData.data.filter(function (item) {
if (item.id == deleteItem.id) {
return false
}
return true
})
tableListData.page.total = tableListData.data.length
tableListData.page.total = tableListData.data.length
global.tableListData = tableListData
res.json({
success: true,
data: tableListData.data,
page: tableListData.page
})
}, 500)
global.tableListData = tableListData
res.json({success: true, data: tableListData.data, page: tableListData.page})
},
'PUT /api/users' (req, res) {
setTimeout(function () {
const editItem = qs.parse(req.body)
const editItem = qs.parse(req.body)
tableListData.data = tableListData.data.map(function (item) {
if (item.id == editItem.id) {
return editItem
}
return item
})
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)
global.tableListData = tableListData
res.json({success: true, data: tableListData.data, page: tableListData.page})
}
}
+2
View File
@@ -4,6 +4,8 @@ const mock = {}
require('fs').readdirSync(require('path').join(__dirname + '/mock'))
.forEach(function (file) {
console.log(file);
console.log(require('./mock/' + file));
Object.assign(mock, require('./mock/' + file))
})
+3 -4
View File
@@ -37,7 +37,6 @@ export default {
*query({ payload }, { call, put }) {
yield put({ type: 'showLoading' })
const data = yield call(query, parse(payload))
console.log("modae",data);
if (data) {
yield put({
type: 'querySuccess',
@@ -53,7 +52,7 @@ export default {
},
*'delete'({ payload }, { call, put }) {
yield put({ type: 'showLoading' })
const { data } = yield call(remove, { id: payload })
const data = yield call(remove, { id: payload })
if (data && data.success) {
yield put({
type: 'deleteSuccess',
@@ -64,7 +63,7 @@ export default {
*create({ payload }, { call, put }) {
yield put({ type: 'hideModal' })
yield put({ type: 'showLoading' })
const { data } = yield call(create, payload)
const data = yield call(create, payload)
if (data && data.success) {
yield put({
type: 'createSuccess',
@@ -77,7 +76,7 @@ export default {
yield put({ type: 'showLoading' })
const id = yield select(({ users }) => users.currentItem.id)
const newUser = { ...payload, id }
const { data } = yield call(update, newUser)
const data = yield call(update, newUser)
if (data && data.success) {
yield put({
type: 'updateSuccess',
+1 -2
View File
@@ -7,7 +7,6 @@ export async function query(params) {
body: qs.stringify(params),
data:params,
})
// return request(`/api/users?${qs.stringify(params)}`)
}
export async function create(params) {
@@ -30,6 +29,6 @@ export async function update(params) {
return request('/api/users', {
method: 'put',
body: qs.stringify(params),
data:params,
data: params,
})
}
+14 -14
View File
@@ -1,6 +1,6 @@
const qs = require('qs')
const Mock = require('mockjs')
const mockUser = require("../../mock/users")
const mockData = [require("../../mock/users")]
function serialize(str) {
let paramArray = str.split("&")
@@ -11,19 +11,19 @@ function serialize(str) {
return query
}
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
}
mockUser[key](options, res)
return result
})
for (let i in mockData) {
for (let key in mockData[i]) {
Mock.mock(eval("/" + key.split(" ")[1]), key.split(" ")[0].toLowerCase(), function (options) {
options.query = !!options.url.split("?")[1] ? serialize(options.url.split("?")[1]) : (!!options.body ? serialize(options.body) : {})
let res = {}
let result = {}
res.json = function (data) {
result = data
}
mockUser[key](options, res)
return result
})
}
}
Mock.setup({timeout: 400})
-9
View File
@@ -1,4 +1,3 @@
import fetch from 'dva/fetch'
const Ajax = require("robe-ajax")
function parseJSON(response) {
@@ -23,14 +22,6 @@ function checkStatus(response) {
* @return {object} An object containing either "data" or "err"
*/
export default function request(url, options) {
// return fetch(url, options)
// .then(checkStatus)
// .then(parseJSON)
// .then((data) => {
// console.log(data);
// return data
// })
// .catch((err) => ({ err }))
return Ajax.ajax({
url: url,