You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
76 lines
2.0 KiB
76 lines
2.0 KiB
<?php
|
|
|
|
namespace app\home\controller;
|
|
|
|
use app\model\UserVoteModel;
|
|
use app\model\VoteModel;
|
|
use think\Request;
|
|
use think\response\Json;
|
|
|
|
class Vote extends HomeBaseController
|
|
{
|
|
|
|
public function index(): Json
|
|
{
|
|
|
|
$list = VoteModel::where('is_delete', VoteModel::IS_DELETE_NO)
|
|
->order('sort', 'asc')
|
|
->select();
|
|
|
|
return json([
|
|
'code' => '0',
|
|
'message' => 'Request successful.',
|
|
'data' => $list,
|
|
]);
|
|
}
|
|
|
|
public function cheer(Request $request): Json
|
|
{
|
|
$param = $this->request->param();
|
|
if (empty($param['id']) || empty($this->request->userId)) {
|
|
return json([
|
|
'code' => '1',
|
|
'message' => 'Error',
|
|
'data' => [],
|
|
]);
|
|
}
|
|
|
|
$vote = VoteModel::find($param['id']);
|
|
if (empty($vote)) {
|
|
return json([
|
|
'code' => '1',
|
|
'message' => 'Error',
|
|
'data' => [],
|
|
]);
|
|
}
|
|
$onedayTimes = $vote->oneday_times;
|
|
$todayStart = date('Y-m-d 00:00:00');
|
|
$todayEnd = date('Y-m-d 23:59:59');
|
|
|
|
$times = UserVoteModel::where('vote_id', $param['id'])->where('user_id', $this->request->userId)->where('create_time', '<=', $todayEnd)->where('create_time', '>=', $todayStart)->count();
|
|
if ($times >= $onedayTimes) {
|
|
return json([
|
|
'code' => '1',
|
|
'message' => 'You have already cast your vote today',
|
|
'data' => [],
|
|
]);
|
|
}
|
|
|
|
$vote->score_num = $vote->score_num + 1;
|
|
$vote->save();
|
|
|
|
UserVoteModel::insert(
|
|
[
|
|
'vote_id' => $param['id'],
|
|
'user_id' => $this->request->userId,
|
|
'create_time' => date('Y-m-d H:i:s')
|
|
]
|
|
);
|
|
|
|
return json([
|
|
'code' => '0',
|
|
'message' => 'Request successful.',
|
|
'data' => [],
|
|
]);
|
|
}
|
|
}
|