Commit 7d19f17e authored by Aria's avatar Aria

add course online

parent 7bc94e02
Pipeline #19398 passed with stage
in 58 seconds
'use strict';
const Controller = require('egg').Controller;
class OnlineController extends Controller {
// 获取在线课程列表
async getClasses() {
const { ctx, service } = this;
const inputParams = ctx.request.query;
const ret = await service.course.v4.online.getClasses(inputParams);
ctx.success(ret);
}
// 获取在线课程详情
async getClass() {
const { ctx, service } = this;
const params = ctx.params;
const inputParams = ctx.request.query;
if (ctx.isEmpty(params) || ctx.isEmpty(params.id)) {
ctx.failed('id error');
}
const input = {
id: params.id,
lng: inputParams.lng || 0,
lat: inputParams.lat || 0,
};
const ret = await service.course.v4.online.getClass(input);
ctx.success(ret);
}
// 获取在线课程选项
async getOption() {
const { ctx, service } = this;
const ret = await service.course.v4.online.getOption();
ctx.success(ret);
}
}
module.exports = OnlineController;
'use strict';
'use strict';
const moment = require('moment');
module.exports = app => {
const { STRING, INTEGER, DATE, DECIMAL, TEXT, ENUM, FLOAT } = app.Sequelize;
const CourseOnlineArea = app.classModel.define('courseOnlineArea', {
id: {
type: INTEGER,
primaryKey: true,
autoIncrement: true
},
institution_id: INTEGER,
name: STRING,
address: STRING,
phone: STRING,
lat: DECIMAL,
lng: DECIMAL,
status: ENUM('offline', 'online'),
is_deleted: INTEGER,
created_time: {
type: DATE,
allowNull: true,
get() {
const date = this.getDataValue('created_time');
return date ? moment(date).format('YYYY-MM-DD HH:mm:ss') : undefined;
},
}
}, {
timestamps: false,
tableName: 'courseOnlineArea',
});
CourseOnlineArea.one = async (data) => {
const attributes = data.attributes ? data.attributes : {};
const where = data.where ? data.where : {};
return await CourseOnlineArea.findOne({
attributes: attributes,
where: where,
});
}
CourseOnlineArea.all = async (data) => {
const attributes = data.attributes ? data.attributes : {};
const where = data.where ? data.where : {};
const order = data.order ? data.order : [];
return await CourseOnlineArea.findAll({
attributes: attributes,
where: where,
order,
});
}
CourseOnlineArea.list = async (data = {}) => {
const limit = data.limit ? Number(data.limit) : 10;
const page = data.page ? data.page : 1;
const order = data.order ? data.order : [];
const attributes = data.attributes ? data.attributes : {};
const where = data.where ? data.where : {};
const condition = {
offset: (page - 1) * limit,
limit,
where: where,
order: order,
attributes: attributes,
};
const { count, rows } = await CourseOnlineArea.findAndCountAll(condition);
return { page, count, rows };
}
CourseOnlineArea.add = async (data) => {
try {
//返回promise对象实力 instance
const res = await CourseOnlineArea.create(data);
//从promise 实例中中获得需要的id号,id 必须是自增长,而且必须主键,否则返回null
return res.id;
} catch (error) {
throw (error);
}
}
CourseOnlineArea.edit = async (data) => {
const where = data.where;
const params = data.params;
try {
const res = await CourseOnlineArea.update(params, { where: where })
return res;
} catch (error) {
throw (error);
}
}
return CourseOnlineArea;
};
\ No newline at end of file
'use strict';
const moment = require('moment');
module.exports = app => {
const { STRING, INTEGER, DATE, DECIMAL, TEXT, ENUM } = app.Sequelize;
const CourseOnlineCat = app.classModel.define('course_online_cat', {
id: {
type: INTEGER,
primaryKey: true,
autoIncrement: true,
},
parent_id: INTEGER,
name: STRING,
image: STRING,
active_image: STRING,
color: STRING,
tips: STRING,
status: ENUM('offline', 'online'),
is_deleted: INTEGER,
sort: INTEGER,
created_time: {
type: DATE,
allowNull: true,
get() {
const date = this.getDataValue('created_time');
return date ? moment(date).format('YYYY-MM-DD HH:mm:ss') : undefined;
},
},
updated_time: {
type: DATE,
allowNull: true,
get() {
const date = this.getDataValue('updated_time');
return date ? moment(date).format('YYYY-MM-DD HH:mm:ss') : undefined;
},
},
}, {
timestamps: false,
tableName: 'course_online_cat',
});
CourseOnlineCat.one = async data => {
const attributes = data.attributes ? data.attributes : {};
const where = data.where ? data.where : {};
return await CourseOnlineCat.findOne({
attributes,
where,
});
};
CourseOnlineCat.all = async data => {
const attributes = data.attributes ? data.attributes : {};
const where = data.where ? data.where : {};
const order = data.order ? data.order : [];
return await CourseOnlineCat.findAll({
attributes,
where,
order,
});
};
CourseOnlineCat.list = async (data = {}) => {
const limit = data.limit ? Number(data.limit) : 10;
const page = data.page ? data.page : 1;
const order = data.order ? data.order : [];
const attributes = data.attributes ? data.attributes : {};
const where = data.where ? data.where : {};
const condition = {
offset: (page - 1) * limit,
limit,
where,
order,
attributes,
};
const { count, rows } = await CourseOnlineCat.findAndCountAll(condition);
return { page, count, rows };
};
CourseOnlineCat.add = async data => {
try {
// 返回promise对象实力 instance
const res = await CourseOnlineCat.create(data);
// 从promise 实例中中获得需要的id号,id 必须是自增长,而且必须主键,否则返回null
return res.id;
} catch (error) {
throw (error);
}
};
CourseOnlineCat.edit = async data => {
const where = data.where;
const params = data.params;
try {
const res = await CourseOnlineCat.update(params, { where });
return res;
} catch (error) {
throw (error);
}
};
return CourseOnlineCat;
};
'use strict';
const moment = require('moment');
module.exports = app => {
const { STRING, INTEGER, DATE, DECIMAL, TEXT, ENUM } = app.Sequelize;
const CourseOnlineClass = app.classModel.define('course_online_class', {
id: {
type: INTEGER,
primaryKey: true,
autoIncrement: true,
},
institution_id: INTEGER,
name: STRING,
price: STRING,
age: STRING,
type: STRING,
mode: INTEGER,
time: STRING,
channel: TEXT,
description: STRING,
status: ENUM('offline', 'online'),
is_deleted: INTEGER,
created_time: {
type: DATE,
allowNull: true,
get() {
const date = this.getDataValue('created_time');
return date ? moment(date).format('YYYY-MM-DD HH:mm:ss') : undefined;
},
},
}, {
timestamps: false,
tableName: 'course_online_class',
});
CourseOnlineClass.one = async data => {
const attributes = data.attributes ? data.attributes : {};
const where = data.where ? data.where : {};
return await CourseOnlineClass.findOne({
attributes,
where,
});
};
CourseOnlineClass.all = async data => {
const attributes = data.attributes ? data.attributes : {};
const where = data.where ? data.where : {};
const order = data.order ? data.order : [];
return await CourseOnlineClass.findAll({
attributes,
where,
order,
});
};
CourseOnlineClass.list = async (data = {}) => {
const limit = data.limit ? Number(data.limit) : 10;
const page = data.page ? data.page : 1;
const order = data.order ? data.order : [];
const attributes = data.attributes ? data.attributes : {};
const where = data.where ? data.where : {};
const condition = {
offset: (page - 1) * limit,
limit,
where,
order,
attributes,
};
const { count, rows } = await CourseOnlineClass.findAndCountAll(condition);
return { page, count, rows };
};
CourseOnlineClass.add = async data => {
try {
// 返回promise对象实力 instance
const res = await CourseOnlineClass.create(data);
// 从promise 实例中中获得需要的id号,id 必须是自增长,而且必须主键,否则返回null
return res.id;
} catch (error) {
throw (error);
}
};
CourseOnlineClass.edit = async data => {
const where = data.where;
const params = data.params;
try {
const res = await CourseOnlineClass.update(params, { where });
return res;
} catch (error) {
throw (error);
}
};
return CourseOnlineClass;
};
'use strict';
'use strict';
const moment = require('moment');
module.exports = app => {
const { STRING, INTEGER, DATE, DECIMAL, TEXT, ENUM } = app.Sequelize;
const CourseOnlineClassToCat = app.classModel.define('course_online_class_to_cat', {
id: {
type: INTEGER,
primaryKey: true,
autoIncrement: true
},
class_id: INTEGER,
cat_id: INTEGER,
status: ENUM('offline', 'online'),
is_deleted: INTEGER,
created_time: {
type: DATE,
allowNull: true,
get() {
const date = this.getDataValue('created_time');
return date ? moment(date).format('YYYY-MM-DD HH:mm:ss') : undefined;
},
},
updated_time: {
type: DATE,
allowNull: true,
get() {
const date = this.getDataValue('updated_time');
return date ? moment(date).format('YYYY-MM-DD HH:mm:ss') : undefined;
},
},
}, {
timestamps: false,
tableName: 'course_online_class_to_cat',
});
CourseOnlineClassToCat.one = async (data) => {
const attributes = data.attributes ? data.attributes : {};
const where = data.where ? data.where : {};
return await CourseOnlineClassToCat.findOne({
attributes: attributes,
where: where,
});
}
CourseOnlineClassToCat.all = async (data) => {
const attributes = data.attributes ? data.attributes : {};
const where = data.where ? data.where : {};
const order = data.order ? data.order : [];
return await CourseOnlineClassToCat.findAll({
attributes: attributes,
where: where,
order,
});
}
CourseOnlineClassToCat.list = async (data = {}) => {
const limit = data.limit ? Number(data.limit) : 10;
const page = data.page ? data.page : 1;
const order = data.order ? data.order : [];
const attributes = data.attributes ? data.attributes : {};
const where = data.where ? data.where : {};
const condition = {
offset: (page - 1) * limit,
limit,
where: where,
order: order,
attributes: attributes,
};
const { count, rows } = await CourseOnlineClassToCat.findAndCountAll(condition);
return { page, count, rows };
}
CourseOnlineClassToCat.add = async (data) => {
try {
//返回promise对象实力 instance
const res = await CourseOnlineClassToCat.create(data);
//从promise 实例中中获得需要的id号,id 必须是自增长,而且必须主键,否则返回null
return res.id;
} catch (error) {
throw (error);
}
}
CourseOnlineClassToCat.edit = async (data) => {
const where = data.where;
const params = data.params;
try {
const res = await CourseOnlineClassToCat.update(params, { where: where })
return res;
} catch (error) {
throw (error);
}
}
return CourseOnlineClassToCat;
};
\ No newline at end of file
'use strict';
const moment = require('moment');
module.exports = app => {
const { STRING, INTEGER, DATE, DECIMAL, TEXT, ENUM } = app.Sequelize;
const CourseOnlineArea = app.classModel.define('course_online_area');
const CourseOnlineInstitution = app.classModel.define('course_online_institution', {
id: {
type: INTEGER,
primaryKey: true,
autoIncrement: true,
},
relation_id: INTEGER,
name: STRING,
logo: STRING,
status: ENUM('offline', 'online'),
is_deleted: INTEGER,
created_time: {
type: DATE,
allowNull: true,
get() {
const date = this.getDataValue('created_time');
return date ? moment(date).format('YYYY-MM-DD HH:mm:ss') : undefined;
},
},
}, {
timestamps: false,
tableName: 'course_online_institution',
});
CourseOnlineInstitution.hasMany(CourseOnlineArea, {
foreignKey: {
name: 'institution_id',
allowNull: false,
},
});
CourseOnlineInstitution.one = async data => {
const attributes = data.attributes ? data.attributes : {};
const where = data.where ? data.where : {};
return await CourseOnlineInstitution.findOne({
attributes,
where,
});
};
CourseOnlineInstitution.all = async data => {
const attributes = data.attributes ? data.attributes : {};
const where = data.where ? data.where : {};
const order = data.order ? data.order : [];
return await CourseOnlineInstitution.findAll({
attributes,
where,
order,
});
};
CourseOnlineInstitution.list = async (data = {}) => {
const limit = data.limit ? Number(data.limit) : 10;
const page = data.page ? data.page : 1;
const order = data.order ? data.order : [];
const attributes = data.attributes ? data.attributes : {};
const where = data.where ? data.where : {};
const condition = {
offset: (page - 1) * limit,
limit,
where,
order,
attributes,
};
const { count, rows } = await CourseOnlineInstitution.findAndCountAll(condition);
return { page, count, rows };
};
CourseOnlineInstitution.add = async data => {
try {
// 返回promise对象实力 instance
const res = await CourseOnlineInstitution.create(data);
// 从promise 实例中中获得需要的id号,id 必须是自增长,而且必须主键,否则返回null
return res.id;
} catch (error) {
throw (error);
}
};
CourseOnlineInstitution.edit = async data => {
const where = data.where;
const params = data.params;
try {
const res = await CourseOnlineInstitution.update(params, { where });
return res;
} catch (error) {
throw (error);
}
};
return CourseOnlineInstitution;
};
......@@ -50,4 +50,8 @@ module.exports = app => {
router.post('third', '/like', miniAuth, 'course.v4.institution.like');// 点赞
router.post('third', '/unlike', miniAuth, 'course.v4.institution.unlike');// 取消点赞
router.get('third', '/online/classes', miniAuth, 'course.v4.online.getClasses');// 在线课程列表
router.get('third', '/online/class/:id', miniAuth, 'course.v4.online.getClass');// 在线课程详情
router.get('third', '/online/option', miniAuth, 'course.v4.online.getOption');// 在线课程选项
};
'use strict';
const Service = require('egg').Service;
const R = require('ramda');
const moment = require('moment');
const _ = require('lodash');
class OnlineService extends Service {
// 获取在线课程选项
async getOption() {
const { ctx } = this;
// 分类
const cats = await ctx.classModel.V4.CourseOnlineCat.findAll({ where: { status: 'online', is_deleted: 0 }, attributes: [ 'id', 'name' ] });
// 年龄段
const ages = [ '学龄前', '幼儿园', '小学', '初中', '高中' ];
// 课程状态
const model = [{ id: 0, name: '全部' }, { id: 1, name: '直播' }, { id: 2, name: '录播' }];
// 课程班型
const type = [ '1对1', '1对多', '讲座' ];
const options = {
cats,
ages,
model,
type,
};
return options;
}
// 获取课程详情
async getClass(input) {
const { ctx } = this;
const { id, lng, lat } = input;
const classInfo = await ctx.classModel.V4.CourseOnlineClass.findOne({ where: { id, status: 'online', is_deleted: 0 } });
if (ctx.isEmpty(classInfo)) {
ctx.failed('课程不存在');
}
// 缩略图
const imageInfo = await ctx.classModel.V4.CourseImages.findOne({ where: { type: 5, type_id: id, status: 'online', is_deleted: 0 } });
let image = {};
if (!ctx.isEmpty(imageInfo)) {
image = imageInfo.is_video > 0 ? { url: imageInfo.video_url, type: 1 } : { url: imageInfo.image_url, type: 0 };
}
// 机构信息
const institution = await ctx.classModel.V4.CourseOnlineInstitution.findOne({ where: { id: classInfo.institution_id } });
// 校区信息
let area = await ctx.classModel.V4.CourseOnlineArea.findOne({ where: { institution_id: classInfo.institution_id, status: 'online', is_deleted: 0 }, raw: true });
area = ctx.isEmpty(area) ? area : await this.service.course.v4.institution.formatArea(area, { lng, lat });
// 分类
const classCats = await ctx.classModel.V4.CourseOnlineClassToCat.findAll({ where: { class_id: id, status: 'online', is_deleted: 0 }, attributes: [ 'cat_id' ] });
const catIds = R.pluck('cat_id', classCats);
const cats = await ctx.classModel.V4.CourseOnlineCat.findAll({ where: { id: { $in: catIds } }, attributes: [ 'id', 'name' ] });
// 收藏信息
const userCollect = await ctx.classModel.V4.CourseUserCollection.findOne({ where: { user_uuid: ctx.userUuid, type: 3, type_id: id, status: 'online', is_deleted: 0 } });
const ret = {
id,
image,
name: classInfo.name,
institution_name: ctx.isEmpty(institution) ? '' : institution.name,
institution_id: classInfo.institution_id,
relation_institution_id: ctx.isEmpty(institution) ? 0 : institution.relation_id,
address: ctx.isEmpty(area) ? '' : area.address,
phone: ctx.isEmpty(area) ? '' : area.phone,
travel_tips: ctx.isEmpty(area) ? '' : area.travel_tips,
price: classInfo.price,
type: classInfo.type,
model: await this.getClassModelInfo(classInfo.model),
time: classInfo.time,
channel: classInfo.channel,
description: classInfo.description,
cats: ctx.isEmpty(cats) ? [] : cats,
is_collected: ctx.isEmpty(userCollect) ? 0 : 1,
};
return ret;
}
// 获取在线课程列表
async getClasses(input) {
const { ctx } = this;
const page = Number(input.page) || 1;
const limit = Number(input.limit) || 10;
const offset = (page - 1) * limit;
const { type, model, age, cat } = input;
const filter = { where: { status: 'online', is_deleted: 0 }, order: [[ 'institution_id', 'desc' ]], limit, offset, attributes: [ 'id', 'institution_id', 'name', 'price', 'type', 'age', 'model', 'time', 'created_time' ] };
// 年龄段筛选
if (!ctx.isEmpty(age)) {
filter.where.age = age;
}
// 课程类型筛选
if (!ctx.isEmpty(cat) && Number(cat) > 0) {
const classes = await ctx.classModel.V4.CourseOnlineClassToCat.findAll({ where: { cat_id: cat, status: 'online', is_deleted: 0 }, attributes: [ 'class_id' ] });
filter.where.id = { $in: R.pluck('class_id', classes) };
}
// 课程状态筛选
if (!ctx.isEmpty(model)) {
filter.where.model = model;
}
// 课程班型筛选
if (!ctx.isEmpty(type)) {
filter.where.type = type;
}
const classes = await ctx.classModel.CourseOnlineClass.findAndCountAll(filter);
const classIds = R.pluck('id', classes.rows);
// 分类
const classToCats = await ctx.classModel.CourseOnlineClassToCat.findAll({ where: { class_id: { $in: classIds }, status: 'online', is_deleted: 0 }, attributes: [ 'class_id', 'cat_id' ] });
const catIds = R.pluck('cat_id', classToCats);
let cats = await ctx.classModel.CourseOnlineCat.findAll({ where: { id: { $in: catIds } } });
cats = _.groupBy(cats, 'id');
let classCats = [];
for (const v of classToCats) {
const tmp = {
id: v.cat_id,
class_id: v.class_id,
name: ctx.isEmpty(cats[v.cat_id]) ? '' : cats[v.cat_id][0].name,
};
classCats.push(tmp);
}
classCats = _.groupBy(classCats, 'class_id');
// 机构
const institutionIds = R.pluck('institution_id', classes.rows);
let institutions = await ctx.classModel.CourseOnlineInstitution.findAll({ where: { id: { $in: institutionIds } }, attributes: [ 'id', 'name', 'logo' ] });
institutions = _.groupBy(institutions, 'id');
const result = [];
for (const v of classes.rows) {
const tmp = {
id: v.id,
name: v.name,
institution_id: v.institution_id,
institution_name: ctx.isEmpty(institutions[v.institution_id]) ? '' : institutions[v.institution_id][0].name,
logo: ctx.isEmpty(institutions[v.institution_id]) ? '' : institutions[v.institution_id][0].logo,
type: v.type,
model: await this.getClassModelInfo(v.model),
price: v.price,
time: v.time,
created_time: v.created_time,
cats: ctx.isEmpty(classCats[v.id]) ? [] : classCats[v.id],
};
result.push(tmp);
}
const ret = {
results: result,
count: classes.count,
page,
};
return ret;
}
// 判断课程状态(直播录播)
async getClassModelInfo(model) {
let ret = '';
switch (model) {
case 1:
ret = '直播';
break;
case 2:
ret = '录播';
break;
case 3:
ret = '直播+录播';
break;
default:
break;
}
return ret;
}
}
module.exports = OnlineService;
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