Authored by huangyi

领券

... ... @@ -38,16 +38,34 @@ const couponController = {
let data = await loadExcel(file.path);
// let arr = [];
//
// for (let item of data) {
// let rowArr = [id];
//
// rowArr.push(item.couponNo);
// arr.push(rowArr);1
// }
console.log(data);
if (!data.length) {
return res.json({
code: 201,
result: false,
message: '文件内容为空'
});
}
try {
// 通用券只允许导入一条数据
let coupon = await req.ctx(CouponModel).couponById(id);
if (coupon[0].type === 3) {
return res.json({
code: 204,
result: false,
message: '无码券不允许导入数据'
});
}
if (coupon[0].type === 1 && data.length > 1) {
return res.json({
code: 202,
result: false,
message: '通用券只允许导入一条数据'
});
}
for (let item of data) {
let param = {couponId: id, couponNo: item.couponNo};
... ... @@ -55,6 +73,7 @@ const couponController = {
}
return res.json({
code: 200,
result: true,
message: DO_SUCCESS
});
} catch (e) {
... ... @@ -172,6 +191,32 @@ const couponController = {
.catch(next);
},
deleteCoupon: async function(req, res) {
let id = req.query.id;
// 查询有没有用户表里有没,如有返回不给删
let userData = await req.ctx(CouponModel).couponUserListByCId(id);
if (userData.length) {
return res.json({
code: 201,
result: false,
message: '删除失败,有用户数据'
});
}
// 查询查no表里并且删除
await req.ctx(CouponModel).deleteCouponNoByCId(id);
// 删除主表
await req.ctx(CouponModel).deleteCouponById(id);
return res.json({
code: 200,
result: true,
message: DO_SUCCESS
});
}
};
... ...
... ... @@ -40,7 +40,6 @@ class CouponModel extends global.yoho.BaseModel {
}
couponNoAdd(arr) {
// return mysqlCli.insert(`insert into ${TABLE_COUPON_NO} (coupon_id, coupon_no) values ?`, arr);
return mysqlCli.insert(`insert into ${TABLE_COUPON_NO} (coupon_id, coupon_no) values (:couponId,:couponNo)`, arr);
}
... ... @@ -68,7 +67,7 @@ class CouponModel extends global.yoho.BaseModel {
);
} else {
return await mysqlCli.insert(
let insertId = await 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);`,
{
... ... @@ -80,6 +79,16 @@ class CouponModel extends global.yoho.BaseModel {
type: obj.type,
sort: obj.sort
});
console.log(insertId);
if (obj.type === 3) {
await mysqlCli.insert(
`insert into ${TABLE_COUPON_NO} (coupon_id) values (:couponId);`,
{
couponId: insertId,
});
}
return Promise.resolve({code: 200, result: true});
}
}
... ... @@ -105,6 +114,14 @@ class CouponModel extends global.yoho.BaseModel {
where coupon_id = :couponId `, {couponId: couponId});
}
deleteCouponNoByCId(couponId) {
return mysqlCli.query(`DELETE FROM ${TABLE_COUPON_NO} WHERE coupon_id = :couponId `, {couponId});
}
deleteCouponById(id) {
return mysqlCli.query(`DELETE FROM ${TABLE_COUPON} WHERE id = :id `, {id});
}
}
module.exports = CouponModel;
... ...
... ... @@ -66,6 +66,7 @@ router.get('/api/coupon/list', coupon.couponList);
router.post('/api/coupon/modify', coupon.modifyCoupon);
router.post('/api/coupon/batchAddNo', mutilpartMiddleware, coupon.loadCouponNoList);
router.get('/api/coupon/downloadNo', coupon.downloadNoList);
router.get('/api/coupon/delete', coupon.deleteCoupon);
// 用户管理[ajax]
router.post('/api/user/delete', user.deleteUser);
... ...
... ... @@ -7,7 +7,7 @@
<div class="clearfix"></div>
</div>
<form id="createForm" class="form-horizontal form-label-left">
<input class="form-imput" type="hidden" data-type="id" value="{{id}}">
<input class="form-imput" type="hidden" data-type="id" id="id" value="{{id}}">
<input id="type" type="hidden" value="{{type}}">
<input id="status" type="hidden" value="{{status}}">
<div class="item form-group">
... ...
... ... @@ -15,7 +15,6 @@
<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>
... ...
... ... @@ -95,10 +95,10 @@ module.exports = {
},
mysql: {
connect: {
host: 'localhost',
host: '0.0.0.0',
port: '3306',
user: 'root',
password: ''
password: '123456'
},
database: 'yoho_activity_platform',
},
... ...
... ... @@ -123,7 +123,13 @@ function bindEditPageEvent() {
} else {
$($('input[name=status]')[0]).attr('checked', true);
}
if ($('#id').val()) {
$('input[name=type]').each(function(index, el) {
$(el).attr('disabled', true);
});
}
}
radioInit();
$('.btn-upload-thumb').on('click', function() {
uploadedFn = new GenUploadedFn($(this));
... ...
... ... @@ -9,7 +9,9 @@ function bind_table_pagination() {
const $up = $('.coupon-pagination');
let uploadFn = function() {
let $form = $('#uploadForm')[0];
let id = $(this).data('id');
let $form = $('#uploadForm_' + id)[0];
let formData = new FormData($form);
formData.append('id', $(this).data('id'));
... ... @@ -22,8 +24,12 @@ function bind_table_pagination() {
cache: false,
processData: false,
contentType: false,
success: function() {
alert('导入成功');//eslint-disable line
success: function(data) {
if (!data.result) {
alert(data.message);
} else {
alert('导入成功');// eslint-disable line
}
}
});
}
... ... @@ -58,23 +64,23 @@ function bind_table_pagination() {
}
html += `
<tr class="even pointer">
<td class="">${item.id}</td>
<td class="">${item.id}</td>1
<td class="">${item.couponName}</td>
<td class="">${item.couponDesc}</td>
<td class="">${item.shopName}</td>
<td class=""><img src="${item.shopLogoUrl}" width="100%"/></td>
<td class=""><img src="${item.shopLogoUrl}" width="100" height="50"/></td>
<td class="">${item.statusText}</td>
<td class="">${item.typeText}</td>
<td class="">${item.sort}</td>
<td class="">${item.createTime}</td>
<td class="">
<form id="uploadForm" enctype="multipart/form-data">
<form id="uploadForm_${item.id}" enctype="multipart/form-data">
<input id="up_excel" name="up_excel" accept="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" style="display: inline-block; width: 80%;" type="file">
<button type="button" class="btn btn-danger btn-import" style="display: inline-block;" data-id="${item.id}">导入券码</button>
</form>
<button type="button" class="btn btn-danger btn-import" style="display: inline-block;" data-id="${item.id}">导入券码</button>
<button class="btn btn-danger btn-export-no" data-id="${item.id}">导出券码
</button>
<a class="btn btn-danger btn-update" href="/admin/coupon/update?id=${item.id}">修改</a>
<a class="btn btn-danger btn-delete" data-id="${item.id}">删除</a>
</td>
</tr>`;
});
... ... @@ -98,6 +104,26 @@ function bind_table_pagination() {
window.location = '/admin/api/coupon/downloadNo?id=' + id;
});
$('.btn-import').on('click', uploadFn);
$('.btn-delete').click(function() {
let id = $(this).attr('data-id');
console.log(id);
$.ajax({
url: '/admin/api/coupon/delete?id=' + id,
method: 'get',
cache: false,
processData: false,
contentType: false,
success: function(data) {
if (!data.result) {
alert(data.message);
} else {
window.location.reload();
}
}
});
});
});
};
... ...