Authored by huangyi

领券中心

  1 +/**
  2 + * 优惠券管理controller
  3 + * @author: leo <y.huang@yoho.cn>
  4 + * @date: 08/08/2018
  5 + */
  6 +
  7 +// const _ = require('lodash');
  8 +const CouponModel = require('../models/coupon');
  9 +const DO_SUCCESS = '操作成功';
  10 +const GET_SUCCESS = '获取成功';
  11 +const couponController = {
  12 + couponListPage(req, res) {
  13 + res.render('coupon/list', {
  14 + bodyClass: 'nav-md',
  15 + module: 'admin',
  16 + page: 'coupon'
  17 + });
  18 + },
  19 + async couponList(req, res) {
  20 + const pageNo = req.query.pageNo || 1;
  21 + const pageSize = req.query.pageSize || 20;
  22 +
  23 + let totalCount = await req.ctx(CouponModel).allCouponNum();
  24 + let list = await req.ctx(CouponModel).couponList({pageNo, pageSize});
  25 +
  26 + res.json({
  27 + code: 200,
  28 + data: list,
  29 + pageNo: +pageNo,
  30 + pageSize: +pageSize,
  31 + totalCount,
  32 + totalPage: Math.ceil(totalCount / pageSize),
  33 + message: GET_SUCCESS
  34 + });
  35 +
  36 + },
  37 + async createCoupon(req, res) {
  38 + let param = req.body;
  39 +
  40 + await req.ctx(CouponModel).couponCreate(param);
  41 + res.json({
  42 + code: 200,
  43 + message: DO_SUCCESS
  44 + });
  45 + },
  46 +
  47 + async updateConpon(req, res) {
  48 + let param = req.body;
  49 +
  50 + await req.ctx(CouponModel).couponUpdate(param);
  51 + res.json({
  52 + code: 200,
  53 + message: DO_SUCCESS
  54 + });
  55 + },
  56 +
  57 + //
  58 +
  59 +};
  60 +
  61 +module.exports = couponController;
  1 +/* eslint-disable array-callback-return */
  2 +const mysqlCli = global.yoho.utils.mysqlCli;
  3 +const _ = require('lodash');
  4 +const TABLE_COUPON = 'act_coupon';
  5 +const TABLE_COUPON_NO = 'act_coupon_no';
  6 +const TABLE_COUPON_USER = 'act_coupon_user';
  7 +const toLine = (str) => {
  8 + return str.replace(/([A-Z])/g, '_$1').toLowerCase();
  9 +};
  10 +
  11 +class CouponModel extends global.yoho.BaseModel {
  12 + constructor(ctx) {
  13 + super(ctx);
  14 + }
  15 +
  16 + couponList({pageNo, pageSize}) {
  17 + return mysqlCli.query(
  18 + `select id, coupon_name couponName, coupon_desc couponDesc, shop_name shopName, shop_logo_url shopLogoUrl, status, type, sort , create_time createTime
  19 + from ${TABLE_COUPON}
  20 + order by sort desc ,create_time desc
  21 + limit :start, :page;`, {
  22 + start: (pageNo - 1) * pageSize,
  23 + page: _.parseInt(pageSize)
  24 + });
  25 + }
  26 +
  27 + couponCreate(obj) {
  28 + return mysqlCli.insert(
  29 + `insert into ${TABLE_COUPON} (coupon_name, coupon_desc, shop_name, shop_logo_url, status,type,sort)
  30 + values (:couponName, :couponDesc, :shopName, :shopLogoUrl, :status, :type, :sort);`,
  31 + {
  32 + couponName: obj.couponName,
  33 + couponDesc: obj.couponDesc,
  34 + shopName: obj.shopName,
  35 + shopLogoUrl: obj.shopLogoUrl,
  36 + status: obj.status,
  37 + type: obj.type,
  38 + sort: obj.sort
  39 + });
  40 + }
  41 +
  42 + couponUpdate(obj) {
  43 + let sql = `UPDATE ${TABLE_COUPON} SET `;
  44 + let sqlParam = {};
  45 + let keyArr = Object.keys(obj);
  46 +
  47 + keyArr.map((value, index) => {
  48 + if (value !== 'id') {
  49 + let col = toLine(value);
  50 +
  51 + sql += col + ' = :' + value;
  52 + if (index !== (keyArr.length - 1)) {
  53 + sql += ',';
  54 + }
  55 + }
  56 + sqlParam[value] = obj[value];
  57 + });
  58 + sql += 'WHERE id = :id';
  59 + return mysqlCli.update(
  60 + sql,
  61 + sqlParam
  62 + );
  63 + }
  64 +
  65 + allCouponNum() {
  66 + return mysqlCli.query(
  67 + `select count(*) as total from ${TABLE_COUPON};`
  68 + );
  69 + }
  70 +
  71 + couponNoListByCId(couponId) {
  72 + return mysqlCli.query(
  73 + `select id, coupon_id couponId,coupon_no couponNo,send_flag sendFlag,create_time createTime
  74 + from ${TABLE_COUPON_NO}
  75 + where coupon_id = :couponId `, {couponId: couponId});
  76 + }
  77 +
  78 + couponNoListJoinUser(couponId) {
  79 + return mysqlCli.query(
  80 + `select cn.coupon_id couponId,cn.coupon_no couponNo,cn.send_flag sendFlag,cu.user_id userId,cu.create_time createTime
  81 + from ${TABLE_COUPON_NO} as cn left join ${TABLE_COUPON_USER} as cu
  82 + where coupon_id = :couponId `, {couponId: couponId});
  83 + }
  84 +
  85 + couponUserListByCId(couponId) {
  86 + return mysqlCli.query(
  87 + `select id, coupon_id couponId,coupon_no_id couponNoId,user_id userId,create_time createTime
  88 + from ${TABLE_COUPON_USER}
  89 + where coupon_id = :couponId `, {couponId: couponId});
  90 + }
  91 +
  92 +}
  93 +
  94 +module.export = CouponModel;
