完善投票接口

main
gzydong 2021-07-17 21:27:48 +08:00
parent 1dcb385280
commit 4f9630b001
4 changed files with 32 additions and 28 deletions

View File

@ -185,7 +185,7 @@ class TalkMessageController extends CController
'receiver_id' => 'required|integer|min:1',
'mode' => 'required|integer|in:0,1',
'title' => 'required',
'options' => 'required|array|max:6',
'options' => 'required|array',
]);
$user_id = $this->uid();
@ -198,10 +198,10 @@ class TalkMessageController extends CController
'user_id' => $user_id,
'receiver_id' => $params['receiver_id'],
], [
'user_id' => $user_id,
'mode' => $params['mode'],
'title' => $params['title'],
'options' => $params['options'],
'user_id' => $user_id,
'title' => $params['title'],
'answer_mode' => $params['mode'],
'answer_option' => $params['options'],
]);
if (!$isTrue) return $this->response->fail('发起投票失败!');
@ -218,7 +218,7 @@ class TalkMessageController extends CController
$params = $this->request->inputs(['record_id', 'options']);
$this->validate($params, [
'record_id' => 'required|integer|min:1',
'options' => 'required|array',
'options' => 'required',
]);
$isTrue = $this->talkMessageService->handleVote($this->uid(), $params);

View File

@ -40,11 +40,12 @@ class TalkRecordsVote extends BaseModel
];
protected $casts = [
'record_id' => 'integer',
'user_id' => 'integer',
'answer_mode' => 'integer',
'answer_num' => 'integer',
'answered_num' => 'integer',
'status' => 'integer'
'record_id' => 'integer',
'user_id' => 'integer',
'answer_mode' => 'integer',
'answer_num' => 'integer',
'answered_num' => 'integer',
'status' => 'integer',
'answer_option' => 'array',
];
}

View File

@ -60,7 +60,7 @@ class FormatMessageService
{
if (empty($rows)) return [];
$files = $codes = $forwards = $invites = [];
$files = $codes = $forwards = $invites = $votes = [];
foreach ($rows as $value) {
switch ($value['msg_type']) {
case TalkMessageType::FILE_MESSAGE:
@ -75,6 +75,10 @@ class FormatMessageService
case TalkMessageType::CODE_MESSAGE:
$codes[] = $value['id'];
break;
case TalkMessageType::VOTE_MESSAGE:
$votes[] = $value['id'];
break;
default:
}
}

View File

@ -127,27 +127,25 @@ class TalkMessageService
Db::beginTransaction();
try {
$message['msg_type'] = TalkMessageType::FILE_MESSAGE;
$message['msg_type'] = TalkMessageType::VOTE_MESSAGE;
$message['created_at'] = date('Y-m-d H:i:s');
$message['updated_at'] = date('Y-m-d H:i:s');
$insert = TalkRecords::create($message);
$insert = TalkRecords::create($message);
$options = [];
foreach ($vote['options'] as $k => $option) {
foreach ($vote['answer_option'] as $k => $option) {
$options[chr(65 + $k)] = $option;
}
$vote['record_id'] = $insert->id;
$vote['user_id'] = $options;
$vote['options'] = $options;
$vote['answer_num'] = $answer_num;
$vote['created_at'] = date('Y-m-d H:i:s');
$vote['updated_at'] = $vote['created_at'];
$vote['record_id'] = $insert->id;
$vote['answer_option'] = $options;
$vote['answer_num'] = $answer_num;
$vote['created_at'] = date('Y-m-d H:i:s');
$vote['updated_at'] = $vote['created_at'];
if (!TalkRecordsVote::create($vote)) {
throw new Exception('插入聊天记录(投票消息)失败...');
}
Db::commit();
} catch (Exception $e) {
Db::rollBack();
@ -181,8 +179,11 @@ class TalkMessageService
{
$record = TalkRecords::join('talk_records_vote as vote', 'vote.record_id', '=', 'talk_records.id')
->where('talk_records.id', $params['record_id'])
->withCasts([
'answer_option' => 'array'
])
->first([
'talk_records.id', 'talk_records.receiver_id', 'talk_records.talk_type',
'talk_records.id', 'talk_records.receiver_id', 'talk_records.talk_type', 'talk_records.msg_type',
'vote.id as vote_id', 'vote.answer_mode', 'vote.answer_option', 'vote.answer_num', 'vote.status as vote_status'
]);
@ -203,9 +204,8 @@ class TalkMessageService
sort($options);
$answerOption = json_decode($record->answer_option, true);
foreach ($options as $value) {
if (!isset($answerOption[$value])) return false;
if (!isset($record->answer_option[$value])) return false;
}
// 单选模式取第一个
@ -231,7 +231,6 @@ class TalkMessageService
}
});
} catch (\Exception $e) {
logger()->error($e->getMessage());
return false;
}