179 lines
5.7 KiB
PHP
179 lines
5.7 KiB
PHP
<?php
|
|
|
|
namespace app\api\controller;
|
|
|
|
use app\common\library\GiftQueue;
|
|
use think\Log;
|
|
use app\common\controller\BaseCom;
|
|
|
|
class GiveGiftBase extends BaseCom
|
|
{
|
|
protected $noNeedLogin = [];
|
|
protected $noNeedRight = ['*'];
|
|
|
|
/**
|
|
* 送礼接口
|
|
*/
|
|
public function give()
|
|
{
|
|
$params = $this->request->param();
|
|
|
|
// 验证必要参数
|
|
$required = ['user_id', 'gift_id', 'gift_user', 'from_id'];
|
|
foreach ($required as $field) {
|
|
if (empty($params[$field])) {
|
|
$this->error("{$field}不能为空");
|
|
}
|
|
}
|
|
|
|
// 构建送礼数据
|
|
$giftData = [
|
|
'user_id' => intval($params['user_id']),
|
|
'gift_id' => intval($params['gift_id']),
|
|
'gift_type' => intval($params['gift_type'] ?? 1),
|
|
'number' => intval($params['number'] ?? 1),
|
|
'gift_user' => intval($params['gift_user']),
|
|
'from_id' => intval($params['from_id']),
|
|
'pit_number' => intval($params['pit_number'] ?? 0),
|
|
'total_price' => floatval($params['total_price'] ?? 0),
|
|
'type' => intval($params['type'] ?? 1),
|
|
'from' => intval($params['from'] ?? 1),
|
|
'createtime' => time(),
|
|
];
|
|
|
|
// 验证价格
|
|
if ($giftData['total_price'] <= 0) {
|
|
$this->error('礼物总价值必须大于0');
|
|
}
|
|
|
|
// 验证数量
|
|
if ($giftData['number'] <= 0) {
|
|
$this->error('礼物数量必须大于0');
|
|
}
|
|
|
|
// 使用队列异步处理
|
|
$result = GiftQueue::push($giftData);
|
|
|
|
if ($result) {
|
|
// 记录日志
|
|
Log::info("用户送礼成功(队列),用户: {$giftData['user_id']}, 收礼人: {$giftData['gift_user']}, 房间: {$giftData['from_id']}");
|
|
|
|
// 立即返回成功,后台异步处理
|
|
$this->success('送礼成功,正在处理中', [
|
|
'queue_status' => 'processing',
|
|
'user_id' => $giftData['user_id'],
|
|
'gift_user' => $giftData['gift_user'],
|
|
'timestamp' => time()
|
|
]);
|
|
} else {
|
|
Log::error("用户送礼失败(队列),用户: {$giftData['user_id']}, 收礼人: {$giftData['gift_user']}");
|
|
$this->error('送礼失败,请重试');
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 批量送礼
|
|
*/
|
|
public function batchGive()
|
|
{
|
|
$gifts = $this->request->param('gifts/a', []);
|
|
|
|
if (empty($gifts) || !is_array($gifts)) {
|
|
$this->error('礼物数据不能为空');
|
|
}
|
|
|
|
$successCount = 0;
|
|
$failCount = 0;
|
|
$giftList = [];
|
|
|
|
foreach ($gifts as $gift) {
|
|
// 验证必要参数
|
|
if (empty($gift['user_id']) || empty($gift['gift_user']) || empty($gift['from_id'])) {
|
|
$failCount++;
|
|
continue;
|
|
}
|
|
|
|
$giftData = [
|
|
'user_id' => intval($gift['user_id']),
|
|
'gift_id' => intval($gift['gift_id'] ?? 0),
|
|
'gift_type' => intval($gift['gift_type'] ?? 1),
|
|
'number' => intval($gift['number'] ?? 1),
|
|
'gift_user' => intval($gift['gift_user']),
|
|
'from_id' => intval($gift['from_id']),
|
|
'pit_number' => intval($gift['pit_number'] ?? 0),
|
|
'total_price' => floatval($gift['total_price'] ?? 0),
|
|
'type' => intval($gift['type'] ?? 1),
|
|
'from' => intval($gift['from'] ?? 1),
|
|
'createtime' => time(),
|
|
];
|
|
|
|
$giftList[] = $giftData;
|
|
}
|
|
|
|
// 批量加入队列
|
|
$result = GiftQueue::pushBatch($giftList);
|
|
|
|
Log::info("批量送礼提交,总计: {$result['total']}, 成功: {$result['success']}, 失败: {$result['failed']}");
|
|
|
|
$this->success("批量送礼提交成功", $result);
|
|
}
|
|
|
|
/**
|
|
* 获取队列状态
|
|
*/
|
|
public function queueStatus()
|
|
{
|
|
$stats = GiftQueue::stats();
|
|
|
|
$this->success('获取成功', $stats);
|
|
}
|
|
|
|
/**
|
|
* 测试送礼接口(直接写入,用于测试)
|
|
*/
|
|
public function testGive()
|
|
{
|
|
// 只有测试环境可用
|
|
if (config('app_debug') != true) {
|
|
$this->error('测试接口仅限开发环境使用');
|
|
}
|
|
|
|
$params = $this->request->param();
|
|
|
|
// 验证必要参数
|
|
$required = ['user_id', 'gift_id', 'gift_user', 'from_id'];
|
|
foreach ($required as $field) {
|
|
if (empty($params[$field])) {
|
|
$this->error("{$field}不能为空");
|
|
}
|
|
}
|
|
|
|
// 构建送礼数据
|
|
$giftData = [
|
|
'user_id' => intval($params['user_id']),
|
|
'gift_id' => intval($params['gift_id']),
|
|
'gift_type' => intval($params['gift_type'] ?? 1),
|
|
'number' => intval($params['number'] ?? 1),
|
|
'gift_user' => intval($params['gift_user']),
|
|
'from_id' => intval($params['from_id']),
|
|
'pit_number' => intval($params['pit_number'] ?? 0),
|
|
'total_price' => floatval($params['total_price'] ?? 0),
|
|
'type' => intval($params['type'] ?? 1),
|
|
'from' => intval($params['from'] ?? 1),
|
|
'createtime' => time(),
|
|
];
|
|
|
|
// 直接写入数据库(测试用)
|
|
$model = new \app\common\model\GiveGiftBase();
|
|
$result = $model->addGiftRecord($giftData);
|
|
|
|
if ($result) {
|
|
$this->success('测试送礼成功(直接写入)', [
|
|
'id' => $result,
|
|
'data' => $giftData
|
|
]);
|
|
} else {
|
|
$this->error('测试送礼失败: ' . $model->getError());
|
|
}
|
|
}
|
|
} |