优化投票功能

main
gzydong 2021-07-18 23:41:25 +08:00
parent 4f9630b001
commit d7d6947be9
4 changed files with 76 additions and 4 deletions

42
app/Cache/VoteCache.php Normal file
View File

@ -0,0 +1,42 @@
<?php
declare(strict_types=1);
namespace App\Cache;
use App\Cache\Repository\StringRedis;
use App\Model\Talk\TalkRecordsVoteAnswer;
use App\Traits\StaticInstance;
class VoteCache extends StringRedis
{
protected $name = 'vote-cache';
use StaticInstance;
/**
* 更新投票缓存
*
* @param int $vote_id 投票ID
* @return array
*/
public function updateVoteCache(int $vote_id): array
{
$vote_users = TalkRecordsVoteAnswer::where('vote_id', $vote_id)->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);
}
}

View File

@ -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('投票失败,请稍后再试!');

View File

@ -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:// 入群消息/退群消息

View File

@ -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;
}
}