Authored by 李奇

用户列表分页添加

... ... @@ -9,6 +9,7 @@ const excelExport = require('excel-export');
const UserModel = require('../models/user');
const DO_SUCCESS = '操作成功';
const GET_SUCCESS = '获取成功';
const timeFmt = (time) => {
if (_.isNumber(time)) {
... ... @@ -23,24 +24,49 @@ const userController = {
* 用户列表页
* @param req
* @param res
* @param next
*/
userListPage(req, res, next) {
req.ctx(UserModel).userList()
.then(result => {
_.map(result, item => {
item.createTime = timeFmt(
item.createTime
);
});
userListPage(req, res) {
res.render('user/list', {
bodyClass: 'nav-md',
userList: result,
module: 'admin',
page: 'user'
});
},
/**
* 用户列表
* @param req
* @param res
* @param next
*/
userList(req, res, next) {
const pageNo = req.query.pageNo || 1;
const pageSize = req.query.pageSize || 20;
req.ctx(UserModel).userList({
pageNo,
pageSize
})
.then(list => {
_.each(list, item => {
item.createTime = timeFmt(item.createTime);
});
return list;
})
.then(list => {
req.ctx(UserModel).allUsersNum()
.then(totalCount => {
res.json({
code: 200,
data: list,
pageNo: +pageNo,
pageSize: +pageSize,
totalCount,
totalPage: Math.ceil(totalCount / pageSize),
message: GET_SUCCESS
});
});
})
.catch(next);
},
... ... @@ -94,7 +120,7 @@ const userController = {
rows: []
};
req.ctx(UserModel).userList()
req.ctx(UserModel).exportUserList()
.then(result => {
let temp = [];
... ...
... ... @@ -6,6 +6,7 @@
const mysqlCli = global.yoho.utils.mysqlCli;
const _ = require('lodash');
const TABLE_USER = 'user';
class UserModel extends global.yoho.BaseModel {
... ... @@ -14,10 +15,38 @@ class UserModel extends global.yoho.BaseModel {
}
/**
* 用户列表
* 用户列表[分页]
* @returns {*}
*/
userList() {
userList({pageNo, pageSize}) {
return mysqlCli.query(
`select id, user_phone userPhone, user_name userName, create_time createTime
from ${TABLE_USER}
order by create_time desc
limit :start, :page;`, {
start: (pageNo - 1) * pageSize,
page: _.parseInt(pageSize)
}
);
}
/**
* 用户总数
* @returns {*}
*/
allUsersNum() {
return mysqlCli.query(
`select count(*) as total from ${TABLE_USER};`
).then(res => {
return res[0].total;
});
}
/**
* 导出用户列表
* @returns {*}
*/
exportUserList() {
return mysqlCli.query(
`select id, user_phone userPhone, user_name userName, create_time createTime from ${TABLE_USER};`
);
... ...
... ... @@ -35,6 +35,7 @@ router.get('/api/activity/exportArticleList', activity.exportArticleList);
// 用户管理[ajax]
router.post('/api/user/delete', user.deleteUser);
router.get('/api/user/list', user.userList);
router.get('/api/user/exportUserList', user.exportUserList);
module.exports = router;
... ...
... ... @@ -23,19 +23,6 @@
</thead>
<tbody class="article-list">
{{#each articleList}}
<tr class="even pointer">
<td>{{id}}</td>
<td>{{goodCount}}</td>
<td>{{userName}}</td>
<td>{{phone}}</td>
<td>{{createTime}}</td>
<td>
<button class="btn btn-danger btn-delete-article" data-id="{{id}}">删除文章
</button>
</td>
</tr>
{{/each}}
</tbody>
</table>
</div>
... ...
... ... @@ -5,7 +5,7 @@
<div class="col-md-12 col-sm-12 col-xs-12">
<div class="x_panel">
<div class="x_title">
<button class="btn btn-success btn-export-user-list" data-id="{{id}}">导出用户列表</button>
<button class="btn btn-primary btn-export-user-list" data-id="{{id}}">导出用户列表</button>
<div class="clearfix"></div>
</div>
<div class="x_content">
... ... @@ -21,24 +21,11 @@
</tr>
</thead>
<tbody>
{{#each userList}}
<tr class="even pointer">
<td class="">{{id}}</td>
<td class="">{{userName}}</td>
<td class="">{{userPhone}}</td>
<td class="">{{createTime}}</td>
<td class="">
<button class="btn btn-danger btn-delete-user" data-id="{{id}}">删除用户
</button>
</td>
</tr>
{{/each}}
<tbody class="user-list">
</tbody>
</table>
</div>
<div class="table-pagination user-pagination pull-right"></div>
</div>
</div>
</div>
... ...
require('admin/user.page.css');
require('bootpag/lib/jquery.bootpag.min');
const _ = require('lodash');
function bind_delete_user() {
const deleteFn = function() {
... ... @@ -27,9 +30,59 @@ function bind_export_user_list() {
$('.btn-export-user-list').on('click', exportFn);
}
function bind_table_pagination() {
const $ul = $('.user-list');
const $up = $('.user-pagination');
const fetchRender = (pageNo, pageSize) => {
$.ajax({
url: '/admin/api/user/list',
data: {
pageNo,
pageSize
}
})
.then(result => {
const list = result.data;
const totalPage = result.totalPage;
let html = '';
_.each(list, item => {
html += `
<tr class="even pointer">
<td class="">${item.id}</td>
<td class="">${item.userName}</td>
<td class="">${item.userPhone}</td>
<td class="">${item.createTime}</td>
<td class="">
<button class="btn btn-danger btn-delete-user" data-id="${item.id}">删除用户
</button>
</td>
</tr>`;
});
$ul.html(html);
if (pageNo === 1) {
$up.bootpag({
total: totalPage,
page: 1,
maxVisible: 10,
}).on('page', function(event, num) {
fetchRender(num, 8);
});
}
});
};
fetchRender(1, 8);
}
(function() {
bind_delete_user();
bind_export_user_list();
bind_table_pagination();
}());
... ...