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 { ...@@ -44,7 +44,9 @@ class InstitutionController extends Controller {
if (!class_id) { if (!class_id) {
ctx.failed('error 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); ctx.success(ret);
} }
...@@ -145,6 +147,15 @@ class InstitutionController extends Controller { ...@@ -145,6 +147,15 @@ class InstitutionController extends Controller {
ctx.success(ret); 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() { async getArticlesByCat() {
const { ctx, service } = this; const { ctx, service } = this;
......
...@@ -129,10 +129,52 @@ class UserController extends Controller { ...@@ -129,10 +129,52 @@ class UserController extends Controller {
ctx.success({ result }); 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() { async getUserInfo() {
const { ctx, service } = this; 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); ctx.success(ret);
} }
......
...@@ -3,102 +3,104 @@ ...@@ -3,102 +3,104 @@
const moment = require('moment'); const moment = require('moment');
module.exports = app => { 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', { const CourseClass = app.classModel.define('course_class', {
id: { id: {
type: INTEGER, type: INTEGER,
primaryKey: true, primaryKey: true,
autoIncrement: true autoIncrement: true,
}, },
institution_id: INTEGER, institution_id: INTEGER,
name: STRING, name: STRING,
// image: STRING, // image: STRING,
price: DECIMAL, class_price: STRING,
min_age: INTEGER, class_frequency: STRING,
max_age: INTEGER, price: DECIMAL,
suit_base: STRING, min_age: INTEGER,
type: STRING, max_age: INTEGER,
class_system: STRING, suit_base: STRING,
class_period: STRING, type: STRING,
class_time: STRING, class_system: STRING,
student_count: STRING, class_period: STRING,
point: STRING, class_time: STRING,
description: STRING, student_count: STRING,
status: ENUM('offline', 'online'), point: STRING,
is_deleted: INTEGER, description: STRING,
created_time: { status: ENUM('offline', 'online'),
type: DATE, is_deleted: INTEGER,
allowNull: true, created_time: {
get() { type: DATE,
const date = this.getDataValue('created_time'); allowNull: true,
return date ? moment(date).format('YYYY-MM-DD HH:mm:ss') : undefined; get() {
}, const date = this.getDataValue('created_time');
} return date ? moment(date).format('YYYY-MM-DD HH:mm:ss') : undefined;
}, { },
timestamps: false, },
tableName: 'course_class', }, {
}); timestamps: false,
tableName: 'course_class',
});
CourseClass.one = async (data) => { CourseClass.one = async data => {
const attributes = data.attributes ? data.attributes : {}; const attributes = data.attributes ? data.attributes : {};
const where = data.where ? data.where : {}; const where = data.where ? data.where : {};
return await CourseClass.findOne({ return await CourseClass.findOne({
attributes: attributes, attributes,
where: where, where,
}); });
} };
CourseClass.all = async (data) => { CourseClass.all = async data => {
const attributes = data.attributes ? data.attributes : {}; const attributes = data.attributes ? data.attributes : {};
const where = data.where ? data.where : {}; const where = data.where ? data.where : {};
const order = data.order ? data.order : []; const order = data.order ? data.order : [];
return await CourseClass.findAll({ return await CourseClass.findAll({
attributes: attributes, attributes,
where: where, where,
order, order,
}); });
} };
CourseClass.list = async (data = {}) => { CourseClass.list = async (data = {}) => {
const limit = data.limit ? Number(data.limit) : 10; const limit = data.limit ? Number(data.limit) : 10;
const page = data.page ? data.page : 1; const page = data.page ? data.page : 1;
const order = data.order ? data.order : []; const order = data.order ? data.order : [];
const attributes = data.attributes ? data.attributes : {}; const attributes = data.attributes ? data.attributes : {};
const where = data.where ? data.where : {}; const where = data.where ? data.where : {};
const condition = { const condition = {
offset: (page - 1) * limit, offset: (page - 1) * limit,
limit, limit,
where: where, where,
order: order, order,
attributes: attributes, attributes,
}; };
const { count, rows } = await CourseClass.findAndCountAll(condition); const { count, rows } = await CourseClass.findAndCountAll(condition);
return { page, count, rows }; return { page, count, rows };
} };
CourseClass.add = async (data) => { CourseClass.add = async data => {
try { try {
//返回promise对象实力 instance // 返回promise对象实力 instance
const res = await CourseClass.create(data); const res = await CourseClass.create(data);
//从promise 实例中中获得需要的id号,id 必须是自增长,而且必须主键,否则返回null // 从promise 实例中中获得需要的id号,id 必须是自增长,而且必须主键,否则返回null
return res.id; return res.id;
} catch (error) { } catch (error) {
throw (error); throw (error);
}
} }
};
CourseClass.edit = async (data) => { CourseClass.edit = async data => {
const where = data.where; const where = data.where;
const params = data.params; const params = data.params;
try { try {
const res = await CourseClass.update(params, { where: where }) const res = await CourseClass.update(params, { where });
return res; return res;
} catch (error) { } catch (error) {
throw (error); throw (error);
}
} }
};
return CourseClass; return CourseClass;
}; };
\ No newline at end of file
...@@ -26,8 +26,11 @@ module.exports = app => { ...@@ -26,8 +26,11 @@ module.exports = app => {
router.delete('third', '/user/baby', miniAuth, 'course.v3.user.delBabyInfo');// 删除baby信息 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/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.post('third', '/user/collection/institution', miniAuth, 'course.v3.user.collectInstitution');// 收藏机构
router.delete('third', '/user/collection/institution', miniAuth, 'course.v3.user.delCollectInstitution');// 取消收藏机构 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.get('third', '/wechat/callbackAction', 'course.v3.wechat.check');
router.post('third', '/wechat/callbackAction', 'course.v3.wechat.callbackAction'); router.post('third', '/wechat/callbackAction', 'course.v3.wechat.callbackAction');
......
...@@ -580,8 +580,14 @@ class InstitutionSubService extends Service { ...@@ -580,8 +580,14 @@ class InstitutionSubService extends Service {
const limit = Number(input.limit) || 10; const limit = Number(input.limit) || 10;
const offset = (page - 1) * limit; const offset = (page - 1) * limit;
const institutionId = Number(input.institution_id) || 0; const institutionId = Number(input.institution_id) || 0;
const inputClassIds = input.classIds || [];
const attributes = [ 'id', 'institution_id', 'name', 'type', 'price', 'status', 'is_deleted' ]; 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); const classIds = R.pluck('id', classes.rows);
// 课程图片 // 课程图片
...@@ -606,20 +612,25 @@ class InstitutionSubService extends Service { ...@@ -606,20 +612,25 @@ class InstitutionSubService extends Service {
// 课程详情 // 课程详情
async getClass(id) { async getClass(input) {
const { ctx } = this; 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)) { if (ctx.isEmpty(classInfo)) {
ctx.failed('数据不存在'); 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 = _.uniqBy(images, function(v) { return (v.is_image === 1 ? v.image_url : v.video_url); });
images = _.orderBy(images, [ 'sort' ], [ 'asc' ]); images = _.orderBy(images, [ 'sort' ], [ 'asc' ]);
classInfo.images = images; 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; return classInfo;
} }
...@@ -629,8 +640,8 @@ class InstitutionSubService extends Service { ...@@ -629,8 +640,8 @@ class InstitutionSubService extends Service {
const { ctx } = this; const { ctx } = this;
const userUuid = ctx.userUuid; const userUuid = ctx.userUuid;
const userCollection = await ctx.classModel.V3.CourseUserCollection.findAll({ where: { user_uuid: userUuid, is_deleted: 0 }, raw: true }); const userCollection = await ctx.classModel.V3.CourseUserCollection.findAll({ where: { user_uuid: userUuid, is_deleted: 0, type: 1 }, raw: true });
const institutionIds = R.pluck('institution_id', userCollection); 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 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); const ret = await this.formatInstitutionList(institutionList, input);
...@@ -638,6 +649,20 @@ class InstitutionSubService extends Service { ...@@ -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) { async getArticlesByCat(input) {
const { ctx } = this; const { ctx } = this;
...@@ -742,7 +767,7 @@ class InstitutionSubService extends Service { ...@@ -742,7 +767,7 @@ class InstitutionSubService extends Service {
ctx.failed('数据不存在'); 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; break;
default: default:
break; break;
......
...@@ -68,12 +68,12 @@ class UserService extends Service { ...@@ -68,12 +68,12 @@ class UserService extends Service {
const { ctx } = this; const { ctx } = this;
const user_uuid = ctx.userUuid; 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 }); let ret = await ctx.classModel.V3.CourseUserCollection.one({ where });
if (ret && ret.id) { 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; return ret;
} }
...@@ -82,10 +82,38 @@ class UserService extends Service { ...@@ -82,10 +82,38 @@ class UserService extends Service {
const { ctx } = this; const { ctx } = this;
const user_uuid = ctx.userUuid; 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 }); let ret = await ctx.classModel.V3.CourseUserCollection.one({ where });
if (!ret || !ret.id) { 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 }); ret = await await ctx.classModel.V3.CourseUserCollection.edit({ params: { is_deleted: 1 }, where });
...@@ -119,20 +147,21 @@ class UserService extends Service { ...@@ -119,20 +147,21 @@ class UserService extends Service {
} }
// 获取用户信息 // 获取用户信息
async getUserInfo() { async getUserInfo(input) {
const { ctx } = this; const { ctx } = this;
const userUuid = ctx.userUuid; const userUuid = ctx.userUuid;
const userInfo = await ctx.classModel.V3.CourseUser.findOne({ where: { uuid: userUuid, is_deleted: 0 } }); const userInfo = await ctx.classModel.V3.CourseUser.findOne({ where: { uuid: userUuid, is_deleted: 0 } });
const type = Number(input.type) || 0;
if (ctx.isEmpty(userInfo)) { if (ctx.isEmpty(userInfo)) {
ctx.failed('用户不存在'); ctx.failed('用户不存在');
} }
let need_bind_phone = 0; let need_bind_phone = 0;
if (ctx.isEmpty(userInfo.phone)) { if (ctx.isEmpty(userInfo.phone) && type > 0) {
const userLog = await ctx.classModel.V3.CourseLogUser.findOne({ where: { user_uuid: userUuid, type: 1 }, order: [[ 'id', 'desc' ]], limit: 1 }); 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)) { 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; 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