feat: 添加投票功能 - 100%

This commit is contained in:
weifashi
2023-12-08 18:05:40 +08:00
parent e9fd223808
commit 9a942c483d
15 changed files with 574 additions and 120 deletions
+114 -6
View File
@@ -1993,7 +1993,7 @@ class DialogController extends AbstractController
}
/**
* @api {post} api/dialog/msg/wordchain 15. 发送接龙消息
* @api {post} api/dialog/msg/wordchain 44. 发送接龙消息
*
* @apiDescription 需要token身份
* @apiVersion 1.0.0
@@ -2018,8 +2018,6 @@ class DialogController extends AbstractController
$text = trim(Request::input('text'));
$list = Request::input('list');
//
$result = [];
//
WebSocketDialog::checkDialog($dialog_id);
$strlen = mb_strlen($text);
$noimglen = mb_strlen(preg_replace("/<img[^>]*?>/i", "", $text));
@@ -2054,8 +2052,118 @@ class DialogController extends AbstractController
'userid' => $userid,
'uuid' => $uuid ?: Base::generatePassword(36),
];
$result = WebSocketDialogMsg::sendMsg(null, $dialog_id, 'word-chain', $msgData, $user->userid);
//
return $result;
return WebSocketDialogMsg::sendMsg(null, $dialog_id, 'word-chain', $msgData, $user->userid);
}
/**
* @api {post} api/dialog/msg/vote 45. 发起投票
*
* @apiDescription 需要token身份
* @apiVersion 1.0.0
* @apiGroup dialog
* @apiName msg__vote
*
* @apiParam {Number} dialog_id 对话ID
* @apiParam {String} text 投票内容
* @apiParam {Array} type 投票类型
* @apiParam {String} [uuid] 投票ID
* @apiParam {Array} [list] 投票列表
* @apiParam {Number} [multiple] 多选
* @apiParam {Number} [anonymous] 匿名
* @apiParam {Array} [vote] 投票
*
* @apiSuccess {Number} ret 返回状态码(1正确、0错误)
* @apiSuccess {String} msg 返回信息(错误描述)
* @apiSuccess {Object} data 返回数据
*/
public function msg__vote()
{
$user = User::auth();
//
$dialog_id = intval(Request::input('dialog_id'));
$uuid = trim(Request::input('uuid'));
$text = trim(Request::input('text'));
$type = trim(Request::input('type','create'));
$multiple = intval(Request::input('multiple')) ?: 0;
$anonymous = intval(Request::input('anonymous')) ?: 0;
$list = Request::input('list');
$vote = Request::input('vote') ?: [];
$votes = is_array($vote) ? $vote : [$vote];
//
WebSocketDialog::checkDialog($dialog_id);
//
$action = null;
$userid = $user->userid;
$result = [];
if($type != 'create'){
if($type == 'vote' && empty($votes)){
return Base::retError('参数错误');
}
if (empty($uuid)) {
return Base::retError('参数错误');
}
$dialogMsgs = WebSocketDialogMsg::whereDialogId($dialog_id)
->whereType('vote')
->orderByDesc('created_at')
->where('msg','like',"%$uuid%")
->get();
//
if($type == 'again'){
$res = WebSocketDialogMsg::sendMsg(null, $dialog_id, 'vote', $dialogMsgs[0]->msg, $user->userid);
if(Base::isError($res)){
return $res;
}
$result[] = $res['data'];
}else{
foreach($dialogMsgs as $dialogMsg){
$action = "change-{$dialogMsg->id}";
$msgData = $dialogMsg->msg;
if($type == 'finish'){
$msgData['state'] = 0;
}else{
$msgDataVotes = $msgData['votes'] ?? [];
if(in_array($userid,array_column($msgDataVotes,'userid'))){
return Base::retError('不能重复投票');
}
$msgDataVotes[] = [
'userid' => $userid,
'votes' => $votes,
];
$msgData['votes'] = $msgDataVotes;
}
$res = WebSocketDialogMsg::sendMsg($action, $dialog_id, 'vote', $msgData, $user->userid);
if(Base::isError($res)){
return $res;
}
$result[] = $res['data'];
}
}
}else{
$strlen = mb_strlen($text);
$noimglen = mb_strlen(preg_replace("/<img[^>]*?>/i", "", $text));
if ($strlen < 1) {
return Base::retError('内容不能为空');
}
if ($noimglen > 200000) {
return Base::retError('内容最大不能超过200000字');
}
$msgData = [
'text' => $text,
'list' => $list,
'userid' => $userid,
'uuid' => $uuid ?: Base::generatePassword(36),
'multiple' => $multiple,
'anonymous' => $anonymous,
'votes' => [],
'state' => 1
];
$res = WebSocketDialogMsg::sendMsg($action, $dialog_id, 'vote', $msgData, $user->userid);
if(Base::isError($res)){
return $res;
}
$result[] = $res['data'];
}
return Base::retSuccess('发送成功', $result);
}
}