@@ -8,6 +8,7 @@ const router = express.Router(); // eslint-disable-line @@ -8,6 +8,7 @@ const router = express.Router(); // eslint-disable-line
8 const admin = require('./controllers/admin'); 8 const admin = require('./controllers/admin');
9 const activity = require('./controllers/activity'); 9 const activity = require('./controllers/activity');
10 const user = require('./controllers/user'); 10 const user = require('./controllers/user');
  11 +const coupon = require('./controllers/coupon');
11 12
12 // 管理员[page] 13 // 管理员[page]
13 router.get('/login', admin.loginPage); 14 router.get('/login', admin.loginPage);
@@ -36,6 +37,8 @@ router.get('/user/prize_users_list', user.prizeUsersListPage); @@ -36,6 +37,8 @@ router.get('/user/prize_users_list', user.prizeUsersListPage);
36 router.get('/user/login-log', user.userLoginLog); 37 router.get('/user/login-log', user.userLoginLog);
37 router.get('/user/exportUserLoginLog', user.exportUserLoginLog); 38 router.get('/user/exportUserLoginLog', user.exportUserLoginLog);
38 39
  40 +// 优惠券管理[page]
  41 +router.get('/coupon/list', coupon.couponListPage);
39 42
40 // 管理员[ajax] 43 // 管理员[ajax]
41 router.post('/api/login', admin.login); 44 router.post('/api/login', admin.login);
@@ -54,7 +57,10 @@ router.get('/api/activity/exportArticleList', activity.exportArticleList); @@ -54,7 +57,10 @@ router.get('/api/activity/exportArticleList', activity.exportArticleList);
54 router.post('/api/activity/createArticle', activity.createArticle); 57 router.post('/api/activity/createArticle', activity.createArticle);
55 router.post('/api/activity/createY100Article', activity.createY100Article); 58 router.post('/api/activity/createY100Article', activity.createY100Article);
56 59
57 - 60 +// 优惠券管理[ajax]
  61 +router.get('/api/coupon/list', coupon.couponList);
  62 +router.post('/api/coupon/add', coupon.createCoupon);
  63 +router.post('/api/coupon/update', coupon.updateConpon);
