This commit is contained in:
zuiidea
2016-10-13 01:12:44 +08:00
parent f2176ba3a7
commit 019074d42c
6 changed files with 66 additions and 85 deletions
+47 -47
View File
@@ -1,10 +1,10 @@
'use strict';
'use strict'
const qs = require('qs');
const mockjs = require('mockjs');
const qs = require('qs')
const mockjs = require('mockjs')
// 数据持久
let tableListData = {};
let tableListData = {}
if (!global.tableListData) {
const data = mockjs.mock({
'data|100': [{
@@ -17,43 +17,43 @@ if (!global.tableListData) {
total: 100,
current: 1
}
});
tableListData = data;
global.tableListData = tableListData;
})
tableListData = data
global.tableListData = tableListData
} else {
tableListData = global.tableListData;
tableListData = global.tableListData
}
module.exports = {
'GET /api/users' (req, res) {
const page = qs.parse(req.query);
const pageSize = page.pageSize || 10;
const currentPage = page.page || 1;
const page = qs.parse(req.query)
const pageSize = page.pageSize || 10
const currentPage = page.page || 1
let data;
let newPage;
let data
let newPage
let newData = tableListData.data.concat();
let newData = tableListData.data.concat()
if (page.field) {
const d = newData.filter(function (item) {
return item[page.field].indexOf(page.keyword) > -1;
});
return item[page.field].indexOf(page.keyword) > -1
})
data = d.slice((currentPage - 1) * pageSize, currentPage * pageSize);
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;
data = tableListData.data.slice((currentPage - 1) * pageSize, currentPage * pageSize)
tableListData.page.current = currentPage * 1
newPage = {
current: tableListData.page.current,
total: tableListData.page.total
};
}
}
@@ -62,69 +62,69 @@ module.exports = {
success: true,
data,
page: newPage
});
}, 500);
})
}, 500)
},
'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;
global.tableListData = tableListData
res.json({
success: true,
data: tableListData.data,
page: tableListData.page
});
}, 500);
})
}, 500)
},
'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 false
}
return true;
});
return true
})
tableListData.page.total = tableListData.data.length;
tableListData.page.total = tableListData.data.length
global.tableListData = tableListData;
global.tableListData = tableListData
res.json({
success: true,
data: tableListData.data,
page: tableListData.page
});
}, 500);
})
}, 500)
},
'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 editItem
}
return item;
});
return item
})
global.tableListData = tableListData;
global.tableListData = tableListData
res.json({
success: true,
data: tableListData.data,
page: tableListData.page
});
}, 500);
})
}, 500)
}
};
}
-14
View File
@@ -1,14 +0,0 @@
import React from 'react';
const Example = (props) => {
return (
<div>
Example
</div>
);
};
Example.propTypes = {
};
export default Example;
+6 -6
View File
@@ -1,5 +1,5 @@
import React, { PropTypes } from 'react';
import { Table, Popconfirm, Pagination } from 'antd';
import React, { PropTypes } from 'react'
import { Table, Popconfirm, Pagination } from 'antd'
function UserList({
total, current, loading, dataSource,
@@ -32,7 +32,7 @@ function UserList({
</Popconfirm>
</p>
),
}];
}]
return (
<div>
@@ -51,7 +51,7 @@ function UserList({
onChange={onPageChange}
/>
</div>
);
)
}
UserList.propTypes = {
@@ -62,6 +62,6 @@ UserList.propTypes = {
loading: PropTypes.any,
total: PropTypes.any,
current: PropTypes.any,
};
}
export default UserList;
export default UserList
-5
View File
@@ -1,5 +0,0 @@
import request from '../utils/request';
export async function query() {
return request('/api/users');
}
+6 -6
View File
@@ -1,27 +1,27 @@
import request from '../utils/request';
import qs from 'qs';
import request from '../utils/request'
import qs from 'qs'
export async function query(params) {
return request(`/api/users?${qs.stringify(params)}`);
return request(`/api/users?${qs.stringify(params)}`)
}
export async function create(params) {
return request('/api/users', {
method: 'post',
body: qs.stringify(params),
});
})
}
export async function remove(params) {
return request('/api/users', {
method: 'delete',
body: qs.stringify(params),
});
})
}
export async function update(params) {
return request('/api/users', {
method: 'put',
body: qs.stringify(params),
});
})
}
+7 -7
View File
@@ -1,17 +1,17 @@
import fetch from 'dva/fetch';
import fetch from 'dva/fetch'
function parseJSON(response) {
return response.json();
return response.json()
}
function checkStatus(response) {
if (response.status >= 200 && response.status < 300) {
return response;
return response
}
const error = new Error(response.statusText);
error.response = response;
throw error;
const error = new Error(response.statusText)
error.response = response
throw error
}
/**
@@ -26,5 +26,5 @@ export default function request(url, options) {
.then(checkStatus)
.then(parseJSON)
.then((data) => ({ data }))
.catch((err) => ({ err }));
.catch((err) => ({ err }))
}