Commit 0323988c authored by 任国军's avatar 任国军

add homePage

parent e179ad94
Pipeline #27740 passed with stage
in 30 seconds
......@@ -135,14 +135,21 @@ class InstitutionController extends Controller {
async getHomeClassList() {
const { ctx, service } = this;
const queryParams = ctx.request.query;
if (ctx.isEmpty(queryParams) || ctx.isEmpty(queryParams.cat_id)) {
ctx.failed('cat_id is empty');
}
const ret = await service.course.v5.institution.getHomeClassList(queryParams);
ctx.success(ret);
}
// 获取首页
async getHomePage() {
const { ctx, service } = this;
const queryParams = ctx.request.query;
const ret = await service.course.v5.institution.getHomePage(queryParams);
ctx.success(ret);
}
}
module.exports = InstitutionController;
......@@ -65,6 +65,7 @@ module.exports = app => {
router.get('third', '/video/:id', auth({ is_force: 1 }), 'course.v5.institution.getVideoInfo');// 获取视频详情
router.get('third', '/home/class/all', auth({ is_force: 0 }), 'course.v5.institution.getHomeClassList');// 获取课程首页
router.get('third', '/home', auth({ is_force: 0 }), 'course.v5.institution.getHomePage');// 获取首页
router.post('third', '/order/class/:class_id', auth({ is_force: 1 }), 'course.v5.user.buyClass');// 购买课程
......
......@@ -26,7 +26,10 @@ class InstitutionSubService extends Service {
}
// 科目类型
if (!ctx.isEmpty(input.category)) {
const filterByCategory = await ctx.classModel.V5.CourseV5ClassToCat.findAll({ where: { cat_id: input.category, status: 1, is_deleted: 0 }, attributes: [ 'class_id' ] });
let categoryList = await ctx.classModel.V5.CourseV5Category.findAll({ where: { parent_id: input.category, type: 1, status: 1, is_deleted: 0 }, attributes: [ 'id' ] });
categoryList = R.pluck('id', categoryList);
categoryList.push(input.category);
const filterByCategory = await ctx.classModel.V5.CourseV5ClassToCat.findAll({ where: { cat_id: { $in: categoryList }, status: 1, is_deleted: 0 }, attributes: [ 'class_id' ] });
filterIds = flag ? _.intersection(filterIds, R.pluck('class_id', filterByCategory)) : R.pluck('class_id', filterByCategory);
flag = true;
}
......@@ -534,57 +537,136 @@ class InstitutionSubService extends Service {
// 课程首页
async getHomeClassList(input) {
const { ctx } = this;
const cat_id = Number(input.cat_id) || 0;
const attributes = [ 'id', 'institution_id', 'name', 'logo', 'age', 'price', 'price_type', 'mode', 'time', 'class_amount', 'multi_classes', 'cycle', 'description', 'sort', 'sub_title', 'pay_count' ];
const catId = Number(input.cat_id) || 0;
const results = [];
let classList = [];
const categoryList = await ctx.classModel.V5.CourseV5Category.findAll({ where: { parent_id: catId, type: 1, status: 1, is_deleted: 0 }, raw: true });
const result = [];
const handle = [];
for (const i in categoryList) {
handle.push(this.getClassListByCategory({ category: categoryList[i].id, page_size: 6 }));
}
const classAll = await ctx.classModel.V5.CourseV5ClassToCat.findAll({ where: { cat_id, status: 1, is_deleted: 0 } });
const classIds = R.pluck('class_id', classAll);
const classList = await Promise.all(handle).then(result => {
return result;
}).catch(error => {
ctx.failed(error);
});
// 体验课
classList = await ctx.classModel.V5.CourseV5Class.findAll({ where: { id: { $in: classIds }, price_type: 1, status: 1, is_deleted: 0 }, attributes, order: [[ 'sort', 'asc' ], [ 'id', 'desc' ]], offset: 0, limit: 6, raw: true });
classList = await this.formatClassList(classList);
for (const i in classList) {
classList[i].institution_icon = '';
for (const i in categoryList) {
result.push({
title: categoryList[i].name,
cat_id: categoryList[i].id,
list: ctx.isEmpty(classList[i]) ? [] : classList[i],
});
}
results.push({
title: '免费课',
value: 1,
class: classList,
});
// 低价课
classList = await ctx.classModel.V5.CourseV5Class.findAll({ where: { id: { $in: classIds }, price_type: 2, status: 1, is_deleted: 0 }, attributes, order: [[ 'sort', 'asc' ], [ 'id', 'desc' ]], offset: 0, limit: 6, raw: true });
classList = await this.formatClassList(classList);
for (const i in classList) {
classList[i].institution_icon = '';
const ret = {
list: result,
};
return ret;
}
// 首页
async getHomePage(input) {
const { ctx } = this;
const age = Number(input.age) || 0;
const categoryList = await ctx.classModel.V5.CourseV5Category.findAll({ where: { parent_id: 0, type: 1, status: 1, is_deleted: 0 }, raw: true });
const result = [];
const handle = [];
for (const i in categoryList) {
if (age > 0) {
handle.push(this.getClassListByCategory({ category: categoryList[i].id, price_type: 2, age }));
} else {
handle.push(this.getClassListByCategory({ category: categoryList[i].id, price_type: 2 }));
}
}
results.push({
title: '低价课',
value: 2,
class: classList,
const classList = await Promise.all(handle).then(result => {
return result;
}).catch(error => {
ctx.failed(error);
});
// 精品课
classList = await ctx.classModel.V5.CourseV5Class.findAll({ where: { id: { $in: classIds }, price_type: 3, status: 1, is_deleted: 0 }, attributes, order: [[ 'sort', 'asc' ], [ 'id', 'desc' ]], offset: 0, limit: 6, raw: true });
classList = await this.formatClassList(classList);
for (const i in classList) {
classList[i].institution_icon = '';
for (const i in categoryList) {
result.push({
title: categoryList[i].name,
cat_id: categoryList[i].id,
list: ctx.isEmpty(classList[i]) ? [] : classList[i],
});
}
results.push({
title: '精品课',
value: 3,
class: classList,
});
const ret = {
list: results,
list: result,
};
return ret;
}
// 列表用课程列表
async getClassListByCategory(input) {
const { ctx } = this;
const page = Number(input.page) || 1;
const page_size = Number(input.page_size) || 4;
const offset = (page - 1) * page_size;
const attributes = [ 'id', 'institution_id', 'name', 'logo', 'age', 'price', 'price_type', 'mode', 'time', 'class_amount', 'multi_classes', 'sort', 'sub_title', 'pay_count' ];
const filter = { where: { status: 1, is_deleted: 0 }, order: [[ 'sort', 'asc' ], [ 'id', 'desc' ]], limit: page_size, offset, attributes, raw: true };
let filterIds = [];
let flag = false;
// 年龄筛选
if (!ctx.isEmpty(input.age)) {
const filterByAge = await ctx.classModel.V5.CourseV5ClassToAge.findAll({ where: { age_id: input.age, status: 1, is_deleted: 0 }, attributes: [ 'class_id' ] });
filterIds = R.pluck('class_id', filterByAge);
flag = true;
}
// 科目类型
if (!ctx.isEmpty(input.category)) {
let categoryList = await ctx.classModel.V5.CourseV5Category.findAll({ where: { parent_id: input.category, type: 1, status: 1, is_deleted: 0 }, attributes: [ 'id' ] });
categoryList = R.pluck('id', categoryList);
categoryList.push(input.category);
const filterByCategory = await ctx.classModel.V5.CourseV5ClassToCat.findAll({ where: { cat_id: { $in: categoryList }, status: 1, is_deleted: 0 }, attributes: [ 'class_id' ] });
filterIds = flag ? _.intersection(filterIds, R.pluck('class_id', filterByCategory)) : R.pluck('class_id', filterByCategory);
flag = true;
}
// 课程状态
if (!ctx.isEmpty(input.mode)) {
filter.where.mode = input.mode;
}
// 课程类型
if (!ctx.isEmpty(input.price_type)) {
filter.where.price_type = input.price_type;
}
// 搜索
if (!ctx.isEmpty(input.word)) {
// 先查询机构名称的
const institutionList = await ctx.classModel.V5.CourseV5Institution.findAll({ where: { name: { $like: `%${input.word}%` }, status: 1, is_deleted: 0 }, attributes: [ 'id', 'name' ], raw: true });
const institutionIds = R.pluck('id', institutionList);
filter.where.$or = [{ name: { $like: `%${input.word}%` } }, { institution_id: { $in: institutionIds } }];
}
if (flag) {
filter.where.id = { $in: filterIds };
}
const classList = await ctx.classModel.V5.CourseV5Class.findAll(filter);
// 获取机构
let institutionList = await ctx.classModel.V5.CourseV5Institution.findAll({ where: { id: { $in: R.pluck('institution_id', classList) } }, attributes: [ 'id', 'name', 'logo' ] });
institutionList = _.groupBy(institutionList, 'id');
// 订单总数
const orderCount = await ctx.classModel.V5.CourseUserOrder.findAll({ where: { class_id: { $in: _.uniq(R.pluck('id', classList)) }, status: 1, is_deleted: 0 } });
const classOrder = _.groupBy(orderCount, 'class_id');
for (const i in classList) {
classList[i].price_type = await this.getClassPriceType(classList[i].price_type);
classList[i].mode = await this.getClassMode(classList[i].mode);
classList[i].institution_name = ctx.isEmpty(institutionList[classList[i].institution_id]) ? '' : institutionList[classList[i].institution_id][0].name;
classList[i].institution_logo = ctx.isEmpty(institutionList[classList[i].institution_id]) ? '' : institutionList[classList[i].institution_id][0].logo;
classList[i].pay_count = ctx.isEmpty(classOrder[classList[i].id]) ? classList[i].pay_count : classList[i].pay_count + classOrder[classList[i].id].length;
}
return classList;
}
}
module.exports = InstitutionSubService;
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