Authored by 邱骏

修改投票规则控制

@@ -10,6 +10,8 @@ const ArticleModel = require('../models/article'); @@ -10,6 +10,8 @@ const ArticleModel = require('../models/article');
10 const POST_SUCCESS = '操作成功'; 10 const POST_SUCCESS = '操作成功';
11 const GET_SUCCESS = '获取成功'; 11 const GET_SUCCESS = '获取成功';
12 const INVALID_PARAMS = '参数错误'; 12 const INVALID_PARAMS = '参数错误';
  13 +const VOTE_REPEAT = '不能给同一位选手重复投票';
  14 +const VOTE_MAX = '达到每天投票上限';
13 15
14 const article = { 16 const article = {
15 /** 17 /**
@@ -457,6 +459,23 @@ const article = { @@ -457,6 +459,23 @@ const article = {
457 like(req, res, next) { 459 like(req, res, next) {
458 const actId = req.body.actId; 460 const actId = req.body.actId;
459 const articleId = req.body.articleId; 461 const articleId = req.body.articleId;
  462 + const voteType = req.body.type || 'v'; // v为投票,s为刷票
  463 + const voteLimit = parseInt(req.body.vl / 100, 10) || 5;
  464 +
  465 + let vote = function() {
  466 + req.ctx(ArticleModel).likeArticle(actId, articleId)
  467 + .then(() => {
  468 + return req.ctx(ArticleModel)
  469 + .insertLikeDetail(actId, articleId);
  470 + })
  471 + .then(() => {
  472 + res.json({
  473 + code: 200,
  474 + message: POST_SUCCESS
  475 + });
  476 + })
  477 + .catch(next);
  478 + };
460 479
461 if (!actId || !articleId) { 480 if (!actId || !articleId) {
462 return res.json({ 481 return res.json({
@@ -465,18 +484,61 @@ const article = { @@ -465,18 +484,61 @@ const article = {
465 }); 484 });
466 } 485 }
467 486
468 - req.ctx(ArticleModel).likeArticle(actId, articleId)  
469 - .then(() => {  
470 - return req.ctx(ArticleModel)  
471 - .insertLikeDetail(actId, articleId);  
472 - })  
473 - .then(() => {  
474 - res.json({  
475 - code: 200,  
476 - message: POST_SUCCESS 487 + if (voteType === 's') {
  488 + vote();
  489 + } else {
  490 + req.ctx(ArticleModel).getArticleLimit(actId, articleId)
  491 + .then(limit_data => {
  492 + let repeat = limit_data[0].repeat_limit;
  493 + let limit = limit_data[0].vote_limit;
  494 + let limit_result = {};
  495 +
  496 + limit_result.repeat = repeat;
  497 + limit_result.limit = limit;
  498 + return limit_result;
  499 + }).then(limit_result => {
  500 + req.ctx(ArticleModel).getArticleIp(actId, articleId, 1)
  501 + .then(count_result => {
  502 + console.log(count_result);
  503 + let now_vote_num = 0;
  504 +
  505 + if (count_result.length > 0) {
  506 + now_vote_num = count_result[0].vote_count || 0;
  507 + }
  508 +
  509 + if (!limit_result.repeat) {
  510 + req.ctx(ArticleModel).getArticleIp(actId, articleId, 0)
  511 + .then(ip_result => {
  512 + if (ip_result.length > 0 && ip_result[0].vote_count > 0) {
  513 + res.json({
  514 + code: 203,
  515 + message: VOTE_REPEAT
  516 + });
  517 + } else {
  518 + if (now_vote_num < voteLimit) {
  519 + vote();
  520 + } else {
  521 + res.json({
  522 + code: 201,
  523 + message: VOTE_MAX
  524 + });
  525 + }
  526 + }
  527 + });
  528 +
  529 + } else {
  530 + if (now_vote_num < voteLimit) {
  531 + vote();
  532 + } else {
  533 + res.json({
  534 + code: 201,
  535 + message: VOTE_MAX
  536 + });
  537 + }
  538 + }
  539 + }).catch(next);
477 }); 540 });
478 - })  
479 - .catch(next); 541 + }
480 } 542 }
481 }; 543 };
482 544
@@ -289,6 +289,44 @@ class ArticleModel extends global.yoho.BaseModel { @@ -289,6 +289,44 @@ class ArticleModel extends global.yoho.BaseModel {
289 } 289 }
290 290
291 /** 291 /**
  292 + * 获取当前点赞用户点赞的文章的投票限制状态(repeat字段表示该文章是否可以重复投票,vote_limit字段表示该文章)
  293 + * @param actId
  294 + * @param articleId
  295 + * @returns {*}
  296 + */
  297 + getArticleLimit(actId, articleId) {
  298 + let sqlStr = `SELECT repeat_limit, vote_limit FROM act_article WHERE
  299 + act_id = ${actId}
  300 + AND id = ${articleId};`;
  301 +
  302 + return mysqlCli.query(sqlStr);
  303 + }
  304 +
  305 + /**
  306 + * 获取当前点赞用户点赞时的IP,对应到当天(如果repeat为false,那么代表当前文章不能重复投票)
  307 + * @param actId 活动id
  308 + * @param articleId 文章id
  309 + * @param repeat 是否对于该文章可以重复投票
  310 + * @returns {*}
  311 + */
  312 + getArticleIp(actId, articleId, repeat) {
  313 + let sqlStr = `SELECT ip, count(ip) as vote_count, to_days(create_time) AS vote_day,
  314 + to_days(now()) as today
  315 + FROM ACT_ARTICLE_GOOD WHERE act_id = ${actId}
  316 + AND to_days(create_time) = to_days(now())`;
  317 +
  318 + let whereSql = '';
  319 +
  320 + if (!repeat) {
  321 + whereSql += ` AND article_id = ${articleId}`;
  322 + }
  323 +
  324 + let groupSql = ' group by ip,vote_day;';
  325 +
  326 + return mysqlCli.query(sqlStr + whereSql + groupSql);
  327 + }
  328 +
  329 + /**
292 * 文章点赞 330 * 文章点赞
293 * @param actId 331 * @param actId
294 * @param articleId 332 * @param articleId
  1 +/*
  2 + 控制用户投票
  3 + @author: qiuj <jun.qiu@yoho.cn>
  4 + @date: 2017-10-20 10:15:44
  5 +*/
  6 +#注意:GO;分割执行块
  7 +ALTER TABLE act_article ADD `repeat_limit` TINYINT DEFAULT 0;
  8 +GO;
  9 +ALTER TABLE act_article ADD `vote_limit` INT DEFAULT 1;
  10 +GO;
1 -module.exports = ['_migration', '20170913102356', '20170927092926']; 1 +module.exports = ['_migration', '20170913102356', '20170927092926', '20171020101544'];