Commit ef341823 authored by 任国军's avatar 任国军

add backRedeemCode

parent b1a7147f
Pipeline #24705 passed with stage
in 4 seconds
......@@ -27,6 +27,45 @@ class BackController extends Controller {
const ret = await service.course.back.getMenuList();
ctx.success(ret);
}
// 获取订单列表
async getOrderList() {
const { ctx, service } = this;
const queryParams = ctx.request.query;
const ret = await service.course.back.getOrderList(queryParams);
ctx.success(ret);
}
// 获取兑换码列表
async getRedeemCodeList() {
const { ctx, service } = this;
const queryParams = ctx.request.query;
const ret = await service.course.back.getRedeemCodeList(queryParams);
ctx.success(ret);
}
// 生成兑换码
async addRedeemCode() {
const { ctx, service } = this;
const params = ctx.request.body;
if (ctx.isEmpty(params) || ctx.isEmpty(params.channel)) {
ctx.failed('channel is empty');
}
if (ctx.isEmpty(params) || ctx.isEmpty(params.class_id)) {
ctx.failed('class_id is empty');
}
if (ctx.isEmpty(params) || ctx.isEmpty(params.num)) {
ctx.failed('num is empty');
}
const ret = await service.course.back.addRedeemCode(params);
ctx.success(ret);
}
}
module.exports = BackController;
......@@ -14,6 +14,7 @@ module.exports = app => {
},
code: STRING,
class_id: INTEGER,
channel: STRING,
user_uuid: STRING,
is_used: INTEGER,
status: INTEGER,
......
......@@ -6,4 +6,7 @@ module.exports = app => {
router.post('third', '/login', 'course.back.login');
router.get('third', '/menu', auth, 'course.back.getMenuList');
router.get('third', '/order', auth, 'course.back.getOrderList');
router.get('third', '/redeem', auth, 'course.back.getRedeemCodeList');
router.post('third', '/redeem', auth, 'course.back.addRedeemCode');
};
......@@ -4,6 +4,7 @@
const Service = require('egg').Service;
const R = require('ramda');
const _ = require('lodash');
const moment = require('moment');
class BackService extends Service {
// 登录
......@@ -80,10 +81,131 @@ class BackService extends Service {
ctx.failed('用户异常');
}
const filter = { where: { status: 1, is_deleted: 0 }, limit, offset, attributes: [ 'id', 'order_no', 'class_id', 'pay', 'pay_time', 'type', 'redeem', 'address' ] };
const filter = { where: { status: 1, is_deleted: 0 }, limit, offset, attributes: [ 'id', 'order_no', 'class_id', 'pay', 'pay_time', 'type', 'redeem', 'address' ], order: [[ 'pay_time', 'desc' ]] };
if (userInfo.is_admin === 0) {
const classList = await ctx.classModel.V5.CourseClass.findAll({ where: { institution_id: { $in: userInfo.institution_id } }, attributes: [ 'id' ] });
const classList = await ctx.classModel.V5.CourseV5Class.findAll({ where: { institution_id: { $in: userInfo.institution_id } }, attributes: [ 'id' ] });
const classIds = R.pluck('id', classList);
filter.where.class_id = { $in: classIds };
}
const orderList = await ctx.classModel.V5.CourseUserOrder.findAndCountAll(filter);
if (ctx.isEmpty(orderList.rows)) {
return {
list: [],
page_size: limit,
total_count: orderList.count,
page,
};
}
let classList = await ctx.classModel.V5.CourseV5Class.findAll({ where: { id: { $in: R.pluck('class_id', orderList.rows) } }, attributes: [ 'id', 'name' ], raw: true });
classList = _.groupBy(classList, 'id');
const results = [];
for (const v of orderList.rows) {
results.push({
id: v.id,
order_no: v.order_no,
class_id: v.class_id,
class_name: ctx.isEmpty(classList[v.class_id]) ? '' : classList[v.class_id][0].name,
pay: v.pay,
pay_time: v.pay_time,
type: v.type,
redeem: v.redeem,
address: ctx.isEmpty(v.address) ? {} : JSON.parse(v.address),
});
}
const ret = {
list: results,
page_size: limit,
total_count: orderList.count,
page,
};
return ret;
}
// 获取兑换码列表
async getRedeemCodeList(input) {
const { ctx } = this;
const userUuid = ctx.userUuid;
const page = Number(input.page) || 1;
const limit = Number(input.limit) || 10;
const offset = (page - 1) * limit;
const userInfo = await ctx.classModel.CourseBackUser.findOne({ where: { id: userUuid, is_deleted: 0 } });
if (ctx.isEmpty(userInfo) || userInfo.is_admin === 0) {
ctx.failed('用户异常');
}
const redeemCodeList = await ctx.classModel.V5.CourseRedeemCode.findAndCountAll({ where: { status: 1, is_deleted: 0 }, limit, offset, order: [[ 'id', 'desc' ]] });
if (ctx.isEmpty(redeemCodeList.rows)) {
return {
list: [],
page_size: limit,
total_count: redeemCodeList.count,
page,
};
}
let classList = await ctx.classModel.V5.CourseV5Class.findAll({ where: { id: { $in: R.pluck('class_id', redeemCodeList.rows) } }, attributes: [ 'id', 'name' ], raw: true });
classList = _.groupBy(classList, 'id');
const results = [];
for (const v of redeemCodeList.rows) {
results.push({
id: v.id,
class_id: v.class_id,
class_name: ctx.isEmpty(classList[v.class_id]) ? '' : classList[v.class_id][0].name,
code: v.code,
channel: v.channel,
is_used: v.is_used,
used_time: v.used_time,
created_time: v.created_time,
});
}
const ret = {
list: results,
page_size: limit,
total_count: redeemCodeList.count,
page,
};
return ret;
}
// 生成兑换码
async addRedeemCode(input) {
const { ctx } = this;
const num = Number(input.num) || 0;
const data = [];
for (let i = 0; i < num; i++) {
const tmp = {
code: (ctx.helper.md5(moment().unix()).slice(-2) + await this.getRandomString(8)).toUpperCase(),
channel: input.channel || '',
class_id: input.class_id || 0,
};
data.push(tmp);
}
await ctx.classModel.V5.CourseRedeemCode.bulkCreate(data);
return { result: true };
}
async getRandomString(n) {
const chars = [ '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' ];
let ret = '';
for (let i = 0; i < n; i++) {
const id = Math.floor(Math.random() * 36);
ret += chars[id];
}
return ret;
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment