Commit 8bab5c76 authored by 任国军's avatar 任国军

add like

parent 7da8d129
Pipeline #18059 passed with stage
in 8 seconds
...@@ -144,6 +144,42 @@ class InstitutionController extends Controller { ...@@ -144,6 +144,42 @@ class InstitutionController extends Controller {
ctx.success(ret); ctx.success(ret);
} }
// 根据分类获取选课指南
async getArticlesByCat() {
const { ctx, service } = this;
const inputParams = ctx.request.query;
if (ctx.isEmpty(inputParams) || ctx.isEmpty(inputParams.cat_id)) {
ctx.failed('cat_id is empty');
}
const ret = await service.course.v3.institution.getArticlesByCat(inputParams);
ctx.success(ret);
}
// 获取选课指南详情
async getArticle() {
const { ctx, service } = this;
const inputParams = ctx.params;
if (ctx.isEmpty(inputParams) || ctx.isEmpty(inputParams.id)) {
ctx.failed('article_id is empty');
}
const ret = await service.course.v3.institution.getArticle(Number(inputParams.id));
ctx.success(ret);
}
// 点赞
async like() {
const { ctx, service } = this;
const inputParams = ctx.request.body;
if (ctx.isEmpty(inputParams) || ctx.isEmpty(inputParams.type) || ctx.isEmpty(inputParams.type_id)) {
ctx.failed('参数错误');
}
const ret = await service.course.v3.institution.like(inputParams);
ctx.success(ret);
}
} }
module.exports = InstitutionController; module.exports = InstitutionController;
...@@ -15,10 +15,12 @@ module.exports = app => { ...@@ -15,10 +15,12 @@ module.exports = app => {
type: INTEGER, type: INTEGER,
cat_id: INTEGER, cat_id: INTEGER,
title: STRING, title: STRING,
description: STRING,
content: TEXT, content: TEXT,
image: STRING, image: STRING,
source: STRING, source: STRING,
like_count: INTEGER, like_count: INTEGER,
read_count: INTEGER,
sort: INTEGER, sort: INTEGER,
status: ENUM('offline', 'online'), status: ENUM('offline', 'online'),
is_deleted: INTEGER, is_deleted: INTEGER,
......
'use strict';
const moment = require('moment');
module.exports = app => {
const { STRING, INTEGER, DATE } = app.Sequelize;
const CourseLike = app.classModel.define('course_like', {
id: {
type: INTEGER,
primaryKey: true,
autoIncrement: true,
},
user_uuid: STRING,
type: INTEGER,
type_id: INTEGER,
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_like',
});
return CourseLike;
};
...@@ -40,4 +40,9 @@ module.exports = app => { ...@@ -40,4 +40,9 @@ module.exports = app => {
router.get('third', '/user/search', miniAuth, 'course.v3.institution.getUserSearch');// 用户搜索历史 router.get('third', '/user/search', miniAuth, 'course.v3.institution.getUserSearch');// 用户搜索历史
router.delete('third', '/user/search', miniAuth, 'course.v3.institution.deleteUserSearch');// 清空用户搜索记录 router.delete('third', '/user/search', miniAuth, 'course.v3.institution.deleteUserSearch');// 清空用户搜索记录
router.get('third', '/article/all', miniAuth, 'course.v3.institution.getArticlesByCat');// 根据分类获取选课指南
router.get('third', '/article/:id', miniAuth, 'course.v3.institution.getArticle');// 获取选课指南详情
router.post('third', '/like', miniAuth, 'course.v3.institution.like');// 点赞
}; };
...@@ -5,6 +5,8 @@ const Service = require('egg').Service; ...@@ -5,6 +5,8 @@ const Service = require('egg').Service;
const R = require('ramda'); const R = require('ramda');
const _ = require('lodash'); const _ = require('lodash');
const moment = require('moment'); const moment = require('moment');
const sequelize = require('sequelize');
class InstitutionSubService extends Service { class InstitutionSubService extends Service {
// 获取分类 // 获取分类
async getCats() { async getCats() {
...@@ -634,6 +636,114 @@ class InstitutionSubService extends Service { ...@@ -634,6 +636,114 @@ class InstitutionSubService extends Service {
const ret = await this.formatInstitutionList(institutionList, input); const ret = await this.formatInstitutionList(institutionList, input);
return ret; return ret;
} }
// 获取指定分类及子分类的选课指南
async getArticlesByCat(input) {
const { ctx } = this;
const page = Number(input.page) || 1;
const limit = Number(input.limit) || 2;
const offset = (page - 1) * limit;
const catId = Number(input.catId) || 0;
let catIds = [];
const filter = { where: { status: 'online', is_deleted: 0 }, order: [[ 'sort', 'asc' ]], limit, offset };
// 获取子分类
if (catId > 0) {
const cats = await ctx.classModel.V3.CourseCat.findAll({ where: { parent_id: catId, status: 'online', is_deleted: 0 } });
catIds = R.pluck('id', cats);
catIds.push(catId);
filter.where.cat_id = { $in: catIds };
}
const articles = await ctx.classModel.V3.CourseArticle.findAndCountAll(filter);
const ret = [];
for (const v of articles.rows) {
ret.push({
id: v.id,
type: v.type,
cat_id: v.cat_id,
title: v.title,
description: v.description,
content: v.content,
image: v.image,
source: v.source,
like_count: v.like_count,
read_count: v.read_count,
sort: v.sort,
created_time: v.created_time,
});
}
return { results: ret, count: articles.count, page };
}
// 获取选课指南
async getArticle(id) {
const { ctx } = this;
const article = await ctx.classModel.V3.CourseArticle.findOne({ where: { id, status: 'online', is_deleted: 0 } });
if (ctx.isEmpty(article)) {
ctx.failed('数据不存在');
}
await ctx.classModel.V3.CourseArticle.update({ read_count: sequelize.literal('`read_count` + 1') }, { where: { id } });
const ret = {
id: article.id,
type: article.type,
cat_id: article.cat_id,
title: article.title,
description: article.description,
content: article.content,
image: article.image,
source: article.source,
like_count: article.like_count,
read_count: article.read_count,
sort: article.sort,
created_time: article.created_time,
};
return ret;
}
// 点赞 1:选课指南;
async like(input) {
const { ctx } = this;
const type = Number(input.type) || 0;
const typeId = Number(input.type_id) || 0;
let tmp = {};
if (type === 0 || typeId === 0) {
ctx.failed('参数异常');
}
// 是否重复点赞
tmp = await ctx.classModel.V3.CourseLike.findOne({ where: { user_uuid: ctx.userUuid, type, type_id: typeId, is_deleted: 0 } });
if (!ctx.isEmpty(tmp)) {
ctx.failed('请勿重复点赞');
}
// 校验点赞对象是否存在
switch (type) {
case 1:
tmp = await ctx.classModel.V3.CourseArticle.findOne({ where: { id: typeId, status: 'online', is_deleted: 0 } });
if (ctx.isEmpty(tmp)) {
ctx.failed('数据不存在');
}
// 更新点赞数
await ctx.classModel.V3.CourseArticle.update({ read_count: sequelize.literal('`read_count` + 1') }, { where: { id: typeId } });
break;
default:
break;
}
await ctx.classModel.V3.CourseLike.create({ user_uuid: ctx.userUuid, type, type_id: typeId, is_deleted: 0, created_time: moment().format('YYYY-MM-DD HH:mm:ss') });
return;
}
} }
module.exports = InstitutionSubService; 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