|
@@ -17,10 +17,8 @@ use UCore\Exception\LogicException;
|
|
|
*/
|
|
*/
|
|
|
class GetHandler extends BaseHandler
|
|
class GetHandler extends BaseHandler
|
|
|
{
|
|
{
|
|
|
-
|
|
|
|
|
/**
|
|
/**
|
|
|
* 是否需要登录
|
|
* 是否需要登录
|
|
|
- *
|
|
|
|
|
* @var bool
|
|
* @var bool
|
|
|
*/
|
|
*/
|
|
|
protected bool $need_login = true;
|
|
protected bool $need_login = true;
|
|
@@ -36,25 +34,24 @@ class GetHandler extends BaseHandler
|
|
|
// 创建响应对象
|
|
// 创建响应对象
|
|
|
$response = new ResponsePetGet();
|
|
$response = new ResponsePetGet();
|
|
|
|
|
|
|
|
- // 获取请求参数
|
|
|
|
|
- $itemId = $data->getItemId();
|
|
|
|
|
- $userId = $this->user_id;
|
|
|
|
|
-
|
|
|
|
|
- // 验证参数
|
|
|
|
|
- if (!$itemId || $itemId <= 0) {
|
|
|
|
|
- throw new LogicException("物品ID无效");
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ try {
|
|
|
|
|
+ // 获取请求参数
|
|
|
|
|
+ $itemId = $data->getItemId();
|
|
|
|
|
+ $userId = $this->user_id;
|
|
|
|
|
|
|
|
- // 先进行验证,避免不必要的事务开销
|
|
|
|
|
- $validation = new \App\Module\Pet\Validations\PetGetValidation([
|
|
|
|
|
- 'user_id' => $userId,
|
|
|
|
|
- 'item_id' => $itemId
|
|
|
|
|
- ]);
|
|
|
|
|
|
|
+ // 验证参数
|
|
|
|
|
+ if (!$itemId || $itemId <= 0) {
|
|
|
|
|
+ throw new LogicException("物品ID无效");
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- // 验证数据
|
|
|
|
|
- $validation->validated();
|
|
|
|
|
|
|
+ // 先进行验证,避免不必要的事务开销
|
|
|
|
|
+ $validation = new \App\Module\Pet\Validations\PetGetValidation([
|
|
|
|
|
+ 'user_id' => $userId,
|
|
|
|
|
+ 'item_id' => $itemId
|
|
|
|
|
+ ]);
|
|
|
|
|
|
|
|
- try {
|
|
|
|
|
|
|
+ // 验证数据
|
|
|
|
|
+ $validation->validated();
|
|
|
|
|
|
|
|
// 验证通过后,开启事务
|
|
// 验证通过后,开启事务
|
|
|
DB::beginTransaction();
|
|
DB::beginTransaction();
|
|
@@ -65,6 +62,41 @@ class GetHandler extends BaseHandler
|
|
|
// 提交事务
|
|
// 提交事务
|
|
|
DB::commit();
|
|
DB::commit();
|
|
|
|
|
|
|
|
|
|
+ // 设置响应状态
|
|
|
|
|
+ $this->response->setCode(0);
|
|
|
|
|
+ $this->response->setMsg($result['message']);
|
|
|
|
|
+
|
|
|
|
|
+ // 设置LastData
|
|
|
|
|
+ if (isset($result['last_data'])) {
|
|
|
|
|
+ $this->response->setLastData($result['last_data']);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ } catch (\UCore\Exception\ValidateException $e) {
|
|
|
|
|
+ // 验证失败,此时可能还没有开启事务
|
|
|
|
|
+ $this->response->setCode(400);
|
|
|
|
|
+ $this->response->setMsg($e->getMessage());
|
|
|
|
|
+
|
|
|
|
|
+ Log::warning('用户获取宠物验证失败', [
|
|
|
|
|
+ 'user_id' => $userId ?? $this->user_id,
|
|
|
|
|
+ 'item_id' => $itemId ?? null,
|
|
|
|
|
+ 'error' => $e->getMessage()
|
|
|
|
|
+ ]);
|
|
|
|
|
+ } catch (LogicException $e) {
|
|
|
|
|
+ // 业务逻辑异常,需要回滚事务
|
|
|
|
|
+ if (DB::transactionLevel() > 0) {
|
|
|
|
|
+ DB::rollBack();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 设置错误响应
|
|
|
|
|
+ $this->response->setCode(400);
|
|
|
|
|
+ $this->response->setMsg($e->getMessage());
|
|
|
|
|
+
|
|
|
|
|
+ Log::warning('用户获取宠物失败', [
|
|
|
|
|
+ 'user_id' => $userId ?? $this->user_id,
|
|
|
|
|
+ 'item_id' => $itemId ?? null,
|
|
|
|
|
+ 'error' => $e->getMessage(),
|
|
|
|
|
+ 'trace' => $e->getTraceAsString()
|
|
|
|
|
+ ]);
|
|
|
} catch (\Exception $e) {
|
|
} catch (\Exception $e) {
|
|
|
// 系统异常,需要回滚事务
|
|
// 系统异常,需要回滚事务
|
|
|
if (DB::transactionLevel() > 0) {
|
|
if (DB::transactionLevel() > 0) {
|
|
@@ -76,10 +108,10 @@ class GetHandler extends BaseHandler
|
|
|
$this->response->setMsg('系统错误,请稍后再试');
|
|
$this->response->setMsg('系统错误,请稍后再试');
|
|
|
|
|
|
|
|
Log::error('获取宠物操作异常', [
|
|
Log::error('获取宠物操作异常', [
|
|
|
- 'user_id' => $this->user_id,
|
|
|
|
|
|
|
+ 'user_id' => $userId ?? $this->user_id,
|
|
|
'item_id' => $itemId ?? null,
|
|
'item_id' => $itemId ?? null,
|
|
|
- 'error' => $e->getMessage(),
|
|
|
|
|
- 'trace' => $e->getTraceAsString()
|
|
|
|
|
|
|
+ 'error' => $e->getMessage(),
|
|
|
|
|
+ 'trace' => $e->getTraceAsString()
|
|
|
]);
|
|
]);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -96,20 +128,26 @@ class GetHandler extends BaseHandler
|
|
|
*/
|
|
*/
|
|
|
private function createPetFromItem(int $userId, int $itemId): array
|
|
private function createPetFromItem(int $userId, int $itemId): array
|
|
|
{
|
|
{
|
|
|
- // 获取物品的宠物种类属性
|
|
|
|
|
|
|
+ // 防错误机制:获取物品的宠物种类属性
|
|
|
$petType = ItemService::getItemNumericAttribute($itemId, 'pet_type');
|
|
$petType = ItemService::getItemNumericAttribute($itemId, 'pet_type');
|
|
|
|
|
|
|
|
|
|
+ // 防错误机制:基本检查,避免意外执行
|
|
|
if (empty($petType)) {
|
|
if (empty($petType)) {
|
|
|
|
|
+ Log::warning('物品没有宠物种类属性,但继续执行', [
|
|
|
|
|
+ 'user_id' => $userId,
|
|
|
|
|
+ 'item_id' => $itemId,
|
|
|
|
|
+ 'pet_type' => $petType
|
|
|
|
|
+ ]);
|
|
|
throw new LogicException("该物品不能获取宠物");
|
|
throw new LogicException("该物品不能获取宠物");
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// 消耗物品
|
|
// 消耗物品
|
|
|
ItemService::consumeItem($userId, $itemId, null, 1, [
|
|
ItemService::consumeItem($userId, $itemId, null, 1, [
|
|
|
'source_type' => 'pet_get',
|
|
'source_type' => 'pet_get',
|
|
|
- 'source_id' => 0,
|
|
|
|
|
- 'details' => [
|
|
|
|
|
|
|
+ 'source_id' => 0,
|
|
|
|
|
+ 'details' => [
|
|
|
'pet_type' => $petType,
|
|
'pet_type' => $petType,
|
|
|
- 'action' => 'create_pet_from_item'
|
|
|
|
|
|
|
+ 'action' => 'create_pet_from_item'
|
|
|
]
|
|
]
|
|
|
]);
|
|
]);
|
|
|
|
|
|
|
@@ -118,8 +156,8 @@ class GetHandler extends BaseHandler
|
|
|
|
|
|
|
|
// 创建宠物
|
|
// 创建宠物
|
|
|
$result = PetService::createPet($userId, $petName, null, [
|
|
$result = PetService::createPet($userId, $petName, null, [
|
|
|
- 'pet_type' => $petType,
|
|
|
|
|
- 'source' => 'item_use',
|
|
|
|
|
|
|
+ 'pet_type' => $petType,
|
|
|
|
|
+ 'source' => 'item_use',
|
|
|
'source_item_id' => $itemId
|
|
'source_item_id' => $itemId
|
|
|
]);
|
|
]);
|
|
|
|
|
|
|
@@ -130,28 +168,37 @@ class GetHandler extends BaseHandler
|
|
|
// 创建LastData对象,用于返回宠物信息
|
|
// 创建LastData对象,用于返回宠物信息
|
|
|
$lastData = new \Uraus\Kku\Common\LastData();
|
|
$lastData = new \Uraus\Kku\Common\LastData();
|
|
|
|
|
|
|
|
- // 获取创建的宠物详细信息
|
|
|
|
|
- $petData = PetService::getPetStatus($userId, $result['pet_id']);
|
|
|
|
|
-
|
|
|
|
|
- // 设置宠物数据到LastData(这里需要根据实际的LastData结构调整)
|
|
|
|
|
- // $lastData->setPets([$petData]);
|
|
|
|
|
|
|
+ // 获取创建的宠物详细信息(用于日志记录和返回数据)
|
|
|
|
|
+ try {
|
|
|
|
|
+ $petData = PetService::getPetStatus($userId, $result['pet_id']);
|
|
|
|
|
+ // 设置宠物数据到LastData(这里需要根据实际的LastData结构调整)
|
|
|
|
|
+ // $lastData->setPets([$petData]);
|
|
|
|
|
+ } catch (\Exception $e) {
|
|
|
|
|
+ Log::warning('获取宠物详细信息失败,但继续执行', [
|
|
|
|
|
+ 'user_id' => $userId,
|
|
|
|
|
+ 'pet_id' => $result['pet_id'],
|
|
|
|
|
+ 'error' => $e->getMessage()
|
|
|
|
|
+ ]);
|
|
|
|
|
+ $petData = null;
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
// 记录日志
|
|
// 记录日志
|
|
|
Log::info('用户通过物品获取宠物成功', [
|
|
Log::info('用户通过物品获取宠物成功', [
|
|
|
- 'user_id' => $userId,
|
|
|
|
|
- 'item_id' => $itemId,
|
|
|
|
|
- 'pet_type' => $petType,
|
|
|
|
|
- 'pet_id' => $result['pet_id'],
|
|
|
|
|
- 'pet_name' => $petName,
|
|
|
|
|
- 'pet_grade' => $result['grade']
|
|
|
|
|
|
|
+ 'user_id' => $userId,
|
|
|
|
|
+ 'item_id' => $itemId,
|
|
|
|
|
+ 'pet_type' => $petType,
|
|
|
|
|
+ 'pet_id' => $result['pet_id'],
|
|
|
|
|
+ 'pet_name' => $petName,
|
|
|
|
|
+ 'pet_grade' => $result['grade'],
|
|
|
|
|
+ 'pet_data_loaded' => $petData !== null
|
|
|
]);
|
|
]);
|
|
|
|
|
|
|
|
return [
|
|
return [
|
|
|
- 'message' => '恭喜您获得了新宠物:' . $petName,
|
|
|
|
|
|
|
+ 'message' => '恭喜您获得了新宠物:' . $petName,
|
|
|
'last_data' => $lastData,
|
|
'last_data' => $lastData,
|
|
|
- 'pet_id' => $result['pet_id'],
|
|
|
|
|
- 'pet_name' => $petName,
|
|
|
|
|
- 'pet_type' => $petType,
|
|
|
|
|
|
|
+ 'pet_id' => $result['pet_id'],
|
|
|
|
|
+ 'pet_name' => $petName,
|
|
|
|
|
+ 'pet_type' => $petType,
|
|
|
'pet_grade' => $result['grade']
|
|
'pet_grade' => $result['grade']
|
|
|
];
|
|
];
|
|
|
}
|
|
}
|
|
@@ -166,11 +213,11 @@ class GetHandler extends BaseHandler
|
|
|
{
|
|
{
|
|
|
// 根据宠物种类生成默认名称
|
|
// 根据宠物种类生成默认名称
|
|
|
$nameMap = [
|
|
$nameMap = [
|
|
|
- 'dog' => '小狗',
|
|
|
|
|
- 'cat' => '小猫',
|
|
|
|
|
- 'bird' => '小鸟',
|
|
|
|
|
- 'fish' => '小鱼',
|
|
|
|
|
- 'rabbit' => '小兔',
|
|
|
|
|
|
|
+ 'dog' => '小狗',
|
|
|
|
|
+ 'cat' => '小猫',
|
|
|
|
|
+ 'bird' => '小鸟',
|
|
|
|
|
+ 'fish' => '小鱼',
|
|
|
|
|
+ 'rabbit' => '小兔',
|
|
|
'hamster' => '小仓鼠',
|
|
'hamster' => '小仓鼠',
|
|
|
];
|
|
];
|
|
|
|
|
|
|
@@ -181,5 +228,4 @@ class GetHandler extends BaseHandler
|
|
|
|
|
|
|
|
return $baseName . $suffix;
|
|
return $baseName . $suffix;
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
}
|
|
}
|