diff --git a/app/Cache/VoteCache.php b/app/Cache/VoteCache.php new file mode 100644 index 0000000..4bba9d2 --- /dev/null +++ b/app/Cache/VoteCache.php @@ -0,0 +1,42 @@ +pluck('user_id')->toArray(); + $vote_users = array_unique($vote_users); + $this->set(strval($vote_id), json_encode($vote_users), 60 * 60 * 24); + + return $vote_users; + } + + /** + * 获取或更新投票缓存 + * + * @param int $vote_id 投票ID + * @return array + */ + public function getOrSetVoteCache(int $vote_id): array + { + $cache = $this->get(strval($vote_id)); + return $cache ? json_decode($cache, true) : $this->updateVoteCache($vote_id); + } +} diff --git a/app/Controller/Api/V1/TalkMessageController.php b/app/Controller/Api/V1/TalkMessageController.php index 3350ce4..775ada9 100644 --- a/app/Controller/Api/V1/TalkMessageController.php +++ b/app/Controller/Api/V1/TalkMessageController.php @@ -221,6 +221,11 @@ class TalkMessageController extends CController 'options' => 'required', ]); + $params['options'] = array_filter(explode(',', $params['options'])); + if (!$params['options']) { + return $this->response->fail('投票失败,请稍后再试!'); + } + $isTrue = $this->talkMessageService->handleVote($this->uid(), $params); if (!$isTrue) return $this->response->fail('投票失败,请稍后再试!'); diff --git a/app/Service/Message/FormatMessageService.php b/app/Service/Message/FormatMessageService.php index 0dc682e..c469ec0 100644 --- a/app/Service/Message/FormatMessageService.php +++ b/app/Service/Message/FormatMessageService.php @@ -2,11 +2,13 @@ namespace App\Service\Message; +use App\Cache\VoteCache; use App\Constants\TalkMessageType; use App\Model\Talk\TalkRecordsCode; use App\Model\Talk\TalkRecordsFile; use App\Model\Talk\TalkRecordsForward; use App\Model\Talk\TalkRecordsInvite; +use App\Model\Talk\TalkRecordsVote; use App\Model\User; class FormatMessageService @@ -102,6 +104,12 @@ class FormatMessageService $forwards = TalkRecordsForward::whereIn('record_id', $forwards)->get(['record_id', 'records_id', 'text'])->keyBy('record_id')->toArray(); } + if ($votes) { + $votes = TalkRecordsVote::whereIn('record_id', $votes)->get([ + 'id', 'record_id', 'title', 'answer_mode', 'status', 'answer_option', 'answer_num', 'answered_num' + ])->keyBy('record_id')->toArray(); + } + foreach ($rows as $k => $row) { $rows[$k]['file'] = []; $rows[$k]['code_block'] = []; @@ -135,7 +143,20 @@ class FormatMessageService break; case TalkMessageType::VOTE_MESSAGE:// 投票消息 - // todo 待开发 + $options = []; + foreach ($votes[$row['id']]['answer_option'] as $k2 => $value) { + $options[] = [ + 'key' => $k2, + 'value' => $value + ]; + } + + $votes[$row['id']]['answer_option'] = $options; + + $rows[$k]['vote'] = [ + 'vote_users' => VoteCache::getInstance()->getOrSetVoteCache($votes[$row['id']]['id']), + 'detail' => $votes[$row['id']] + ]; break; case TalkMessageType::GROUP_INVITE_MESSAGE:// 入群消息/退群消息 diff --git a/app/Service/TalkMessageService.php b/app/Service/TalkMessageService.php index 668bbed..53a6164 100644 --- a/app/Service/TalkMessageService.php +++ b/app/Service/TalkMessageService.php @@ -3,6 +3,7 @@ namespace App\Service; use App\Cache\LastMessage; +use App\Cache\VoteCache; use App\Constants\TalkMessageEvent; use App\Constants\TalkMessageType; use App\Model\Group\GroupMember; @@ -197,11 +198,12 @@ class TalkMessageService return false; } - $options = explode(',', $params['options']); - if (!$options) { + if (TalkRecordsVoteAnswer::where('vote_id', $record->vote_id)->where('user_id', $user_id)->exists()) { return false; } + $options = $params['options']; + sort($options); foreach ($options as $value) { @@ -209,7 +211,7 @@ class TalkMessageService } // 单选模式取第一个 - if ($record->answer_mode == 1) { + if ($record->answer_mode == 0) { $options = [$options[0]]; } @@ -234,6 +236,8 @@ class TalkMessageService return false; } + VoteCache::getInstance()->updateVoteCache($record->vote_id); + return true; } }