Authored by 邱骏

yohood答题

/**
* 优惠券管理controller
* @author: leo <y.huang@yoho.cn>
* @date: 08/08/2018
*/
// const _ = require('lodash');
const CouponModel = require('../models/coupon');
const DO_SUCCESS = '操作成功';
const GET_SUCCESS = '获取成功';
const couponController = {
couponListPage(req, res) {
res.render('coupon/list', {
bodyClass: 'nav-md',
module: 'admin',
page: 'coupon'
});
},
async couponList(req, res) {
const pageNo = req.query.pageNo || 1;
const pageSize = req.query.pageSize || 20;
let totalCount = await req.ctx(CouponModel).allCouponNum();
let list = await req.ctx(CouponModel).couponList({pageNo, pageSize});
res.json({
code: 200,
data: list,
pageNo: +pageNo,
pageSize: +pageSize,
totalCount,
totalPage: Math.ceil(totalCount / pageSize),
message: GET_SUCCESS
});
},
async createCoupon(req, res) {
let param = req.body;
await req.ctx(CouponModel).couponCreate(param);
res.json({
code: 200,
message: DO_SUCCESS
});
},
async updateConpon(req, res) {
let param = req.body;
await req.ctx(CouponModel).couponUpdate(param);
res.json({
code: 200,
message: DO_SUCCESS
});
},
//
};
module.exports = couponController;
... ...
/* eslint-disable array-callback-return */
const mysqlCli = global.yoho.utils.mysqlCli;
const _ = require('lodash');
const TABLE_COUPON = 'act_coupon';
const TABLE_COUPON_NO = 'act_coupon_no';
const TABLE_COUPON_USER = 'act_coupon_user';
const toLine = (str) => {
return str.replace(/([A-Z])/g, '_$1').toLowerCase();
};
class CouponModel extends global.yoho.BaseModel {
constructor(ctx) {
super(ctx);
}
couponList({pageNo, pageSize}) {
return mysqlCli.query(
`select id, coupon_name couponName, coupon_desc couponDesc, shop_name shopName, shop_logo_url shopLogoUrl, status, type, sort , create_time createTime
from ${TABLE_COUPON}
order by sort desc ,create_time desc
limit :start, :page;`, {
start: (pageNo - 1) * pageSize,
page: _.parseInt(pageSize)
});
}
couponCreate(obj) {
return mysqlCli.insert(
`insert into ${TABLE_COUPON} (coupon_name, coupon_desc, shop_name, shop_logo_url, status,type,sort)
values (:couponName, :couponDesc, :shopName, :shopLogoUrl, :status, :type, :sort);`,
{
couponName: obj.couponName,
couponDesc: obj.couponDesc,
shopName: obj.shopName,
shopLogoUrl: obj.shopLogoUrl,
status: obj.status,
type: obj.type,
sort: obj.sort
});
}
couponUpdate(obj) {
let sql = `UPDATE ${TABLE_COUPON} SET `;
let sqlParam = {};
let keyArr = Object.keys(obj);
keyArr.map((value, index) => {
if (value !== 'id') {
let col = toLine(value);
sql += col + ' = :' + value;
if (index !== (keyArr.length - 1)) {
sql += ',';
}
}
sqlParam[value] = obj[value];
});
sql += 'WHERE id = :id';
return mysqlCli.update(
sql,
sqlParam
);
}
allCouponNum() {
return mysqlCli.query(
`select count(*) as total from ${TABLE_COUPON};`
);
}
couponNoListByCId(couponId) {
return mysqlCli.query(
`select id, coupon_id couponId,coupon_no couponNo,send_flag sendFlag,create_time createTime
from ${TABLE_COUPON_NO}
where coupon_id = :couponId `, {couponId: couponId});
}
couponNoListJoinUser(couponId) {
return mysqlCli.query(
`select cn.coupon_id couponId,cn.coupon_no couponNo,cn.send_flag sendFlag,cu.user_id userId,cu.create_time createTime
from ${TABLE_COUPON_NO} as cn left join ${TABLE_COUPON_USER} as cu
where coupon_id = :couponId `, {couponId: couponId});
}
couponUserListByCId(couponId) {
return mysqlCli.query(
`select id, coupon_id couponId,coupon_no_id couponNoId,user_id userId,create_time createTime
from ${TABLE_COUPON_USER}
where coupon_id = :couponId `, {couponId: couponId});
}
}
module.export = CouponModel;
... ...
... ... @@ -8,6 +8,7 @@ const router = express.Router(); // eslint-disable-line
const admin = require('./controllers/admin');
const activity = require('./controllers/activity');
const user = require('./controllers/user');
const coupon = require('./controllers/coupon');
// 管理员[page]
router.get('/login', admin.loginPage);
... ... @@ -36,6 +37,8 @@ router.get('/user/prize_users_list', user.prizeUsersListPage);
router.get('/user/login-log', user.userLoginLog);
router.get('/user/exportUserLoginLog', user.exportUserLoginLog);
// 优惠券管理[page]
router.get('/coupon/list', coupon.couponListPage);
// 管理员[ajax]
router.post('/api/login', admin.login);
... ... @@ -54,7 +57,10 @@ router.get('/api/activity/exportArticleList', activity.exportArticleList);
router.post('/api/activity/createArticle', activity.createArticle);
router.post('/api/activity/createY100Article', activity.createY100Article);
// 优惠券管理[ajax]
router.get('/api/coupon/list', coupon.couponList);
router.post('/api/coupon/add', coupon.createCoupon);
router.post('/api/coupon/update', coupon.updateConpon);
// 用户管理[ajax]
router.post('/api/user/delete', user.deleteUser);
... ...
<!-- 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">
<button class="btn btn-primary btn-export-user-list" data-id="{{id}}">导出用户列表</button>
<div class="clearfix"></div>
</div>
<div class="x_content">
<div class="table-responsive">
<table class="table table-bordered">
<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">商铺图标</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 class="user-list">
</tbody>
</table>
</div>
<div class="table-pagination user-pagination pull-right"></div>
</div>
</div>
</div>
</div>
</div>
</div>
<!-- /page content -->
... ...
/* eslint-disable eqeqeq */
require('admin/user.page.css');
require('bootpag/lib/jquery.bootpag.min');
const _ = require('lodash');
function bind_table_pagination() {
const $ul = $('.user-list');
const $up = $('.user-pagination');
const fetchRender = (pageNo, pageSize) => {
$.ajax({
url: '/admin/api/coupon/list',
data: {
pageNo,
pageSize
}
})
.then(result => {
const list = result.data;
const totalPage = result.totalPage;
let html = '';
_.each(list, item => {
if (item.status) {
item.statusText = '已上架';
} else {
item.statusText = '已下架';
}
if (item.type == 1) {
item.typeText = '通用码';
} else if (item.type == 2) {
item.typeText = '一人一码';
} else if (item.type == 3) {
item.typeText = '不用码';
}
html += `
<tr class="even pointer">
<td class="">${item.id}</td>
<td class="">${item.couponName}</td>
<td class="">${item.couponDesc}</td>
<td class="">${item.shopName}</td>
<td class="">${item.shopLogoUrl}</td>
<td class="">${item.status}</td>
<td class="">${item.statusText}</td>
<td class="">${item.typeText}</td>
<td class="">${item.sort}</td>
<td class="">${item.createTime}</td>
<td class="">
<button class="btn btn-danger" data-id="${item.id}">导入券码
</button>
<button class="btn btn-danger" data-id="${item.id}">导出券码
</button>
<button class="btn btn-danger" 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, 20);
});
}
});
};
fetchRender(1, 20);
}
(function() {
bind_table_pagination();
}());
... ...