function vote($aim_type, $aim_id, $user_id, $score) { if (!is_numeric($aim_id) || !$aim_id || !$user_id) { return; } //$aim_id; //$aim_type = self::AIM_TYPE_FEED; $score = $score > 0 ? 1 : -1; $this->init('votes'); $votes = $this->get(compact('aim_id', 'aim_type', 'user_id')); $last_vote_type = $votes['vote_type']; $vote_id = $votes['vote_id']; $OPR = array(); $this->begin(); try { if ($aim_type == self::AIM_TYPE_FEED) { $this->init(); //锁行 $feed = $this->get($aim_id, true); $ups = $feed['ups']; $downs = $feed['downs']; } elseif ($aim_type == self::AIM_TYPE_COMMENT) { $this->init('comments'); //锁行 $comment = $this->get($aim_id, true); $ups = $comment['ups']; $downs = $comment['downs']; } if ($score > 0 && $last_vote_type != self::VOTE_TYPE_LIKE) { //改成喜欢 $ups += 1; $OPR['ups[+]'] = 1; if ($last_vote_type == self::VOTE_TYPE_DISLIKE) { $downs -= 1; $OPR['downs[-]'] = 1; } $vote_type = self::VOTE_TYPE_LIKE; } elseif ($score < 0 && $last_vote_type != self::VOTE_TYPE_DISLIKE) { //改成不喜欢 $downs += 1; $OPR['downs[+]'] = 1; if ($last_vote_type == self::VOTE_TYPE_LIKE) { $ups -= 1; $OPR['ups[+]'] = 1; } $vote_type = self::VOTE_TYPE_DISLIKE; } else { //改成未打过 if ($last_vote_type == self::VOTE_TYPE_DISLIKE) { $downs -= 1; $OPR['downs[-]'] = 1; } if ($last_vote_type == self::VOTE_TYPE_LIKE) { $ups -= 1; $OPR['ups[-]'] = 1; } $vote_type = self::VOTE_TYPE_UNVOTE; } if ($aim_type == self::AIM_TYPE_FEED) { //为了方便计算和存储,省略两位小数,转为整形 $hot_score = sorts::hot_score($ups, $downs, $feed['add_time']); $controversy_score = sorts::controversy($ups, $downs); $this->init(); $this->update(compact('OPR', 'hot_score', 'controversy_score'), array('feed_id' => $aim_id)); $this->user_vote_feed($user_id, 'refresh'); } elseif ($aim_type == self::AIM_TYPE_COMMENT) { //为了方便计算和存储,省略两位小数,转为整形 $hot_score = sorts::comment_hot_score($ups, $downs); $this->init('comments'); $this->update(compact('OPR', 'hot_score'), array('comment_id' => $aim_id)); } $this->init('votes'); if ($last_vote_type != self::VOTE_TYPE_UNVOTE && $vote_type == self::VOTE_TYPE_UNVOTE) { $this->delete($vote_id); } else { $add_time = $_SERVER['REQUEST_TIME']; $this->edit(compact('vote_id', 'aim_id', 'aim_type', 'user_id', 'vote_type', 'add_time')); } $this->init(); $this->commit(); return true; } catch (Exception $e) { $this->message = '失败'; $this->rollBack(); return false; } }