Authored by 邱骏

修改投票逻辑语法

... ... @@ -7,7 +7,8 @@
"html"
],
"rules": {
"camelcase": "off"
"camelcase": "off",
"no-trailing-spaces": "off"
}
}
\ No newline at end of file
}
... ...
... ... @@ -459,8 +459,7 @@ const article = {
like(req, res, next) {
const actId = req.body.actId;
const articleId = req.body.articleId;
let voteLimit = 5;
let vote = function() {
req.ctx(ArticleModel).likeArticle(actId, articleId)
.then(() => {
... ... @@ -484,19 +483,22 @@ const article = {
}
const user_ip = req.headers['X-Forwarded-For'] || req.ip || req.connection.remoteAddress;
// 获取活动投票限制参数
return req.ctx(ArticleModel).getActLimit(actId).then(actInfo => {
const {repeat_limit, vote_limit} = actInfo[0];
return req.ctx(ArticleModel).getArticleIp(actId, 0, user_ip).then(userCount => {
if (userCount > vote_limit) {
return Promise.reject({code: 400});
// 获取用户IP今日已投票次数
return req.ctx(ArticleModel).getIpCount(actId, user_ip).then(userCount => {
if (userCount.length > 0 && userCount[0].vote_count > vote_limit) {
return Promise.reject({code: 201, message: VOTE_MAX});
}
if (repeat_limit) {
return Promise.resolve();
}
return req.ctx(ArticleModel).getArticleIp(actId, articleId, user_ip).then(actCount => {
if (actCount > 0) {
return Promise.reject({code: 400});
console.log(actCount);
if (actCount.length > 0 && actCount[0].vote_count > 0) {
return Promise.reject({code: 203, message: VOTE_REPEAT});
}
return Promise.resolve();
});
... ... @@ -506,56 +508,6 @@ const article = {
}, (result) => {
return res.json(result);
}).catch(next);
// 获取活动投票限制参数
req.ctx(ArticleModel).getActLimit(actId)
.then(limit_data => {
console.log(limit_data);
let repeat = limit_data[0].repeat_limit;
let limit = limit_data[0].vote_limit;
voteLimit = limit;
return {repeat: repeat, limit: limit};
}).then(limit_result => {
console.log(limit_result);
// 获取用户IP今日已投票次数
req.ctx(ArticleModel).getArticleIp(actId, articleId, user_ip)
.then(count_result => {
let now_vote_num = 0; // 用户已投票次数
if (count_result.length > 0) {
now_vote_num = count_result[0].vote_count || 0;
}
if (now_vote_num < voteLimit) { // 如果投票次数未满,则判断该活动是否可以对单个用户重复投票
if (!limit_result.repeat) {
// 获取用户IP对此文章是否投过票
req.ctx(ArticleModel).getArticleIp(actId, articleId, user_ip)
.then(ip_result => {
// vote_count > 0的情况下表示已经投过票,则返回提示。否则则投票
if (ip_result.length > 0 && ip_result[0].vote_count > 0) {
res.json({
code: 203,
message: VOTE_REPEAT
});
} else {
vote();
}
});
} else {
vote();
}
} else { // 如果投票次数已满,则返回提示
res.json({
code: 201,
message: VOTE_MAX
});
}
}).catch(next);
});
}
};
... ...
... ... @@ -310,13 +310,11 @@ class ArticleModel extends global.yoho.BaseModel {
* @returns {*}
*/
getArticleIp(actId, articleId, ip) {
let sqlStr = `SELECT ip, count(ip) as vote_count, to_days(create_time) AS vote_day,
to_days(now()) as today
let sqlStr = `SELECT COUNT(*) as vote_count
FROM ACT_ARTICLE_GOOD WHERE act_id = ${actId}
AND ip = '${ip}'
AND article_id = ${articleId}
AND to_days(create_time) = to_days(now())
GROUP BY ip, vote_day`;
AND to_days(create_time) = to_days(now())`;
console.log(sqlStr);
return mysqlCli.query(sqlStr);
... ... @@ -329,12 +327,10 @@ class ArticleModel extends global.yoho.BaseModel {
* @returns {*}
*/
getIpCount(actId, ip) {
let sqlStr = `SELECT ip, count(ip) AS vote_count, to_days(create_time) AS vote_day,
to_days(now()) as today
let sqlStr = `SELECT count(*) AS vote_count
FROM ACT_ARTICLE_GOOD WHERE act_id = ${actId}
AND ip = '${ip}'
AND to_days(create_time) = to_days(now())
GROUP BY ip, vote_day`;
AND to_days(create_time) = to_days(now())`;
return mysqlCli.query(sqlStr);
}
... ...