Authored by 邱骏

修改投票规则控制

... ... @@ -10,6 +10,8 @@ const ArticleModel = require('../models/article');
const POST_SUCCESS = '操作成功';
const GET_SUCCESS = '获取成功';
const INVALID_PARAMS = '参数错误';
const VOTE_REPEAT = '不能给同一位选手重复投票';
const VOTE_MAX = '达到每天投票上限';
const article = {
/**
... ... @@ -457,6 +459,23 @@ const article = {
like(req, res, next) {
const actId = req.body.actId;
const articleId = req.body.articleId;
const voteType = req.body.type || 'v'; // v为投票,s为刷票
const voteLimit = parseInt(req.body.vl / 100, 10) || 5;
let vote = function() {
req.ctx(ArticleModel).likeArticle(actId, articleId)
.then(() => {
return req.ctx(ArticleModel)
.insertLikeDetail(actId, articleId);
})
.then(() => {
res.json({
code: 200,
message: POST_SUCCESS
});
})
.catch(next);
};
if (!actId || !articleId) {
return res.json({
... ... @@ -465,18 +484,61 @@ const article = {
});
}
req.ctx(ArticleModel).likeArticle(actId, articleId)
.then(() => {
return req.ctx(ArticleModel)
.insertLikeDetail(actId, articleId);
})
.then(() => {
res.json({
code: 200,
message: POST_SUCCESS
if (voteType === 's') {
vote();
} else {
req.ctx(ArticleModel).getArticleLimit(actId, articleId)
.then(limit_data => {
let repeat = limit_data[0].repeat_limit;
let limit = limit_data[0].vote_limit;
let limit_result = {};
limit_result.repeat = repeat;
limit_result.limit = limit;
return limit_result;
}).then(limit_result => {
req.ctx(ArticleModel).getArticleIp(actId, articleId, 1)
.then(count_result => {
console.log(count_result);
let now_vote_num = 0;
if (count_result.length > 0) {
now_vote_num = count_result[0].vote_count || 0;
}
if (!limit_result.repeat) {
req.ctx(ArticleModel).getArticleIp(actId, articleId, 0)
.then(ip_result => {
if (ip_result.length > 0 && ip_result[0].vote_count > 0) {
res.json({
code: 203,
message: VOTE_REPEAT
});
} else {
if (now_vote_num < voteLimit) {
vote();
} else {
res.json({
code: 201,
message: VOTE_MAX
});
}
}
});
} else {
if (now_vote_num < voteLimit) {
vote();
} else {
res.json({
code: 201,
message: VOTE_MAX
});
}
}
}).catch(next);
});
})
.catch(next);
}
}
};
... ...
... ... @@ -289,6 +289,44 @@ class ArticleModel extends global.yoho.BaseModel {
}
/**
* 获取当前点赞用户点赞的文章的投票限制状态(repeat字段表示该文章是否可以重复投票,vote_limit字段表示该文章)
* @param actId
* @param articleId
* @returns {*}
*/
getArticleLimit(actId, articleId) {
let sqlStr = `SELECT repeat_limit, vote_limit FROM act_article WHERE
act_id = ${actId}
AND id = ${articleId};`;
return mysqlCli.query(sqlStr);
}
/**
* 获取当前点赞用户点赞时的IP,对应到当天(如果repeat为false,那么代表当前文章不能重复投票)
* @param actId 活动id
* @param articleId 文章id
* @param repeat 是否对于该文章可以重复投票
* @returns {*}
*/
getArticleIp(actId, articleId, repeat) {
let sqlStr = `SELECT ip, count(ip) as vote_count, to_days(create_time) AS vote_day,
to_days(now()) as today
FROM ACT_ARTICLE_GOOD WHERE act_id = ${actId}
AND to_days(create_time) = to_days(now())`;
let whereSql = '';
if (!repeat) {
whereSql += ` AND article_id = ${articleId}`;
}
let groupSql = ' group by ip,vote_day;';
return mysqlCli.query(sqlStr + whereSql + groupSql);
}
/**
* 文章点赞
* @param actId
* @param articleId
... ...
/*
控制用户投票
@author: qiuj <jun.qiu@yoho.cn>
@date: 2017-10-20 10:15:44
*/
#注意:GO;分割执行块
ALTER TABLE act_article ADD `repeat_limit` TINYINT DEFAULT 0;
GO;
ALTER TABLE act_article ADD `vote_limit` INT DEFAULT 1;
GO;
... ...
module.exports = ['_migration', '20170913102356', '20170927092926'];
module.exports = ['_migration', '20170913102356', '20170927092926', '20171020101544'];
... ...