58 64
59 // 用户管理[ajax] 65 // 用户管理[ajax]
60 router.post('/api/user/delete', user.deleteUser); 66 router.post('/api/user/delete', user.deleteUser);
  1 +<!-- page content -->
  2 +<div class="right_col" role="main">
  3 + <div class="">
  4 + <div class="row">
  5 + <div class="col-md-12 col-sm-12 col-xs-12">
  6 + <div class="x_panel">
  7 + <div class="x_title">
  8 + <button class="btn btn-primary btn-export-user-list" data-id="{{id}}">导出用户列表</button>
  9 + <div class="clearfix"></div>
  10 + </div>
  11 + <div class="x_content">
  12 + <div class="table-responsive">
  13 + <table class="table table-bordered">
  14 + <thead>
  15 + <tr class="headings">
  16 + <th class="column-title">ID</th>
  17 + <th class="column-title">券名称</th>
  18 + <th class="column-title">券描述</th>
  19 + <th class="column-title">商品名称</th>
  20 + <th class="column-title">商铺图标</th>
  21 + <th class="column-title">上下架状态</th>
  22 + <th class="column-title">券类型</th>
  23 + <th class="column-title">排序权重</th>
  24 + <th class="column-title">创建时间</th>
  25 + <th class="column-title">操作</th>
  26 + </tr>
  27 + </thead>
  28 + <tbody class="user-list">
  29 + </tbody>
  30 + </table>
  31 + </div>
  32 + <div class="table-pagination user-pagination pull-right"></div>
  33 + </div>
  34 + </div>
  35 + </div>
  36 + </div>
  37 + </div>
  38 +</div>
  39 +<!-- /page content -->
  1 +/* eslint-disable eqeqeq */
  2 +require('admin/user.page.css');
  3 +require('bootpag/lib/jquery.bootpag.min');
  4 +
  5 +const _ = require('lodash');
  6 +
  7 +function bind_table_pagination() {
  8 + const $ul = $('.user-list');
  9 + const $up = $('.user-pagination');
  10 +
  11 + const fetchRender = (pageNo, pageSize) => {
  12 + $.ajax({
  13 + url: '/admin/api/coupon/list',
  14 + data: {
  15 + pageNo,
  16 + pageSize
  17 + }
  18 + })
  19 + .then(result => {
  20 + const list = result.data;
  21 + const totalPage = result.totalPage;
  22 +
  23 + let html = '';
  24 +
  25 + _.each(list, item => {
  26 + if (item.status) {
  27 + item.statusText = '已上架';
  28 + } else {
  29 + item.statusText = '已下架';
  30 + }
  31 + if (item.type == 1) {
  32 + item.typeText = '通用码';
  33 + } else if (item.type == 2) {
  34 + item.typeText = '一人一码';
  35 + } else if (item.type == 3) {
  36 + item.typeText = '不用码';
  37 + }
  38 + html += `
  39 + <tr class="even pointer">
  40 + <td class="">${item.id}</td>
  41 + <td class="">${item.couponName}</td>
  42 + <td class="">${item.couponDesc}</td>
  43 + <td class="">${item.shopName}</td>
  44 + <td class="">${item.shopLogoUrl}</td>
  45 + <td class="">${item.status}</td>
  46 + <td class="">${item.statusText}</td>
  47 + <td class="">${item.typeText}</td>
  48 + <td class="">${item.sort}</td>
  49 + <td class="">${item.createTime}</td>
  50 + <td class="">
  51 + <button class="btn btn-danger" data-id="${item.id}">导入券码
  52 + </button>
  53 + <button class="btn btn-danger" data-id="${item.id}">导出券码
  54 + </button>
  55 + <button class="btn btn-danger" data-id="${item.id}">修改</button>
  56 + </td>
  57 + </tr>`;
  58 + });
  59 +
  60 + $ul.html(html);
  61 +
  62 + if (pageNo === 1) {
  63 + $up.bootpag({
  64 + total: totalPage,
  65 + page: 1,
  66 + maxVisible: 10,
  67 + }).on('page', function(event, num) {
  68 + fetchRender(num, 20);
  69 + });
  70 + }
  71 + });
  72 + };
  73 +
  74 + fetchRender(1, 20);
  75 +}
  76 +
  77 +(function() {
  78 + bind_table_pagination();
  79 +}());
  80 +
  81 +