Files
midi-php/application/api/controller/Redpacket.php
2025-10-10 19:15:41 +08:00

104 lines
2.3 KiB
PHP

<?php
namespace app\api\controller;
use app\common\controller\BaseCom;
use app\common\service\RedpacketService;
/**
* 红包接口
*/
class Redpacket extends BaseCom
{
/**
* 发红包
*/
public function create()
{
$data = $this->request->post();
$data['user_id'] = $this->uid;
$service = new RedpacketService();
$reslut = $service->create($data);
return V($reslut['code'], $reslut['msg'], $reslut['data']);
}
/**
* 抢红包
*/
public function grab()
{
$redpacketId = $this->request->post('redpacket_id');
$password = $this->request->post('password', '');
if (empty($redpacketId)) {
return V(0, '红包ID不能为空');
}
$service = new RedpacketService();
$reslut = $service->grabWithResult($redpacketId, $this->uid, $password);
return V($reslut['code'], $reslut['msg'], $reslut['data']);
}
/**
* 获取抢红包结果
*/
public function grabResult()
{
$user = $this->auth->getUser();
$redpacketId = $this->request->get('redpacket_id');
if (empty($redpacketId)) {
$this->error('红包ID不能为空');
}
$service = new RedpacketService();
$result = $service->getGrabResult($redpacketId, $user->id);
if (!$result) {
$this->error('红包不存在');
}
$this->success('获取成功', $result);
}
/**
* 红包详情
*/
public function detail()
{
$redpacketId = $this->request->get('redpacket_id');
$currentUserId = $this->auth->isLogin() ? $this->auth->id : 0;
if (empty($redpacketId)) {
$this->error('红包ID不能为空');
}
try {
$service = new RedpacketService();
$detail = $service->getDetail($redpacketId, $currentUserId);
if (!$detail) {
$this->error('红包不存在');
}
$this->success('获取成功', $detail);
} catch (\Exception $e) {
$this->error($e->getMessage());
}
}
/**
* 获取倒计时选项
*/
public function countdownOptions()
{
$options = \app\common\model\Redpacket::$countdownOptions;
$this->success('获取成功', $options);
}
}