Authored by 李奇

用户管理列表和删除导出添加

/**
* 文章controller
* @author: leo <qi.li@yoho.cn>
* @date: 23/06/2017
*/
const _ = require('lodash');
const moment = require('moment');
const excelExport = require('excel-export');
const ActivityModel = require('../models/activity');
const POST_SUCCESS = '操作成功';
const INVALID_PARAMS = '参数错误';
const timeFormat = (time) => {
if (_.isNumber(time)) {
time = moment.unix(time);
}
return moment(time).format('YYYY-MM-DD HH:mm:ss');
};
const activity = {
/**
* 活动列表
* @param req
* @param res
* @param next
*/
activityList(req, res, next) {
const title = req.body.title;
const startTime = req.body.startTime;
const endTime = req.body.endTime;
if (!title || !startTime || !endTime) {
return res.json({
code: 400,
message: INVALID_PARAMS
});
}
const params = {
title,
startTime,
endTime
};
req.ctx(ActivityModel).createActivity(params)
.then(() => {
return res.json({
code: 200,
message: POST_SUCCESS
});
})
.catch(next);
},
/**
* 活动列表页
* @param req
* @param res
* @param next
*/
activityListPage(req, res, next) {
req.ctx(ActivityModel).activityList()
.then(result => {
_.each(result, item => {
item.endTime = timeFormat(item.endTime);
item.startTime = timeFormat(item.startTime);
item.createTime = timeFormat(item.createTime);
});
res.render('activity/list', {
bodyClass: 'nav-md',
activityList: result,
module: 'admin',
page: 'activity'
});
})
.catch(next);
},
/**
* 新建活动
* @param req
* @param res
* @param next
*/
createActivity(req, res, next) {
const title = req.body.title;
const startTime = req.body.startTime;
const endTime = req.body.endTime;
if (!title || !startTime || !endTime) {
return res.json({
code: 400,
message: INVALID_PARAMS
});
}
const params = {
title,
startTime,
endTime
};
req.ctx(ActivityModel).createActivity(params)
.then(() => {
return res.json({
code: 200,
message: POST_SUCCESS
});
})
.catch(next);
},
/**
* 新建活动页
* @param req
* @param res
*/
createActivityPage(req, res) {
res.render('activity/create', {
bodyClass: 'nav-md',
module: 'admin',
page: 'activity'
});
},
/**
* 活动删除
* @param req
* @param res
* @param next
*/
deleteActivity(req, res, next) {
const id = req.body.id;
if (!id) {
return res.json({
code: 400,
message: INVALID_PARAMS
});
}
req.ctx(ActivityModel).deleteActivity(id)
.then(() => {
return res.json({
code: 200,
message: POST_SUCCESS
});
})
.catch(next);
},
/**
* 活动文章列表
* @param req
* @param res
* @param next
*/
actArticleListPage(req, res, next) {
const actId = req.query.actId;
req.ctx(ActivityModel).actArticleList(actId)
.then(result => {
_.each(result, item => {
item.createTime = timeFormat(item.createTime);
});
res.render('activity/article-list', {
actId,
bodyClass: 'nav-md',
articleList: result,
module: 'admin',
page: 'activity'
});
})
.catch(next);
},
/**
* 删除文章
* @param req
* @param res
* @param next
*/
deleteArticle(req, res, next) {
const id = req.body.id;
if (!id) {
return res.json({
code: 400,
message: INVALID_PARAMS
});
}
req.ctx(ActivityModel).deleteArticle(id)
.then(() => {
return res.json({
code: 200,
message: POST_SUCCESS
});
})
.catch(next);
},
/**
* 文章列表导出
* @param req
* @param res
* @param next
*/
exportArticleList(req, res, next) {
const actId = req.query.actId;
if (!actId) {
return res.json({
code: 400,
message: INVALID_PARAMS
});
}
let conf = {
name: 'mysheet',
cols: [
{
caption: '文章ID',
type: 'string'
},
{
caption: '赞数',
type: 'number'
},
{
caption: '发布者',
type: 'string'
},
{
caption: '手机号',
type: 'string'
},
{
caption: '创建时间',
type: 'string'
}
],
rows: []
};
req.ctx(ActivityModel).actArticleList(actId)
.then(result => {
let temp = [];
_.each(result, item => {
temp = [];
temp.push(item.id);
temp.push(item.goodCount);
temp.push(item.userName);
temp.push(item.phone);
temp.push(timeFormat(item.createTime));
conf.rows.push(temp);
});
let exportFile = excelExport.execute(conf);
res.setHeader('Content-Type', 'application/vnd.openxmlformats');
res.setHeader('Content-Disposition', 'attachment; filename=articleList.xlsx');
res.end(exportFile, 'binary');
})
.catch(next);
},
/**
* 活动参与用户列表
* @param req
* @param res
* @param next
*/
activityUserListPage(req, res, next) {
req.ctx(ActivityModel).activityList()
.then(result => {
res.render('activity/user-list', {
bodyClass: 'nav-md',
activityList: result,
module: 'admin',
page: 'activity'
});
})
.catch(next);
}
};
module.exports = activity;
... ...
... ... @@ -4,22 +4,8 @@
* @date: 23/06/2017
*/
const _ = require('lodash');
const moment = require('moment');
const excelExport = require('excel-export');
const AdminModel = require('../models/admin');
const POST_SUCCESS = '操作成功';
const INVALID_PARAMS = '参数错误';
const timeFormat = (time) => {
if (_.isNumber(time)) {
time = moment.unix(time);
}
return moment(time).format('YYYY-MM-DD HH:mm:ss');
};
const article = {
const adminController = {
/**
* 首页
* @param req
... ... @@ -90,279 +76,8 @@ const article = {
code: 200,
message: '登出成功'
});
},
/**
* 活动列表
* @param req
* @param res
* @param next
*/
activityList(req, res, next) {
const title = req.body.title;
const startTime = req.body.startTime;
const endTime = req.body.endTime;
if (!title || !startTime || !endTime) {
return res.json({
code: 400,
message: INVALID_PARAMS
});
}
const params = {
title,
startTime,
endTime
};
req.ctx(AdminModel).createActivity(params)
.then(() => {
return res.json({
code: 200,
message: POST_SUCCESS
});
})
.catch(next);
},
/**
* 活动列表页
* @param req
* @param res
* @param next
*/
activityListPage(req, res, next) {
req.ctx(AdminModel).activityList()
.then(result => {
_.each(result, item => {
item.endTime = timeFormat(item.endTime);
item.startTime = timeFormat(item.startTime);
item.createTime = timeFormat(item.createTime);
});
res.render('activity/list', {
bodyClass: 'nav-md',
activityList: result,
module: 'admin',
page: 'activity'
});
})
.catch(next);
},
/**
* 新建活动
* @param req
* @param res
* @param next
*/
createActivity(req, res, next) {
const title = req.body.title;
const startTime = req.body.startTime;
const endTime = req.body.endTime;
if (!title || !startTime || !endTime) {
return res.json({
code: 400,
message: INVALID_PARAMS
});
}
const params = {
title,
startTime,
endTime
};
req.ctx(AdminModel).createActivity(params)
.then(() => {
return res.json({
code: 200,
message: POST_SUCCESS
});
})
.catch(next);
},
/**
* 新建活动页
* @param req
* @param res
*/
createActivityPage(req, res) {
res.render('activity/create', {
bodyClass: 'nav-md',
module: 'admin',
page: 'activity'
});
},
/**
* 活动删除
* @param req
* @param res
* @param next
*/
deleteActivity(req, res, next) {
const id = req.body.id;
if (!id) {
return res.json({
code: 400,
message: INVALID_PARAMS
});
}
req.ctx(AdminModel).deleteActivity(id)
.then(() => {
return res.json({
code: 200,
message: POST_SUCCESS
});
})
.catch(next);
},
/**
* 活动文章列表
* @param req
* @param res
* @param next
*/
actArticleListPage(req, res, next) {
const actId = req.query.actId;
req.ctx(AdminModel).actArticleList(actId)
.then(result => {
_.each(result, item => {
item.createTime = timeFormat(item.createTime);
});
res.render('activity/article-list', {
actId,
bodyClass: 'nav-md',
articleList: result,
module: 'admin',
page: 'activity'
});
})
.catch(next);
},
/**
* 删除文章
* @param req
* @param res
* @param next
*/
deleteArticle(req, res, next) {
const id = req.body.id;
if (!id) {
return res.json({
code: 400,
message: INVALID_PARAMS
});
}
req.ctx(AdminModel).deleteArticle(id)
.then(() => {
return res.json({
code: 200,
message: POST_SUCCESS
});
})
.catch(next);
},
/**
* 文章列表导出
* @param req
* @param res
* @param next
*/
exportArticleList(req, res, next) {
const actId = req.query.actId;
if (!actId) {
return res.json({
code: 400,
message: INVALID_PARAMS
});
}
let conf = {
name: 'mysheet',
cols: [
{
caption: '文章ID',
type: 'string'
},
{
caption: '赞数',
type: 'number'
},
{
caption: '发布者',
type: 'string'
},
{
caption: '手机号',
type: 'string'
},
{
caption: '创建时间',
type: 'string'
}
],
rows: []
};
req.ctx(AdminModel).actArticleList(actId)
.then(result => {
let temp = [];
_.each(result, item => {
temp = [];
temp.push(item.id);
temp.push(item.goodCount);
temp.push(item.userName);
temp.push(item.phone);
temp.push(timeFormat(item.createTime));
conf.rows.push(temp);
});
let exportFile = excelExport.execute(conf);
res.setHeader('Content-Type', 'application/vnd.openxmlformats');
res.setHeader('Content-Disposition', 'attachment; filename=articleList.xlsx');
res.end(exportFile, 'binary');
})
.catch(next);
},
/**
* 活动参与用户列表
* @param req
* @param res
* @param next
*/
activityUserListPage(req, res, next) {
req.ctx(AdminModel).activityList()
.then(result => {
res.render('activity/user-list', {
bodyClass: 'nav-md',
activityList: result,
module: 'admin',
page: 'activity'
});
})
.catch(next);
}
};
module.exports = article;
module.exports = adminController;
... ...
/**
* 用户管理controller
* @author: leo <qi.li@yoho.cn>
* @date: 23/06/2017
*/
const _ = require('lodash');
const moment = require('moment');
const excelExport = require('excel-export');
const UserModel = require('../models/user');
const DO_SUCCESS = '操作成功';
const timeFmt = (time) => {
if (_.isNumber(time)) {
time = moment.unix(time);
}
return moment(time).format('YYYY-MM-DD HH:mm:ss');
};
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
);
});
res.render('user/list', {
bodyClass: 'nav-md',
userList: result,
module: 'admin',
page: 'user'
});
})
.catch(next);
},
/**
* 用户删除
* @param req
* @param res
* @param next
*/
deleteUser(req, res, next) {
const userId = req.body.userId;
req.ctx(UserModel).deleteUser(userId)
.then(() => {
res.json({
code: 200,
message: DO_SUCCESS
});
})
.catch(next);
},
/**
* 用户列表导出
* @param req
* @param res
* @param next
*/
exportUserList(req, res, next) {
let conf = {
name: 'mysheet',
cols: [
{
caption: '用户ID',
type: 'number'
},
{
caption: '昵称',
type: 'string'
},
{
caption: '手机号',
type: 'string'
},
{
caption: '创建时间',
type: 'string'
}
],
rows: []
};
req.ctx(UserModel).userList()
.then(result => {
let temp = [];
_.each(result, item => {
temp = [];
temp.push(item.id);
temp.push(item.userName);
temp.push(item.userPhone);
temp.push(timeFmt(item.createTime));
conf.rows.push(temp);
});
let exportFile = excelExport.execute(conf);
res.setHeader('Content-Type', 'application/vnd.openxmlformats');
res.setHeader('Content-Disposition', 'attachment; filename=articleList.xlsx');
res.end(exportFile, 'binary');
})
.catch(next);
}
};
module.exports = userController;
... ...
/**
* 管理model
* @author: leo <qi.li@yoho.cn>
* @date: 28/06/2017
*/
const mysqlCli = global.yoho.utils.mysqlCli;
const TABLE_ACTIVITY = 'activity';
const TABLE_ACT_ARTICLE = 'act_article';
const TABLE_USER = 'user';
class AdminModel extends global.yoho.BaseModel {
constructor(ctx) {
super(ctx);
}
/**
* 创建活动
* @param title 活动标题
* @param startTime 活动开始时间
* @param endTime 活动结束时间
* @returns {*}
*/
createActivity({title, startTime, endTime}) {
return mysqlCli.insert(
`insert into ${TABLE_ACTIVITY} (title, start_time, end_time) values (:title, :startTime, :endTime);`,
{
title,
startTime,
endTime
}
);
}
/**
* 活动列表
* @returns {*}
*/
activityList() {
return mysqlCli.query(
`select id, title, start_time startTime, end_time endTime, create_time createTime from ${TABLE_ACTIVITY};`
);
}
/**
* 删除活动
* @param actId 活动ID
* @returns {*}
*/
deleteActivity(actId) {
return mysqlCli.delete(
`delete from ${TABLE_ACTIVITY} where id = :actId;`,
{
actId
}
);
}
/**
* 活动文章列表
* @returns {*}
*/
actArticleList(actId) {
return mysqlCli.query(
`select taa.id, taa.create_time createTime, taa.good_count goodCount,
tu.user_name userName, tu.user_phone phone
from ${TABLE_ACT_ARTICLE} taa
left join ${TABLE_USER} tu
on taa.user_id = tu.id
where act_id = :actId;`, {
actId
}
);
}
/**
* 删除文章
* @param id 文章ID
* @returns {*}
*/
deleteArticle(id) {
return mysqlCli.delete(
`delete from ${TABLE_ACT_ARTICLE} where id = :id;`,
{
id
}
);
}
/**
* 参与活动用户列表
* @returns {*}
*/
activityUserList(actId) {
return mysqlCli.query(
`select tu.user_phone phone
from ${TABLE_ACTIVITY} ta
inner join act_article taa
on ta.id = taa.act_id
inner join user tu
on taa.user_id = tu.id
where ta.id = :actId;`, {
actId
}
);
}
}
module.exports = AdminModel;
... ...
/**
* 管理model
* 管理model
* @author: leo <qi.li@yoho.cn>
* @date: 28/06/2017
*/
const mysqlCli = global.yoho.utils.mysqlCli;
const TABLE_ACTIVITY = 'activity';
const TABLE_ACT_ARTICLE = 'act_article';
const TABLE_USER = 'user';
class AdminModel extends global.yoho.BaseModel {
constructor(ctx) {
super(ctx);
}
/**
* 创建活动
* @param title 活动标题
* @param startTime 活动开始时间
* @param endTime 活动结束时间
* @returns {*}
*/
createActivity({title, startTime, endTime}) {
return mysqlCli.insert(
`insert into ${TABLE_ACTIVITY} (title, start_time, end_time) values (:title, :startTime, :endTime);`,
{
title,
startTime,
endTime
}
);
}
/**
* 活动列表
* @returns {*}
*/
activityList() {
return mysqlCli.query(
`select id, title, start_time startTime, end_time endTime, create_time createTime from ${TABLE_ACTIVITY};`
);
}
/**
* 删除活动
* @param actId 活动ID
* @returns {*}
*/
deleteActivity(actId) {
return mysqlCli.delete(
`delete from ${TABLE_ACTIVITY} where id = :actId;`,
{
actId
}
);
}
/**
* 活动文章列表
* @returns {*}
*/
actArticleList(actId) {
return mysqlCli.query(
`select taa.id, taa.create_time createTime, taa.good_count goodCount,
tu.user_name userName, tu.user_phone phone
from ${TABLE_ACT_ARTICLE} taa
inner join ${TABLE_USER} tu
on taa.user_id = tu.id
where act_id = :actId;`, {
actId
}
);
}
/**
* 删除文章
* @param id 文章ID
* @returns {*}
*/
deleteArticle(id) {
return mysqlCli.delete(
`delete from ${TABLE_ACT_ARTICLE} where id = :id;`,
{
id
}
);
}
/**
* 参与活动用户列表
* @returns {*}
*/
activityUserList(actId) {
return mysqlCli.query(
`select tu.user_phone phone
from ${TABLE_ACTIVITY} ta
inner join act_article taa
on ta.id = taa.act_id
inner join user tu
on taa.user_id = tu.id
where ta.id = :actId;`, {
actId
}
);
}
}
module.exports = AdminModel;
... ...
/**
* 用户管理model
* @author: leo <qi.li@yoho.cn>
* @date: 28/06/2017
*/
const mysqlCli = global.yoho.utils.mysqlCli;
const TABLE_USER = 'user';
class UserModel extends global.yoho.BaseModel {
constructor(ctx) {
super(ctx);
}
/**
* 用户列表
* @returns {*}
*/
userList() {
return mysqlCli.query(
`select id, user_phone userPhone, user_name userName, create_time createTime from ${TABLE_USER};`
);
}
/**
* 用户删除
* @returns {*}
*/
deleteUser(userId) {
return mysqlCli.delete(
`delete from ${TABLE_USER} where id = :userId;`, {
userId
}
);
}
}
module.exports = UserModel;
... ...
... ... @@ -6,23 +6,36 @@
const express = require('express');
const router = express.Router(); // eslint-disable-line
const admin = require('./controllers/admin');
const activity = require('./controllers/activity');
const user = require('./controllers/user');
router.get('/login', admin.loginPage);
router.get('/home', admin.homePage);
// 活动管理
router.get('/activity/list', admin.activityListPage);
router.get('/activity/create', admin.createActivityPage);
router.get('/activity/user', admin.activityUserListPage);
router.get('/activity/article', admin.actArticleListPage);
// 活动管理[menu]
router.get('/activity/list', activity.activityListPage);
router.get('/activity/create', activity.createActivityPage);
router.get('/activity/article', activity.actArticleListPage);
// 用户管理[menu]
router.get('/user/list', user.userListPage);
// ajax
router.post('/api/login', admin.login);
router.post('/api/logout', admin.logout);
router.post('/api/activity/list', admin.activityList);
router.post('/api/activity/create', admin.createActivity);
router.post('/api/activity/delete', admin.deleteActivity);
router.post('/api/activity/deleteArticle', admin.deleteArticle);
router.get('/api/activity/exportArticleList', admin.exportArticleList);
// 活动管理[ajax]
router.post('/api/activity/list', activity.activityList);
router.post('/api/activity/create', activity.createActivity);
router.post('/api/activity/delete', activity.deleteActivity);
router.post('/api/activity/deleteArticle', activity.deleteArticle);
router.get('/api/activity/exportArticleList', activity.exportArticleList);
// 用户管理[ajax]
router.post('/api/user/delete', user.deleteUser);
router.get('/api/user/exportUserList', user.exportUserList);
module.exports = router;
... ...
... ... @@ -8,7 +8,7 @@
<h2>文章列表</h2>
<div class="clearfix"></div>
</div>
<button class="btn btn-primary btn-export-article" data-id="{{actId}}">列表导出</button>
<button class="btn btn-primary btn-export-article" data-id="{{actId}}">导出列表</button>
<div class="x_content">
<div class="table-responsive">
<table class="table table-striped jambo_table bulk_action">
... ...
<!-- page content -->
<div class="right_col" role="main">
<div class="">
<div class="row">
<div class="col-md-12 col-sm-12 col-xs-12">
<div class="x_panel">
<div class="x_title">
<h2>活动列表</h2>
<div class="clearfix"></div>
</div>
<div class="x_content">
<div class="table-responsive">
<table class="table table-striped jambo_table bulk_action">
<thead>
<tr class="headings">
<th class="column-title">活动名称</th>
<th class="column-title">开始时间</th>
<th class="column-title">结束时间</th>
<th class="column-title">创建时间</th>
<th class="column-title">活动状态</th>
<th class="column-title">操作</th>
</tr>
</thead>
<tbody>
{{#each activityList}}
<tr class="even pointer">
<td class=" ">{{title}}</td>
<td class=" ">{{startTime}}</td>
<td class=" ">{{endTime}}</td>
<td class=" ">{{createTime}}</td>
<td class=" ">未开始</td>
<td class=" ">
<button class="btn btn-info">活动文章</button>
<button class="btn btn-success">用户统计</button>
<button class="btn btn-primary">编辑</button>
<button class="btn btn-danger btn-delete" data-id="{{id}}">删除</button>
</td>
</tr>
{{/each}}
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<!-- /page content -->
... ... @@ -8,32 +8,30 @@
<h2>用户列表</h2>
<div class="clearfix"></div>
</div>
<button class="btn btn-primary btn-export-user-list" data-id="{{id}}">导出列表</button>
<div class="x_content">
<div class="table-responsive">
<table class="table table-striped jambo_table bulk_action">
<thead>
<tr class="headings">
<th class="column-title">活动ID</th>
<th class="column-title">活动名称</th>
<th class="column-title">开始时间</th>
<th class="column-title">结束时间</th>
<th class="column-title">ID</th>
<th class="column-title">昵称</th>
<th class="column-title">手机号</th>
<th class="column-title">创建时间</th>
<th class="column-title">操作</th>
</tr>
</thead>
<tbody>
{{#each activityList}}
{{#each userList}}
<tr class="even pointer">
<td class="">{{id}}</td>
<td class="">{{title}}</td>
<td class="">{{startTime}}</td>
<td class="">{{endTime}}</td>
<td class="">{{userName}}</td>
<td class="">{{userPhone}}</td>
<td class="">{{createTime}}</td>
<td class="">
<a class="btn btn-info" href="/admin/activity/article?actId={{id}}">活动文章</a>
<button class="btn btn-danger btn-delete" data-id="{{id}}">删除活动</button>
<button class="btn btn-danger btn-delete-user" data-id="{{id}}">删除用户
</button>
</td>
</tr>
{{/each}}
... ...
function bind_delete_user() {
const deleteFn = function() {
const userId = $(this).data('id');
$.ajax({
method: 'post',
url: '/admin/api/user/delete',
data: {
userId
}
})
.then(() => {
location.reload();
});
};
$('.btn-delete-user').on('click', deleteFn);
}
function bind_export_user_list() {
const exportFn = function() {
window.open('/admin/api/user/exportUserList', '_blank');
};
$('.btn-export-user-list').on('click', exportFn);
}
(function() {
bind_delete_user();
bind_export_user_list();
}());
... ...