Commit 28de6fce authored by 任国军's avatar 任国军

add course V3

parent a0d0b5d4
Pipeline #18256 passed with stage
in 1 minute 1 second
......@@ -44,7 +44,9 @@ class InstitutionController extends Controller {
if (!class_id) {
ctx.failed('error class_id');
}
const ret = await ctx.service.course.v3.institution.getClass(class_id);
const inputParams = ctx.request.query;
inputParams.id = class_id;
const ret = await ctx.service.course.v3.institution.getClass(inputParams);
ctx.success(ret);
}
......@@ -145,6 +147,15 @@ class InstitutionController extends Controller {
ctx.success(ret);
}
// 用户收藏课程列表
async getUserCollectedClasses() {
const { ctx, service } = this;
const inputParams = ctx.request.query;
const ret = await service.course.v3.institution.getUserCollectedClasses(inputParams);
ctx.success(ret);
}
// 根据分类获取选课指南
async getArticlesByCat() {
const { ctx, service } = this;
......
......@@ -129,10 +129,52 @@ class UserController extends Controller {
ctx.success({ result });
}
async getCollectClasses() {
const { ctx } = this;
let input_params = ctx.request.body;
input_params = Object.assign(input_params, ctx.request.query);
const result = await ctx.service.course.v3.user.getCollectClasses(input_params);
ctx.success({ result });
}
/**
* 收藏课程
*/
async collectClass() {
const { ctx } = this;
const class_id = ctx.request.body.class_id;
if (!class_id) {
ctx.failed('error class_id');
}
const result = await ctx.service.course.v3.user.collectClass(class_id);
ctx.success({ result });
}
/**
* 取消收藏课程
*/
async delCollectClass() {
const { ctx } = this;
const class_id = ctx.request.body.class_id;
if (!class_id) {
ctx.failed('error class_id');
}
const result = await ctx.service.course.v3.user.delCollectClass(class_id);
ctx.success({ result });
}
// 获取用户信息
async getUserInfo() {
const { ctx, service } = this;
const ret = await service.course.v3.user.getUserInfo();
const inputParams = ctx.request.query;
const ret = await service.course.v3.user.getUserInfo(inputParams);
ctx.success(ret);
}
......
......@@ -3,102 +3,104 @@
const moment = require('moment');
module.exports = app => {
const { STRING, INTEGER, DATE, DECIMAL, TEXT, ENUM } = app.Sequelize;
const { STRING, INTEGER, DATE, DECIMAL, TEXT, ENUM } = app.Sequelize;
const CourseClass = app.classModel.define('course_class', {
id: {
type: INTEGER,
primaryKey: true,
autoIncrement: true
},
institution_id: INTEGER,
name: STRING,
// image: STRING,
price: DECIMAL,
min_age: INTEGER,
max_age: INTEGER,
suit_base: STRING,
type: STRING,
class_system: STRING,
class_period: STRING,
class_time: STRING,
student_count: STRING,
point: STRING,
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_class',
});
const CourseClass = app.classModel.define('course_class', {
id: {
type: INTEGER,
primaryKey: true,
autoIncrement: true,
},
institution_id: INTEGER,
name: STRING,
// image: STRING,
class_price: STRING,
class_frequency: STRING,
price: DECIMAL,
min_age: INTEGER,
max_age: INTEGER,
suit_base: STRING,
type: STRING,
class_system: STRING,
class_period: STRING,
class_time: STRING,
student_count: STRING,
point: STRING,
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_class',
});
CourseClass.one = async (data) => {
const attributes = data.attributes ? data.attributes : {};
const where = data.where ? data.where : {};
return await CourseClass.findOne({
attributes: attributes,
where: where,
});
}
CourseClass.one = async data => {
const attributes = data.attributes ? data.attributes : {};
const where = data.where ? data.where : {};
return await CourseClass.findOne({
attributes,
where,
});
};
CourseClass.all = async (data) => {
const attributes = data.attributes ? data.attributes : {};
const where = data.where ? data.where : {};
const order = data.order ? data.order : [];
return await CourseClass.findAll({
attributes: attributes,
where: where,
order,
});
}
CourseClass.all = async data => {
const attributes = data.attributes ? data.attributes : {};
const where = data.where ? data.where : {};
const order = data.order ? data.order : [];
return await CourseClass.findAll({
attributes,
where,
order,
});
};
CourseClass.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 CourseClass.findAndCountAll(condition);
return { page, count, rows };
}
CourseClass.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 CourseClass.findAndCountAll(condition);
return { page, count, rows };
};
CourseClass.add = async (data) => {
try {
//返回promise对象实力 instance
const res = await CourseClass.create(data);
//从promise 实例中中获得需要的id号,id 必须是自增长,而且必须主键,否则返回null
return res.id;
} catch (error) {
throw (error);
}
CourseClass.add = async data => {
try {
// 返回promise对象实力 instance
const res = await CourseClass.create(data);
// 从promise 实例中中获得需要的id号,id 必须是自增长,而且必须主键,否则返回null
return res.id;
} catch (error) {
throw (error);
}
};
CourseClass.edit = async (data) => {
const where = data.where;
const params = data.params;
try {
const res = await CourseClass.update(params, { where: where })
return res;
} catch (error) {
throw (error);
}
CourseClass.edit = async data => {
const where = data.where;
const params = data.params;
try {
const res = await CourseClass.update(params, { where });
return res;
} catch (error) {
throw (error);
}
};
return CourseClass;
return CourseClass;
};
\ No newline at end of file
};
......@@ -26,8 +26,11 @@ module.exports = app => {
router.delete('third', '/user/baby', miniAuth, 'course.v3.user.delBabyInfo');// 删除baby信息
router.get('third', '/user/collection/institution', miniAuth, 'course.v3.institution.getUserCollectedInstitutions');// 收藏的机构列表
router.get('third', '/user/collection/class', miniAuth, 'course.v3.institution.getUserCollectedClasses');// 收藏的课程列表
router.post('third', '/user/collection/institution', miniAuth, 'course.v3.user.collectInstitution');// 收藏机构
router.delete('third', '/user/collection/institution', miniAuth, 'course.v3.user.delCollectInstitution');// 取消收藏机构
router.post('third', '/user/collection/class', miniAuth, 'course.v3.user.collectClass');// 收藏课程
router.delete('third', '/user/collection/class', miniAuth, 'course.v3.user.delCollectClass');// 取消收藏课程
router.get('third', '/wechat/callbackAction', 'course.v3.wechat.check');
router.post('third', '/wechat/callbackAction', 'course.v3.wechat.callbackAction');
......
......@@ -580,8 +580,14 @@ class InstitutionSubService extends Service {
const limit = Number(input.limit) || 10;
const offset = (page - 1) * limit;
const institutionId = Number(input.institution_id) || 0;
const inputClassIds = input.classIds || [];
const attributes = [ 'id', 'institution_id', 'name', 'type', 'price', 'status', 'is_deleted' ];
const classes = await ctx.classModel.V3.CourseClass.findAndCountAll({ where: { institution_id: institutionId, status: 'online', is_deleted: 0 }, raw: true, offset, limit, attributes });
let classes = { rows: [], count: 0 };
if (institutionId > 0) {
classes = await ctx.classModel.V3.CourseClass.findAndCountAll({ where: { institution_id: institutionId, status: 'online', is_deleted: 0 }, raw: true, offset, limit, attributes });
} else if (inputClassIds.length > 0) {
classes = await ctx.classModel.V3.CourseClass.findAndCountAll({ where: { id: { $in: inputClassIds }, status: 'online', is_deleted: 0 }, raw: true, offset, limit, attributes });
}
const classIds = R.pluck('id', classes.rows);
// 课程图片
......@@ -606,20 +612,25 @@ class InstitutionSubService extends Service {
// 课程详情
async getClass(id) {
async getClass(input) {
const { ctx } = this;
const classInfo = await ctx.classModel.V3.CourseClass.findOne({ where: { id, status: 'online', is_deleted: 0 }, raw: true });
const classInfo = await ctx.classModel.V3.CourseClass.findOne({ where: { id: input.id, status: 'online', is_deleted: 0 }, raw: true });
if (ctx.isEmpty(classInfo)) {
ctx.failed('数据不存在');
}
// 课程图片
let images = await ctx.classModel.V3.CourseImages.findAll({ where: { type: 2, type_id: id, status: 'online', is_deleted: 0 }, raw: true });
let images = await ctx.classModel.V3.CourseImages.findAll({ where: { type: 2, type_id: input.id, status: 'online', is_deleted: 0 }, raw: true });
// 去重
images = _.uniqBy(images, function(v) { return (v.is_image === 1 ? v.image_url : v.video_url); });
images = _.orderBy(images, [ 'sort' ], [ 'asc' ]);
classInfo.images = images;
classInfo.class_price = ctx.isEmpty(classInfo.class_price) ? '' : classInfo.class_price + '元/课时';
input.institution_id = classInfo.institution_id;
const institution = await this.getInstitution(input);
classInfo.institution = ctx.isEmpty(institution) ? {} : institution.detail;
return classInfo;
}
......@@ -629,8 +640,8 @@ class InstitutionSubService extends Service {
const { ctx } = this;
const userUuid = ctx.userUuid;
const userCollection = await ctx.classModel.V3.CourseUserCollection.findAll({ where: { user_uuid: userUuid, is_deleted: 0 }, raw: true });
const institutionIds = R.pluck('institution_id', userCollection);
const userCollection = await ctx.classModel.V3.CourseUserCollection.findAll({ where: { user_uuid: userUuid, is_deleted: 0, type: 1 }, raw: true });
const institutionIds = R.pluck('type_id', userCollection);
const institutionList = await ctx.classModel.V3.CourseInstitution.findAll({ where: { id: { $in: institutionIds }, status: 'online', is_deleted: 0 }, raw: true });
const ret = await this.formatInstitutionList(institutionList, input);
......@@ -638,6 +649,20 @@ class InstitutionSubService extends Service {
}
// 用户收藏课程列表
async getUserCollectedClasses(input) {
const { ctx } = this;
const userUuid = ctx.userUuid;
const userCollection = await ctx.classModel.V3.CourseUserCollection.findAll({ where: { user_uuid: userUuid, is_deleted: 0, type: 2 }, raw: true });
const classIds = R.pluck('type_id', userCollection);
input.classIds = classIds;
const ret = await this.getClasses(input);
return ret;
}
// 获取指定分类及子分类的选课指南
async getArticlesByCat(input) {
const { ctx } = this;
......@@ -742,7 +767,7 @@ class InstitutionSubService extends Service {
ctx.failed('数据不存在');
}
// 更新点赞数
await ctx.classModel.V3.CourseArticle.update({ read_count: sequelize.literal('`read_count` + 1') }, { where: { id: typeId } });
await ctx.classModel.V3.CourseArticle.update({ read_count: sequelize.literal('`like_count` + 1') }, { where: { id: typeId } });
break;
default:
break;
......
......@@ -68,12 +68,12 @@ class UserService extends Service {
const { ctx } = this;
const user_uuid = ctx.userUuid;
const where = { user_uuid, is_deleted: 0, institution_id };
const where = { user_uuid, is_deleted: 0, type: 1, type_id: institution_id };
let ret = await ctx.classModel.V3.CourseUserCollection.one({ where });
if (ret && ret.id) {
return ret.id;
ctx.failed('请勿重复收藏');
}
ret = await await ctx.classModel.V3.CourseUserCollection.add({ user_uuid, institution_id });
ret = await await ctx.classModel.V3.CourseUserCollection.add({ user_uuid, type: 1, type_id: institution_id });
return ret;
}
......@@ -82,10 +82,38 @@ class UserService extends Service {
const { ctx } = this;
const user_uuid = ctx.userUuid;
const where = { user_uuid, is_deleted: 0, institution_id };
const where = { user_uuid, is_deleted: 0, type: 1, type_id: institution_id };
let ret = await ctx.classModel.V3.CourseUserCollection.one({ where });
if (!ret || !ret.id) {
return true;
ctx.failed('尚未收藏');
}
ret = await await ctx.classModel.V3.CourseUserCollection.edit({ params: { is_deleted: 1 }, where });
return true;
}
async collectClass(class_id) {
const { ctx } = this;
const user_uuid = ctx.userUuid;
const where = { user_uuid, is_deleted: 0, type: 2, type_id: class_id };
let ret = await ctx.classModel.V3.CourseUserCollection.one({ where });
if (ret && ret.id) {
ctx.failed('请勿重复收藏');
}
ret = await await ctx.classModel.V3.CourseUserCollection.add({ user_uuid, type: 2, type_id: class_id });
return ret;
}
async delCollectClass(class_id) {
const { ctx } = this;
const user_uuid = ctx.userUuid;
const where = { user_uuid, is_deleted: 0, type: 2, type_id: class_id };
let ret = await ctx.classModel.V3.CourseUserCollection.one({ where });
if (!ret || !ret.id) {
ctx.failed('尚未收藏');
}
ret = await await ctx.classModel.V3.CourseUserCollection.edit({ params: { is_deleted: 1 }, where });
......@@ -119,20 +147,21 @@ class UserService extends Service {
}
// 获取用户信息
async getUserInfo() {
async getUserInfo(input) {
const { ctx } = this;
const userUuid = ctx.userUuid;
const userInfo = await ctx.classModel.V3.CourseUser.findOne({ where: { uuid: userUuid, is_deleted: 0 } });
const type = Number(input.type) || 0;
if (ctx.isEmpty(userInfo)) {
ctx.failed('用户不存在');
}
let need_bind_phone = 0;
if (ctx.isEmpty(userInfo.phone)) {
const userLog = await ctx.classModel.V3.CourseLogUser.findOne({ where: { user_uuid: userUuid, type: 1 }, order: [[ 'id', 'desc' ]], limit: 1 });
if (ctx.isEmpty(userInfo.phone) && type > 0) {
const userLog = await ctx.classModel.V3.CourseLogUser.findOne({ where: { user_uuid: userUuid, type }, order: [[ 'id', 'desc' ]], limit: 1 });
if ((!ctx.isEmpty(userLog) && moment(userLog.created_time, 'YYYY-MM-DD HH:mm:ss').format('YYYY-MM-DD') !== moment().format('YYYY-MM-DD')) || ctx.isEmpty(userLog)) {
need_bind_phone = 1;
await ctx.classModel.V3.CourseLogUser.create({ user_uuid: userUuid, type: 1, created_time: moment().format('YYYY-MM-DD HH:mm:ss') });
await ctx.classModel.V3.CourseLogUser.create({ user_uuid: userUuid, type, created_time: moment().format('YYYY-MM-DD HH:mm:ss') });
}
}
......
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